Conflicts:
	app/modules/net.c
This commit is contained in:
HuangRui 2015-06-04 15:04:12 +08:00
commit 3cd5a6a1ed
6 changed files with 225 additions and 5 deletions

View File

@ -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

View File

@ -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 ) },

View File

@ -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;

43
lua_examples/adc_rgb.lua Normal file
View File

@ -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)

View File

@ -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 ("<h1>ESP8266 Servo &amp; Light Server</h1>\r\n")
if (params["light"] ~= nil) then
if ("0" == params["light"]) then
gpio.write(pinLight, gpio.LOW)
else
gpio.write(pinLight, gpio.HIGH)
end
end
if (params["s0"] ~= nil) then
servo.value = tonumber(params["s0"]);
end
-- Close the connection for the request
cn:close ( )
end)
end
-- Build and return a table of the http request data
function get_http_req (instr)
local t = {}
local str = string.sub(instr, 0, 200)
local v = string.gsub(split(str, ' ')[2], '+', ' ')
parts = split(v, '?')
local params = {}
if (table.maxn(parts) > 1) then
for idx,part in ipairs(split(parts[2], '&')) do
parmPart = split(part, '=')
params[parmPart[1]] = parmPart[2]
end
end
return params
end
-- Source: http://lua-users.org/wiki/MakingLuaLikePhp
-- Credit: http://richard.warburton.it/
function split(str, splitOn)
if (splitOn=='') then return false end
local pos,arr = 0,{}
for st,sp in function() return string.find(str,splitOn,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1))
pos = sp + 1
end
table.insert(arr,string.sub(str,pos))
return arr
end
-- Configure the ESP as a station (client)
wifi.setmode (wifi.STATION)
wifi.sta.config (SSID, SSID_PASSWORD)
wifi.sta.autoconnect (1)
-- Hang out until we get a wifi connection before the httpd server is started.
tmr.alarm (1, 800, 1, function ( )
if wifi.sta.getip ( ) == nil then
print ("Waiting for Wifi connection")
else
tmr.stop (1)
print ("Config done, IP is " .. wifi.sta.getip ( ))
end
end)
-- Create the httpd server
svr = net.createServer (net.TCP, 30)
-- Server listening on port 80, call connect function if a request is received
svr:listen (80, connect)

View File

@ -16,11 +16,11 @@ local id = 0
local dev_addr = 0x68
local function decToBcd(val)
return((val/10*16) + (val%10))
return((((val/10) - ((val/10)%1)) *16) + (val%10))
end
local function bcdToDec(val)
return((val/16*10) + (val%16))
return((((val/16) - ((val/16)%1)) *10) + (val%16))
end
-- initialize i2c