forked from nodemcu/nodemcu-firmware
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathws2812_compat.lua
69 lines (54 loc) · 1.64 KB
/
ws2812_compat.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-- ****************************************************************************
--
-- Compatability wrapper for mapping ESP8266's ws2812 module API to ESP32
--
-- Usage:
--
-- ws2812 = require("ws2812_compat")(pin_strip1[, pin_strip2])
--
-- pin_strip1: GPIO pin of first led strip, mandatory
-- pin_strip2: GPIO pin of second led strip, optional
--
-- ****************************************************************************
local M = {}
local _pin_strip1, _pin_strip2
local _mode
local _ws2812
-- ****************************************************************************
-- Implement esp8266 compatability API
--
function M.init(mode)
if _pin_strip2 == nil and mode == M.MODE_DUAL then
error("gpio for data2 undefined")
end
_mode = mode or M.MODE_SINGLE
end
function M.write(data1, data2)
if _mode == nil then
error("call init() first")
end
local strip1 = {pin = _pin_strip1, data = data1}
local strip2
if _pin_strip2 and data2 then
strip2 = {pin = _pin_strip2, data = data2}
end
_ws2812.write(strip1, strip2)
end
return function (pin_strip1, pin_strip2)
-- cache built-in module
_ws2812 = ws2812
-- invalidate built-in module
ws2812 = nil
-- forward unchanged functions
M.newBuffer = _ws2812.newBuffer
-- forward constant definitions
M.FADE_IN = _ws2812.FADE_IN
M.FADE_OUT = _ws2812.FADE_OUT
M.MODE_SINGLE = 0 -- encoding from ws2812.c
M.MODE_DUAL = 1 -- encoding from ws2812.c
M.SHIFT_LOGICAL = _ws2812.SHIFT_LOGICAL
M.SHIFT_CIRCULAR = _ws2812.SHIFT_CIRCULAR
_pin_strip1 = pin_strip1 or error("pin_strip1 not defined")
_pin_strip2 = pin_strip2
return M
end