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
4cd2b554
Commit
4cd2b554
authored
Apr 24, 2015
by
devsaurus
Browse files
Merge remote-tracking branch 'upstream/dev' into dev
parents
fc19725f
6eb5b9c8
Changes
34
Expand all
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
4cd2b554
...
...
@@ -36,6 +36,12 @@ Tencent QQ group: 309957875<br />
-
cross compiler (done)
# Change log
2015-03-31
<br
/>
polish mqtt module, add queue for mqtt module.
<br
/>
add reconnect option to mqtt.connect api, :connect( host, port, secure, auto_reconnect, function(client) )
<br
/>
move node.readvdd33 to adc.readvdd33.
<br
/>
tools/esptool.py supported NodeMCU devkit automatic flash.
2015-03-18
<br
/>
update u8glib.
<br
/>
merge everything to master.
...
...
@@ -225,8 +231,10 @@ m:on("message", function(conn, topic, data)
end
end
)
-- for secure: m:connect("192.168.11.118", 1880, 1)
m
:
connect
(
"192.168.11.118"
,
1880
,
0
,
function
(
conn
)
print
(
"connected"
)
end
)
-- m:connect( host, port, secure, auto_reconnect, function(client) )
-- for secure: m:connect("192.168.11.118", 1880, 1, 0)
-- for auto-reconnect: m:connect("192.168.11.118", 1880, 0, 1)
m
:
connect
(
"192.168.11.118"
,
1880
,
0
,
0
,
function
(
conn
)
print
(
"connected"
)
end
)
-- subscribe topic with qos = 0
m
:
subscribe
(
"/topic"
,
0
,
function
(
conn
)
print
(
"subscribe success"
)
end
)
...
...
@@ -235,7 +243,7 @@ m:subscribe("/topic",0, function(conn) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
m
:
publish
(
"/topic"
,
"hello"
,
0
,
0
,
function
(
conn
)
print
(
"sent"
)
end
)
m
:
close
();
m
:
close
();
-- if auto-reconnect == 1, will disable auto-reconnect and then disconnect from host.
-- you can call m:connect again
```
...
...
app/cjson/strbuf.c
View file @
4cd2b554
...
...
@@ -46,7 +46,7 @@ int strbuf_init(strbuf_t *s, int len)
s
->
reallocs
=
0
;
s
->
debug
=
0
;
s
->
buf
=
c_malloc
(
size
);
s
->
buf
=
(
char
*
)
c_malloc
(
size
);
if
(
!
s
->
buf
){
NODE_ERR
(
"not enough memory
\n
"
);
return
-
1
;
...
...
@@ -60,7 +60,7 @@ strbuf_t *strbuf_new(int len)
{
strbuf_t
*
s
;
s
=
c_malloc
(
sizeof
(
strbuf_t
));
s
=
(
strbuf_t
*
)
c_malloc
(
sizeof
(
strbuf_t
));
if
(
!
s
){
NODE_ERR
(
"not enough memory
\n
"
);
return
NULL
;
...
...
app/include/user_version.h
View file @
4cd2b554
...
...
@@ -7,6 +7,6 @@
#define NODE_VERSION_INTERNAL 0U
#define NODE_VERSION "NodeMCU 0.9.5"
#define BUILD_DATE "build 20150
318
"
#define BUILD_DATE "build 20150
405
"
#endif
/* __USER_VERSION_H__ */
app/libc/c_stdio.h
View file @
4cd2b554
...
...
@@ -47,9 +47,9 @@ extern int c_stderr;
#define SEEK_END 2
/* set file offset to EOF plus offset */
#endif
#define c_malloc os_malloc
#define c_zalloc os_zalloc
#define c_free os_free
//
#define c_malloc os_malloc
//
#define c_zalloc os_zalloc
//
#define c_free os_free
extern
void
output_redirect
(
const
char
*
str
);
#define c_puts output_redirect
...
...
app/libc/c_stdlib.h
View file @
4cd2b554
...
...
@@ -29,9 +29,9 @@
#define os_realloc(p, s) mem_realloc((p), (s))
#endif
//
#define c_free os_free
//
#define c_malloc os_malloc
//
#define c_zalloc os_zalloc
#define c_free os_free
#define c_malloc os_malloc
#define c_zalloc os_zalloc
#define c_realloc os_realloc
#define c_abs abs
...
...
@@ -47,9 +47,9 @@
// c_getenv() get env "LUA_INIT" string for lua initialization.
const
char
*
c_getenv
(
const
char
*
__string
);
void
*
c_malloc
(
size_t
__size
);
void
*
c_zalloc
(
size_t
__size
);
void
c_free
(
void
*
);
//
void *c_malloc(size_t __size);
//
void *c_zalloc(size_t __size);
//
void c_free(void *);
// int c_rand(void);
// void c_srand(unsigned int __seed);
...
...
app/lua/luaconf.h
View file @
4cd2b554
...
...
@@ -541,8 +541,12 @@ extern int readline4lua(const char *prompt, char *buffer, int length);
/*
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
** Attention: This value should probably not be set higher than 1K.
** The size has direct impact on the C stack size needed be auxlib functions.
** For example: If set to 4K a call to string.gsub will need more than
** 5k C stack space.
*/
#define LUAL_BUFFERSIZE
((
BUFSIZ
)*4)
#define LUAL_BUFFERSIZE BUFSIZ
/* }================================================================== */
...
...
app/modules/adc.c
View file @
4cd2b554
...
...
@@ -8,6 +8,7 @@
#include "lrotable.h"
#include "c_types.h"
#include "user_interface.h"
// Lua: read(id) , return system adc
static
int
adc_sample
(
lua_State
*
L
)
...
...
@@ -19,12 +20,33 @@ static int adc_sample( lua_State* L )
return
1
;
}
// Lua: readvdd33()
static
int
adc_readvdd33
(
lua_State
*
L
)
{
uint32_t
vdd33
=
0
;
if
(
STATION_MODE
==
wifi_get_opmode
())
{
// Bug fix
wifi_set_opmode
(
STATIONAP_MODE
);
vdd33
=
readvdd33
();
wifi_set_opmode
(
STATION_MODE
);
}
else
{
vdd33
=
readvdd33
();
}
lua_pushinteger
(
L
,
vdd33
);
return
1
;
}
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const
LUA_REG_TYPE
adc_map
[]
=
{
{
LSTRKEY
(
"read"
),
LFUNCVAL
(
adc_sample
)
},
{
LSTRKEY
(
"readvdd33"
),
LFUNCVAL
(
adc_readvdd33
)
},
#if LUA_OPTIMIZE_MEMORY > 0
#endif
...
...
app/modules/auxmods.h
View file @
4cd2b554
...
...
@@ -80,7 +80,7 @@ LUALIB_API int ( luaopen_file )( lua_State *L );
LUALIB_API
int
(
luaopen_ow
)(
lua_State
*
L
);
#define AUXLIB_CJSON "cjson"
LUALIB_API
int
(
luaopen_
ow
)(
lua_State
*
L
);
LUALIB_API
int
(
luaopen_
cjson
)(
lua_State
*
L
);
// Helper macros
#define MOD_CHECK_ID( mod, id )\
...
...
app/modules/gpio.c
View file @
4cd2b554
...
...
@@ -146,6 +146,82 @@ static int lgpio_write( lua_State* L )
return
0
;
}
#define DELAY_TABLE_MAX_LEN 256
#define noInterrupts os_intr_lock
#define interrupts os_intr_unlock
#define delayMicroseconds os_delay_us
#define DIRECT_WRITE(pin, level) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), level))
// Lua: serout( pin, firstLevel, delay_table, [repeatNum] )
// -- serout( pin, firstLevel, delay_table, [repeatNum] )
// gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
// gpio.serout(1,1,{30,30,60,60,30,30}) -- serial one byte, b10110010
// gpio.serout(1,1,{30,70},8) -- serial 30% pwm 10k, lasts 8 cycles
// gpio.serout(1,1,{3,7},8) -- serial 30% pwm 100k, lasts 8 cycles
// gpio.serout(1,1,{0,0},8) -- serial 50% pwm as fast as possible, lasts 8 cycles
// gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
// gpio.serout(1,0,{20,10,10,20,10,10,10,100}) -- sim uart one byte 0x5A at about 100kbps
// gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles
static
int
lgpio_serout
(
lua_State
*
L
)
{
unsigned
level
;
unsigned
pin
;
unsigned
table_len
=
0
;
unsigned
repeat
=
0
;
int
delay_table
[
DELAY_TABLE_MAX_LEN
];
pin
=
luaL_checkinteger
(
L
,
1
);
MOD_CHECK_ID
(
gpio
,
pin
);
level
=
luaL_checkinteger
(
L
,
2
);
if
(
level
!=
HIGH
&&
level
!=
LOW
)
return
luaL_error
(
L
,
"wrong arg type"
);
if
(
lua_istable
(
L
,
3
)
)
{
table_len
=
lua_objlen
(
L
,
3
);
if
(
table_len
<=
0
||
table_len
>
DELAY_TABLE_MAX_LEN
)
return
luaL_error
(
L
,
"wrong arg range"
);
int
i
;
for
(
i
=
0
;
i
<
table_len
;
i
++
)
{
lua_rawgeti
(
L
,
3
,
i
+
1
);
delay_table
[
i
]
=
(
int
)
luaL_checkinteger
(
L
,
-
1
);
lua_pop
(
L
,
1
);
if
(
delay_table
[
i
]
<
0
||
delay_table
[
i
]
>
1000000
)
// can not delay more than 1000000 us
return
luaL_error
(
L
,
"delay must < 1000000 us"
);
}
}
else
{
return
luaL_error
(
L
,
"wrong arg range"
);
}
if
(
lua_isnumber
(
L
,
4
))
repeat
=
lua_tointeger
(
L
,
4
);
if
(
repeat
<
0
||
repeat
>
DELAY_TABLE_MAX_LEN
)
return
luaL_error
(
L
,
"delay must < 256"
);
if
(
repeat
==
0
)
repeat
=
1
;
int
j
;
bool
skip_loop
=
true
;
do
{
if
(
skip_loop
){
// skip the first loop.
skip_loop
=
false
;
continue
;
}
for
(
j
=
0
;
j
<
table_len
;
j
++
){
noInterrupts
();
// platform_gpio_write(pin, level);
DIRECT_WRITE
(
pin
,
level
);
interrupts
();
delayMicroseconds
(
delay_table
[
j
]);
level
=!
level
;
}
repeat
--
;
}
while
(
repeat
>
0
);
return
0
;
}
#undef DELAY_TABLE_MAX_LEN
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
...
...
@@ -154,6 +230,7 @@ const LUA_REG_TYPE gpio_map[] =
{
LSTRKEY
(
"mode"
),
LFUNCVAL
(
lgpio_mode
)
},
{
LSTRKEY
(
"read"
),
LFUNCVAL
(
lgpio_read
)
},
{
LSTRKEY
(
"write"
),
LFUNCVAL
(
lgpio_write
)
},
{
LSTRKEY
(
"serout"
),
LFUNCVAL
(
lgpio_serout
)
},
#ifdef GPIO_INTERRUPT_ENABLE
{
LSTRKEY
(
"trig"
),
LFUNCVAL
(
lgpio_trig
)
},
#endif
...
...
app/modules/mqtt.c
View file @
4cd2b554
This diff is collapsed.
Click to expand it.
app/modules/node.c
View file @
4cd2b554
...
...
@@ -100,13 +100,15 @@ static int node_chipid( lua_State* L )
lua_pushinteger
(
L
,
id
);
return
1
;
}
// deprecated, moved to adc module
// Lua: readvdd33()
static
int
node_readvdd33
(
lua_State
*
L
)
{
uint32_t
vdd33
=
readvdd33
();
lua_pushinteger
(
L
,
vdd33
);
return
1
;
}
//
static int node_readvdd33( lua_State* L )
//
{
//
uint32_t vdd33 = readvdd33();
//
lua_pushinteger(L, vdd33);
//
return 1;
//
}
// Lua: flashid()
static
int
node_flashid
(
lua_State
*
L
)
...
...
@@ -430,7 +432,8 @@ const LUA_REG_TYPE node_map[] =
#endif
{
LSTRKEY
(
"input"
),
LFUNCVAL
(
node_input
)
},
{
LSTRKEY
(
"output"
),
LFUNCVAL
(
node_output
)
},
{
LSTRKEY
(
"readvdd33"
),
LFUNCVAL
(
node_readvdd33
)
},
// Moved to adc module, use adc.readvdd33()
// { LSTRKEY( "readvdd33" ), LFUNCVAL( node_readvdd33) },
{
LSTRKEY
(
"compile"
),
LFUNCVAL
(
node_compile
)
},
{
LSTRKEY
(
"CPU80MHZ"
),
LNUMVAL
(
CPU80MHZ
)
},
{
LSTRKEY
(
"CPU160MHZ"
),
LNUMVAL
(
CPU160MHZ
)
},
...
...
app/modules/wifi.c
View file @
4cd2b554
...
...
@@ -8,6 +8,7 @@
#include "lrotable.h"
#include "c_string.h"
#include "c_stdlib.h"
#include "c_types.h"
#include "user_interface.h"
...
...
@@ -327,8 +328,8 @@ static int wifi_station_config( lua_State* L )
if
(
sl
>
32
||
ssid
==
NULL
)
return
luaL_error
(
L
,
"ssid:<32"
);
const
char
*
password
=
luaL_checklstring
(
L
,
2
,
&
pl
);
if
(
pl
>
64
||
password
==
NULL
)
return
luaL_error
(
L
,
"pwd:
<
64"
);
if
(
pl
<
8
||
pl
>
64
||
password
==
NULL
)
return
luaL_error
(
L
,
"pwd:
8~
64"
);
c_memset
(
sta_conf
.
ssid
,
0
,
32
);
c_memset
(
sta_conf
.
password
,
0
,
64
);
...
...
@@ -380,6 +381,7 @@ static int wifi_station_setauto( lua_State* L )
return
0
;
}
// Lua: table = wifi.sta.getap()
static
int
wifi_station_listap
(
lua_State
*
L
)
{
if
(
wifi_get_opmode
()
==
SOFTAP_MODE
)
...
...
@@ -437,44 +439,46 @@ static int wifi_ap_getbroadcast( lua_State* L ){
// Lua: wifi.ap.config(table)
static
int
wifi_ap_config
(
lua_State
*
L
)
{
struct
softap_config
config
;
size_t
len
;
wifi_softap_get_config
(
&
config
);
if
(
!
lua_istable
(
L
,
1
))
return
luaL_error
(
L
,
"wrong arg type"
);
struct
softap_config
config
;
wifi_softap_get_config
(
&
config
);
size_t
len
;
lua_getfield
(
L
,
1
,
"ssid"
);
if
(
!
lua_isnil
(
L
,
-
1
)){
/* found? */
if
(
lua_isstring
(
L
,
-
1
)
)
// deal with the ssid string
{
const
char
*
ssid
=
luaL_checklstring
(
L
,
-
1
,
&
len
);
if
(
len
>
32
)
return
luaL_error
(
L
,
"ssid:
<
32"
);
if
(
len
<
1
||
len
>
32
||
ssid
==
NULL
)
return
luaL_error
(
L
,
"ssid:
1~
32"
);
c_memset
(
config
.
ssid
,
0
,
32
);
c_memcpy
(
config
.
ssid
,
ssid
,
len
);
config
.
ssid_len
=
len
;
config
.
ssid_hidden
=
0
;
NODE_DBG
(
config
.
ssid
);
NODE_DBG
(
"
\n
"
);
config
.
ssid_len
=
len
;
config
.
ssid_hidden
=
0
;
}
else
return
luaL_error
(
L
,
"wrong arg type"
);
}
else
return
luaL_error
(
L
,
"
wrong arg type
"
);
return
luaL_error
(
L
,
"
ssid required
"
);
lua_getfield
(
L
,
1
,
"pwd"
);
if
(
!
lua_isnil
(
L
,
-
1
)){
/* found? */
if
(
lua_isstring
(
L
,
-
1
)
)
// deal with the password string
{
const
char
*
pwd
=
luaL_checklstring
(
L
,
-
1
,
&
len
);
if
(
len
>
64
)
return
luaL_error
(
L
,
"pwd:
<
64"
);
if
(
len
<
8
||
len
>
64
||
pwd
==
NULL
)
return
luaL_error
(
L
,
"pwd:
8~
64"
);
c_memset
(
config
.
password
,
0
,
64
);
c_memcpy
(
config
.
password
,
pwd
,
len
);
config
.
authmode
=
AUTH_WPA_WPA2_PSK
;
NODE_DBG
(
config
.
password
);
NODE_DBG
(
"
\n
"
);
config
.
authmode
=
AUTH_WPA_WPA2_PSK
;
}
else
return
luaL_error
(
L
,
"wrong arg type"
);
...
...
@@ -483,11 +487,162 @@ static int wifi_ap_config( lua_State* L )
config
.
authmode
=
AUTH_OPEN
;
}
config
.
max_connection
=
4
;
lua_getfield
(
L
,
1
,
"auth"
);
if
(
!
lua_isnil
(
L
,
-
1
))
{
config
.
authmode
=
(
uint8_t
)
luaL_checkinteger
(
L
,
-
1
);
NODE_DBG
(
config
.
authmode
);
NODE_DBG
(
"
\n
"
);
}
else
{
// keep whatever value resulted from "pwd" logic above
}
lua_getfield
(
L
,
1
,
"channel"
);
if
(
!
lua_isnil
(
L
,
-
1
))
{
unsigned
channel
=
luaL_checkinteger
(
L
,
-
1
);
if
(
channel
<
1
||
channel
>
13
)
return
luaL_error
(
L
,
"channel:1~13"
);
config
.
channel
=
(
uint8_t
)
channel
;
NODE_DBG
(
config
.
channel
);
NODE_DBG
(
"
\n
"
);
}
else
{
config
.
channel
=
6
;
}
lua_getfield
(
L
,
1
,
"hidden"
);
if
(
!
lua_isnil
(
L
,
-
1
))
{
config
.
ssid_hidden
=
(
uint8_t
)
luaL_checkinteger
(
L
,
-
1
);
NODE_DBG
(
config
.
ssid_hidden
);
NODE_DBG
(
"
\n
"
);
}
else
{
config
.
ssid_hidden
=
0
;
}
lua_getfield
(
L
,
1
,
"max"
);
if
(
!
lua_isnil
(
L
,
-
1
))
{
unsigned
max
=
luaL_checkinteger
(
L
,
-
1
);
if
(
max
<
1
||
max
>
4
)
return
luaL_error
(
L
,
"max:1~4"
);
config
.
max_connection
=
(
uint8_t
)
max
;
NODE_DBG
(
config
.
max_connection
);
NODE_DBG
(
"
\n
"
);
}
else
{
config
.
max_connection
=
4
;
}
lua_getfield
(
L
,
1
,
"beacon"
);
if
(
!
lua_isnil
(
L
,
-
1
))
{
unsigned
beacon
=
luaL_checkinteger
(
L
,
-
1
);
if
(
beacon
<
100
||
beacon
>
60000
)
return
luaL_error
(
L
,
"beacon:100~60000"
);
config
.
beacon_interval
=
(
uint16_t
)
beacon
;
NODE_DBG
(
config
.
beacon_interval
);
NODE_DBG
(
"
\n
"
);
}
else
{
config
.
beacon_interval
=
100
;
}
wifi_softap_set_config
(
&
config
);
// system_restart();
return
0
;
return
0
;
}
// Lua: table = wifi.ap.getclient()
static
int
wifi_ap_listclient
(
lua_State
*
L
)
{
if
(
wifi_get_opmode
()
==
STATION_MODE
)
{
return
luaL_error
(
L
,
"Can't list client in STATION_MODE mode"
);
}
char
temp
[
64
];
lua_newtable
(
L
);
struct
station_info
*
station
=
wifi_softap_get_station_info
();
struct
station_info
*
next_station
;
while
(
station
!=
NULL
)
{
c_sprintf
(
temp
,
IPSTR
,
IP2STR
(
&
station
->
ip
));
lua_pushstring
(
L
,
temp
);
c_sprintf
(
temp
,
MACSTR
,
MAC2STR
(
station
->
bssid
));
lua_setfield
(
L
,
-
2
,
temp
);
next_station
=
STAILQ_NEXT
(
station
,
next
);
c_free
(
station
);
station
=
next_station
;
}
return
1
;
}
// Lua: ip = wifi.ap.dhcp.config()
static
int
wifi_ap_dhcp_config
(
lua_State
*
L
)
{
if
(
!
lua_istable
(
L
,
1
))
return
luaL_error
(
L
,
"wrong arg type"
);
struct
dhcps_lease
lease
;
uint32_t
ip
;
ip
=
parse_key
(
L
,
"start"
);
if
(
ip
==
0
)
return
luaL_error
(
L
,
"wrong arg type"
);
lease
.
start_ip
=
ip
;
NODE_DBG
(
IPSTR
,
IP2STR
(
&
lease
.
start_ip
));
NODE_DBG
(
"
\n
"
);
// use configured max_connection to determine end
struct
softap_config
config
;
wifi_softap_get_config
(
&
config
);
lease
.
end_ip
=
lease
.
start_ip
;
ip4_addr4
(
&
lease
.
end_ip
)
+=
config
.
max_connection
-
1
;
char
temp
[
64
];
c_sprintf
(
temp
,
IPSTR
,
IP2STR
(
&
lease
.
start_ip
));
lua_pushstring
(
L
,
temp
);
c_sprintf
(
temp
,
IPSTR
,
IP2STR
(
&
lease
.
end_ip
));
lua_pushstring
(
L
,
temp
);
// note: DHCP max range = 101 from start_ip to end_ip
wifi_softap_dhcps_stop
();
wifi_softap_set_dhcps_lease
(
&
lease
);
wifi_softap_dhcps_start
();
return
2
;
}
// Lua: wifi.ap.dhcp.start()
static
int
wifi_ap_dhcp_start
(
lua_State
*
L
)
{
lua_pushboolean
(
L
,
wifi_softap_dhcps_start
());
return
1
;
}
// Lua: wifi.ap.dhcp.stop()
static
int
wifi_ap_dhcp_stop
(
lua_State
*
L
)
{
lua_pushboolean
(
L
,
wifi_softap_dhcps_stop
());
return
1
;
}
// Module function map
...
...
@@ -509,6 +664,14 @@ static const LUA_REG_TYPE wifi_station_map[] =
{
LNILKEY
,
LNILVAL
}
};
static
const
LUA_REG_TYPE
wifi_ap_dhcp_map
[]
=
{
{
LSTRKEY
(
"config"
),
LFUNCVAL
(
wifi_ap_dhcp_config
)
},
{
LSTRKEY
(
"start"
),
LFUNCVAL
(
wifi_ap_dhcp_start
)
},
{
LSTRKEY
(
"stop"
),
LFUNCVAL
(
wifi_ap_dhcp_stop
)
},
{
LNILKEY
,
LNILVAL
}
};
static
const
LUA_REG_TYPE
wifi_ap_map
[]
=
{
{
LSTRKEY
(
"config"
),
LFUNCVAL
(
wifi_ap_config
)
},
...
...
@@ -517,6 +680,12 @@ static const LUA_REG_TYPE wifi_ap_map[] =
{
LSTRKEY
(
"getbroadcast"
),
LFUNCVAL
(
wifi_ap_getbroadcast
)
},
{
LSTRKEY
(
"getmac"
),
LFUNCVAL
(
wifi_ap_getmac
)
},
{
LSTRKEY
(
"setmac"
),
LFUNCVAL
(
wifi_ap_setmac
)
},
{
LSTRKEY
(
"getclient"
),
LFUNCVAL
(
wifi_ap_listclient
)
},
#if LUA_OPTIMIZE_MEMORY > 0
{
LSTRKEY
(
"dhcp"
),
LROVAL
(
wifi_ap_dhcp_map
)
},
// { LSTRKEY( "__metatable" ), LROVAL( wifi_ap_map ) },
#endif
{
LNILKEY
,
LNILVAL
}
};
...
...
@@ -540,6 +709,12 @@ const LUA_REG_TYPE wifi_map[] =
{
LSTRKEY
(
"LIGHT_SLEEP"
),
LNUMVAL
(
LIGHT_SLEEP_T
)
},
{
LSTRKEY
(
"MODEM_SLEEP"
),
LNUMVAL
(
MODEM_SLEEP_T
)
},
{
LSTRKEY
(
"OPEN"
),
LNUMVAL
(
AUTH_OPEN
)
},
// { LSTRKEY( "WEP" ), LNUMVAL( AUTH_WEP ) },
{
LSTRKEY
(
"WPA_PSK"
),
LNUMVAL
(
AUTH_WPA_PSK
)
},
{
LSTRKEY
(
"WPA2_PSK"
),
LNUMVAL
(
AUTH_WPA2_PSK
)
},
{
LSTRKEY
(
"WPA_WPA2_PSK"
),
LNUMVAL
(
AUTH_WPA_WPA2_PSK
)
},
// { LSTRKEY( "STA_IDLE" ), LNUMVAL( STATION_IDLE ) },
// { LSTRKEY( "STA_CONNECTING" ), LNUMVAL( STATION_CONNECTING ) },
// { LSTRKEY( "STA_WRONGPWD" ), LNUMVAL( STATION_WRONG_PASSWORD ) },
...
...
@@ -573,6 +748,12 @@ LUALIB_API int luaopen_wifi( lua_State *L )
MOD_REG_NUMBER
(
L
,
"LIGHT_SLEEP"
,
LIGHT_SLEEP_T
);
MOD_REG_NUMBER
(
L
,
"MODEM_SLEEP"
,
MODEM_SLEEP_T
);
MOD_REG_NUMBER
(
L
,
"OPEN"
,
AUTH_OPEN
);
// MOD_REG_NUMBER( L, "WEP", AUTH_WEP );
MOD_REG_NUMBER
(
L
,
"WPA_PSK"
,
AUTH_WPA_PSK
);
MOD_REG_NUMBER
(
L
,
"WPA2_PSK"
,
AUTH_WPA2_PSK
);
MOD_REG_NUMBER
(
L
,
"WPA_WPA2_PSK"
,
AUTH_WPA_WPA2_PSK
);
// 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 );
...
...
@@ -589,6 +770,11 @@ LUALIB_API int luaopen_wifi( lua_State *L )
luaL_register
(
L
,
NULL
,
wifi_ap_map
);
lua_setfield
(
L
,
-
2
,
"ap"
);
// Setup the new table (dhcp) inside ap
lua_newtable
(
L
);
luaL_register
(
L
,
NULL
,
wifi_ap_dhcp_map
);
lua_setfield
(
L
,
-
1
,
"dhcp"
);
return
1
;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}
app/modules/ws2812.c
View file @
4cd2b554
...
...
@@ -3,6 +3,8 @@
#include "platform.h"
#include "auxmods.h"
#include "lrotable.h"
#include "c_stdlib.h"
#include "c_string.h"
/**
* All this code is mostly from http://www.esp8266.com/viewtopic.php?f=21&t=1143&sid=a620a377672cfe9f666d672398415fcb
* from user Markus Gritsch.
...
...
@@ -35,7 +37,10 @@ static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L)
{
const
uint8_t
pin
=
luaL_checkinteger
(
L
,
1
);
size_t
length
;
char
*
buffer
=
(
char
*
)
luaL_checklstring
(
L
,
2
,
&
length
);
// Cast away the constness.
const
char
*
rgb
=
luaL_checklstring
(
L
,
2
,
&
length
);
// dont modify lua-internal lstring - make a copy instead
char
*
buffer
=
(
char
*
)
c_malloc
(
length
);
c_memcpy
(
buffer
,
rgb
,
length
);
// Initialize the output pin:
platform_gpio_mode
(
pin
,
PLATFORM_GPIO_OUTPUT
,
PLATFORM_GPIO_FLOAT
);
...
...
@@ -59,17 +64,17 @@ static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L)
// Send the buffer:
os_intr_lock
();
const
char
*
const
end
=
buffer
+
length
;
while
(
buffer
!=
end
)
{
for
(
i
=
0
;
i
<
length
;
i
++
)
{
uint8_t
mask
=
0x80
;
while
(
mask
)
{
(
*
buffer
&
mask
)
?
send_ws_1
(
pin_num
[
pin
])
:
send_ws_0
(
pin_num
[
pin
]);
(
buffer
[
i
]
&
mask
)
?
send_ws_1
(
pin_num
[
pin
])
:
send_ws_0
(
pin_num
[
pin
]);
mask
>>=
1
;
}
++
buffer
;
}
os_intr_unlock
();
c_free
(
buffer
);
return
0
;
}
...
...
app/mqtt/mqtt_msg.c
View file @
4cd2b554
...
...
@@ -29,7 +29,7 @@
*
*/
#include
<
string.h
>
#include
"c_
string.h
"
#include "mqtt_msg.h"
#define MQTT_MAX_FIXED_HEADER_SIZE 3
...
...
@@ -61,7 +61,7 @@ static int append_string(mqtt_connection_t* connection, const char* string, int
connection
->
buffer
[
connection
->
message
.
length
++
]
=
len
>>
8
;
connection
->
buffer
[
connection
->
message
.
length
++
]
=
len
&
0xff
;
memcpy
(
connection
->
buffer
+
connection
->
message
.
length
,
string
,
len
);
c_
memcpy
(
connection
->
buffer
+
connection
->
message
.
length
,
string
,
len
);
connection
->
message
.
length
+=
len
;
return
len
+
2
;
...
...
@@ -121,7 +121,7 @@ static mqtt_message_t* fini_message(mqtt_connection_t* connection, int type, int
void
mqtt_msg_init
(
mqtt_connection_t
*
connection
,
uint8_t
*
buffer
,
uint16_t
buffer_length
)
{
memset
(
connection
,
0
,
sizeof
(
connection
));
c_
memset
(
connection
,
0
,
sizeof
(
connection
));
connection
->
buffer
=
buffer
;
connection
->
buffer_length
=
buffer_length
;
}
...
...
@@ -294,7 +294,7 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
variable_header
->
lengthMsb
=
0
;
variable_header
->
lengthLsb
=
4
;
memcpy
(
variable_header
->
magic
,
"MQTT"
,
4
);
c_
memcpy
(
variable_header
->
magic
,
"MQTT"
,
4
);
variable_header
->
version
=
4
;
variable_header
->
flags
=
0
;
variable_header
->
keepaliveMsb
=
info
->
keepalive
>>
8
;
...
...
@@ -305,7 +305,7 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
if
(
info
->
client_id
!=
NULL
&&
info
->
client_id
[
0
]
!=
'\0'
)
{
if
(
append_string
(
connection
,
info
->
client_id
,
strlen
(
info
->
client_id
))
<
0
)
if
(
append_string
(
connection
,
info
->
client_id
,
c_
strlen
(
info
->
client_id
))
<
0
)
return
fail_message
(
connection
);
}
else
...
...
@@ -313,10 +313,10 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
if
(
info
->
will_topic
!=
NULL
&&
info
->
will_topic
[
0
]
!=
'\0'
)
{
if
(
append_string
(
connection
,
info
->
will_topic
,
strlen
(
info
->
will_topic
))
<
0
)
if
(
append_string
(
connection
,
info
->
will_topic
,
c_
strlen
(
info
->
will_topic
))
<
0
)
return
fail_message
(
connection
);
if
(
append_string
(
connection
,
info
->
will_message
,
strlen
(
info
->
will_message
))
<
0
)
if
(
append_string
(
connection
,
info
->
will_message
,
c_
strlen
(
info
->
will_message
))
<
0
)
return
fail_message
(
connection
);
variable_header
->
flags
|=
MQTT_CONNECT_FLAG_WILL
;
...
...
@@ -327,7 +327,7 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
if
(
info
->
username
!=
NULL
&&
info
->
username
[
0
]
!=
'\0'
)
{
if
(
append_string
(
connection
,
info
->
username
,
strlen
(
info
->
username
))
<
0
)
if
(
append_string
(
connection
,
info
->
username
,
c_
strlen
(
info
->
username
))
<
0
)
return
fail_message
(
connection
);
variable_header
->
flags
|=
MQTT_CONNECT_FLAG_USERNAME
;
...
...
@@ -335,7 +335,7 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
if
(
info
->
password
!=
NULL
&&
info
->
password
[
0
]
!=
'\0'
)
{
if
(
append_string
(
connection
,
info
->
password
,
strlen
(
info
->
password
))
<
0
)
if
(
append_string
(
connection
,
info
->
password
,
c_
strlen
(
info
->
password
))
<
0
)
return
fail_message
(
connection
);
variable_header
->
flags
|=
MQTT_CONNECT_FLAG_PASSWORD
;
...
...
@@ -351,7 +351,7 @@ mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topi
if
(
topic
==
NULL
||
topic
[
0
]
==
'\0'
)
return
fail_message
(
connection
);
if
(
append_string
(
connection
,
topic
,
strlen
(
topic
))
<
0
)
if
(
append_string
(
connection
,
topic
,
c_
strlen
(
topic
))
<
0
)
return
fail_message
(
connection
);
if
(
qos
>
0
)
...
...
@@ -364,7 +364,7 @@ mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topi
if
(
connection
->
message
.
length
+
data_length
>
connection
->
buffer_length
)
return
fail_message
(
connection
);
memcpy
(
connection
->
buffer
+
connection
->
message
.
length
,
data
,
data_length
);
c_
memcpy
(
connection
->
buffer
+
connection
->
message
.
length
,
data
,
data_length
);
connection
->
message
.
length
+=
data_length
;
return
fini_message
(
connection
,
MQTT_MSG_TYPE_PUBLISH
,
0
,
qos
,
retain
);
...
...
@@ -412,7 +412,7 @@ mqtt_message_t* mqtt_msg_subscribe(mqtt_connection_t* connection, const char* to
if
((
*
message_id
=
append_message_id
(
connection
,
0
))
==
0
)
return
fail_message
(
connection
);
if
(
append_string
(
connection
,
topic
,
strlen
(
topic
))
<
0
)
if
(
append_string
(
connection
,
topic
,
c_
strlen
(
topic
))
<
0
)
return
fail_message
(
connection
);
if
(
connection
->
message
.
length
+
1
>
connection
->
buffer_length
)
...
...
@@ -432,7 +432,7 @@ mqtt_message_t* mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char*
if
((
*
message_id
=
append_message_id
(
connection
,
0
))
==
0
)
return
fail_message
(
connection
);
if
(
append_string
(
connection
,
topic
,
strlen
(
topic
))
<
0
)
if
(
append_string
(
connection
,
topic
,
c_
strlen
(
topic
))
<
0
)
return
fail_message
(
connection
);
return
fini_message
(
connection
,
MQTT_MSG_TYPE_SUBSCRIBE
,
0
,
1
,
0
);
...
...
app/mqtt/msg_queue.c
0 → 100644
View file @
4cd2b554
#include "c_string.h"
#include "c_stdlib.h"
#include "c_stdio.h"
#include "msg_queue.h"
msg_queue_t
*
msg_enqueue
(
msg_queue_t
**
head
,
mqtt_message_t
*
msg
,
uint16_t
msg_id
,
int
msg_type
,
int
publish_qos
){
if
(
!
head
){
return
NULL
;
}
if
(
!
msg
||
!
msg
->
data
||
msg
->
length
==
0
){
NODE_DBG
(
"empty message
\n
"
);
return
NULL
;
}
msg_queue_t
*
node
=
(
msg_queue_t
*
)
c_zalloc
(
sizeof
(
msg_queue_t
));
if
(
!
node
){
NODE_DBG
(
"not enough memory
\n
"
);
return
NULL
;
}
node
->
msg
.
data
=
(
uint8_t
*
)
c_zalloc
(
msg
->
length
);
if
(
!
node
->
msg
.
data
){
NODE_DBG
(
"not enough memory
\n
"
);
c_free
(
node
);
return
NULL
;
}
c_memcpy
(
node
->
msg
.
data
,
msg
->
data
,
msg
->
length
);
node
->
msg
.
length
=
msg
->
length
;
node
->
next
=
NULL
;
node
->
msg_id
=
msg_id
;
node
->
msg_type
=
msg_type
;
node
->
publish_qos
=
publish_qos
;
msg_queue_t
*
tail
=
*
head
;
if
(
tail
){
while
(
tail
->
next
!=
NULL
)
tail
=
tail
->
next
;
tail
->
next
=
node
;
}
else
{
*
head
=
node
;
}
return
node
;
}
void
msg_destroy
(
msg_queue_t
*
node
){
if
(
!
node
)
return
;
if
(
node
->
msg
.
data
){
c_free
(
node
->
msg
.
data
);
node
->
msg
.
data
=
NULL
;
}
c_free
(
node
);
}
msg_queue_t
*
msg_dequeue
(
msg_queue_t
**
head
){
if
(
!
head
||
!*
head
){
return
NULL
;
}
msg_queue_t
*
node
=
*
head
;
// fetch head.
*
head
=
node
->
next
;
// update head.
node
->
next
=
NULL
;
return
node
;
}
msg_queue_t
*
msg_peek
(
msg_queue_t
**
head
){
if
(
!
head
||
!*
head
){
return
NULL
;
}
return
*
head
;
// fetch head.
}
int
msg_size
(
msg_queue_t
**
head
){
if
(
!
head
||
!*
head
){
return
0
;
}
int
i
=
1
;
msg_queue_t
*
tail
=
*
head
;
if
(
tail
){
while
(
tail
->
next
!=
NULL
){
tail
=
tail
->
next
;
i
++
;
}
}
return
i
;
}
app/mqtt/msg_queue.h
0 → 100644
View file @
4cd2b554
#ifndef _MSG_QUEUE_H
#define _MSG_QUEUE_H 1
#include "mqtt_msg.h"
#ifdef __cplusplus
extern
"C"
{
#endif
struct
msg_queue_t
;
typedef
struct
msg_queue_t
{
struct
msg_queue_t
*
next
;
mqtt_message_t
msg
;
uint16_t
msg_id
;
int
msg_type
;
int
publish_qos
;
}
msg_queue_t
;
msg_queue_t
*
msg_enqueue
(
msg_queue_t
**
head
,
mqtt_message_t
*
msg
,
uint16_t
msg_id
,
int
msg_type
,
int
publish_qos
);
void
msg_destroy
(
msg_queue_t
*
node
);
msg_queue_t
*
msg_dequeue
(
msg_queue_t
**
head
);
msg_queue_t
*
msg_peek
(
msg_queue_t
**
head
);
int
msg_size
(
msg_queue_t
**
head
);
#ifdef __cplusplus
}
#endif
#endif
app/platform/flash_api.c
View file @
4cd2b554
...
...
@@ -321,7 +321,7 @@ bool flash_init_data_written(void)
// FLASH SEC - 4
uint32_t
data
[
2
]
ICACHE_STORE_ATTR
;
#if defined(FLASH_SAFE_API)
if
(
SPI_FLASH_RESULT_OK
==
flash_safe_read
((
flash_
rom
_get_sec_num
()
-
4
)
*
SPI_FLASH_SEC_SIZE
,
(
uint32
*
)
data
,
sizeof
(
data
)))
if
(
SPI_FLASH_RESULT_OK
==
flash_safe_read
((
flash_
safe
_get_sec_num
()
-
4
)
*
SPI_FLASH_SEC_SIZE
,
(
uint32
*
)
data
,
sizeof
(
data
)))
#else
if
(
SPI_FLASH_RESULT_OK
==
spi_flash_read
((
flash_rom_get_sec_num
()
-
4
)
*
SPI_FLASH_SEC_SIZE
,
(
uint32
*
)
data
,
sizeof
(
data
)))
#endif // defined(FLASH_SAFE_API)
...
...
@@ -369,8 +369,8 @@ bool flash_init_data_blank(void)
// It will init system config to blank!
bool
result
=
false
;
#if defined(FLASH_SAFE_API)
if
((
SPI_FLASH_RESULT_OK
==
flash_safe_erase_sector
((
flash_
rom
_get_sec_num
()
-
2
)))
&&
(
SPI_FLASH_RESULT_OK
==
flash_safe_erase_sector
((
flash_
rom
_get_sec_num
()
-
1
))))
if
((
SPI_FLASH_RESULT_OK
==
flash_safe_erase_sector
((
flash_
safe
_get_sec_num
()
-
2
)))
&&
(
SPI_FLASH_RESULT_OK
==
flash_safe_erase_sector
((
flash_
safe
_get_sec_num
()
-
1
))))
#else
if
((
SPI_FLASH_RESULT_OK
==
spi_flash_erase_sector
((
flash_rom_get_sec_num
()
-
2
)))
&&
(
SPI_FLASH_RESULT_OK
==
spi_flash_erase_sector
((
flash_rom_get_sec_num
()
-
1
))))
...
...
app/platform/flash_fs.h
View file @
4cd2b554
...
...
@@ -71,6 +71,9 @@
#define fs_rename myspiffs_rename
#define fs_size myspiffs_size
#define fs_mount myspiffs_mount
#define fs_unmount myspiffs_unmount
#define FS_NAME_MAX_LENGTH SPIFFS_OBJ_NAME_LEN
#endif
...
...
app/spiffs/spiffs.c
View file @
4cd2b554
...
...
@@ -42,7 +42,7 @@ The small 4KB sectors allow for greater flexibility in applications th
********************/
void
spiffs_mount
()
{
void
my
spiffs_mount
()
{
spiffs_config
cfg
;
cfg
.
phys_addr
=
(
u32_t
)
platform_flash_get_first_free_block_address
(
NULL
);
cfg
.
phys_addr
+=
0x3000
;
...
...
@@ -69,6 +69,10 @@ void spiffs_mount() {
NODE_DBG
(
"mount res: %i
\n
"
,
res
);
}
void
myspiffs_unmount
()
{
SPIFFS_unmount
(
&
fs
);
}
// FS formatting function
// Returns 1 if OK, 0 for error
int
myspiffs_format
(
void
)
...
...
@@ -85,7 +89,7 @@ int myspiffs_format( void )
while
(
sect_first
<=
sect_last
)
if
(
platform_flash_erase_sector
(
sect_first
++
)
==
PLATFORM_ERR
)
return
0
;
spiffs_mount
();
my
spiffs_mount
();
return
1
;
}
...
...
app/spiffs/spiffs.h
View file @
4cd2b554
...
...
@@ -477,6 +477,8 @@ u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages);
#if SPIFFS_CACHE
#endif
void
myspiffs_mount
();
void
myspiffs_unmount
();
int
myspiffs_open
(
const
char
*
name
,
int
flags
);
int
myspiffs_close
(
int
fd
);
size_t
myspiffs_write
(
int
fd
,
const
void
*
ptr
,
size_t
len
);
...
...
Prev
1
2
Next
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