diff --git a/app/Makefile b/app/Makefile index f28bb3a8..f52b890b 100644 --- a/app/Makefile +++ b/app/Makefile @@ -54,11 +54,11 @@ TARGET_LDFLAGS = \ --text-section-literals ifeq ($(FLAVOR),debug) - TARGET_LDFLAGS += -g -O2 + TARGET_LDFLAGS += -g -Os endif ifeq ($(FLAVOR),release) - TARGET_LDFLAGS += -g -O0 + TARGET_LDFLAGS += -Os endif LD_FILE = $(LDDIR)/eagle.app.v6.ld diff --git a/app/modules/net.c b/app/modules/net.c index 37e4023f..bb36d45b 100644 --- a/app/modules/net.c +++ b/app/modules/net.c @@ -1368,6 +1368,55 @@ static int net_socket_dns( lua_State* L ) return net_dns(L, mt); } +static int net_multicastJoinLeave( lua_State *L, int join) +{ + size_t il; + ip_addr_t multicast_addr; + ip_addr_t if_addr; + const char *multicast_ip; + const char *if_ip; + + NODE_DBG("net_multicastJoin is called.\n"); + if(! lua_isstring(L,1) ) return luaL_error( L, "wrong arg type" ); + if_ip = luaL_checklstring( L, 1, &il ); + if (if_ip != NULL) + if ( if_ip[0] == '\0' || stricmp(if_ip,"any") == 0) + { + if_ip = "0.0.0.0"; + il = 7; + } + if (if_ip == NULL || il > 15 || il < 7) return luaL_error( L, "invalid if ip" ); + if_addr.addr = ipaddr_addr(if_ip); + + if(! lua_isstring(L,2) ) return luaL_error( L, "wrong arg type" ); + multicast_ip = luaL_checklstring( L, 2, &il ); + if (multicast_ip == NULL || il > 15 || il < 7) return luaL_error( L, "invalid multicast ip" ); + multicast_addr.addr = ipaddr_addr(multicast_ip); + if (join) + { + espconn_igmp_join(&if_addr, &multicast_addr); + } + else + { + espconn_igmp_leave(&if_addr, &multicast_addr); + } + return 0; +} +// Lua: net.multicastJoin(ifip, multicastip) +// if ifip "" or "any" all interfaces are affected +static int net_multicastJoin( lua_State* L ) +{ + return net_multicastJoinLeave(L,1); +} + +// Lua: net.multicastLeave(ifip, multicastip) +// if ifip "" or "any" all interfaces are affected +static int net_multicastLeave( lua_State* L ) +{ + return net_multicastJoinLeave(L,0); +} + + // Lua: s = net.dns.setdnsserver(ip_addr, [index]) static int net_setdnsserver( lua_State* L ) { @@ -1493,7 +1542,8 @@ const LUA_REG_TYPE net_map[] = { { LSTRKEY( "createServer" ), LFUNCVAL ( net_createServer ) }, { LSTRKEY( "createConnection" ), LFUNCVAL ( net_createConnection ) }, - + { LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) }, + { LSTRKEY( "multicastLeave"), LFUNCVAL( net_multicastLeave ) }, #if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "dns" ), LROVAL( net_dns_map ) }, { LSTRKEY( "TCP" ), LNUMVAL( TCP ) }, diff --git a/app/modules/tmr.c b/app/modules/tmr.c index 1a0fa478..ee92f4d5 100644 --- a/app/modules/tmr.c +++ b/app/modules/tmr.c @@ -71,6 +71,12 @@ static int tmr_delay( lua_State* L ) us = luaL_checkinteger( L, 1 ); if ( us <= 0 ) return luaL_error( L, "wrong arg range" ); + if(us<1000000) + { + os_delay_us( us ); + WRITE_PERI_REG(0x60000914, 0x73); + return 0; + } unsigned sec = (unsigned)us / 1000000; unsigned remain = (unsigned)us % 1000000; int i = 0; diff --git a/lua_examples/adc_rgb.lua b/lua_examples/adc_rgb.lua new file mode 100644 index 00000000..ff8b70e6 --- /dev/null +++ b/lua_examples/adc_rgb.lua @@ -0,0 +1,43 @@ +-- +-- Light sensor on ADC(0), RGB LED connected to gpio12(6) Green, gpio13(7) Blue & gpio15(8) Red. +-- This works out of the box on the typical ESP8266 evaluation boards with Battery Holder +-- +-- It uses the input from the sensor to drive a "rainbow" effect on the RGB LED +-- Includes a very "pseudoSin" function +-- + +function led(r,Sg,b) + pwm.setduty(8,r) + pwm.setduty(6,g) + pwm.setduty(7,b) +end + +-- this is perhaps the lightest weight sin function in existance +-- Given an integer from 0..128, 0..512 appximating 256 + 256 * sin(idx*Pi/256) +-- This is first order square approximation of sin, it's accurate around 0 and any multiple of 128 (Pi/2), +-- 92% accurate at 64 (Pi/4). +function pseudoSin (idx) + idx = idx % 128 + lookUp = 32 - idx % 64 + val = 256 - (lookUp * lookUp) / 4 + if (idx > 64) then + val = - val; + end + return 256+val +end + +pwm.setup(6,500,512) +pwm.setup(7,500,512) +pwm.setup(8,500,512) +pwm.start(6) +pwm.start(7) +pwm.start(8) + +tmr.alarm(1,20,1,function() + idx = 3 * adc.read(0) / 2 + r = pseudoSin(idx) + g = pseudoSin(idx + 43) + b = pseudoSin(idx + 85) + led(r,g,b) + idx = (idx + 1) % 128 + end) diff --git a/lua_examples/http_server.lua b/lua_examples/http_server.lua new file mode 100644 index 00000000..c9ad275a --- /dev/null +++ b/lua_examples/http_server.lua @@ -0,0 +1,121 @@ +-- +-- Simple NodeMCU web server (done is a not so nodeie fashion :-) +-- +-- Highly modified by Bruce Meacham, based on work by Scott Beasley 2015 +-- Open and free to change and use. Enjoy. [Beasley/Meacham 2015] +-- +-- Meacham Update: I streamlined/improved the parsing to focus on simple HTTP GET request and their simple parameters +-- Also added the code to drive a servo/light. Comment out as you see fit. +-- +-- Usage: +-- Change SSID and SSID_PASSPHRASE for your wifi network +-- Download to NodeMCU +-- node.compile("http_server.lua") +-- dofile("http_server.lc") +-- When the server is esablished it will output the IP address. +-- http://{ip address}/?s0=1200&light=1 +-- s0 is the servo position (actually the PWM hertz), 500 - 2000 are all good values +-- light chanel high(1)/low(0), some evaluation boards have LEDs pre-wired in a "pulled high" confguration, so '0' ground the emitter and turns it on backwards. +-- +-- Add to init.lua if you want it to autoboot. +-- + +-- Your Wifi connection data +local SSID = "YOUR WIFI SSID" +local SSID_PASSWORD = "YOUR SSID PASSPHRASE" + +-- General setup +local pinLight = 2 -- this is GPIO4 +gpio.mode(pinLight,gpio.OUTPUT) +gpio.write(pinLight,gpio.HIGH) + +servo = {} +servo.pin = 4 --this is GPIO2 +servo.value = 1500 +servo.id = "servo" +gpio.mode(servo.pin, gpio.OUTPUT) +gpio.write(servo.pin, gpio.LOW) + +-- This alarm drives the servo +tmr.alarm(0,10,1,function() -- 50Hz + if servo.value then -- generate pulse + gpio.write(servo.pin, gpio.HIGH) + tmr.delay(servo.value) + gpio.write(servo.pin, gpio.LOW) + end +end) + +local function connect (conn, data) + local query_data + + conn:on ("receive", + function (cn, req_data) + params = get_http_req (req_data) + cn:send("HTTP/1.1 200/OK\r\nServer: NodeLuau\r\nContent-Type: text/html\r\n\r\n") + cn:send ("