Commit 6ea3d639 authored by HuangRui's avatar HuangRui
Browse files

Merge branch 'dev' of https://github.com/nodemcu/nodemcu-firmware

Conflicts:
	app/include/user_config.h
	ld/eagle.app.v6.ld
parents f51b4725 1d1f0857
...@@ -22,7 +22,7 @@ ifeq ($(OS),Windows_NT) ...@@ -22,7 +22,7 @@ ifeq ($(OS),Windows_NT)
else else
# It is gcc, may be cygwin # It is gcc, may be cygwin
# Can we use -fdata-sections? # Can we use -fdata-sections?
CCFLAGS += -Os -ffunction-sections -fno-jump-tables CCFLAGS += -Os -ffunction-sections -fno-jump-tables -fdata-sections
AR = xtensa-lx106-elf-ar AR = xtensa-lx106-elf-ar
CC = xtensa-lx106-elf-gcc CC = xtensa-lx106-elf-gcc
NM = xtensa-lx106-elf-nm NM = xtensa-lx106-elf-nm
...@@ -49,7 +49,7 @@ else ...@@ -49,7 +49,7 @@ else
else else
ESPPORT = $(COMPORT) ESPPORT = $(COMPORT)
endif endif
CCFLAGS += -Os -ffunction-sections -fno-jump-tables CCFLAGS += -Os -ffunction-sections -fno-jump-tables -fdata-sections
AR = xtensa-lx106-elf-ar AR = xtensa-lx106-elf-ar
CC = xtensa-lx106-elf-gcc CC = xtensa-lx106-elf-gcc
NM = xtensa-lx106-elf-nm NM = xtensa-lx106-elf-nm
......
...@@ -159,7 +159,7 @@ baudrate:9600 ...@@ -159,7 +159,7 @@ baudrate:9600
``` ```
####Manipulate hardware like a arduino ####Manipulate hardware like a arduino
```lua ```lua
pin = 1 pin = 1
gpio.mode(pin,gpio.OUTPUT) gpio.mode(pin,gpio.OUTPUT)
...@@ -168,10 +168,10 @@ baudrate:9600 ...@@ -168,10 +168,10 @@ baudrate:9600
``` ```
####Write network application in nodejs style ####Write network application in nodejs style
```lua ```lua
-- A simple http client -- 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:on("receive", function(conn, payload) print(payload) end )
conn:connect(80,"115.239.210.27") conn:connect(80,"115.239.210.27")
conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n" conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n"
...@@ -179,15 +179,15 @@ baudrate:9600 ...@@ -179,15 +179,15 @@ baudrate:9600
``` ```
####Or a simple http server ####Or a simple http server
```lua ```lua
-- A simple http server -- A simple http server
srv=net.createServer(net.TCP) srv=net.createServer(net.TCP)
srv:listen(80,function(conn) srv:listen(80,function(conn)
conn:on("receive",function(conn,payload) conn:on("receive",function(conn,payload)
print(payload) print(payload)
conn:send("<h1> Hello, NodeMcu.</h1>") conn:send("<h1> Hello, NodeMcu.</h1>")
end) end)
conn:on("sent",function(conn) conn:close() end) conn:on("sent",function(conn) conn:close() end)
end) end)
``` ```
...@@ -199,7 +199,7 @@ baudrate:9600 ...@@ -199,7 +199,7 @@ baudrate:9600
m = mqtt.Client("clientid", 120, "user", "password") m = mqtt.Client("clientid", 120, "user", "password")
-- setup Last Will and Testament (optional) -- 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 -- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0) m:lwt("/lwt", "offline", 0, 0)
...@@ -207,8 +207,8 @@ m:on("connect", function(con) print ("connected") end) ...@@ -207,8 +207,8 @@ m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end) m:on("offline", function(con) print ("offline") end)
-- on publish message receive event -- on publish message receive event
m:on("message", function(conn, topic, data) m:on("message", function(conn, topic, data)
print(topic .. ":" ) print(topic .. ":" )
if data ~= nil then if data ~= nil then
print(data) print(data)
end end
...@@ -232,29 +232,29 @@ m:close(); ...@@ -232,29 +232,29 @@ m:close();
#### UDP client and server #### UDP client and server
```lua ```lua
-- a udp server -- a udp server
s=net.createServer(net.UDP) s=net.createServer(net.UDP)
s:on("receive",function(s,c) print(c) end) s:on("receive",function(s,c) print(c) end)
s:listen(5683) s:listen(5683)
-- a udp client -- a udp client
cu=net.createConnection(net.UDP) cu=net.createConnection(net.UDP)
cu:on("receive",function(cu,c) print(c) end) cu:on("receive",function(cu,c) print(c) end)
cu:connect(5683,"192.168.18.101") cu:connect(5683,"192.168.18.101")
cu:send("hello") cu:send("hello")
``` ```
####Do something shining ####Do something shining
```lua ```lua
function led(r,g,b) function led(r,g,b)
pwm.setduty(1,r) pwm.setduty(1,r)
pwm.setduty(2,g) pwm.setduty(2,g)
pwm.setduty(3,b) pwm.setduty(3,b)
end end
pwm.setup(1,500,512) pwm.setup(1,500,512)
pwm.setup(2,500,512) pwm.setup(2,500,512)
pwm.setup(3,500,512) pwm.setup(3,500,512)
pwm.start(1) pwm.start(1)
pwm.start(2) pwm.start(2)
pwm.start(3) pwm.start(3)
led(512,0,0) -- red led(512,0,0) -- red
led(0,0,512) -- blue led(0,0,512) -- blue
...@@ -264,13 +264,13 @@ cu:send("hello") ...@@ -264,13 +264,13 @@ cu:send("hello")
```lua ```lua
lighton=0 lighton=0
tmr.alarm(1,1000,1,function() tmr.alarm(1,1000,1,function()
if lighton==0 then if lighton==0 then
lighton=1 lighton=1
led(512,512,512) led(512,512,512)
else else
lighton=0 lighton=0
led(0,0,0) led(0,0,0)
end end
end) end)
``` ```
...@@ -286,20 +286,20 @@ cu:send("hello") ...@@ -286,20 +286,20 @@ cu:send("hello")
####With below code, you can telnet to your esp8266 now ####With below code, you can telnet to your esp8266 now
```lua ```lua
-- a simple telnet server -- a simple telnet server
s=net.createServer(net.TCP,180) s=net.createServer(net.TCP,180)
s:listen(2323,function(c) s:listen(2323,function(c)
function s_output(str) function s_output(str)
if(c~=nil) if(c~=nil)
then c:send(str) then c:send(str)
end end
end end
node.output(s_output, 0) -- re-direct output to function s_ouput. 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 node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
end) end)
c:on("disconnection",function(c) c:on("disconnection",function(c)
node.output(nil) -- un-regist the redirect output function, output goes to serial node.output(nil) -- un-regist the redirect output function, output goes to serial
end) end)
print("Welcome to NodeMcu world.") print("Welcome to NodeMcu world.")
end) end)
``` ```
...@@ -328,15 +328,67 @@ cu:send("hello") ...@@ -328,15 +328,67 @@ cu:send("hello")
-- Don't forget to release it after use -- Don't forget to release it after use
t = nil t = nil
ds18b20 = nil ds18b20 = nil
package.loaded["ds18b20"]=nil package.loaded["ds18b20"]=nil
```
####Operate a display via I2c with u8glib
u8glib is a graphics library with support for many different displays.
The integration in nodemcu is developed for SSD1306 based display attached via the I2C port. Further display types and SPI connectivity will be added in the future.
U8glib v1.17
#####I2C connection
Hook up SDA and SCL to any free GPIOs. Eg. [lua_examples/graphics_test.lua](https://github.com/devsaurus/nodemcu-firmware/blob/dev/lua_examples/graphics_test.lua) expects SDA=5 (GPIO14) and SCL=6 (GPIO12). They are used to set up nodemcu's I2C driver before accessing the display:
```lua
sda = 5
scl = 6
i2c.setup(0, sda, scl, i2c.SLOW)
``` ```
#####Library usage
The Lua bindings for this library closely follow u8glib's object oriented C++ API. Based on the u8g class, you create an object for your display type:
```lua
sla = 0x3c
disp = u8g.ssd1306_128x64_i2c(sla)
```
This object provides all of u8glib's methods to control the display.
Again, refer to [lua_examples/graphics_test.lua](https://github.com/devsaurus/nodemcu-firmware/blob/dev/lua_examples/u8g_graphics_test.lua) to get an impression how this is achieved with Lua code. Visit the [u8glib homepage](https://code.google.com/p/u8glib/) for technical details.
#####Fonts
u8glib comes with a wide range of fonts for small displays. Since they need to be compiled into the firmware image, you'd need to include them in [app/include/user_config.h](https://github.com/devsaurus/nodemcu-firmware/blob/dev/app/include/user_config.h) and recompile. Simply add the desired fonts to the font table:
```c
#define U8G_FONT_TABLE \
U8G_FONT_TABLE_ENTRY(font_6x10) \
U8G_FONT_TABLE_ENTRY(font_chikita)
```
They'll be available as `u8g.<font_name>` in Lua.
#####Unimplemented functions
- [ ] Cursor handling
- [ ] disableCursor()
- [ ] enableCursor()
- [ ] setCursorColor()
- [ ] setCursorFont()
- [ ] setCursorPos()
- [ ] setCursorStyle()
- [ ] Bitmaps
- [ ] drawBitmap()
- [ ] drawXBM()
- [ ] General functions
- [x] begin()
- [ ] print()
- [ ] setContrast()
- [ ] setPrintPos()
- [ ] setHardwareBackup()
- [ ] setRGB()
####Control a WS2812 based light strip ####Control a WS2812 based light strip
```lua ```lua
-- set the color of one LED on GPIO 2 to red -- set the color of one LED on GPIO2 to red
ws2812.write(4, string.char(0, 255, 0)) ws2812.writergb(4, string.char(255, 0, 0))
-- set the color of 10 LEDs on GPIO 0 to blue -- set the color of 10 LEDs on GPIO0 to blue
ws2812.write(3, string.char(0, 0, 255):rep(10)) ws2812.writergb(3, string.char(0, 0, 255):rep(10))
-- first LED green, second LED white -- 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))
``` ```
*.output* *.output*
mapfile
!.gitignore !.gitignore
...@@ -31,6 +31,7 @@ SUBDIRS= \ ...@@ -31,6 +31,7 @@ SUBDIRS= \
libc \ libc \
lua \ lua \
mqtt \ mqtt \
u8glib \
smart \ smart \
wofs \ wofs \
modules \ modules \
...@@ -77,6 +78,7 @@ COMPONENTS_eagle.app.v6 = \ ...@@ -77,6 +78,7 @@ COMPONENTS_eagle.app.v6 = \
libc/liblibc.a \ libc/liblibc.a \
lua/liblua.a \ lua/liblua.a \
mqtt/mqtt.a \ mqtt/mqtt.a \
u8glib/u8glib.a \
smart/smart.a \ smart/smart.a \
wofs/wofs.a \ wofs/wofs.a \
spiffs/spiffs.a \ spiffs/spiffs.a \
...@@ -84,6 +86,8 @@ COMPONENTS_eagle.app.v6 = \ ...@@ -84,6 +86,8 @@ COMPONENTS_eagle.app.v6 = \
LINKFLAGS_eagle.app.v6 = \ LINKFLAGS_eagle.app.v6 = \
-L../lib \ -L../lib \
-Wl,--gc-sections \
-Xlinker -Map=mapfile \
-nostdlib \ -nostdlib \
-T$(LD_FILE) \ -T$(LD_FILE) \
-Wl,--no-check-sections \ -Wl,--no-check-sections \
......
#ifndef __USER_CONFIG_H__ #ifndef __USER_CONFIG_H__
#define __USER_CONFIG_H__ #define __USER_CONFIG_H__
#define NODE_VERSION_MAJOR 0U
#define NODE_VERSION_MINOR 9U
#define NODE_VERSION_REVISION 6U
#define NODE_VERSION_INTERNAL 0U
#define NODE_VERSION "NodeMCU 0.9.6"
#define BUILD_DATE "build 20150216"
// #define DEVKIT_VERSION_0_9 1 // define this only if you use NodeMCU devkit v0.9 // #define DEVKIT_VERSION_0_9 1 // define this only if you use NodeMCU devkit v0.9
// #define FLASH_512K // #define FLASH_512K
...@@ -51,31 +43,7 @@ ...@@ -51,31 +43,7 @@
// #define BUILD_WOFS 1 // #define BUILD_WOFS 1
#define BUILD_SPIFFS 1 #define BUILD_SPIFFS 1
#define LUA_USE_MODULES // #define LUA_NUMBER_INTEGRAL
#ifdef LUA_USE_MODULES
#define LUA_USE_MODULES_NODE
#define LUA_USE_MODULES_FILE
#define LUA_USE_MODULES_GPIO
#define LUA_USE_MODULES_WIFI
#define LUA_USE_MODULES_NET
#define LUA_USE_MODULES_PWM
#define LUA_USE_MODULES_I2C
#define LUA_USE_MODULES_SPI
#define LUA_USE_MODULES_TMR
#define LUA_USE_MODULES_ADC
#define LUA_USE_MODULES_UART
#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.
#endif /* LUA_USE_MODULES */
// TODO: put device specific module to device driver section.
#ifdef LUA_USE_DEVICE_DRIVER
#define LUA_USE_DEVICE_WS2812
#endif /* LUA_USE_DEVICE_DRIVER */
#define LUA_OPTRAM #define LUA_OPTRAM
#ifdef LUA_OPTRAM #ifdef LUA_OPTRAM
...@@ -96,4 +64,12 @@ ...@@ -96,4 +64,12 @@
#define LED_LOW_COUNT_DEFAULT 0 #define LED_LOW_COUNT_DEFAULT 0
#endif #endif
// Configure U8glib fonts
// add a U8G_FONT_TABLE_ENTRY for each font you want to compile into the image
#define U8G_FONT_TABLE_ENTRY(font)
#define U8G_FONT_TABLE \
U8G_FONT_TABLE_ENTRY(font_6x10) \
U8G_FONT_TABLE_ENTRY(font_chikita)
#undef U8G_FONT_TABLE_ENTRY
#endif /* __USER_CONFIG_H__ */ #endif /* __USER_CONFIG_H__ */
#ifndef __USER_MODULES_H__
#define __USER_MODULES_H__
#define LUA_USE_MODULES
#ifdef LUA_USE_MODULES
#define LUA_USE_MODULES_NODE
#define LUA_USE_MODULES_FILE
#define LUA_USE_MODULES_GPIO
#define LUA_USE_MODULES_WIFI
#define LUA_USE_MODULES_NET
#define LUA_USE_MODULES_PWM
#define LUA_USE_MODULES_I2C
#define LUA_USE_MODULES_SPI
#define LUA_USE_MODULES_TMR
#define LUA_USE_MODULES_ADC
#define LUA_USE_MODULES_UART
#define LUA_USE_MODULES_OW
#define LUA_USE_MODULES_BIT
#define LUA_USE_MODULES_U8G
#define LUA_USE_MODULES_MQTT
#define LUA_USE_MODULES_WS2812
#endif /* LUA_USE_MODULES */
#endif /* __USER_MODULES_H__ */
#ifndef __USER_VERSION_H__
#define __USER_VERSION_H__
#define NODE_VERSION_MAJOR 0U
#define NODE_VERSION_MINOR 9U
#define NODE_VERSION_REVISION 6U
#define NODE_VERSION_INTERNAL 0U
#define NODE_VERSION "NodeMCU 0.9.6"
#define BUILD_DATE "build 20150216"
#endif /* __USER_VERSION_H__ */
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "c_stdlib.h" #include "c_stdlib.h"
#include "c_string.h" #include "c_string.h"
#include "flash_fs.h" #include "flash_fs.h"
#include "user_version.h"
#define lua_c #define lua_c
......
...@@ -40,6 +40,7 @@ INCLUDES := $(INCLUDES) -I $(PDIR)include ...@@ -40,6 +40,7 @@ INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./ INCLUDES += -I ./
INCLUDES += -I ../libc INCLUDES += -I ../libc
INCLUDES += -I ../mqtt INCLUDES += -I ../mqtt
INCLUDES += -I ../u8glib
INCLUDES += -I ../lua INCLUDES += -I ../lua
INCLUDES += -I ../platform INCLUDES += -I ../platform
INCLUDES += -I ../wofs INCLUDES += -I ../wofs
......
...@@ -64,6 +64,9 @@ LUALIB_API int ( luaopen_wifi )( lua_State *L ); ...@@ -64,6 +64,9 @@ LUALIB_API int ( luaopen_wifi )( lua_State *L );
#define AUXLIB_MQTT "mqtt" #define AUXLIB_MQTT "mqtt"
LUALIB_API int ( luaopen_mqtt )( lua_State *L ); LUALIB_API int ( luaopen_mqtt )( lua_State *L );
#define AUXLIB_U8G "u8g"
LUALIB_API int ( luaopen_u8g )( lua_State *L );
#define AUXLIB_NODE "node" #define AUXLIB_NODE "node"
LUALIB_API int ( luaopen_node )( lua_State *L ); LUALIB_API int ( luaopen_node )( lua_State *L );
......
...@@ -111,7 +111,7 @@ static int file_seek (lua_State *L) ...@@ -111,7 +111,7 @@ static int file_seek (lua_State *L)
long offset = luaL_optlong(L, 2, 0); long offset = luaL_optlong(L, 2, 0);
op = fs_seek(file_fd, offset, mode[op]); op = fs_seek(file_fd, offset, mode[op]);
if (op) if (op)
lua_pushboolean(L, 1); /* error */ lua_pushnil(L); /* error */
else else
lua_pushinteger(L, fs_tell(file_fd)); lua_pushinteger(L, fs_tell(file_fd));
return 1; return 1;
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include "lrotable.h" #include "lrotable.h"
#include "luaconf.h" #include "luaconf.h"
#include "user_modules.h"
#if defined(LUA_USE_MODULES) #if defined(LUA_USE_MODULES)
#include "modules.h" #include "modules.h"
#endif #endif
......
...@@ -45,6 +45,14 @@ ...@@ -45,6 +45,14 @@
#define ROM_MODULES_MQTT #define ROM_MODULES_MQTT
#endif #endif
#if defined(LUA_USE_MODULES_U8G)
#define MODULES_U8G "u8g"
#define ROM_MODULES_U8G \
_ROM(MODULES_U8G, luaopen_u8g, lu8g_map)
#else
#define ROM_MODULES_U8G
#endif
#if defined(LUA_USE_MODULES_I2C) #if defined(LUA_USE_MODULES_I2C)
#define MODULES_I2C "i2c" #define MODULES_I2C "i2c"
#define ROM_MODULES_I2C \ #define ROM_MODULES_I2C \
...@@ -120,17 +128,18 @@ ...@@ -120,17 +128,18 @@
#if defined(LUA_USE_MODULES_WS2812) #if defined(LUA_USE_MODULES_WS2812)
#define MODULES_WS2812 "ws2812" #define MODULES_WS2812 "ws2812"
#define ROM_MODULES_WS2812 \ #define ROM_MODULES_WS2812 \
_ROM(MODULES_WS2812, luaopen_ws2812, ws2812_map) _ROM(MODULES_WS2812, luaopen_ws2812, ws2812_map)
#else #else
#define ROM_MODULES_WS2812 #define ROM_MODULES_WS2812
#endif #endif
#define LUA_MODULES_ROM \ #define LUA_MODULES_ROM \
ROM_MODULES_GPIO \ ROM_MODULES_GPIO \
ROM_MODULES_PWM \ ROM_MODULES_PWM \
ROM_MODULES_WIFI \ ROM_MODULES_WIFI \
ROM_MODULES_MQTT \ ROM_MODULES_MQTT \
ROM_MODULES_U8G \
ROM_MODULES_I2C \ ROM_MODULES_I2C \
ROM_MODULES_SPI \ ROM_MODULES_SPI \
ROM_MODULES_TMR \ ROM_MODULES_TMR \
...@@ -140,8 +149,8 @@ ...@@ -140,8 +149,8 @@
ROM_MODULES_ADC \ ROM_MODULES_ADC \
ROM_MODULES_UART \ ROM_MODULES_UART \
ROM_MODULES_OW \ ROM_MODULES_OW \
ROM_MODULES_BIT \ ROM_MODULES_BIT \
ROM_MODULES_WS2812 ROM_MODULES_WS2812
#endif #endif
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "user_interface.h" #include "user_interface.h"
#include "flash_api.h" #include "flash_api.h"
#include "flash_fs.h" #include "flash_fs.h"
#include "user_version.h"
// Lua: restart() // Lua: restart()
static int node_restart( lua_State* L ) static int node_restart( lua_State* L )
......
This diff is collapsed.
...@@ -8,47 +8,56 @@ ...@@ -8,47 +8,56 @@
* from user Markus Gritsch. * from user Markus Gritsch.
* I just put this code into its own module and pushed into a forked repo, * 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. * 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/ // -- 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) { static void ICACHE_FLASH_ATTR send_ws_0(uint8_t gpio) {
uint8_t i; uint8_t i;
i = 4; i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
while (i--) i = 9; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
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) { static void ICACHE_FLASH_ATTR send_ws_1(uint8_t gpio) {
uint8_t i; uint8_t i;
i = 8; i = 8; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio);
while (i--) i = 6; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio);
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") // Lua: ws2812.write(pin, "string")
// Byte triples in the string are interpreted as G R B values. // 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(0, 255, 0)) uses GPIO2 and sets the first LED red. // 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(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. // ws2812.write(4, string.char(0, 255, 0, 255, 255, 255)) first LED green, second LED white.
static int ICACHE_FLASH_ATTR ws2812_write(lua_State* L) { static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L)
{
const uint8_t pin = luaL_checkinteger(L, 1); const uint8_t pin = luaL_checkinteger(L, 1);
size_t length; 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_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
platform_gpio_write(pin, 0); 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(); os_intr_lock();
const char * const end = buffer + length; const char * const end = buffer + length;
while (buffer != end) { while (buffer != end) {
...@@ -68,7 +77,7 @@ static int ICACHE_FLASH_ATTR ws2812_write(lua_State* L) { ...@@ -68,7 +77,7 @@ static int ICACHE_FLASH_ATTR ws2812_write(lua_State* L) {
#include "lrodefs.h" #include "lrodefs.h"
const LUA_REG_TYPE ws2812_map[] = const LUA_REG_TYPE ws2812_map[] =
{ {
{ LSTRKEY( "write" ), LFUNCVAL( ws2812_write )}, { LSTRKEY( "writergb" ), LFUNCVAL( ws2812_writergb )},
{ LNILKEY, LNILVAL} { LNILKEY, LNILVAL}
}; };
...@@ -77,6 +86,3 @@ LUALIB_API int luaopen_ws2812(lua_State *L) { ...@@ -77,6 +86,3 @@ LUALIB_API int luaopen_ws2812(lua_State *L) {
LREGISTER(L, "ws2812", ws2812_map); LREGISTER(L, "ws2812", ws2812_map);
return 1; return 1;
} }
// ----------------------------------------------------------------------------
#############################################################
# Required variables for each makefile
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
#
ifndef PDIR
GEN_LIBS = u8glib.a
endif
#############################################################
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#
#DEFINES +=
#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#
INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./
INCLUDES += -I ../libc
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile
This diff is collapsed.
/*
u8g_bitmap.c
Universal 8bit Graphics Library
Copyright (c) 2011, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "u8g.h"
void u8g_DrawHBitmap(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t cnt, const uint8_t *bitmap)
{
while( cnt > 0 )
{
u8g_Draw8Pixel(u8g, x, y, 0, *bitmap);
bitmap++;
cnt--;
x+=8;
}
}
void u8g_DrawBitmap(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t cnt, u8g_uint_t h, const uint8_t *bitmap)
{
if ( u8g_IsBBXIntersection(u8g, x, y, cnt*8, h) == 0 )
return;
while( h > 0 )
{
u8g_DrawHBitmap(u8g, x, y, cnt, bitmap);
bitmap += cnt;
y++;
h--;
}
}
void u8g_DrawHBitmapP(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t cnt, const u8g_pgm_uint8_t *bitmap)
{
while( cnt > 0 )
{
u8g_Draw8Pixel(u8g, x, y, 0, u8g_pgm_read(bitmap));
bitmap++;
cnt--;
x+=8;
}
}
void u8g_DrawBitmapP(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t cnt, u8g_uint_t h, const u8g_pgm_uint8_t *bitmap)
{
if ( u8g_IsBBXIntersection(u8g, x, y, cnt*8, h) == 0 )
return;
while( h > 0 )
{
u8g_DrawHBitmapP(u8g, x, y, cnt, bitmap);
bitmap += cnt;
y++;
h--;
}
}
/*=========================================================================*/
static void u8g_DrawHXBM(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, const uint8_t *bitmap)
{
uint8_t d;
x+=7;
while( w >= 8 )
{
u8g_Draw8Pixel(u8g, x, y, 2, *bitmap);
bitmap++;
w-= 8;
x+=8;
}
if ( w > 0 )
{
d = *bitmap;
x -= 7;
do
{
if ( d & 1 )
u8g_DrawPixel(u8g, x, y);
x++;
w--;
d >>= 1;
} while ( w > 0 );
}
}
void u8g_DrawXBM(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h, const uint8_t *bitmap)
{
u8g_uint_t b;
b = w;
b += 7;
b >>= 3;
if ( u8g_IsBBXIntersection(u8g, x, y, w, h) == 0 )
return;
while( h > 0 )
{
u8g_DrawHXBM(u8g, x, y, w, bitmap);
bitmap += b;
y++;
h--;
}
}
static void u8g_DrawHXBMP(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, const u8g_pgm_uint8_t *bitmap)
{
uint8_t d;
x+=7;
while( w >= 8 )
{
u8g_Draw8Pixel(u8g, x, y, 2, u8g_pgm_read(bitmap));
bitmap++;
w-= 8;
x+=8;
}
if ( w > 0 )
{
d = u8g_pgm_read(bitmap);
x -= 7;
do
{
if ( d & 1 )
u8g_DrawPixel(u8g, x, y);
x++;
w--;
d >>= 1;
} while ( w > 0 );
}
}
void u8g_DrawXBMP(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h, const u8g_pgm_uint8_t *bitmap)
{
u8g_uint_t b;
b = w;
b += 7;
b >>= 3;
if ( u8g_IsBBXIntersection(u8g, x, y, w, h) == 0 )
return;
while( h > 0 )
{
u8g_DrawHXBMP(u8g, x, y, w, bitmap);
bitmap += b;
y++;
h--;
}
}
/*
u8g_circle.c
Utility to draw empty and filled circles.
Universal 8bit Graphics Library
Copyright (c) 2011, bjthom@gmail.com
u8g_DrawCircle & u8g_DrawDisc by olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Addition to the U8G Library 02/25/12
*/
#include "u8g.h"
#ifdef OLD_CODE
void circ_upperRight(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
u8g_DrawPixel(u8g, x0 + x, y0 - y);
u8g_DrawPixel(u8g, x0 + y, y0 - x);
}
void circ_upperLeft(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
u8g_DrawPixel(u8g, x0 - x, y0 - y);
u8g_DrawPixel(u8g, x0 - y, y0 - x);
}
void circ_lowerRight(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
u8g_DrawPixel(u8g, x0 + x, y0 + y);
u8g_DrawPixel(u8g, x0 + y, y0 + x);
}
void circ_lowerLeft(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
u8g_DrawPixel(u8g, x0 - x, y0 + y);
u8g_DrawPixel(u8g, x0 - y, y0 + x);
}
void circ_all(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
circ_upperRight(u8g, x, y, x0, y0);
circ_upperLeft(u8g, x, y, x0, y0);
circ_lowerRight(u8g, x, y, x0, y0);
circ_lowerLeft(u8g, x, y, x0, y0);
}
void u8g_DrawEmpCirc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
if ( u8g_IsBBXIntersection(u8g, x0-rad-1, y0-rad-1, 2*rad+1, 2*rad+1) == 0)
return;
int f = 1 - rad;
int ddF_x = 1;
int ddF_y = -2*rad;
uint8_t x = 0;
uint8_t y = rad;
void ( *circ_util )(u8g_t *, u8g_uint_t, u8g_uint_t, u8g_uint_t, u8g_uint_t);
switch (option)
{
case U8G_CIRC_UPPER_RIGHT:
u8g_DrawPixel(u8g, x0, y0 - rad);
u8g_DrawPixel(u8g, x0 + rad, y0);
circ_util = circ_upperRight;
break;
case U8G_CIRC_UPPER_LEFT:
u8g_DrawPixel(u8g, x0, y0 - rad);
u8g_DrawPixel(u8g, x0 - rad, y0);
circ_util = circ_upperLeft;
break;
case U8G_CIRC_LOWER_RIGHT:
u8g_DrawPixel(u8g, x0, y0 + rad);
u8g_DrawPixel(u8g, x0 + rad, y0);
circ_util = circ_lowerRight;
break;
case U8G_CIRC_LOWER_LEFT:
u8g_DrawPixel(u8g, x0, y0 + rad);
u8g_DrawPixel(u8g, x0 - rad, y0);
circ_util = circ_lowerLeft;
break;
default:
case U8G_CIRC_ALL:
u8g_DrawPixel(u8g, x0, y0 + rad);
u8g_DrawPixel(u8g, x0, y0 - rad);
u8g_DrawPixel(u8g, x0 + rad, y0);
u8g_DrawPixel(u8g, x0 - rad, y0);
circ_util = circ_all;
break;
}
while( x < y )
{
if(f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
circ_util(u8g, x, y, x0, y0);
}
}
void u8g_DrawFillCirc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
if ( u8g_IsBBXIntersection(u8g, x0-rad-1, y0-rad-1, 2*rad+1, 2*rad+1) == 0)
return;
int f = 1 - rad;
int ddF_x = 1;
int ddF_y = -2*rad;
uint8_t x = 0;
uint8_t y = rad;
// Draw vertical diameter at the horiz. center
// u8g_DrawVLine(u8g, x0, y0 - rad, 2*rad+1);
if (option == U8G_CIRC_UPPER_LEFT || option == U8G_CIRC_UPPER_RIGHT) {
u8g_DrawVLine(u8g, x0, y0 - rad, rad+1);
}
else if (option == U8G_CIRC_LOWER_LEFT || option == U8G_CIRC_LOWER_RIGHT) {
u8g_DrawVLine(u8g, x0, y0, rad+1);
}
else {
u8g_DrawVLine(u8g, x0, y0 - rad, 2*rad+1);
}
while( x < y )
{
if(f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
//Draw vertical lines from one point to another
switch (option)
{
case U8G_CIRC_UPPER_RIGHT:
u8g_DrawVLine(u8g, x0+x, y0-y, y+1);
u8g_DrawVLine(u8g, x0+y, y0-x, x+1);
break;
case U8G_CIRC_UPPER_LEFT:
u8g_DrawVLine(u8g, x0-x, y0-y, y+1);
u8g_DrawVLine(u8g, x0-y, y0-x, x+1);
break;
case U8G_CIRC_LOWER_RIGHT:
u8g_DrawVLine(u8g, x0+x, y0, y+1);
u8g_DrawVLine(u8g, x0+y, y0, x+1);
break;
case U8G_CIRC_LOWER_LEFT:
u8g_DrawVLine(u8g, x0-x, y0, y+1);
u8g_DrawVLine(u8g, x0-y, y0, x+1);
break;
case U8G_CIRC_ALL:
u8g_DrawVLine(u8g, x0+x, y0-y, 2*y+1);
u8g_DrawVLine(u8g, x0-x, y0-y, 2*y+1);
u8g_DrawVLine(u8g, x0+y, y0-x, 2*x+1);
u8g_DrawVLine(u8g, x0-y, y0-x, 2*x+1);
break;
}
}
}
#endif
/*=========================================================================*/
static void u8g_draw_circle_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option) U8G_NOINLINE;
static void u8g_draw_circle_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
{
/* upper right */
if ( option & U8G_DRAW_UPPER_RIGHT )
{
u8g_DrawPixel(u8g, x0 + x, y0 - y);
u8g_DrawPixel(u8g, x0 + y, y0 - x);
}
/* upper left */
if ( option & U8G_DRAW_UPPER_LEFT )
{
u8g_DrawPixel(u8g, x0 - x, y0 - y);
u8g_DrawPixel(u8g, x0 - y, y0 - x);
}
/* lower right */
if ( option & U8G_DRAW_LOWER_RIGHT )
{
u8g_DrawPixel(u8g, x0 + x, y0 + y);
u8g_DrawPixel(u8g, x0 + y, y0 + x);
}
/* lower left */
if ( option & U8G_DRAW_LOWER_LEFT )
{
u8g_DrawPixel(u8g, x0 - x, y0 + y);
u8g_DrawPixel(u8g, x0 - y, y0 + x);
}
}
void u8g_draw_circle(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
u8g_int_t f;
u8g_int_t ddF_x;
u8g_int_t ddF_y;
u8g_uint_t x;
u8g_uint_t y;
f = 1;
f -= rad;
ddF_x = 1;
ddF_y = 0;
ddF_y -= rad;
ddF_y *= 2;
x = 0;
y = rad;
u8g_draw_circle_section(u8g, x, y, x0, y0, option);
while ( x < y )
{
if (f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
u8g_draw_circle_section(u8g, x, y, x0, y0, option);
}
}
void u8g_DrawCircle(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
/* check for bounding box */
{
u8g_uint_t radp, radp2;
radp = rad;
radp++;
radp2 = radp;
radp2 *= 2;
if ( u8g_IsBBXIntersection(u8g, x0-radp, y0-radp, radp2, radp2) == 0)
return;
}
/* draw circle */
u8g_draw_circle(u8g, x0, y0, rad, option);
}
static void u8g_draw_disc_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option) U8G_NOINLINE;
static void u8g_draw_disc_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
{
/* upper right */
if ( option & U8G_DRAW_UPPER_RIGHT )
{
u8g_DrawVLine(u8g, x0+x, y0-y, y+1);
u8g_DrawVLine(u8g, x0+y, y0-x, x+1);
}
/* upper left */
if ( option & U8G_DRAW_UPPER_LEFT )
{
u8g_DrawVLine(u8g, x0-x, y0-y, y+1);
u8g_DrawVLine(u8g, x0-y, y0-x, x+1);
}
/* lower right */
if ( option & U8G_DRAW_LOWER_RIGHT )
{
u8g_DrawVLine(u8g, x0+x, y0, y+1);
u8g_DrawVLine(u8g, x0+y, y0, x+1);
}
/* lower left */
if ( option & U8G_DRAW_LOWER_LEFT )
{
u8g_DrawVLine(u8g, x0-x, y0, y+1);
u8g_DrawVLine(u8g, x0-y, y0, x+1);
}
}
void u8g_draw_disc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
u8g_int_t f;
u8g_int_t ddF_x;
u8g_int_t ddF_y;
u8g_uint_t x;
u8g_uint_t y;
f = 1;
f -= rad;
ddF_x = 1;
ddF_y = 0;
ddF_y -= rad;
ddF_y *= 2;
x = 0;
y = rad;
u8g_draw_disc_section(u8g, x, y, x0, y0, option);
while ( x < y )
{
if (f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
u8g_draw_disc_section(u8g, x, y, x0, y0, option);
}
}
void u8g_DrawDisc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
/* check for bounding box */
{
u8g_uint_t radp, radp2;
radp = rad;
radp++;
radp2 = radp;
radp2 *= 2;
if ( u8g_IsBBXIntersection(u8g, x0-radp, y0-radp, radp2, radp2) == 0)
return;
}
/* draw disc */
u8g_draw_disc(u8g, x0, y0, rad, option);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment