2016-01-11 22:36:25 +01:00
# WS2812 Module
2016-03-05 10:47:01 +01:00
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
2019-01-13 22:01:57 +01:00
| 2015-02-05 | [Till Klocke ](https://github.com/dereulenspiegel ), [Thomas Soëte ](https://github.com/Alkorin ) | [Till Klocke ](https://github.com/dereulenspiegel ) | [ws2812.c ](../../app/modules/ws2812.c )|
2016-03-05 10:47:01 +01:00
2016-05-09 07:21:50 +02:00
ws2812 is a library to handle ws2812-like led strips.
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
2016-07-20 22:28:47 +02:00
generate the bitstream. It can use UART0 routed to TXD0 as well to
handle two led strips at the same time.
2016-05-09 07:21:50 +02:00
2016-07-20 22:28:47 +02:00
**WARNING**: In dual mode, you will loose access to the Lua's console
2019-02-17 19:26:29 +01:00
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
2020-11-25 12:53:46 +01:00
use an other input channel like a TCP server (see [example ](https://github.com/nodemcu/nodemcu-firmware/blob/release/lua_modules/telnet/telnet.lua ))
2016-07-20 22:28:47 +02:00
2021-02-14 08:41:17 +01:00
!!! caution
This module has an _optional_ dependency to the [pixbuf module ](pixbuf.md ) i.e. it can work without. However, if you compile the firmware without pixbuf the respective features will be missing from this module.
2017-02-24 21:20:09 +01:00
## ws2812.init()
2016-07-20 22:28:47 +02:00
Initialize UART1 and GPIO2, should be called once and before write().
Initialize UART0 (TXD0) too if `ws2812.MODE_DUAL` is set.
2016-05-09 07:21:50 +02:00
2017-02-24 21:20:09 +01:00
#### Syntax
`ws2812.init([mode])`
2016-05-09 07:21:50 +02:00
#### Parameters
2017-02-24 21:20:09 +01:00
- `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.
2016-05-09 07:21:50 +02:00
#### Returns
`nil`
2016-01-11 22:36:25 +01:00
## ws2812.write()
2021-01-07 00:35:34 +01:00
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.
(However, ws2812 drivers have been observed wired up in other orders.)
Because this function uses the hardware UART(s), it is able to return and allow
Lua to resume execution up to 300 microseconds before the data has finished
being sent. If you wish to perform actions synchronous with the end of the
data transmission, [`tmr.delay()` ](../tmr#tmr.delay( )) for 300 microseconds.
Separately, because this function returns early, back-to-back invocations may
not leave enough time for the strip to latch, and so may appear to the ws2812
drivers to be simply writes to a longer LED strip. Please ensure that you have
more than 350 microseconds between the return of `ws2812.write()` to your Lua
and the next invocation thereof.
2016-01-11 22:36:25 +01:00
#### Syntax
2016-07-20 22:28:47 +02:00
`ws2812.write(data1, [data2])`
2016-01-11 22:36:25 +01:00
#### Parameters
2016-07-20 22:28:47 +02:00
- `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:
2021-02-14 08:41:17 +01:00
2016-07-20 22:28:47 +02:00
- `nil` nothing is done
- `string` representing bytes to send
2021-01-07 00:35:34 +01:00
- a [pixbuf ](pixbuf ) object containing the bytes to send. The pixbuf's type is not checked!
2016-01-11 22:36:25 +01:00
#### Returns
`nil`
2016-05-09 07:21:50 +02:00
#### Example
```lua
ws2812.init()
2016-07-20 22:28:47 +02:00
ws2812.write(string.char(255, 0, 0, 255, 0, 0)) -- turn the two first RGB leds to green
2016-05-09 07:21:50 +02:00
```
2016-01-11 22:36:25 +01:00
```lua
2016-05-09 07:21:50 +02:00
ws2812.init()
2016-07-20 22:28:47 +02:00
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
2016-01-11 22:36:25 +01:00
```
2021-01-07 00:35:34 +01:00
# Pixbuf support
2016-05-09 07:21:50 +02:00
For more advanced animations, it is useful to keep a "framebuffer" of the strip,
interact with it and flush it to the strip.
2021-01-07 00:35:34 +01:00
For this purpose, the [pixbuf ](pixbuf ) library offers a read/write buffer and
convenient functions for pixel value manipulation.
For backwards-compatibility, `pixbuf.newBuffer()` is aliased as
2021-02-14 08:41:17 +01:00
`ws2812.newBuffer()` , but this will be removed in the next nodemcu-firmware
2021-01-07 00:35:34 +01:00
release.
2016-05-09 07:21:50 +02:00
#### Example
Led chaser with a RGBW strip
2021-01-07 00:35:34 +01:00
2016-05-09 07:21:50 +02:00
```lua
2016-07-20 22:28:47 +02:00
ws2812.init()
2021-01-07 00:35:34 +01:00
i, buffer = 0, pixbuf.newBuffer(300, 4)
buffer:fill(0, 0, 0, 0)
tmr.create():alarm(50, 1, function()
2017-01-03 21:50:56 +01:00
i = i + 1
buffer:fade(2)
buffer:set(i % buffer:size() + 1, 0, 0, 0, 255)
ws2812.write(buffer)
2016-05-09 07:21:50 +02:00
end)
```