# ws2812b_spi

Library to support a chain of ws2812b chips via MSSP interface (SPI)

Author | Rob Hamerling, Copyright © 2014..2014, all rights reserved.
Adapted-by |
Compiler | 2.4q2

Description

    Library to control a chain of WS2812Bs
    This library uses the MSSP module with SPI-master protocol.
    - A PIC with Fosc of at least 32 MHz and MSSP module is required.
    - The MSSP module must have the capability to set the baudrate in SPI master mode
      with SSPCON1_SSPM = 0b1010 : SPI Master mode, clock = FOSC/(4 * (SSPADD+1)).
      like with the 18f25k22 (note: e.g. with the 18f14k22 this works only for I2C).
      This requirement cannot be checked by the library, check the datasheet!
    - The DIN wire of the WS2812B must be connected to pin_SDO of the PIC
      The pin must be configured for digital output.
      Notes: - The pin_SDO may have to be configured with Peripheral Pin Select (PPS)
               or Alternate Pin Function (APFCON_SDOSEL).
             - The SDI and SCK pins are not used.
    - Initialize the interface with procedure ws2812b_async_init().
    - The number of WS2812Bs in the chain must be defined, e.g.:
         const byte WS2812B_NUM = 30       -- for 30 chained WS2812Bs
    - The user program should contain 3 byte arrays of WS2812B_NUM bytes each, e.g.:
         var byte agreen[WS2812B_NUM]
         var byte ared[WS2812B_NUM]
         var byte ablue[WS2812B_NUM]
      and call the procedure ws2812b_refresh() to pass the current color settings
      to the WS2812B LEDs.
    - Procedure ws2812b_all_color() can be used to set all LEDs to the same color
      while keeping the color settings in the array unchanged.

Notes

    - Warnings are temporary(?) commented out.

Dependencies

- ws2812b_common

## Summary

### Global variables/contants

### Procedures

- `ws2812b_spi_init()`

Private

- `_ws2812b_write_color(byte in color)`
- `_ws2812b_write_pixel(byte in green, byte in red, byte in blue)`
- `_transfer_byte(byte in data)`

### Functions

## API details

### Global variables/contants

### Procedures

- `ws2812b_spi_init()`

      Title:   Initialize the MSSP for SPI master transmit at the required speed
      Input:   (none)

Private

- `_ws2812b_write_color(byte in color)`

      Title:  Transmit 1 color byte to the WS2812B chain
      Input:  color intensity (byte)
      Notes:  - Each WS2812B bit is represented by 4 SPI bits:
      (WS2812B '0' = 0b1000, WS2812B '1' = 0b1110)

- `_ws2812b_write_pixel(byte in green, byte in red, byte in blue)`

      Title:  Transfer 3 color bytes to the WS2812B chain
      Input:  color intensities of green, red and blue (3 bytes)
      Notes:

- `_transfer_byte(byte in data)`

      Title:   Transmit a byte (with 2 WS2812B 'bits')
      Input:   Byte with 2 WS2812B bits in the form of 8 SPI bits.
      Notes: - We do not wait for completion immediately after a write, but
      wait for completion of the previous write before the next write!
      This allows to execute instructions during the transfer time to
      prepare for the transmission of the next byte.

### Functions

## Related samples

Here are the list of samples which use this library:

12f1840 | 12f1840_ws2812b_spi.jal
18f25k22 | 18f25k22_ws2812b_spi.jal