From 2acfa53ee8ee09cb30d93949e9a5911b663f10c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnim=20L=C3=A4uger?= Date: Sat, 6 May 2017 17:36:14 +0200 Subject: [PATCH] add ws2812 compatibility layer (#1947) --- docs/en/modules/ws2812.md | 4 ++ lua_compat/ws2812_compat.lua | 74 ++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 lua_compat/ws2812_compat.lua diff --git a/docs/en/modules/ws2812.md b/docs/en/modules/ws2812.md index 910f8a0c..a01cb4a7 100644 --- a/docs/en/modules/ws2812.md +++ b/docs/en/modules/ws2812.md @@ -6,6 +6,10 @@ ws2812 is a library to handle ws2812-like led strips. It works at least on WS2812, WS2812b, APA104, SK6812 (RGB or RGBW). +!!! note + + The API on ESP32 differs from the API on ESP8266. For backwards compatibility please refer to [`lua_compat/ws2812_compat.lua`](../../../lua_compat/ws2812_compat.lua`). + ## ws2812.write() Send data to up to 8 led strip using its native format which is generally Green,Red,Blue for RGB strips and Green,Red,Blue,White for RGBW strips. diff --git a/lua_compat/ws2812_compat.lua b/lua_compat/ws2812_compat.lua new file mode 100644 index 00000000..f50f7a74 --- /dev/null +++ b/lua_compat/ws2812_compat.lua @@ -0,0 +1,74 @@ +-- **************************************************************************** +-- +-- 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_strip1 == nil then + error("gpio for data1 undefined") + end + + 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 + _pin_strip2 = pin_strip2 + + return M +end +