From 6f31a202409bf9d1fd007ad3e5acdd6b36be9464 Mon Sep 17 00:00:00 2001 From: cal Date: Fri, 1 May 2015 17:39:47 +0200 Subject: [PATCH 1/7] Makefile fix: "-Os" was overridden by "-O2" --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 85bc27a9..19feb38d 100644 --- a/Makefile +++ b/Makefile @@ -103,9 +103,15 @@ OIMAGES := $(GEN_IMAGES:%=$(IMAGEODIR)/%) BINODIR := $(ODIR)/$(TARGET)/$(FLAVOR)/bin OBINS := $(GEN_BINS:%=$(BINODIR)/%) +# +# Note: +# https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html +# If you add global optimize options like "-O2" here +# they will override "-Os" defined above. +# "-Os" should be used to reduce code size +# CCFLAGS += \ -g \ - -O2 \ -Wpointer-arith \ -Wundef \ -Werror \ From 8ceb536f7ced7019dc76a94729f41b3c910b697a Mon Sep 17 00:00:00 2001 From: devsaurus Date: Thu, 14 May 2015 23:32:34 +0200 Subject: [PATCH 2/7] update missing functions --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 75bdcc7f..49d40c5e 100644 --- a/README.md +++ b/README.md @@ -426,6 +426,9 @@ In contrast to the source code based inclusion of XBMs into u8glib, it's require - [ ] setPrintPos() - [ ] setHardwareBackup() - [ ] setRGB() +- [ ] getStrWidth() +- [ ] setDefaultMidColor() +- [ ] setFontLineSpacingFactor() ####Control a WS2812 based light strip From de56cb084eed15d4d7522da71f7583e40d49b2fa Mon Sep 17 00:00:00 2001 From: devsaurus Date: Fri, 15 May 2015 21:13:59 +0200 Subject: [PATCH 3/7] sort member functions --- app/modules/u8g.c | 68 +++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/app/modules/u8g.c b/app/modules/u8g.c index fc39803b..326eea59 100644 --- a/app/modules/u8g.c +++ b/app/modules/u8g.c @@ -1140,53 +1140,53 @@ static int lu8g_pcd8544_84x48( lua_State *L ) static const LUA_REG_TYPE lu8g_display_map[] = { { LSTRKEY( "begin" ), LFUNCVAL( lu8g_begin ) }, - { LSTRKEY( "setFont" ), LFUNCVAL( lu8g_setFont ) }, - { LSTRKEY( "setFontRefHeightAll" ), LFUNCVAL( lu8g_setFontRefHeightAll ) }, - { LSTRKEY( "setFontRefHeightExtendedText" ), LFUNCVAL( lu8g_setFontRefHeightExtendedText ) }, - { LSTRKEY( "setFontRefHeightText" ), LFUNCVAL( lu8g_setFontRefHeightText ) }, - { LSTRKEY( "setDefaultBackgroundColor" ), LFUNCVAL( lu8g_setDefaultBackgroundColor ) }, - { LSTRKEY( "setDefaultForegroundColor" ), LFUNCVAL( lu8g_setDefaultForegroundColor ) }, - { LSTRKEY( "setFontPosBaseline" ), LFUNCVAL( lu8g_setFontPosBaseline ) }, - { LSTRKEY( "setFontPosBottom" ), LFUNCVAL( lu8g_setFontPosBottom ) }, - { LSTRKEY( "setFontPosCenter" ), LFUNCVAL( lu8g_setFontPosCenter ) }, - { LSTRKEY( "setFontPosTop" ), LFUNCVAL( lu8g_setFontPosTop ) }, - { LSTRKEY( "getFontAscent" ), LFUNCVAL( lu8g_getFontAscent ) }, - { LSTRKEY( "getFontDescent" ), LFUNCVAL( lu8g_getFontDescent ) }, - { LSTRKEY( "getFontLineSpacing" ), LFUNCVAL( lu8g_getFontLineSpacing ) }, - { LSTRKEY( "getMode" ), LFUNCVAL( lu8g_getMode ) }, - { LSTRKEY( "setColorIndex" ), LFUNCVAL( lu8g_setColorIndex ) }, - { LSTRKEY( "getColorIndex" ), LFUNCVAL( lu8g_getColorIndex ) }, + { LSTRKEY( "drawBitmap" ), LFUNCVAL( lu8g_drawBitmap ) }, + { LSTRKEY( "drawBox" ), LFUNCVAL( lu8g_drawBox ) }, + { LSTRKEY( "drawCircle" ), LFUNCVAL( lu8g_drawCircle ) }, + { LSTRKEY( "drawDisc" ), LFUNCVAL( lu8g_drawDisc ) }, + { LSTRKEY( "drawEllipse" ), LFUNCVAL( lu8g_drawEllipse ) }, + { LSTRKEY( "drawFilledEllipse" ), LFUNCVAL( lu8g_drawFilledEllipse ) }, + { LSTRKEY( "drawFrame" ), LFUNCVAL( lu8g_drawFrame ) }, + { LSTRKEY( "drawHLine" ), LFUNCVAL( lu8g_drawHLine ) }, + { LSTRKEY( "drawLine" ), LFUNCVAL( lu8g_drawLine ) }, + { LSTRKEY( "drawPixel" ), LFUNCVAL( lu8g_drawPixel ) }, + { LSTRKEY( "drawRBox" ), LFUNCVAL( lu8g_drawRBox ) }, + { LSTRKEY( "drawRFrame" ), LFUNCVAL( lu8g_drawRFrame ) }, { LSTRKEY( "drawStr" ), LFUNCVAL( lu8g_drawStr ) }, { LSTRKEY( "drawStr90" ), LFUNCVAL( lu8g_drawStr90 ) }, { LSTRKEY( "drawStr180" ), LFUNCVAL( lu8g_drawStr180 ) }, { LSTRKEY( "drawStr270" ), LFUNCVAL( lu8g_drawStr270 ) }, - { LSTRKEY( "drawBox" ), LFUNCVAL( lu8g_drawBox ) }, - { LSTRKEY( "drawLine" ), LFUNCVAL( lu8g_drawLine ) }, { LSTRKEY( "drawTriangle" ), LFUNCVAL( lu8g_drawTriangle ) }, - { LSTRKEY( "drawRBox" ), LFUNCVAL( lu8g_drawRBox ) }, - { LSTRKEY( "drawFrame" ), LFUNCVAL( lu8g_drawFrame ) }, - { LSTRKEY( "drawRFrame" ), LFUNCVAL( lu8g_drawRFrame ) }, - { LSTRKEY( "drawDisc" ), LFUNCVAL( lu8g_drawDisc ) }, - { LSTRKEY( "drawCircle" ), LFUNCVAL( lu8g_drawCircle ) }, - { LSTRKEY( "drawEllipse" ), LFUNCVAL( lu8g_drawEllipse ) }, - { LSTRKEY( "drawFilledEllipse" ), LFUNCVAL( lu8g_drawFilledEllipse ) }, - { LSTRKEY( "drawPixel" ), LFUNCVAL( lu8g_drawPixel ) }, - { LSTRKEY( "drawHLine" ), LFUNCVAL( lu8g_drawHLine ) }, { LSTRKEY( "drawVLine" ), LFUNCVAL( lu8g_drawVLine ) }, - { LSTRKEY( "drawBitmap" ), LFUNCVAL( lu8g_drawBitmap ) }, { LSTRKEY( "drawXBM" ), LFUNCVAL( lu8g_drawXBM ) }, - { LSTRKEY( "setScale2x2" ), LFUNCVAL( lu8g_setScale2x2 ) }, - { LSTRKEY( "undoScale" ), LFUNCVAL( lu8g_undoScale ) }, { LSTRKEY( "firstPage" ), LFUNCVAL( lu8g_firstPage ) }, + { LSTRKEY( "getColorIndex" ), LFUNCVAL( lu8g_getColorIndex ) }, + { LSTRKEY( "getFontAscent" ), LFUNCVAL( lu8g_getFontAscent ) }, + { LSTRKEY( "getFontDescent" ), LFUNCVAL( lu8g_getFontDescent ) }, + { LSTRKEY( "getFontLineSpacing" ), LFUNCVAL( lu8g_getFontLineSpacing ) }, + { LSTRKEY( "getHeight" ), LFUNCVAL( lu8g_getHeight ) }, + { LSTRKEY( "getMode" ), LFUNCVAL( lu8g_getMode ) }, + { LSTRKEY( "getWidth" ), LFUNCVAL( lu8g_getWidth ) }, { LSTRKEY( "nextPage" ), LFUNCVAL( lu8g_nextPage ) }, - { LSTRKEY( "sleepOn" ), LFUNCVAL( lu8g_sleepOn ) }, - { LSTRKEY( "sleepOff" ), LFUNCVAL( lu8g_sleepOff ) }, + { LSTRKEY( "setColorIndex" ), LFUNCVAL( lu8g_setColorIndex ) }, + { LSTRKEY( "setDefaultBackgroundColor" ), LFUNCVAL( lu8g_setDefaultBackgroundColor ) }, + { LSTRKEY( "setDefaultForegroundColor" ), LFUNCVAL( lu8g_setDefaultForegroundColor ) }, + { LSTRKEY( "setFont" ), LFUNCVAL( lu8g_setFont ) }, + { LSTRKEY( "setFontPosBaseline" ), LFUNCVAL( lu8g_setFontPosBaseline ) }, + { LSTRKEY( "setFontPosBottom" ), LFUNCVAL( lu8g_setFontPosBottom ) }, + { LSTRKEY( "setFontPosCenter" ), LFUNCVAL( lu8g_setFontPosCenter ) }, + { LSTRKEY( "setFontPosTop" ), LFUNCVAL( lu8g_setFontPosTop ) }, + { LSTRKEY( "setFontRefHeightAll" ), LFUNCVAL( lu8g_setFontRefHeightAll ) }, + { LSTRKEY( "setFontRefHeightExtendedText" ), LFUNCVAL( lu8g_setFontRefHeightExtendedText ) }, + { LSTRKEY( "setFontRefHeightText" ), LFUNCVAL( lu8g_setFontRefHeightText ) }, { LSTRKEY( "setRot90" ), LFUNCVAL( lu8g_setRot90 ) }, { LSTRKEY( "setRot180" ), LFUNCVAL( lu8g_setRot180 ) }, { LSTRKEY( "setRot270" ), LFUNCVAL( lu8g_setRot270 ) }, + { LSTRKEY( "setScale2x2" ), LFUNCVAL( lu8g_setScale2x2 ) }, + { LSTRKEY( "sleepOff" ), LFUNCVAL( lu8g_sleepOff ) }, + { LSTRKEY( "sleepOn" ), LFUNCVAL( lu8g_sleepOn ) }, { LSTRKEY( "undoRotation" ), LFUNCVAL( lu8g_undoRotation ) }, - { LSTRKEY( "getWidth" ), LFUNCVAL( lu8g_getWidth ) }, - { LSTRKEY( "getHeight" ), LFUNCVAL( lu8g_getHeight ) }, + { LSTRKEY( "undoScale" ), LFUNCVAL( lu8g_undoScale ) }, { LSTRKEY( "__gc" ), LFUNCVAL( lu8g_close_display ) }, #if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "__index" ), LROVAL ( lu8g_display_map ) }, From 5b588b7bd33c2426f41f91f170ba9255b4017411 Mon Sep 17 00:00:00 2001 From: devsaurus Date: Fri, 15 May 2015 21:32:28 +0200 Subject: [PATCH 4/7] implement getStrWidth() and setFontLineSpacingFactor() --- README.md | 6 +++--- app/modules/u8g.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 49d40c5e..2b149e4d 100644 --- a/README.md +++ b/README.md @@ -426,9 +426,9 @@ In contrast to the source code based inclusion of XBMs into u8glib, it's require - [ ] setPrintPos() - [ ] setHardwareBackup() - [ ] setRGB() -- [ ] getStrWidth() -- [ ] setDefaultMidColor() -- [ ] setFontLineSpacingFactor() + - [ ] setDefaultMidColor() +- [x] getStrWidth() +- [x] setFontLineSpacingFactor() ####Control a WS2812 based light strip diff --git a/app/modules/u8g.c b/app/modules/u8g.c index 326eea59..3596ab06 100644 --- a/app/modules/u8g.c +++ b/app/modules/u8g.c @@ -760,6 +760,39 @@ static int lu8g_getHeight( lua_State *L ) return 1; } +// Lua: width = u8g.getStrWidth( self, string ) +static int lu8g_getStrWidth( lua_State *L ) +{ + lu8g_userdata_t *lud; + + if ((lud = get_lud( L )) == NULL) + return 0; + + const char *s = luaL_checkstring( L, 2 ); + if (s == NULL) + return 0; + + lua_pushinteger( L, u8g_GetStrWidth( LU8G, s ) ); + + return 1; +} + +// Lua: u8g.setFontLineSpacingFactor( self, factor ) +static int lu8g_setFontLineSpacingFactor( lua_State *L ) +{ + lu8g_userdata_t *lud; + + if ((lud = get_lud( L )) == NULL) + return 0; + + u8g_uint_t factor = luaL_checkinteger( L, 2 ); + + u8g_SetFontLineSpacingFactor( LU8G, factor ); + + return 0; +} + + // ------------------------------------------------------------ // comm functions // @@ -1166,12 +1199,14 @@ static const LUA_REG_TYPE lu8g_display_map[] = { LSTRKEY( "getFontLineSpacing" ), LFUNCVAL( lu8g_getFontLineSpacing ) }, { LSTRKEY( "getHeight" ), LFUNCVAL( lu8g_getHeight ) }, { LSTRKEY( "getMode" ), LFUNCVAL( lu8g_getMode ) }, + { LSTRKEY( "getStrWidth" ), LFUNCVAL( lu8g_getStrWidth ) }, { LSTRKEY( "getWidth" ), LFUNCVAL( lu8g_getWidth ) }, { LSTRKEY( "nextPage" ), LFUNCVAL( lu8g_nextPage ) }, { LSTRKEY( "setColorIndex" ), LFUNCVAL( lu8g_setColorIndex ) }, { LSTRKEY( "setDefaultBackgroundColor" ), LFUNCVAL( lu8g_setDefaultBackgroundColor ) }, { LSTRKEY( "setDefaultForegroundColor" ), LFUNCVAL( lu8g_setDefaultForegroundColor ) }, { LSTRKEY( "setFont" ), LFUNCVAL( lu8g_setFont ) }, + { LSTRKEY( "setFontLineSpacingFactor" ), LFUNCVAL( lu8g_setFontLineSpacingFactor ) }, { LSTRKEY( "setFontPosBaseline" ), LFUNCVAL( lu8g_setFontPosBaseline ) }, { LSTRKEY( "setFontPosBottom" ), LFUNCVAL( lu8g_setFontPosBottom ) }, { LSTRKEY( "setFontPosCenter" ), LFUNCVAL( lu8g_setFontPosCenter ) }, From 4ca95a52bcb358fc9a3c240c4640120e891a6a3b Mon Sep 17 00:00:00 2001 From: devsaurus Date: Sat, 16 May 2015 13:42:09 +0200 Subject: [PATCH 5/7] clean --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 2b149e4d..295ea337 100644 --- a/README.md +++ b/README.md @@ -427,8 +427,6 @@ In contrast to the source code based inclusion of XBMs into u8glib, it's require - [ ] setHardwareBackup() - [ ] setRGB() - [ ] setDefaultMidColor() -- [x] getStrWidth() -- [x] setFontLineSpacingFactor() ####Control a WS2812 based light strip From 70580dc4ae8be12334753814eba1b11962cc774f Mon Sep 17 00:00:00 2001 From: Jorge Mesquita Date: Sat, 16 May 2015 20:53:31 -0400 Subject: [PATCH 6/7] added net.dns.setdnsserver('ip',[index]), net.dns.getdnsserver([index]), net.dns.resolve('domain.com', function cb('ip' or nil if fail) ) modified: app/modules/net.c --- app/modules/net.c | 151 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 147 insertions(+), 4 deletions(-) diff --git a/app/modules/net.c b/app/modules/net.c index 7fd785ab..37e4023f 100644 --- a/app/modules/net.c +++ b/app/modules/net.c @@ -204,13 +204,12 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) NODE_DBG("self_ref null.\n"); return; } - +/* original if(ipaddr == NULL) { NODE_ERR( "DNS Fail!\n" ); goto end; } - // ipaddr->addr is a uint32_t ip char ip_str[20]; c_memset(ip_str, 0, sizeof(ip_str)); @@ -220,9 +219,30 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg) } lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function - lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua + //lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua lua_pushstring(gL, ip_str); // the ip para - lua_call(gL, 2, 0); +*/ + + // "enhanced" + lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function + + if(ipaddr == NULL) + { + NODE_DBG( "DNS Fail!\n" ); + lua_pushnil(gL); + }else{ + // ipaddr->addr is a uint32_t ip + char ip_str[20]; + c_memset(ip_str, 0, sizeof(ip_str)); + if(host_ip.addr == 0 && ipaddr->addr != 0) + { + c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr))); + } + lua_pushstring(gL, ip_str); // the ip para + } + // "enhanced" end + + lua_call(gL, 1, 0); end: if((pesp_conn->type == ESPCONN_TCP && pesp_conn->proto.tcp->remote_port == 0) @@ -1119,6 +1139,70 @@ static int net_dns( lua_State* L, const char* mt ) return 0; } +// Lua: net.dns.resolve( domain, function(ip) ) +static int net_dns_static( lua_State* L ) +{ + const char *mt = "net.socket"; + if (!lua_isstring( L, 1 )) + return luaL_error( L, "wrong parameter type (domain)" ); + + int rfunc = LUA_NOREF; //save reference to func + if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION){ + rfunc = luaL_ref(L, LUA_REGISTRYINDEX); + } + int rdom = luaL_ref(L, LUA_REGISTRYINDEX); //save reference to domain + + lua_settop(L,0); //empty stack + lua_getfield(L, LUA_GLOBALSINDEX, "net"); + lua_getfield(L, -1, "createConnection"); + lua_remove(L, -2); //remove "net" from stack + lua_pushinteger(L, UDP); // we are going to create a new dummy UDP socket + lua_call(L,1,1);// after this the stack should have a socket + + lua_rawgeti(gL, LUA_REGISTRYINDEX, rdom); // load domain back to the stack + lua_rawgeti(gL, LUA_REGISTRYINDEX, rfunc); // load the callback function back to the stack + + luaL_unref(L, LUA_REGISTRYINDEX, rdom); //free reference + luaL_unref(L, LUA_REGISTRYINDEX, rfunc); //free reference + + bool isserver = false; + struct espconn *pesp_conn = NULL; + lnet_userdata *nud; + size_t l; + + nud = (lnet_userdata *)luaL_checkudata(L, 1, mt); + luaL_argcheck(L, nud, 1, "Server/Socket expected"); + if(nud==NULL){ + NODE_DBG("userdata is nil.\n"); + return 0; + } + if(nud->pesp_conn == NULL){ + NODE_DBG("nud->pesp_conn is NULL.\n"); + return 0; + } + pesp_conn = nud->pesp_conn; + + lua_pushvalue(L, 1); // copy to the top of stack + if(nud->self_ref != LUA_NOREF) + luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref); + nud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + const char *domain = luaL_checklstring( L, 2, &l ); + if (l>128 || domain == NULL) + return luaL_error( L, "need <128 domain" ); + + if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION){ + lua_pushvalue(L, 3); // copy argument (func) to the top of stack + if(nud->cb_dns_found_ref != LUA_NOREF) + luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); + nud->cb_dns_found_ref = luaL_ref(L, LUA_REGISTRYINDEX); + } + + host_ip.addr = 0; + espconn_gethostbyname(pesp_conn, domain, &host_ip, net_dns_found); + + return 0; +} // Lua: s = net.createServer(type, function(server)) static int net_createServer( lua_State* L ) @@ -1284,6 +1368,48 @@ static int net_socket_dns( lua_State* L ) return net_dns(L, mt); } +// Lua: s = net.dns.setdnsserver(ip_addr, [index]) +static int net_setdnsserver( lua_State* L ) +{ + size_t l; + u32_t ip32; + + const char *server = luaL_checklstring( L, 1, &l ); + if (l>16 || server == NULL || (ip32 = ipaddr_addr(server)) == IPADDR_NONE || ip32 == IPADDR_ANY) + return luaL_error( L, "invalid dns server ip" ); + + int numdns = luaL_optint(L, 2, 0); + if (numdns >= DNS_MAX_SERVERS) + return luaL_error( L, "server index out of range [0-%d]", DNS_MAX_SERVERS - 1); + + ip_addr_t ipaddr; + ip4_addr_set_u32(&ipaddr, ip32); + dns_setserver(numdns,&ipaddr); + + return 0; +} + +// Lua: s = net.dns.getdnsserver([index]) +static int net_getdnsserver( lua_State* L ) +{ + int numdns = luaL_optint(L, 1, 0); + if (numdns >= DNS_MAX_SERVERS) + return luaL_error( L, "server index out of range [0-%d]", DNS_MAX_SERVERS - 1); + + ip_addr_t ipaddr; + dns_getserver(numdns,&ipaddr); + + if ( ip_addr_isany(&ipaddr) ) { + lua_pushnil( L ); + } else { + char temp[20] = {0}; + c_sprintf(temp, IPSTR, IP2STR( &ipaddr ) ); + lua_pushstring( L, temp ); + } + + return 1; +} + #if 0 static int net_array_index( lua_State* L ) { @@ -1354,11 +1480,22 @@ static const LUA_REG_TYPE net_array_map[] = { LNILKEY, LNILVAL } }; #endif + +static const LUA_REG_TYPE net_dns_map[] = +{ + { LSTRKEY( "setdnsserver" ), LFUNCVAL ( net_setdnsserver ) }, + { LSTRKEY( "getdnsserver" ), LFUNCVAL ( net_getdnsserver ) }, + { LSTRKEY( "resolve" ), LFUNCVAL ( net_dns_static ) }, + { LNILKEY, LNILVAL } +}; + const LUA_REG_TYPE net_map[] = { { LSTRKEY( "createServer" ), LFUNCVAL ( net_createServer ) }, { LSTRKEY( "createConnection" ), LFUNCVAL ( net_createConnection ) }, + #if LUA_OPTIMIZE_MEMORY > 0 + { LSTRKEY( "dns" ), LROVAL( net_dns_map ) }, { LSTRKEY( "TCP" ), LNUMVAL( TCP ) }, { LSTRKEY( "UDP" ), LNUMVAL( UDP ) }, @@ -1421,6 +1558,12 @@ LUALIB_API int luaopen_net( lua_State *L ) // Setup the methods inside metatable luaL_register( L, NULL, net_array_map ); #endif + + lua_settop(L, n); + lua_newtable( L ); + luaL_register( L, NULL, net_dns_map ); + lua_setfield( L, -2, "dns" ); + return 1; #endif // #if LUA_OPTIMIZE_MEMORY > 0 } From de053cf47966c20c91e1734a6a38de87a9956214 Mon Sep 17 00:00:00 2001 From: Levente Tamas Date: Sun, 17 May 2015 11:57:43 +0300 Subject: [PATCH 7/7] Add support for lm92 temperature sensor Added LUA module for the lm92 +-0.33C 12bit+sign temperature sensor. --- lua_modules/lm92/README.md | 98 ++++++++++++++++++++++++++++++++++++++ lua_modules/lm92/lm92.lua | 94 ++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 lua_modules/lm92/README.md create mode 100644 lua_modules/lm92/lm92.lua diff --git a/lua_modules/lm92/README.md b/lua_modules/lm92/README.md new file mode 100644 index 00000000..261e1ebe --- /dev/null +++ b/lua_modules/lm92/README.md @@ -0,0 +1,98 @@ +# LM92 module +This module adds basic support for the LM92 +-0.33C 12bit+sign temperature sensor. More details in the [datasheet](http://www.ti.com/lit/ds/symlink/lm92.pdf). +Works: +- getting the temperature +- entering the chip's to shutdown mode (350uA -> 5uA power consumption) +- waking up the chip from shutdown + +##Require +```lua +LM92 = require("lm92") +``` +## Release +```lua +LM92 = nil +package.loaded["lm92"]=nil +``` + +##init() +####Description +Setting the i2c pins and address for lm92. + +####Syntax +init(sda, scl, address) + +####Parameters +sda: 1~12, IO index.
+scl: 1~12, IO index.
+address: 0x48~0x4b, i2c address (depends on tha A0~A1 pins) +####Returns +nil + +####Example +```lua +LM92 = require("lm92") +gpio0 = 3 +gpio2 = 4 +sda = gpio0 +scl = gpio2 +addr = 0x48 +LM92.init(sda, scl,addr) +``` +##getTemperature() +####Description +Returns the temperature register's content. + +####Syntax +getTemperature() + +####Parameters +- + +####Returns +Temperature in degree Celsius. + +####Example +```lua +t = LM92.getTemperature() +print("Got temperature: "..t.." C") +``` + +##wakeup() +####Description +Makes the chip exit the low power shutdown mode. + +####Syntax +wakeup() + +####Parameters +- + +####Returns +- + +####Example +```lua +LM92.wakeup() +tmr.delay( 1 * 1000 * 1000 ) +``` + +##shutdown() +####Description +Makes the chip enter the low power shutdown mode. + +####Syntax +shutdown() + +####Parameters +- + +####Returns +- + +####Example +```lua +LM92.shutdown() +``` +#### TODO: +- add full support of the features, including interrupt and critical alert support diff --git a/lua_modules/lm92/lm92.lua b/lua_modules/lm92/lm92.lua new file mode 100644 index 00000000..fcec7098 --- /dev/null +++ b/lua_modules/lm92/lm92.lua @@ -0,0 +1,94 @@ +-- ****************************************************** +-- LM92 module for ESP8266 with nodeMCU +-- +-- Written by Levente Tamas +-- +-- GNU LGPL, see https://www.gnu.org/copyleft/lesser.html +-- ****************************************************** + +-- Module Bits +local moduleName = ... +local M = {} +_G[moduleName] = M + +-- Default ID +local id = 0 + +-- Local vars +local address = 0 + +-- read regs for len number of bytes +-- return table with data +local function read_reg(reg_addr, len) + local ret={} + local c + local x + i2c.start(id) + i2c.address(id, address ,i2c.TRANSMITTER) + i2c.write(id,reg_addr) + i2c.stop(id) + i2c.start(id) + i2c.address(id, address,i2c.RECEIVER) + c=i2c.read(id,len) + for x=1,len,1 do + tc=string.byte(c,x) + table.insert(ret,tc) + end + i2c.stop(id) + return ret +end + +--write reg with data table +local function write_reg(reg_addr, data) + i2c.start(id) + i2c.address(id, address, i2c.TRANSMITTER) + i2c.write(id, reg_addr) + i2c.write(id, data) + i2c.stop(id) +end + +-- initialize i2c +-- d: sda +-- c: scl +-- a: i2c addr 0x48|A1<<1|A0 (A0-A1: chip pins) +function M.init(d,c,a) +if (d ~= nil) and (c ~= nil) and (d >= 0) and (d <= 11) and (c >= 0) and ( c <= 11) and (d ~= l) and (a ~= nil) and (a >= 0x48) and (a <= 0x4b ) then + sda = d + scl = c + address = a + i2c.start(id) + res = i2c.address(id, address, i2c.TRANSMITTER) --verify that the address is valid + i2c.stop(id) + if (res == false) then + print("device not found") + return nil + end + else + print("i2c configuration failed") return nil + end + i2c.setup(id,sda,scl,i2c.SLOW) +end + +-- Return the temperature data +function M.getTemperature() + local temperature + local tmp=read_reg(0x00,2) --read 2 bytes from the temperature register + temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits + if (temperature>=0x1000) then + temperature= temperature-0x2000 --convert the two's complement + end + return temperature * 0.0625 +end + +-- Put the LM92 into shutdown mode +function M.shutdown() + write_reg(0x01,0x01) +end + +-- Bring the LM92 out of shutdown mode +function M.wakeup() + write_reg(0x01,0x00) +end + + +return M \ No newline at end of file