From 85727485cfc8b42c57b18f8a400635eaba0c5145 Mon Sep 17 00:00:00 2001 From: robcazzaro Date: Mon, 16 Feb 2015 19:17:23 +0000 Subject: [PATCH 1/8] Update dht22.lua Addresses issue #225, slower loop timing due to the use of floats. Should be safe to use with integer-only builds, since on integer-only builds I could never see "3" as a valid value for either low or high --- lua_modules/dht22/dht22.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua_modules/dht22/dht22.lua b/lua_modules/dht22/dht22.lua index b7b04092..a80a1bd6 100644 --- a/lua_modules/dht22/dht22.lua +++ b/lua_modules/dht22/dht22.lua @@ -62,17 +62,17 @@ function M.read(pin) --DHT data acquired, process. for i = 1, 16, 1 do - if (bitStream[i] > 4) then + if (bitStream[i] > 3) then humidity = humidity + 2 ^ (16 - i) end end for i = 1, 16, 1 do - if (bitStream[i + 16] > 4) then + if (bitStream[i + 16] > 3) then temperature = temperature + 2 ^ (16 - i) end end for i = 1, 8, 1 do - if (bitStream[i + 32] > 4) then + if (bitStream[i + 32] > 3) then checksum = checksum + 2 ^ (8 - i) end end From 888fbdf338cd2e48aa8ca79fc5ff8916a05955cb Mon Sep 17 00:00:00 2001 From: Damian Date: Fri, 20 Feb 2015 20:24:00 +0100 Subject: [PATCH 2/8] ver 0.9.5 reports error in tmr.alarm() There was no alarm ID. --- lua_examples/telnet.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua_examples/telnet.lua b/lua_examples/telnet.lua index 18645187..22610445 100644 --- a/lua_examples/telnet.lua +++ b/lua_examples/telnet.lua @@ -29,11 +29,11 @@ function startServer() print("===Now, logon and input LUA.====") end -tmr.alarm(1000, 1, function() +tmr.alarm(1, 1000, 1, function() if wifi.sta.getip()=="0.0.0.0" then print("Connect AP, Waiting...") else startServer() - tmr.stop() + tmr.stop(1) end end) From c869eddb8a79945a05aebee641814ee93d9ad343 Mon Sep 17 00:00:00 2001 From: Ezekiel-DA Date: Wed, 25 Feb 2015 14:34:07 -0500 Subject: [PATCH 3/8] Update file.c Made file.seek() return nil on error / EOF, as specified in documentation, instead of returning true. --- app/modules/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/modules/file.c b/app/modules/file.c index c402bbf2..cf10a3a7 100644 --- a/app/modules/file.c +++ b/app/modules/file.c @@ -111,7 +111,7 @@ static int file_seek (lua_State *L) long offset = luaL_optlong(L, 2, 0); op = fs_seek(file_fd, offset, mode[op]); if (op) - lua_pushboolean(L, 1); /* error */ + lua_pushnil(L); /* error */ else lua_pushinteger(L, fs_tell(file_fd)); return 1; From 2b5ee453a80bedbc26a96d951bc755c001e06836 Mon Sep 17 00:00:00 2001 From: George Hahn Date: Thu, 26 Feb 2015 20:13:27 -0500 Subject: [PATCH 4/8] Move contributing guidelines to repository Moves guidelines for contribution from issue #176 to a file in the repository. Github will show a message with this file to contributors, increasing its visibility. Closes #176 --- CONTRIBUTING.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..42be531f --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,7 @@ +## Contributing + +Thank you everyone for contributing to this project. + +Pull requests for new features and major fixes should be opened against the `dev` branch. + +*Note that the `pre_build` bin in the dev branch is never up-to-date.* From 194ee39629f121bcea3c70381439a3cd1df962e6 Mon Sep 17 00:00:00 2001 From: Felix Richter Date: Fri, 27 Feb 2015 22:55:20 +0100 Subject: [PATCH 5/8] Fix Pinout HTML table layout when trying to copy the layout into html, the output breaks due to a missing closing backet --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d4f88930..dbf3cace 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ build pre_build bin. 6GPIO12 - 7GPIO13 + 7GPIO13 #### [*] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported. From 34d0b773d98226888ea351ec0fdcee0375193965 Mon Sep 17 00:00:00 2001 From: AllAboutEE Date: Mon, 2 Mar 2015 13:21:26 -0600 Subject: [PATCH 6/8] Added I/O expander MCP23008 module and two examples: digital output with LEDS, and digital input with buttons. --- lua_examples/mcp23008/mcp23008_buttons.lua | 57 ++++++++ lua_examples/mcp23008/mcp23008_leds.lua | 45 ++++++ lua_modules/mcp23008/mcp23008.lua | 162 +++++++++++++++++++++ 3 files changed, 264 insertions(+) create mode 100644 lua_examples/mcp23008/mcp23008_buttons.lua create mode 100644 lua_examples/mcp23008/mcp23008_leds.lua create mode 100644 lua_modules/mcp23008/mcp23008.lua diff --git a/lua_examples/mcp23008/mcp23008_buttons.lua b/lua_examples/mcp23008/mcp23008_buttons.lua new file mode 100644 index 00000000..98120fcb --- /dev/null +++ b/lua_examples/mcp23008/mcp23008_buttons.lua @@ -0,0 +1,57 @@ +--- +-- @description Shows how to read 8 GPIO pins/buttons via I2C with the MCP23008 I/O expander. +-- Tested on NodeMCU 0.9.5 build 20150213. +-- @circuit +-- Connect GPIO0 of the ESP8266-01 module to the SCL pin of the MCP23008. +-- Connect GPIO2 of the ESP8266-01 module to the SDA pin of the MCP23008. +-- Use 3.3V for VCC. +-- Connect switches or buttons to the GPIOs of the MCP23008 and GND. +-- Connect two 4.7k pull-up resistors on SDA and SCL +-- We will enable the internal pull up resistors for the GPIOS of the MCP23008. +-- @author Miguel (AllAboutEE) +-- GitHub: https://github.com/AllAboutEE +-- YouTube: https://www.youtube.com/user/AllAboutEE +-- Website: http://AllAboutEE.com +--------------------------------------------------------------------------------------------- + +require ("mcp23008") + +-- ESP-01 GPIO Mapping as per GPIO Table in https://github.com/nodemcu/nodemcu-firmware +gpio0, gpio2 = 3, 4 + +-- Setup the MCP23008 +mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW) + +mcp23008.writeIODIR(0xff) +mcp23008.writeGPPU(0xff) + +--- +-- @name showButtons +-- @description Shows the state of each GPIO pin +-- @return void +--------------------------------------------------------- +function showButtons() + + local gpio = mcp23008.readGPIO() -- read the GPIO/buttons states + + -- get/extract the state of one pin at a time + for pin=0,7 do + + local pinState = bit.band(bit.rshift(gpio,pin),0x1) -- extract one pin state + + -- change to string state (HIGH, LOW) instead of 1 or 0 respectively + if(pinState == mcp23008.HIGH) then + pinState = "HIGH" + else + pinState = "LOW" + end + + print("Pin ".. pin .. ": ".. pinState) + end + print("\r\n") +end + +tmr.alarm(0,2000,1,showButtons) -- run showButtons() every 2 seconds + + + diff --git a/lua_examples/mcp23008/mcp23008_leds.lua b/lua_examples/mcp23008/mcp23008_leds.lua new file mode 100644 index 00000000..463e2fc8 --- /dev/null +++ b/lua_examples/mcp23008/mcp23008_leds.lua @@ -0,0 +1,45 @@ +--- +-- @description Shows control of 8 GPIO pins/LEDs via I2C with the MCP23008 I/O expander. +-- Tested on odeMCU 0.9.5 build 20150213. +-- @date March 02, 2015 +-- @circuit Connect 8 LEDs withs resistors to the GPIO pins of the MCP23008. +-- Connect GPIO0 of the ESP8266-01 module to the SCL pin of the MCP23008. +-- Connect GPIO2 of the ESP8266-01 module to the SDA pin of the MCP23008. +-- Connect two 4.7k pull-up resistors on SDA and SCL +-- Use 3.3V for VCC. +-- @author Miguel (AllAboutEE) +-- GitHub: https://github.com/AllAboutEE +-- Working Example Video: https://www.youtube.com/watch?v=KphAJMZZed0 +-- Website: http://AllAboutEE.com +--------------------------------------------------------------------------------------------- + +require ("mcp23008") + +-- ESP-01 GPIO Mapping as per GPIO Table in https://github.com/nodemcu/nodemcu-firmware +gpio0, gpio2 = 3, 4 + +-- Setup MCP23008 +mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW) + +mcp23008.writeIODIR(0x00) -- make all GPIO pins as outputs +mcp23008.writeGPIO(0x00) -- make all GIPO pins off/low + +--- +-- @name count() +-- @description Reads the value from the GPIO register, increases the read value by 1 +-- and writes it back so the LEDs will display a binary count up to 255 or 0xFF in hex. +local function count() + + local gpio = 0x00 + + gpio = mcp23008.readGPIO() + + if(gpio<0xff) then + mcp23008.writeGPIO(gpio+1) + else + mcp23008.writeGPIO(0x00) + end + +end +-- Run count() every 100ms +tmr.alarm(0,100,1,count) diff --git a/lua_modules/mcp23008/mcp23008.lua b/lua_modules/mcp23008/mcp23008.lua new file mode 100644 index 00000000..0a977d03 --- /dev/null +++ b/lua_modules/mcp23008/mcp23008.lua @@ -0,0 +1,162 @@ +--- +-- @description Expands the ESP8266's GPIO to 8 more I/O pins via I2C with the MCP23008 I/O Expander +-- MCP23008 Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21919e.pdf +-- Tested on NodeMCU 0.9.5 build 20150213. +-- @date March 02, 2015 +-- @author Miguel +-- GitHub: https://github.com/AllAboutEE +-- YouTube: https://www.youtube.com/user/AllAboutEE +-- Website: http://AllAboutEE.com +-------------------------------------------------------------------------------- + +local moduleName = ... +local M = {} +_G[moduleName] = M + +-- Constant device address. +local MCP23008_ADDRESS = 0x20 + +-- Registers' address as defined in the MCP23008's datashseet +local MCP23008_IODIR = 0x00 +local MCP23008_IPOL = 0x01 +local MCP23008_GPINTEN = 0x02 +local MCP23008_DEFVAL = 0x03 +local MCP23008_INTCON = 0x04 +local MCP23008_IOCON = 0x05 +local MCP23008_GPPU = 0x06 +local MCP23008_INTF = 0x07 +local MCP23008_INTCAP = 0x08 +local MCP23008_GPIO = 0x09 +local MCP23008_OLAT = 0x0A + +-- Default value for i2c communication +local id = 0 + +-- pin modes for I/O direction +M.INPUT = 1 +M.OUTPUT = 0 + +-- pin states for I/O i.e. on/off +M.HIGH = 1 +M.LOW = 0 + +-- Weak pull-up resistor state +M.ENABLE = 1 +M.DISABLE = 0 + + +--- +-- @name write +-- @description Writes one byte to a register +-- @param registerAddress The register where we'll write data +-- @param data The data to write to the register +-- @return void +---------------------------------------------------------- +local function write(registerAddress, data) + i2c.start(id) + i2c.address(id,MCP23008_ADDRESS,i2c.TRANSMITTER) -- send MCP's address and write bit + i2c.write(id,registerAddress) + i2c.write(id,data) + i2c.stop(id) +end + +--- +-- @name read +-- @description Reads the value of a regsiter +-- @param registerAddress The reigster address from which to read +-- @return The byte stored in the register +---------------------------------------------------------- +local function read(registerAddress) + -- Tell the MCP which register you want to read from + i2c.start(id) + i2c.address(id,MCP23008_ADDRESS,i2c.TRANSMITTER) -- send MCP's address and write bit + i2c.write(id,registerAddress) + i2c.stop(id) + i2c.start(id) + -- Read the data form the register + i2c.address(id,MCP23008_ADDRESS,i2c.RECEIVER) -- send the MCP's address and read bit + local data = 0x00 + data = i2c.read(id,1) -- we expect only one byte of data + i2c.stop(id) + + return string.byte(data) -- i2c.read returns a string so we convert to it's int value +end + +--- +--! @name begin +--! @description Sets the MCP23008 device address's last three bits. +-- Note: The address is defined as binary 0100[A2][A1][A0] where +-- A2, A1, and A0 are defined by the connection of the pins, +-- e.g. if the pins are connected all to GND then the paramter address +-- will need to be 0x0. +-- @param address The 3 least significant bits (LSB) of the address +-- @param pinSDA The pin to use for SDA +-- @param pinSCL The pin to use for SCL +-- @param speed The speed of the I2C signal +-- @return void +--------------------------------------------------------------------------- +function M.begin(address,pinSDA,pinSCL,speed) + MCP23008_ADDRESS = bit.bor(MCP23008_ADDRESS,address) + i2c.setup(id,pinSDA,pinSCL,speed) +end + +--- +-- @name writeGPIO +-- @description Writes a byte of data to the GPIO register +-- @param dataByte The byte of data to write +-- @return void +---------------------------------------------------------- +function M.writeGPIO(dataByte) + write(MCP23008_GPIO,dataByte) +end + +--- +-- @name readGPIO +-- @description reads a byte of data from the GPIO register +-- @return One byte of data +---------------------------------------------------------- +function M.readGPIO() + return read(MCP23008_GPIO) +end + +--- +-- @name writeIODIR +-- @description Writes one byte of data to the IODIR register +-- @param dataByte The byte of data to write +-- @return void +---------------------------------------------------------- +function M.writeIODIR(dataByte) + write(MCP23008_IODIR,dataByte) +end + +--- +-- @name readIODIR +-- @description Reads a byte from the IODIR register +-- @return The byte of data in IODIR +---------------------------------------------------------- +function M.readIODIR() + return read(MCP23008_IODIR) +end + +--- +-- @name writeGPPU The byte to write to the GPPU +-- @description Writes a byte of data to the +-- PULL-UP RESISTOR CONFIGURATION (GPPU)REGISTER +-- @param databyte the value to write to the GPPU register. +-- each bit in this byte is assigned to an individual GPIO pin +-- @return void +---------------------------------------------------------------- +function M.writeGPPU(dataByte) + write(MCP23008_GPPU,dataByte) +end + +--- +-- @name readGPPU +-- @description Reads the GPPU (Pull-UP resistors register) byte +-- @return The GPPU byte i.e. state of all internal pull-up resistors +------------------------------------------------------------------- +function M.readGPPU() + return read(MCP23008_GPPU) +end + +return M From 8432e8c8beacd5580e19148001c0e383f92b2ac7 Mon Sep 17 00:00:00 2001 From: Markus Gritsch Date: Tue, 3 Mar 2015 10:20:02 +0100 Subject: [PATCH 7/8] Changed the ws2812 byte order from GRB to RGB There seems to be interest in using the more intuitive RGB byte order to specify the LED color. Also fixed the documentation. --- README.md | 96 +++++++++++++++++++-------------------- app/include/user_config.h | 2 +- app/modules/modules.h | 14 +++--- app/modules/ws2812.c | 56 +++++++++++++---------- 4 files changed, 87 insertions(+), 81 deletions(-) diff --git a/README.md b/README.md index d4f88930..8d66eae6 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ baudrate:9600 ``` ####Manipulate hardware like a arduino - + ```lua pin = 1 gpio.mode(pin,gpio.OUTPUT) @@ -165,10 +165,10 @@ baudrate:9600 ``` ####Write network application in nodejs style - + ```lua -- A simple http client - conn=net.createConnection(net.TCP, 0) + conn=net.createConnection(net.TCP, 0) conn:on("receive", function(conn, payload) print(payload) end ) conn:connect(80,"115.239.210.27") conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n" @@ -176,15 +176,15 @@ baudrate:9600 ``` ####Or a simple http server - + ```lua -- A simple http server - srv=net.createServer(net.TCP) - srv:listen(80,function(conn) - conn:on("receive",function(conn,payload) - print(payload) + srv=net.createServer(net.TCP) + srv:listen(80,function(conn) + conn:on("receive",function(conn,payload) + print(payload) conn:send("

Hello, NodeMcu.

") - end) + end) conn:on("sent",function(conn) conn:close() end) end) ``` @@ -196,7 +196,7 @@ baudrate:9600 m = mqtt.Client("clientid", 120, "user", "password") -- setup Last Will and Testament (optional) --- Broker will publish a message with qos = 0, retain = 0, data = "offline" +-- Broker will publish a message with qos = 0, retain = 0, data = "offline" -- to topic "/lwt" if client don't send keepalive packet m:lwt("/lwt", "offline", 0, 0) @@ -204,8 +204,8 @@ m:on("connect", function(con) print ("connected") end) m:on("offline", function(con) print ("offline") end) -- on publish message receive event -m:on("message", function(conn, topic, data) - print(topic .. ":" ) +m:on("message", function(conn, topic, data) + print(topic .. ":" ) if data ~= nil then print(data) end @@ -229,29 +229,29 @@ m:close(); #### UDP client and server ```lua -- a udp server -s=net.createServer(net.UDP) +s=net.createServer(net.UDP) s:on("receive",function(s,c) print(c) end) s:listen(5683) -- a udp client -cu=net.createConnection(net.UDP) -cu:on("receive",function(cu,c) print(c) end) -cu:connect(5683,"192.168.18.101") +cu=net.createConnection(net.UDP) +cu:on("receive",function(cu,c) print(c) end) +cu:connect(5683,"192.168.18.101") cu:send("hello") ``` ####Do something shining ```lua - function led(r,g,b) - pwm.setduty(1,r) - pwm.setduty(2,g) - pwm.setduty(3,b) + function led(r,g,b) + pwm.setduty(1,r) + pwm.setduty(2,g) + pwm.setduty(3,b) end - pwm.setup(1,500,512) - pwm.setup(2,500,512) + pwm.setup(1,500,512) + pwm.setup(2,500,512) pwm.setup(3,500,512) - pwm.start(1) - pwm.start(2) + pwm.start(1) + pwm.start(2) pwm.start(3) led(512,0,0) -- red led(0,0,512) -- blue @@ -261,13 +261,13 @@ cu:send("hello") ```lua lighton=0 tmr.alarm(1,1000,1,function() - if lighton==0 then - lighton=1 - led(512,512,512) - else - lighton=0 - led(0,0,0) - end + if lighton==0 then + lighton=1 + led(512,512,512) + else + lighton=0 + led(0,0,0) + end end) ``` @@ -283,20 +283,20 @@ cu:send("hello") ####With below code, you can telnet to your esp8266 now ```lua -- a simple telnet server - s=net.createServer(net.TCP,180) - s:listen(2323,function(c) - function s_output(str) - if(c~=nil) - then c:send(str) - end - end + s=net.createServer(net.TCP,180) + s:listen(2323,function(c) + function s_output(str) + if(c~=nil) + then c:send(str) + end + end node.output(s_output, 0) -- re-direct output to function s_ouput. - c:on("receive",function(c,l) + c:on("receive",function(c,l) node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line - end) - c:on("disconnection",function(c) + end) + c:on("disconnection",function(c) node.output(nil) -- un-regist the redirect output function, output goes to serial - end) + end) print("Welcome to NodeMcu world.") end) ``` @@ -325,15 +325,15 @@ cu:send("hello") -- Don't forget to release it after use t = nil ds18b20 = nil - package.loaded["ds18b20"]=nil + package.loaded["ds18b20"]=nil ``` ####Control a WS2812 based light strip ```lua - -- set the color of one LED on GPIO 2 to red - ws2812.write(4, string.char(0, 255, 0)) - -- set the color of 10 LEDs on GPIO 0 to blue - ws2812.write(3, string.char(0, 0, 255):rep(10)) + -- set the color of one LED on GPIO2 to red + ws2812.writergb(4, string.char(255, 0, 0)) + -- set the color of 10 LEDs on GPIO0 to blue + ws2812.writergb(3, string.char(0, 0, 255):rep(10)) -- first LED green, second LED white - ws2812.write(4, string.char(255, 0, 0, 255, 255, 255)) + ws2812.writergb(4, string.char(0, 255, 0, 255, 255, 255)) ``` diff --git a/app/include/user_config.h b/app/include/user_config.h index c6ec1e0e..cf207dc3 100644 --- a/app/include/user_config.h +++ b/app/include/user_config.h @@ -68,7 +68,7 @@ #define LUA_USE_MODULES_OW #define LUA_USE_MODULES_BIT #define LUA_USE_MODULES_MQTT -// #define LUA_USE_MODULES_WS2812 // TODO: put this device specific module to device driver section. +#define LUA_USE_MODULES_WS2812 // TODO: put this device specific module to device driver section. #endif /* LUA_USE_MODULES */ // TODO: put device specific module to device driver section. diff --git a/app/modules/modules.h b/app/modules/modules.h index aaa98053..619bca89 100644 --- a/app/modules/modules.h +++ b/app/modules/modules.h @@ -120,17 +120,17 @@ #if defined(LUA_USE_MODULES_WS2812) #define MODULES_WS2812 "ws2812" #define ROM_MODULES_WS2812 \ - _ROM(MODULES_WS2812, luaopen_ws2812, ws2812_map) + _ROM(MODULES_WS2812, luaopen_ws2812, ws2812_map) #else #define ROM_MODULES_WS2812 #endif -#define LUA_MODULES_ROM \ +#define LUA_MODULES_ROM \ ROM_MODULES_GPIO \ - ROM_MODULES_PWM \ - ROM_MODULES_WIFI \ - ROM_MODULES_MQTT \ + ROM_MODULES_PWM \ + ROM_MODULES_WIFI \ + ROM_MODULES_MQTT \ ROM_MODULES_I2C \ ROM_MODULES_SPI \ ROM_MODULES_TMR \ @@ -140,8 +140,8 @@ ROM_MODULES_ADC \ ROM_MODULES_UART \ ROM_MODULES_OW \ - ROM_MODULES_BIT \ - ROM_MODULES_WS2812 + ROM_MODULES_BIT \ + ROM_MODULES_WS2812 #endif diff --git a/app/modules/ws2812.c b/app/modules/ws2812.c index aab28e92..b9419181 100644 --- a/app/modules/ws2812.c +++ b/app/modules/ws2812.c @@ -8,47 +8,56 @@ * from user Markus Gritsch. * I just put this code into its own module and pushed into a forked repo, * to easily create a pull request. Thanks to Markus Gritsch for the code. - * */ // ---------------------------------------------------------------------------- -// -- This WS2812 code must be compiled with -O2 to get the timing right. +// -- This WS2812 code must be compiled with -O2 to get the timing right. Read this: // -- http://wp.josh.com/2014/05/13/ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/ -// The ICACHE_FLASH_ATTR is there to trick the compiler and get the very first pulse width correct. +// -- The ICACHE_FLASH_ATTR is there to trick the compiler and get the very first pulse width correct. static void ICACHE_FLASH_ATTR send_ws_0(uint8_t gpio) { uint8_t i; - i = 4; - while (i--) - GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); - i = 9; - while (i--) - GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); + i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); + i = 9; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); } static void ICACHE_FLASH_ATTR send_ws_1(uint8_t gpio) { uint8_t i; - i = 8; - while (i--) - GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); - i = 6; - while (i--) - GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); + i = 8; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); + i = 6; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); } // Lua: ws2812.write(pin, "string") -// Byte triples in the string are interpreted as G R B values. -// ws2812.write(4, string.char(0, 255, 0)) uses GPIO2 and sets the first LED red. +// Byte triples in the string are interpreted as R G B values and sent to the hardware as G R B. +// ws2812.write(4, string.char(255, 0, 0)) uses GPIO2 and sets the first LED red. // ws2812.write(3, string.char(0, 0, 255):rep(10)) uses GPIO0 and sets ten LEDs blue. -// ws2812.write(4, string.char(255, 0, 0, 255, 255, 255)) first LED green, second LED white. -static int ICACHE_FLASH_ATTR ws2812_write(lua_State* L) { +// ws2812.write(4, string.char(0, 255, 0, 255, 255, 255)) first LED green, second LED white. +static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L) +{ const uint8_t pin = luaL_checkinteger(L, 1); size_t length; - const char *buffer = luaL_checklstring(L, 2, &length); + char *buffer = (char *)luaL_checklstring(L, 2, &length); // Cast away the constness. + // Initialize the output pin: platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(pin, 0); - os_delay_us(10); + // Ignore incomplete Byte triples at the end of buffer: + length -= length % 3; + + // Rearrange R G B values to G R B order needed by WS2812 LEDs: + size_t i; + for (i = 0; i < length; i += 3) { + const char r = buffer[i]; + const char g = buffer[i + 1]; + buffer[i] = g; + buffer[i + 1] = r; + } + + // Do not remove these: + os_delay_us(1); + os_delay_us(1); + + // Send the buffer: os_intr_lock(); const char * const end = buffer + length; while (buffer != end) { @@ -68,7 +77,7 @@ static int ICACHE_FLASH_ATTR ws2812_write(lua_State* L) { #include "lrodefs.h" const LUA_REG_TYPE ws2812_map[] = { - { LSTRKEY( "write" ), LFUNCVAL( ws2812_write )}, + { LSTRKEY( "writergb" ), LFUNCVAL( ws2812_writergb )}, { LNILKEY, LNILVAL} }; @@ -77,6 +86,3 @@ LUALIB_API int luaopen_ws2812(lua_State *L) { LREGISTER(L, "ws2812", ws2812_map); return 1; } - -// ---------------------------------------------------------------------------- - From ab1eb0b93b673a161dc29542eb92bc4eb9f0ff89 Mon Sep 17 00:00:00 2001 From: Markus Gritsch Date: Tue, 3 Mar 2015 20:51:16 +0100 Subject: [PATCH 8/8] Accidentally removed ROM_MODULES_U8G I am sorry, I made an error in my last pull request and accidentally removed ROM_MODULES_U8G. Here is it back again. --- app/modules/modules.h | 1 + 1 file changed, 1 insertion(+) diff --git a/app/modules/modules.h b/app/modules/modules.h index 20f13961..0730fd74 100644 --- a/app/modules/modules.h +++ b/app/modules/modules.h @@ -139,6 +139,7 @@ ROM_MODULES_PWM \ ROM_MODULES_WIFI \ ROM_MODULES_MQTT \ + ROM_MODULES_U8G \ ROM_MODULES_I2C \ ROM_MODULES_SPI \ ROM_MODULES_TMR \