         mailbox.cleanup()
     }
 
-    internal func initPWMPattern(bytes count: Int, at frequency: Int, with resetDelay: Int, dutyzero: Int, dutyone: Int) {
+    /// Initiliazes the PWM signal generator
+    ///
+    /// - Parameter bytes: length of the data in bytes, fixed
+    /// - Parameter at: signal frequency
+    /// - Parameter with: length in us of the section with low signal that will be put at the end of the generated bit stream
+    /// - Parameter dutyzero: duty cycle of the pattern for zero
+    /// - Parameter dutyone: duty cycle of the pattern for one
+    ///
+    public func initPWMPattern(bytes count: Int, at frequency: Int, with resetDelay: Int, dutyzero: Int, dutyone: Int) {
 
         (zeroPattern,onePattern,symbolBits) = getRepresentation(zero: dutyzero, one: dutyone) 
         guard symbolBits > 0 else {fatalError("Couldn't generate a valid pattern for the provided duty cycle values, try with more spaced values.")}
         
         patternFrequency = frequency
         patternDelay = resetDelay
+        dataLength = count
 
         // Round to the next 32 bit size
-        let dataSize = (( ((count * 3 * 8 * symbolBits) + ((patternDelay * (patternFrequency * symbolBits)) / 1000000)) / 8) & ~0x3) + 4
+        let dataSize = (( ((dataLength * 8 * symbolBits) + ((patternDelay * (patternFrequency * symbolBits)) / 1000000)) / 8) & ~0x3) + 4
         let size = dataSize + MemoryLayout<DMACallback>.stride
 
         // Round up to page size multiple


        for idx in 0..<stream.count {
            printBinary(stream[idx])
            pwm_raw[idx] = UInt(stream[idx])
        }

        // Wait for any previous DMA operation to complete.

        clockBasePointer.advanced(by: 40).pointee = CLKM_PASSWD | CLKM_CTL_KILL     //Set KILL flag
        usleep(10)

        // Configure clock
        let idiv = calculateUnscaledDIVI(base: .Oscillator, desired: UInt(symbolBits * patternFrequency))
        clockBasePointer.advanced(by: 41).pointee = CLKM_PASSWD | (idiv << CLKM_DIV_DIVI)            //Set DIVI value  
        clockBasePointer.advanced(by: 40).pointee = CLKM_PASSWD | CLKM_CTL_ENAB | CLKM_CTL_SRC_OSC   //Enable clock, MASH 0, source OSC
        usleep(10)

        // Configure PWM 
        pwmBasePointer.advanced(by: 4).pointee = 32         // RNG1: 32-bits per word to serialize
        usleep(10)
        pwmBasePointer.pointee = PWMCTL_CLRF1
        usleep(10)
        pwmBasePointer.advanced(by: 2).pointee = PWMDMAC_ENAB | 7 << PWMDMAC_PANIC | 3 << PWMDMAC_DREQ
        usleep(10)
        pwmBasePointer.pointee = PWMCTL_USEF1 | PWMCTL_MODE1 //| PWMCTL_USEF2 | PWMCTL_MODE2 // For 2nd chan 
        usleep(10)
        pwmBasePointer.pointee |= PWMCTL_PWEN1               //| PWMCTL_PWEN2 // For 2nd chan 

        // Initialize the DMA control block
        dmaCallbackPointer.pointee = DMACallback(
                ti: DMATI_NO_WIDE_BURSTS |              // 32-bit transfers
                     DMATI_WAIT_RESP |                  // wait for write complete
                     DMATI_DEST_DREQ |                  // user peripheral flow control
                     UInt32(0x5  << DMATI_PERMAP) |     // PWM peripheral
                     DMATI_SRC_INC,                     // Increment src addr
                source_ad: UInt32(mailbox.virtualTobaseBusAddress(UnsafeMutableRawPointer(pwmRawPointer))),
                dest_ad: UInt32(PWM_PHY_BASE + 0x18),   // PWM FIF1 Register, shared between channels
                txfr_len: UInt32(dataSize),
                stride: 0,
                nextconbk: 0)

        dmaBasePointer.pointee = 0                  //0 CS
        dmaBasePointer.advanced(by: 5).pointee = 0  //0 TXFR_LEN

    }

    public func sendDataWithPattern(values: [UInt8]) {

        guard symbolBits > 0 else {fatalError("Couldn't generate a valid pattern for the provided duty cycle values, try with more spaced values.")}

        dma_wait()
        
        let stream = dataToBitStream(data: values, zero: zeroPattern, one: onePattern, width: symbolBits)

        func printBinary(_ value: UInt32) {
