Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
265d2705
Commit
265d2705
authored
Dec 30, 2014
by
zeroday
Browse files
Merge pull request #63 from nodemcu/dev094
merge Dev094
parents
3e25a105
ee2e7912
Changes
9
Show whitespace changes
Inline
Side-by-side
app/driver/readline.c
View file @
265d2705
...
...
@@ -11,15 +11,14 @@ extern UartDevice UartDev;
#define uart_putc uart0_putc
char
ICACHE_FLASH_ATTR
uart_getc
(
void
){
char
c
=
0
;
bool
ICACHE_FLASH_ATTR
uart_getc
(
char
*
c
){
RcvMsgBuff
*
pRxBuff
=
&
(
UartDev
.
rcv_buff
);
if
(
pRxBuff
->
pWritePos
==
pRxBuff
->
pReadPos
){
// empty
return
0
;
return
false
;
}
// ETS_UART_INTR_DISABLE();
ETS_INTR_LOCK
();
c
=
(
char
)
*
(
pRxBuff
->
pReadPos
);
*
c
=
(
char
)
*
(
pRxBuff
->
pReadPos
);
if
(
pRxBuff
->
pReadPos
==
(
pRxBuff
->
pRcvMsgBuff
+
RX_BUFF_SIZE
))
{
pRxBuff
->
pReadPos
=
pRxBuff
->
pRcvMsgBuff
;
}
else
{
...
...
@@ -27,7 +26,7 @@ char ICACHE_FLASH_ATTR uart_getc(void){
}
// ETS_UART_INTR_ENABLE();
ETS_INTR_UNLOCK
();
return
c
;
return
true
;
}
#if 0
...
...
@@ -43,13 +42,13 @@ start:
os_memset(buffer, 0, length);
while (1)
{
while (
(ch =
uart_getc(
)) != 0
)
while (uart_getc(
&ch)
)
{
/* handle CR key */
if (ch == '\r')
{
char next;
if (
(next =
uart_getc(
)) != 0
)
if (uart_getc(
&next)
)
ch = next;
}
/* backspace key */
...
...
app/lua/lua.c
View file @
265d2705
...
...
@@ -490,34 +490,6 @@ void ICACHE_FLASH_ATTR dojob(lua_Load *load){
do
{
if
(
load
->
done
==
1
){
l
=
c_strlen
(
b
);
#if 0
if(log_fd!=-1 && l>0)
{
if(l>=10 && (c_strstr(b,"log.stop()")) )
{
fs_close(log_fd);
log_fd = -1;
noparse = 0;
NODE_ERR( "log stopping.\n" );
}
if(log_fd!=-1){ // test again
rs = fs_write(log_fd, b, l);
if(rs!=l){
fs_close(log_fd);
log_fd = -1;
} else {
rs = fs_write(log_fd, "\r\n", 2);
if(rs!=2){
fs_close(log_fd);
log_fd = -1;
}
}
}
if(noparse){
break;
}
}
#endif
if
(
l
>
0
&&
b
[
l
-
1
]
==
'\n'
)
/* line ends with newline? */
b
[
l
-
1
]
=
'\0'
;
/* remove it */
if
(
load
->
firstline
&&
b
[
0
]
==
'='
)
/* first line starts with `=' ? */
...
...
@@ -613,17 +585,22 @@ void ICACHE_FLASH_ATTR update_key_led(){
#endif
extern
bool
uart_on_data_cb
(
const
char
*
buf
,
size_t
len
);
extern
bool
uart0_echo
;
extern
bool
run_input
;
extern
uint16_t
need_len
;
extern
int16_t
end_char
;
void
ICACHE_FLASH_ATTR
readline
(
lua_Load
*
load
){
// NODE_DBG("readline() is called.\n");
update_key_led
();
char
ch
;
while
((
ch
=
uart_getc
())
!=
0
)
while
(
uart_getc
(
&
ch
))
{
if
(
run_input
)
{
/* handle CR key */
if
(
ch
==
'\r'
)
{
char
next
;
if
(
(
next
=
uart_getc
(
))
!=
0
)
if
(
uart_getc
(
&
next
)
)
ch
=
next
;
}
/* backspace key */
...
...
@@ -639,23 +616,22 @@ void ICACHE_FLASH_ATTR readline(lua_Load *load){
load
->
line
[
load
->
line_position
]
=
0
;
continue
;
}
/* EOF(ctrl+d) */
else
if
(
ch
==
0x04
)
{
if
(
load
->
line_position
==
0
)
// No input which makes lua interpreter close
donejob
(
load
);
else
continue
;
}
/* EOT(ctrl+d) */
// else if (ch == 0x04)
// {
// if (load->line_position == 0)
// // No input which makes lua interpreter close
// donejob(load);
// else
// continue;
// }
/* end of line */
if
(
ch
==
'\r'
||
ch
==
'\n'
)
{
load
->
line
[
load
->
line_position
]
=
0
;
if
(
uart0_echo
)
uart_putc
(
'\n'
);
if
(
uart_on_data_cb
(
load
->
line
,
load
->
line_position
)){
load
->
line_position
=
0
;
}
uart_on_data_cb
(
load
->
line
,
load
->
line_position
);
if
(
load
->
line_position
==
0
)
{
/* Get a empty line, then go to get a new line */
...
...
@@ -670,29 +646,40 @@ void ICACHE_FLASH_ATTR readline(lua_Load *load){
os_timer_arm
(
&
lua_timer
,
READLINE_INTERVAL
,
0
);
// no repeat
}
}
/* other control character or not an acsii character */
if
(
ch
<
0x20
||
ch
>=
0x80
)
{
continue
;
}
// if (ch < 0x20 || ch >= 0x80)
// {
// continue;
// }
/* echo */
if
(
uart0_echo
)
uart_putc
(
ch
);
/* it's a large line, discard it */
if
(
load
->
line_position
+
1
>=
load
->
len
){
load
->
line_position
=
0
;
}
}
load
->
line
[
load
->
line_position
]
=
ch
;
ch
=
0
;
load
->
line_position
++
;
/* it's a large line, discard it */
if
(
load
->
line_position
>=
load
->
len
)
if
(
!
run_input
)
{
if
(
((
need_len
!=
0
)
&&
(
load
->
line_position
>=
need_len
))
||
\
(
load
->
line_position
>=
load
->
len
)
||
\
((
end_char
>=
0
)
&&
((
unsigned
char
)
ch
==
(
unsigned
char
)
end_char
))
)
{
uart_on_data_cb
(
load
->
line
,
load
->
line_position
);
load
->
line_position
=
0
;
}
}
ch
=
0
;
}
// if there is no input from user, repeat readline()
os_timer_disarm
(
&
readline_timer
);
os_timer_setfn
(
&
readline_timer
,
(
os_timer_func_t
*
)
readline
,
load
);
os_timer_arm
(
&
readline_timer
,
READLINE_INTERVAL
,
0
);
// no repeat
}
void
ICACHE_FLASH_ATTR
dogc
(
void
){
if
(
globalL
){
lua_gc
(
globalL
,
LUA_GCCOLLECT
,
0
);
}
}
app/modules/tmr.c
View file @
265d2705
...
...
@@ -154,6 +154,16 @@ static int ICACHE_FLASH_ATTR tmr_wdclr( lua_State* L )
return
0
;
}
// Lua: time() , return rtc time in us
static
int
ICACHE_FLASH_ATTR
tmr_time
(
lua_State
*
L
)
{
unsigned
t
=
0xFFFFFFFF
&
system_get_rtc_time
();
unsigned
c
=
0xFFFFFFFF
&
system_rtc_clock_cali_proc
();
lua_pushinteger
(
L
,
t
);
lua_pushinteger
(
L
,
c
);
return
2
;
}
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
...
...
@@ -164,6 +174,7 @@ const LUA_REG_TYPE tmr_map[] =
{
LSTRKEY
(
"alarm"
),
LFUNCVAL
(
tmr_alarm
)
},
{
LSTRKEY
(
"stop"
),
LFUNCVAL
(
tmr_stop
)
},
{
LSTRKEY
(
"wdclr"
),
LFUNCVAL
(
tmr_wdclr
)
},
{
LSTRKEY
(
"time"
),
LFUNCVAL
(
tmr_time
)
},
#if LUA_OPTIMIZE_MEMORY > 0
#endif
...
...
app/modules/uart.c
View file @
265d2705
...
...
@@ -12,7 +12,7 @@
static
lua_State
*
gL
=
NULL
;
static
int
uart_receive_rf
=
LUA_NOREF
;
static
bool
run_input
=
true
;
bool
run_input
=
true
;
bool
ICACHE_FLASH_ATTR
uart_on_data_cb
(
const
char
*
buf
,
size_t
len
){
if
(
!
buf
||
len
==
0
)
return
false
;
...
...
@@ -26,21 +26,46 @@ bool ICACHE_FLASH_ATTR uart_on_data_cb(const char *buf, size_t len){
return
!
run_input
;
}
uint16_t
need_len
=
0
;
int16_t
end_char
=
-
1
;
// Lua: uart.on("method", function, [run_input])
static
int
ICACHE_FLASH_ATTR
uart_on
(
lua_State
*
L
)
{
size_t
sl
;
size_t
sl
,
el
;
int32_t
run
=
1
;
const
char
*
method
=
luaL_checklstring
(
L
,
1
,
&
sl
);
uint8_t
stack
=
1
;
const
char
*
method
=
luaL_checklstring
(
L
,
stack
,
&
sl
);
stack
++
;
if
(
method
==
NULL
)
return
luaL_error
(
L
,
"wrong arg type"
);
// luaL_checkanyfunction(L, 2);
if
(
lua_type
(
L
,
2
)
==
LUA_TFUNCTION
||
lua_type
(
L
,
2
)
==
LUA_TLIGHTFUNCTION
){
if
(
lua_isnumber
(
L
,
3
)
){
run
=
lua_tointeger
(
L
,
3
);
if
(
lua_type
(
L
,
stack
)
==
LUA_TNUMBER
)
{
need_len
=
(
uint16_t
)
luaL_checkinteger
(
L
,
stack
);
stack
++
;
end_char
=
-
1
;
if
(
need_len
>
255
){
need_len
=
255
;
return
luaL_error
(
L
,
"wrong arg range"
);
}
}
else
if
(
lua_isstring
(
L
,
stack
))
{
const
char
*
end
=
luaL_checklstring
(
L
,
stack
,
&
el
);
stack
++
;
if
(
el
!=
1
){
return
luaL_error
(
L
,
"wrong arg range"
);
}
end_char
=
(
int16_t
)
end
[
0
];
need_len
=
0
;
}
// luaL_checkanyfunction(L, stack);
if
(
lua_type
(
L
,
stack
)
==
LUA_TFUNCTION
||
lua_type
(
L
,
stack
)
==
LUA_TLIGHTFUNCTION
){
if
(
lua_isnumber
(
L
,
stack
+
1
)
){
run
=
lua_tointeger
(
L
,
stack
+
1
);
}
lua_pushvalue
(
L
,
2
);
// copy argument (func) to the top of stack
lua_pushvalue
(
L
,
stack
);
// copy argument (func) to the top of stack
}
else
{
lua_pushnil
(
L
);
}
...
...
app/modules/wifi.c
View file @
265d2705
...
...
@@ -159,6 +159,18 @@ static int ICACHE_FLASH_ATTR wifi_getmac( lua_State* L, uint8_t mode )
return
1
;
}
// Lua: mac = wifi.xx.setmac()
static
int
ICACHE_FLASH_ATTR
wifi_setmac
(
lua_State
*
L
,
uint8_t
mode
)
{
unsigned
len
=
0
;
const
char
*
mac
=
luaL_checklstring
(
L
,
1
,
&
len
);
if
(
len
!=
6
)
return
luaL_error
(
L
,
"wrong arg type"
);
lua_pushboolean
(
L
,
wifi_set_macaddr
(
mode
,
(
uint8
*
)
mac
));
return
1
;
}
// Lua: ip = wifi.xx.getip()
static
int
ICACHE_FLASH_ATTR
wifi_getip
(
lua_State
*
L
,
uint8_t
mode
)
{
...
...
@@ -167,10 +179,71 @@ static int ICACHE_FLASH_ATTR wifi_getip( lua_State* L, uint8_t mode )
wifi_get_ip_info
(
mode
,
&
pTempIp
);
if
(
pTempIp
.
ip
.
addr
==
0
){
lua_pushnil
(
L
);
return
1
;
}
else
{
c_sprintf
(
temp
,
"%d.%d.%d.%d"
,
IP2STR
(
&
pTempIp
.
ip
)
);
lua_pushstring
(
L
,
temp
);
// c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.netmask) );
// lua_pushstring( L, temp );
// c_sprintf(temp, "%d.%d.%d.%d", IP2STR(&pTempIp.gw) );
// lua_pushstring( L, temp );
// return 3;
return
1
;
}
}
static
uint32_t
parse_key
(
lua_State
*
L
,
const
char
*
key
){
lua_getfield
(
L
,
1
,
key
);
if
(
lua_isstring
(
L
,
-
1
)
)
// deal with the ssid string
{
const
char
*
ip
=
luaL_checkstring
(
L
,
-
1
);
return
ipaddr_addr
(
ip
);
}
lua_pop
(
L
,
1
);
return
0
;
}
// Lua: ip = wifi.xx.setip()
static
int
ICACHE_FLASH_ATTR
wifi_setip
(
lua_State
*
L
,
uint8_t
mode
)
{
struct
ip_info
pTempIp
;
wifi_get_ip_info
(
mode
,
&
pTempIp
);
if
(
!
lua_istable
(
L
,
1
))
return
luaL_error
(
L
,
"wrong arg type"
);
uint32_t
ip
=
parse_key
(
L
,
"ip"
);
if
(
ip
!=
0
)
pTempIp
.
ip
.
addr
=
ip
;
ip
=
parse_key
(
L
,
"netmask"
);
if
(
ip
!=
0
)
pTempIp
.
netmask
.
addr
=
ip
;
ip
=
parse_key
(
L
,
"gateway"
);
if
(
ip
!=
0
)
pTempIp
.
gw
.
addr
=
ip
;
lua_pushboolean
(
L
,
wifi_set_ip_info
(
mode
,
&
pTempIp
));
return
1
;
}
// Lua: realtype = sleeptype(type)
static
int
ICACHE_FLASH_ATTR
wifi_sleeptype
(
lua_State
*
L
)
{
unsigned
type
;
if
(
lua_isnumber
(
L
,
1
)
){
type
=
lua_tointeger
(
L
,
1
);
if
(
type
!=
NONE_SLEEP_T
&&
type
!=
LIGHT_SLEEP_T
&&
type
!=
MODEM_SLEEP_T
)
return
luaL_error
(
L
,
"wrong arg type"
);
if
(
!
wifi_set_sleep_type
(
type
)){
lua_pushnil
(
L
);
return
1
;
}
}
type
=
wifi_get_sleep_type
();
lua_pushinteger
(
L
,
type
);
return
1
;
}
...
...
@@ -179,11 +252,21 @@ static int ICACHE_FLASH_ATTR wifi_station_getmac( lua_State* L ){
return
wifi_getmac
(
L
,
STATION_IF
);
}
// Lua: wifi.sta.setmac()
static
int
ICACHE_FLASH_ATTR
wifi_station_setmac
(
lua_State
*
L
){
return
wifi_setmac
(
L
,
STATION_IF
);
}
// Lua: wifi.sta.getip()
static
int
ICACHE_FLASH_ATTR
wifi_station_getip
(
lua_State
*
L
){
return
wifi_getip
(
L
,
STATION_IF
);
}
// Lua: wifi.sta.setip()
static
int
ICACHE_FLASH_ATTR
wifi_station_setip
(
lua_State
*
L
){
return
wifi_setip
(
L
,
STATION_IF
);
}
// Lua: wifi.sta.config(ssid, password)
static
int
ICACHE_FLASH_ATTR
wifi_station_config
(
lua_State
*
L
)
{
...
...
@@ -281,11 +364,21 @@ static int ICACHE_FLASH_ATTR wifi_ap_getmac( lua_State* L ){
return
wifi_getmac
(
L
,
SOFTAP_IF
);
}
// Lua: wifi.ap.setmac()
static
int
ICACHE_FLASH_ATTR
wifi_ap_setmac
(
lua_State
*
L
){
return
wifi_setmac
(
L
,
SOFTAP_IF
);
}
// Lua: wifi.ap.getip()
static
int
ICACHE_FLASH_ATTR
wifi_ap_getip
(
lua_State
*
L
){
return
wifi_getip
(
L
,
SOFTAP_IF
);
}
// Lua: wifi.ap.setip()
static
int
ICACHE_FLASH_ATTR
wifi_ap_setip
(
lua_State
*
L
){
return
wifi_setip
(
L
,
SOFTAP_IF
);
}
// Lua: wifi.ap.config(table)
static
int
ICACHE_FLASH_ATTR
wifi_ap_config
(
lua_State
*
L
)
{
...
...
@@ -352,7 +445,9 @@ static const LUA_REG_TYPE wifi_station_map[] =
{
LSTRKEY
(
"disconnect"
),
LFUNCVAL
(
wifi_station_disconnect4lua
)
},
{
LSTRKEY
(
"autoconnect"
),
LFUNCVAL
(
wifi_station_setauto
)
},
{
LSTRKEY
(
"getip"
),
LFUNCVAL
(
wifi_station_getip
)
},
{
LSTRKEY
(
"setip"
),
LFUNCVAL
(
wifi_station_setip
)
},
{
LSTRKEY
(
"getmac"
),
LFUNCVAL
(
wifi_station_getmac
)
},
{
LSTRKEY
(
"setmac"
),
LFUNCVAL
(
wifi_station_setmac
)
},
{
LSTRKEY
(
"getap"
),
LFUNCVAL
(
wifi_station_listap
)
},
{
LSTRKEY
(
"status"
),
LFUNCVAL
(
wifi_station_status
)
},
{
LNILKEY
,
LNILVAL
}
...
...
@@ -362,7 +457,9 @@ static const LUA_REG_TYPE wifi_ap_map[] =
{
{
LSTRKEY
(
"config"
),
LFUNCVAL
(
wifi_ap_config
)
},
{
LSTRKEY
(
"getip"
),
LFUNCVAL
(
wifi_ap_getip
)
},
{
LSTRKEY
(
"setip"
),
LFUNCVAL
(
wifi_ap_setip
)
},
{
LSTRKEY
(
"getmac"
),
LFUNCVAL
(
wifi_ap_getmac
)
},
{
LSTRKEY
(
"setmac"
),
LFUNCVAL
(
wifi_ap_setmac
)
},
{
LNILKEY
,
LNILVAL
}
};
...
...
@@ -372,6 +469,7 @@ const LUA_REG_TYPE wifi_map[] =
{
LSTRKEY
(
"getmode"
),
LFUNCVAL
(
wifi_getmode
)
},
{
LSTRKEY
(
"startsmart"
),
LFUNCVAL
(
wifi_start_smart
)
},
{
LSTRKEY
(
"stopsmart"
),
LFUNCVAL
(
wifi_exit_smart
)
},
{
LSTRKEY
(
"sleeptype"
),
LFUNCVAL
(
wifi_sleeptype
)
},
#if LUA_OPTIMIZE_MEMORY > 0
{
LSTRKEY
(
"sta"
),
LROVAL
(
wifi_station_map
)
},
{
LSTRKEY
(
"ap"
),
LROVAL
(
wifi_ap_map
)
},
...
...
@@ -381,6 +479,10 @@ const LUA_REG_TYPE wifi_map[] =
{
LSTRKEY
(
"SOFTAP"
),
LNUMVAL
(
SOFTAP_MODE
)
},
{
LSTRKEY
(
"STATIONAP"
),
LNUMVAL
(
STATIONAP_MODE
)
},
{
LSTRKEY
(
"NONE_SLEEP"
),
LNUMVAL
(
NONE_SLEEP_T
)
},
{
LSTRKEY
(
"LIGHT_SLEEP"
),
LNUMVAL
(
LIGHT_SLEEP_T
)
},
{
LSTRKEY
(
"MODEM_SLEEP"
),
LNUMVAL
(
MODEM_SLEEP_T
)
},
// { LSTRKEY( "STA_IDLE" ), LNUMVAL( STATION_IDLE ) },
// { LSTRKEY( "STA_CONNECTING" ), LNUMVAL( STATION_CONNECTING ) },
// { LSTRKEY( "STA_WRONGPWD" ), LNUMVAL( STATION_WRONG_PASSWORD ) },
...
...
@@ -410,6 +512,10 @@ LUALIB_API int ICACHE_FLASH_ATTR luaopen_wifi( lua_State *L )
MOD_REG_NUMBER
(
L
,
"SOFTAP"
,
SOFTAP_MODE
);
MOD_REG_NUMBER
(
L
,
"STATIONAP"
,
STATIONAP_MODE
);
MOD_REG_NUMBER
(
L
,
"NONE_SLEEP"
,
NONE_SLEEP_T
);
MOD_REG_NUMBER
(
L
,
"LIGHT_SLEEP"
,
LIGHT_SLEEP_T
);
MOD_REG_NUMBER
(
L
,
"MODEM_SLEEP"
,
MODEM_SLEEP_T
);
// MOD_REG_NUMBER( L, "STA_IDLE", STATION_IDLE );
// MOD_REG_NUMBER( L, "STA_CONNECTING", STATION_CONNECTING );
// MOD_REG_NUMBER( L, "STA_WRONGPWD", STATION_WRONG_PASSWORD );
...
...
bin/.gitignore
View file @
265d2705
...
...
@@ -4,4 +4,5 @@
*.bin
*.bin_rep
!.gitignore
!blank.bin
!esp_init_data_default.bin
bin/blank.bin
0 → 100644
View file @
265d2705
\ No newline at end of file
bin/esp_init_data_default.bin
0 → 100644
View file @
265d2705
File added
examples/fragment.lua
View file @
265d2705
...
...
@@ -301,3 +301,11 @@ sk:send("GET / HTTP/1.1\r\nHost: 115.239.210.27\r\nConnection: keep-alive\r\nAcc
sk
=
net
.
createConnection
(
net
.
TCP
,
1
)
sk
:
on
(
"receive"
,
function
(
sck
,
c
)
print
(
c
)
end
)
sk
:
on
(
"connection"
,
function
(
sck
)
sck
:
send
(
"GET / HTTPS/1.1\r\nHost: www.google.com.hk\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
)
end
)
sk
:
connect
(
443
,
"173.194.72.199"
)
wifi
.
sta
.
setip
({
ip
=
"192.168.18.119"
,
netmask
=
"255.255.255.0"
,
gateway
=
"192.168.18.1"
})
uart
.
on
(
"data"
,
"
\r
"
,
function
(
input
)
if
input
==
"quit\r"
then
uart
.
on
(
"data"
)
else
print
(
input
)
end
end
,
0
)
uart
.
on
(
"data"
,
"
\n
"
,
function
(
input
)
if
input
==
"quit\n"
then
uart
.
on
(
"data"
)
else
print
(
input
)
end
end
,
0
)
uart
.
on
(
"data"
,
5
,
function
(
input
)
if
input
==
"quit\r"
then
uart
.
on
(
"data"
)
else
print
(
input
)
end
end
,
0
)
uart
.
on
(
"data"
,
"
\r
"
,
function
(
input
)
if
input
==
"quit"
then
uart
.
on
(
"data"
)
else
print
(
input
)
end
end
,
1
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment