@@ -7,26 +7,41 @@ ws2812 is a library to handle ws2812-like led strips.
...
@@ -7,26 +7,41 @@ ws2812 is a library to handle ws2812-like led strips.
It works at least on WS2812, WS2812b, APA104, SK6812 (RGB or RGBW).
It works at least on WS2812, WS2812b, APA104, SK6812 (RGB or RGBW).
The library uses UART1 routed on GPIO2 (Pin D4 on NodeMCU DEVKIT) to
The library uses UART1 routed on GPIO2 (Pin D4 on NodeMCU DEVKIT) to
generate the bitstream.
generate the bitstream. It can use UART0 routed to TXD0 as well to
handle two led strips at the same time.
## ws2812.init()
**WARNING**: In dual mode, you will loose access to the Lua's console
Initialize UART1 and GPIO2, should be called once and before write()
through the serial port (it will be reconfigured to support WS2812-like
protocol). If you want to keep access to Lua's console, you will have to
use an other input channel like a TCP server (see [example](https://github.com/nodemcu/nodemcu-firmware/blob/master/examples/telnet.lua))
## ws2812.init(mode)
Initialize UART1 and GPIO2, should be called once and before write().
Initialize UART0 (TXD0) too if `ws2812.MODE_DUAL` is set.
#### Parameters
#### Parameters
none
-`mode` (optional) either `ws2812.MODE_SINGLE` (default if omitted) or `ws2812.MODE_DUAL`.
In `ws2812.MODE_DUAL` mode you will be able to handle two strips in parallel but will lose access
to Lua's serial console as it shares the same UART and PIN.
#### Returns
#### Returns
`nil`
`nil`
## ws2812.write()
## ws2812.write()
Send data to a led strip using its native format which is generally Green,Red,Blue for RGB strips
Send data to one or two led strip using its native format which is generally Green,Red,Blue for RGB strips
and Green,Red,Blue,White for RGBW strips.
and Green,Red,Blue,White for RGBW strips.
#### Syntax
#### Syntax
`ws2812.write(string)`
`ws2812.write(data1, [data2])`
#### Parameters
#### Parameters
-`string` payload to be sent to one or more WS2812 like leds.
-`data1` payload to be sent to one or more WS2812 like leds through GPIO2
-`data2` (optional) payload to be sent to one or more WS2812 like leds through TXD0 (`ws2812.MODE_DUAL` mode required)
Payload type could be:
-`nil` nothing is done
-`string` representing bytes to send
-`ws2812.buffer` see [Buffer module](#buffer-module)
#### Returns
#### Returns
`nil`
`nil`
...
@@ -34,12 +49,22 @@ and Green,Red,Blue,White for RGBW strips.
...
@@ -34,12 +49,22 @@ and Green,Red,Blue,White for RGBW strips.
#### Example
#### Example
```lua
```lua
ws2812.init()
ws2812.init()
ws2812.write(string.char(255,0,0,255,0,0)-- turn the two first RGB leds to green
ws2812.write(string.char(255,0,0,255,0,0))-- turn the two first RGB leds to green
```
```
```lua
```lua
ws2812.init()
ws2812.init()
ws2812.write(string.char(0,0,0,255,0,0,0,255)-- turn the two first RGBW leds to white
ws2812.write(string.char(0,0,0,255,0,0,0,255))-- turn the two first RGBW leds to white
```
```lua
ws2812.init(ws2812.MODE_DUAL)
ws2812.write(string.char(255,0,0,255,0,0),string.char(0,255,0,0,255,0))-- turn the two first RGB leds to green on the first strip and red on the second strip
```
```lua
ws2812.init(ws2812.MODE_DUAL)
ws2812.write(nil,string.char(0,255,0,0,255,0))-- turn the two first RGB leds to red on the second strip, do nothing on the first
```
```
# Buffer module
# Buffer module
...
@@ -51,11 +76,12 @@ For this purpose, the ws2812 library offers a read/write buffer.
...
@@ -51,11 +76,12 @@ For this purpose, the ws2812 library offers a read/write buffer.