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
606f9166
Unverified
Commit
606f9166
authored
Aug 29, 2020
by
Philip Gladstone
Committed by
GitHub
Aug 29, 2020
Browse files
First phase of number to integer conversion (#3221)
parent
0e02c0e5
Changes
30
Hide whitespace changes
Inline
Side-by-side
app/lua/lauxlib.h
View file @
606f9166
...
...
@@ -110,7 +110,9 @@ LUALIB_API void luaL_assertfail(const char *file, int line, const char *message)
#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
#define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a))
#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
#define luaL_optunsigned(L,a,d) ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d)))
#define luaL_checktable(L,n) luaL_checktype(L, (n), LUA_TTABLE)
#define luaL_checkfunction(L,n) luaL_checktype(L, (n), LUA_TFUNCTION)
...
...
app/lua/lua.h
View file @
606f9166
...
...
@@ -105,12 +105,12 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
/* type of numbers in Lua */
typedef
LUA_NUMBER
lua_Number
;
typedef
LUA_FLOAT
lua_Float
;
/* type for integer functions */
typedef
LUA_INTEGER
lua_Integer
;
typedef
LUAI_UINT32
lua_Unsigned
;
/*
** state manipulation
...
...
@@ -297,6 +297,9 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT)
#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
#define lua_tounsigned(L,i) ((lua_Unsigned)lua_tointeger(L,i))
/* error codes from cross-compiler returned by lua_dump */
/* target integer is too small to hold a value */
#define LUA_ERR_CC_INTOVERFLOW 101
...
...
app/lua/luaconf.h
View file @
606f9166
...
...
@@ -546,6 +546,7 @@ extern int readline4lua(const char *prompt, char *buffer, int length);
#define LUA_NUMBER_DOUBLE
#define LUA_NUMBER double
#endif
#define LUA_FLOAT double
/*
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
...
...
app/lua53/lua.h
View file @
606f9166
...
...
@@ -87,6 +87,7 @@ typedef struct lua_State lua_State;
/* type of numbers in Lua */
typedef
LUA_NUMBER
lua_Number
;
typedef
LUA_FLOAT
lua_Float
;
/* type for integer functions */
...
...
app/lua53/luaconf.h
View file @
606f9166
...
...
@@ -302,7 +302,7 @@
#error "numeric float type not defined"
#endif
/* } */
#define LUA_FLOAT LUA_NUMBER
/*
...
...
app/modules/bit.c
View file @
606f9166
...
...
@@ -48,15 +48,13 @@ typedef size_t lua_UInteger;
#define LOGICAL_SHIFT(name, op) \
static int bit_ ## name(lua_State *L) { \
lua_pushinteger(L, (lua_UInteger)TOBIT(L, 1) op \
(unsigned)luaL_checknumber(L, 2)); \
lua_pushinteger(L, (lua_UInteger)TOBIT(L, 1) op luaL_checkunsigned(L, 2)); \
return 1; \
}
#define ARITHMETIC_SHIFT(name, op) \
static int bit_ ## name(lua_State *L) { \
lua_pushinteger(L, (lua_Integer)TOBIT(L, 1) op \
(unsigned)luaL_checknumber(L, 2)); \
lua_pushinteger(L, (lua_Integer)TOBIT(L, 1) op luaL_checkunsigned(L, 2)); \
return 1; \
}
...
...
@@ -78,7 +76,7 @@ static int bit_bit( lua_State* L )
// Lua: res = isset( value, position )
static
int
bit_isset
(
lua_State
*
L
)
{
lua_UInteger
val
=
(
lua
_UInteger
)
luaL_checkinteger
(
L
,
1
);
lua_UInteger
val
=
lua
L_checkunsigned
(
L
,
1
);
unsigned
pos
=
(
unsigned
)
luaL_checkinteger
(
L
,
2
);
lua_pushboolean
(
L
,
val
&
(
1
<<
pos
)
?
1
:
0
);
...
...
@@ -88,7 +86,7 @@ static int bit_isset( lua_State* L )
// Lua: res = isclear( value, position )
static
int
bit_isclear
(
lua_State
*
L
)
{
lua_UInteger
val
=
(
lua
_UInteger
)
luaL_checkinteger
(
L
,
1
);
lua_UInteger
val
=
lua
L_checkunsigned
(
L
,
1
);
unsigned
pos
=
(
unsigned
)
luaL_checkinteger
(
L
,
2
);
lua_pushboolean
(
L
,
val
&
(
1
<<
pos
)
?
0
:
1
);
...
...
@@ -98,7 +96,7 @@ static int bit_isclear( lua_State* L )
// Lua: res = set( value, pos1, pos2, ... )
static
int
bit_set
(
lua_State
*
L
)
{
lua_UInteger
val
=
(
lua
_UInteger
)
luaL_checkinteger
(
L
,
1
);
lua_UInteger
val
=
lua
L_checkunsigned
(
L
,
1
);
unsigned
total
=
lua_gettop
(
L
),
i
;
for
(
i
=
2
;
i
<=
total
;
i
++
)
...
...
@@ -110,7 +108,7 @@ static int bit_set( lua_State* L )
// Lua: res = clear( value, pos1, pos2, ... )
static
int
bit_clear
(
lua_State
*
L
)
{
lua_UInteger
val
=
(
lua
_UInteger
)
luaL_checkinteger
(
L
,
1
);
lua_UInteger
val
=
lua
L_checkunsigned
(
L
,
1
);
unsigned
total
=
lua_gettop
(
L
),
i
;
for
(
i
=
2
;
i
<=
total
;
i
++
)
...
...
app/modules/color_utils.c
View file @
606f9166
...
...
@@ -159,9 +159,9 @@ static int cu_hsv2grb(lua_State *L) {
uint32_t
tmp_color
=
hsv2grb
(
hue
,
sat
,
val
);
// return
lua_push
number
(
L
,
(
tmp_color
&
0x00FF0000
)
>>
16
);
lua_push
number
(
L
,
(
tmp_color
&
0x0000FF00
)
>>
8
);
lua_push
number
(
L
,
(
tmp_color
&
0x000000FF
));
lua_push
unsigned
(
L
,
(
tmp_color
&
0x00FF0000
)
>>
16
);
lua_push
unsigned
(
L
,
(
tmp_color
&
0x0000FF00
)
>>
8
);
lua_push
unsigned
(
L
,
(
tmp_color
&
0x000000FF
));
return
3
;
}
...
...
@@ -181,10 +181,10 @@ static int cu_hsv2grbw(lua_State *L) {
uint32_t
tmp_color
=
hsv2grbw
(
hue
,
sat
,
val
);
// return g, r, b, w
lua_push
number
(
L
,
(
tmp_color
&
0xFF000000
)
>>
24
);
lua_push
number
(
L
,
(
tmp_color
&
0x00FF0000
)
>>
16
);
lua_push
number
(
L
,
(
tmp_color
&
0x0000FF00
)
>>
8
);
lua_push
number
(
L
,
(
tmp_color
&
0x000000FF
));
lua_push
unsigned
(
L
,
(
tmp_color
&
0xFF000000
)
>>
24
);
lua_push
unsigned
(
L
,
(
tmp_color
&
0x00FF0000
)
>>
16
);
lua_push
unsigned
(
L
,
(
tmp_color
&
0x0000FF00
)
>>
8
);
lua_push
unsigned
(
L
,
(
tmp_color
&
0x000000FF
));
return
4
;
}
...
...
@@ -203,9 +203,9 @@ static int cu_color_wheel(lua_State *L) {
uint8_t
b
=
(
color
&
0x000000FF
)
>>
0
;
// return
lua_push
number
(
L
,
g
);
lua_push
number
(
L
,
r
);
lua_push
number
(
L
,
b
);
lua_push
unsigned
(
L
,
g
);
lua_push
unsigned
(
L
,
r
);
lua_push
unsigned
(
L
,
b
);
return
3
;
}
...
...
@@ -226,9 +226,9 @@ static int cu_grb2hsv(lua_State *L) {
uint8_t
v
=
(
hsv
&
0x000000FF
)
>>
0
;
// return
lua_push
number
(
L
,
h
);
lua_push
number
(
L
,
s
);
lua_push
number
(
L
,
v
);
lua_push
unsigned
(
L
,
h
);
lua_push
unsigned
(
L
,
s
);
lua_push
unsigned
(
L
,
v
);
return
3
;
}
...
...
app/modules/dht.c
View file @
606f9166
...
...
@@ -15,20 +15,25 @@ int platform_dht_exists( unsigned id )
return
((
id
<
NUM_DHT
)
&&
(
id
>
0
));
}
static
void
aux_read
(
lua_State
*
L
)
{
double
temp
=
dht_getTemperature
();
double
humi
=
dht_getHumidity
();
int
tempdec
=
(
int
)((
temp
-
(
int
)
temp
)
*
1000
);
int
humidec
=
(
int
)((
humi
-
(
int
)
humi
)
*
1000
);
lua_pushnumber
(
L
,
(
lua_Float
)
temp
);
lua_pushnumber
(
L
,
(
lua_Float
)
humi
);
lua_pushinteger
(
L
,
tempdec
);
lua_pushinteger
(
L
,
humidec
);
}
// Lua: status, temp, humi, tempdec, humidec = dht.read( id )
static
int
dht_lapi_read
(
lua_State
*
L
)
{
unsigned
id
=
luaL_checkinteger
(
L
,
1
);
MOD_CHECK_ID
(
dht
,
id
);
lua_pushinteger
(
L
,
dht_read_universal
(
id
)
);
double
temp
=
dht_getTemperature
();
double
humi
=
dht_getHumidity
();
int
tempdec
=
(
int
)((
temp
-
(
int
)
temp
)
*
1000
);
int
humidec
=
(
int
)((
humi
-
(
int
)
humi
)
*
1000
);
lua_pushnumber
(
L
,
temp
);
lua_pushnumber
(
L
,
humi
);
lua_pushnumber
(
L
,
tempdec
);
lua_pushnumber
(
L
,
humidec
);
aux_read
(
L
);
return
5
;
}
...
...
@@ -38,14 +43,7 @@ static int dht_lapi_read11( lua_State *L )
unsigned
id
=
luaL_checkinteger
(
L
,
1
);
MOD_CHECK_ID
(
dht
,
id
);
lua_pushinteger
(
L
,
dht_read11
(
id
)
);
double
temp
=
dht_getTemperature
();
double
humi
=
dht_getHumidity
();
int
tempdec
=
(
int
)((
temp
-
(
int
)
temp
)
*
1000
);
int
humidec
=
(
int
)((
humi
-
(
int
)
humi
)
*
1000
);
lua_pushnumber
(
L
,
temp
);
lua_pushnumber
(
L
,
humi
);
lua_pushnumber
(
L
,
tempdec
);
lua_pushnumber
(
L
,
humidec
);
aux_read
(
L
);
return
5
;
}
...
...
@@ -55,14 +53,7 @@ static int dht_lapi_readxx( lua_State *L )
unsigned
id
=
luaL_checkinteger
(
L
,
1
);
MOD_CHECK_ID
(
dht
,
id
);
lua_pushinteger
(
L
,
dht_read
(
id
)
);
double
temp
=
dht_getTemperature
();
double
humi
=
dht_getHumidity
();
int
tempdec
=
(
int
)((
temp
-
(
int
)
temp
)
*
1000
);
int
humidec
=
(
int
)((
humi
-
(
int
)
humi
)
*
1000
);
lua_pushnumber
(
L
,
temp
);
lua_pushnumber
(
L
,
humi
);
lua_pushnumber
(
L
,
tempdec
);
lua_pushnumber
(
L
,
humidec
);
aux_read
(
L
);
return
5
;
}
...
...
app/modules/enduser_setup.c
View file @
606f9166
...
...
@@ -194,7 +194,7 @@ static void enduser_setup_error(int line, const char *str, int err)
if
(
state
!=
NULL
&&
state
->
lua_err_cb_ref
!=
LUA_NOREF
)
{
lua_rawgeti
(
L
,
LUA_REGISTRYINDEX
,
state
->
lua_err_cb_ref
);
lua_push
numb
er
(
L
,
err
);
lua_push
integ
er
(
L
,
err
);
lua_pushfstring
(
L
,
"%d:
\t
%s"
,
line
,
str
);
luaL_pcallx
(
L
,
2
,
0
);
}
...
...
app/modules/http.c
View file @
606f9166
...
...
@@ -33,7 +33,7 @@ static void http_callback( char * response, int http_status, char ** full_respon
lua_rawgeti
(
L
,
LUA_REGISTRYINDEX
,
http_callback_registry
);
lua_push
numb
er
(
L
,
http_status
);
lua_push
integ
er
(
L
,
http_status
);
if
(
http_status
!=
HTTP_STATUS_GENERIC_ERROR
&&
response
)
{
lua_pushlstring
(
L
,
response
,
(
size_t
)
body_size
);
...
...
app/modules/mcp4725.c
View file @
606f9166
...
...
@@ -30,7 +30,7 @@ static uint8 get_address(lua_State* L, uint8 i2c_address){
{
if
(
lua_isnumber
(
L
,
-
1
)
)
{
temp_var
=
lua_to
numb
er
(
L
,
-
1
);
temp_var
=
lua_to
integ
er
(
L
,
-
1
);
if
(
temp_var
<
2
){
temp_var
=
MCP4725_I2C_ADDR_A2_MASK
&
(
temp_var
<<
2
);
addr_temp
|=
temp_var
;
...
...
@@ -50,7 +50,7 @@ static uint8 get_address(lua_State* L, uint8 i2c_address){
{
if
(
lua_isnumber
(
L
,
-
1
)
)
{
temp_var
=
lua_to
numb
er
(
L
,
-
1
);
temp_var
=
lua_to
integ
er
(
L
,
-
1
);
if
(
temp_var
<
2
){
temp_var
=
MCP4725_I2C_ADDR_A1_MASK
&
(
temp_var
<<
1
);
addr_temp
|=
temp_var
;
...
...
@@ -70,7 +70,7 @@ static uint8 get_address(lua_State* L, uint8 i2c_address){
{
if
(
lua_isnumber
(
L
,
-
1
)
)
{
temp_var
=
lua_to
numb
er
(
L
,
-
1
);
temp_var
=
lua_to
integ
er
(
L
,
-
1
);
if
(
temp_var
<
2
){
temp_var
=
MCP4725_I2C_ADDR_A0_MASK
&
(
temp_var
);
addr_temp
|=
temp_var
;
...
...
@@ -103,7 +103,7 @@ static int mcp4725_write(lua_State* L){
{
if
(
lua_isnumber
(
L
,
-
1
)
)
{
temp_var
=
lua_to
numb
er
(
L
,
-
1
);
temp_var
=
lua_to
integ
er
(
L
,
-
1
);
if
(
temp_var
>=
0
&&
temp_var
<=
4095
){
dac_value
=
temp_var
<<
4
;
}
...
...
@@ -149,7 +149,7 @@ static int mcp4725_write(lua_State* L){
{
if
(
lua_isnumber
(
L
,
-
1
)
)
{
temp_var
=
lua_to
numb
er
(
L
,
-
1
);
temp_var
=
lua_to
integ
er
(
L
,
-
1
);
if
(
temp_var
>=
0
&&
temp_var
<=
3
){
cmd_byte
|=
temp_var
<<
1
;
}
...
...
@@ -193,12 +193,12 @@ static int mcp4725_read(lua_State* L){
}
platform_i2c_send_stop
(
mcp4725_i2c_id
);
lua_push
numb
er
(
L
,
(
recieve_buffer
[
0
]
&
0x06
)
>>
1
);
lua_push
numb
er
(
L
,
(
recieve_buffer
[
1
]
<<
4
)
|
(
recieve_buffer
[
2
]
>>
4
));
lua_push
numb
er
(
L
,
(
recieve_buffer
[
3
]
&
0x60
)
>>
5
);
lua_push
numb
er
(
L
,
((
recieve_buffer
[
3
]
&
0xf
)
<<
8
)
|
recieve_buffer
[
4
]);
lua_push
numb
er
(
L
,
(
recieve_buffer
[
0
]
&
0x80
)
>>
7
);
lua_push
numb
er
(
L
,
(
recieve_buffer
[
0
]
&
0x40
)
>>
6
);
lua_push
integ
er
(
L
,
(
recieve_buffer
[
0
]
&
0x06
)
>>
1
);
lua_push
integ
er
(
L
,
(
recieve_buffer
[
1
]
<<
4
)
|
(
recieve_buffer
[
2
]
>>
4
));
lua_push
integ
er
(
L
,
(
recieve_buffer
[
3
]
&
0x60
)
>>
5
);
lua_push
integ
er
(
L
,
((
recieve_buffer
[
3
]
&
0xf
)
<<
8
)
|
recieve_buffer
[
4
]);
lua_push
integ
er
(
L
,
(
recieve_buffer
[
0
]
&
0x80
)
>>
7
);
lua_push
integ
er
(
L
,
(
recieve_buffer
[
0
]
&
0x40
)
>>
6
);
return
6
;
}
...
...
app/modules/mdns.c
View file @
606f9166
...
...
@@ -44,7 +44,7 @@ static int mdns_register(lua_State *L)
const
char
*
key
=
luaL_checkstring
(
L
,
-
2
);
if
(
strcmp
(
key
,
"port"
)
==
0
)
{
info
.
service_port
=
luaL_check
numb
er
(
L
,
-
1
);
info
.
service_port
=
luaL_check
integ
er
(
L
,
-
1
);
}
else
if
(
strcmp
(
key
,
"service"
)
==
0
)
{
info
.
service_name
=
luaL_checkstring
(
L
,
-
1
);
}
else
if
(
strcmp
(
key
,
"description"
)
==
0
)
{
...
...
app/modules/node.c
View file @
606f9166
...
...
@@ -18,6 +18,10 @@
#define DELAY2SEC 2000
#ifndef LUA_MAXINTEGER
#define LUA_MAXINTEGER INT_MAX
#endif
static
void
restart_callback
(
void
*
arg
)
{
UNUSED
(
arg
);
system_restart
();
...
...
@@ -67,7 +71,8 @@ static int node_restart( lua_State* L )
}
static
int
dsleepMax
(
lua_State
*
L
)
{
lua_pushnumber
(
L
,
(
uint64_t
)
system_rtc_clock_cali_proc
()
*
(
0x80000000
-
1
)
/
(
0x1000
));
uint64_t
dsm
=
(((
uint64_t
)
system_rtc_clock_cali_proc
())
*
(
0x80000000
-
1
))
/
0x1000
;
lua_pushnumber
(
L
,
(
lua_Float
)
dsm
);
return
1
;
}
...
...
@@ -76,34 +81,28 @@ static int node_deepsleep( lua_State* L )
{
uint64
us
;
uint8
option
;
//us = luaL_checkinteger( L, 1 );
// Set deleep option, skip if nil
if
(
lua_isnumber
(
L
,
2
)
)
{
option
=
lua_tointeger
(
L
,
2
);
if
(
option
<
0
||
option
>
4
)
return
luaL_error
(
L
,
"wrong arg range"
);
else
system_deep_sleep_set_option
(
option
);
option
=
lua_tounsigned
(
L
,
2
);
luaL_argcheck
(
L
,
2
,
option
<=
4
,
"wrong option value"
);
system_deep_sleep_set_option
(
option
);
}
bool
instant
=
false
;
if
(
lua_isnumber
(
L
,
3
))
instant
=
lua_tointeger
(
L
,
3
);
// Set deleep time, skip if nil
bool
instant
=
(
lua_isnumber
(
L
,
3
)
&&
luaL_checkinteger
(
L
,
3
))
?
true
:
false
;
if
(
lua_isnumber
(
L
,
1
)
)
{
#if LUA_VERSION_NUM == 501
us
=
luaL_checknumber
(
L
,
1
);
// if ( us <= 0 )
if
(
us
<
0
)
return
luaL_error
(
L
,
"wrong arg range"
);
#else
/* 503 */
us
=
lua_isinteger
(
L
,
1
)
?
lua_tounsigned
(
L
,
1
)
:
(
uint64
)
lua_tonumber
(
L
,
1
);
#endif
luaL_argcheck
(
L
,
1
,
us
<
36000000000ull
,
"invalid time value"
);
if
(
instant
)
system_deep_sleep_instant
(
us
);
else
{
if
(
instant
)
system_deep_sleep_instant
(
us
);
else
system_deep_sleep
(
us
);
}
}
system_deep_sleep
(
us
);
}
return
0
;
}
...
...
@@ -201,7 +200,7 @@ static int node_info( lua_State* L ){
lua_createtable
(
L
,
0
,
4
);
lua_pushboolean
(
L
,
BUILDINFO_SSL
);
lua_setfield
(
L
,
-
2
,
"ssl"
);
lua_push
numb
er
(
L
,
BUILDINFO_LFS_SIZE
);
lua_push
integ
er
(
L
,
BUILDINFO_LFS_SIZE
);
lua_setfield
(
L
,
-
2
,
"lfs_size"
);
add_string_field
(
L
,
BUILDINFO_MODULES
,
"modules"
);
add_string_field
(
L
,
BUILDINFO_BUILD_TYPE
,
"number_type"
);
...
...
@@ -545,16 +544,25 @@ static int node_osprint( lua_State* L )
return
0
;
}
int
node_random_range
(
int
l
,
int
u
)
{
static
lua_Unsigned
random_value
()
{
// Hopefully the compiler is smart enought to spot the constant IF check
if
(
sizeof
(
lua_Unsigned
)
==
4
)
{
return
os_random
();
}
else
{
return
(((
uint64_t
)
os_random
())
<<
32
)
+
(
uint32_t
)
os_random
();
}
}
lua_Integer
node_random_range
(
lua_Integer
l
,
lua_Integer
u
)
{
// The range is the number of different values to return
u
nsigned
int
range
=
u
+
1
-
l
;
lua_U
nsigned
range
=
u
+
1
-
l
;
// If this is very large then use simpler code
if
(
range
>=
0x7fffffff
)
{
u
nsigned
in
t
v
;
if
(
range
>=
LUA_MAXINTEGER
)
{
u
int64_
t
v
;
// This cannot loop more than half the time
while
((
v
=
os_
random
())
>=
range
)
{
while
((
v
=
random
_value
())
>=
range
)
{
}
// Now v is in the range [0, range)
...
...
@@ -566,19 +574,19 @@ int node_random_range(int l, int u) {
return
l
;
}
// Another easy case -- uniform 32-bit
// Another easy case -- uniform 32
/64
-bit
if
(
range
==
0
)
{
return
os_
random
();
return
random
_value
();
}
// Now we have to figure out what a large multiple of range is
// that just fits into 32 bits.
// that just fits into 32
/64
bits.
// The limit will be less than 1 << 32 by some amount (not much)
uint32_t
limit
=
((
0x80000000
/
((
range
+
1
)
>>
1
))
-
1
)
*
range
;
lua_Unsigned
limit
=
((
(
1
+
(
lua_Unsigned
)
LUA_MAXINTEGER
)
/
((
range
+
1
)
>>
1
))
-
1
)
*
range
;
uint32_t
v
;
lua_Unsigned
v
;
while
((
v
=
os_
random
())
>=
limit
)
{
while
((
v
=
random
_value
())
>=
limit
)
{
}
// Now v is uniformly distributed in [0, limit) and limit is a multiple of range
...
...
@@ -587,33 +595,33 @@ int node_random_range(int l, int u) {
}
static
int
node_random
(
lua_State
*
L
)
{
int
u
;
int
l
;
lua_Integer
u
;
lua_Integer
l
;
switch
(
lua_gettop
(
L
))
{
/* check number of arguments */
case
0
:
{
/* no arguments */
#ifdef LUA_NUMBER_INTEGRAL
lua_push
numb
er
(
L
,
0
);
/* Number between 0 and 1 - always 0 with ints */
lua_push
integ
er
(
L
,
0
);
/* Number between 0 and 1 - always 0 with ints */
#else
lua_pushnumber
(
L
,
(
lua_Number
)
os_random
()
/
(
lua_Number
)(
1LL
<<
32
));
lua_pushnumber
(
L
,
(
(
double
)
random_value
()
/
16
/
(
1LL
<<
(
8
*
sizeof
(
lua_Unsigned
)
-
4
))
));
#endif
return
1
;
}
case
1
:
{
/* only upper limit */
l
=
1
;
u
=
luaL_checkint
(
L
,
1
);
u
=
luaL_checkint
eger
(
L
,
1
);
break
;
}
case
2
:
{
/* lower and upper limits */
l
=
luaL_checkint
(
L
,
1
);
u
=
luaL_checkint
(
L
,
2
);
l
=
luaL_checkint
eger
(
L
,
1
);
u
=
luaL_checkint
eger
(
L
,
2
);
break
;
}
default:
return
luaL_error
(
L
,
"wrong number of arguments"
);
}
luaL_argcheck
(
L
,
l
<=
u
,
2
,
"interval is empty"
);
lua_push
numb
er
(
L
,
node_random_range
(
l
,
u
));
/* int between `l' and `u' */
lua_push
integ
er
(
L
,
node_random_range
(
l
,
u
));
/* int between `l' and `u' */
return
1
;
}
...
...
app/modules/perf.c
View file @
606f9166
...
...
@@ -115,20 +115,20 @@ static int perf_stop(lua_State *L)
DATA
*
d
=
data
;
data
=
NULL
;
lua_push
number
(
L
,
d
->
total_samples
);
lua_push
number
(
L
,
d
->
outside_samples
);
lua_push
unsigned
(
L
,
d
->
total_samples
);
lua_push
unsigned
(
L
,
d
->
outside_samples
);
lua_newtable
(
L
);
int
i
;
uint32_t
addr
=
d
->
start
;
for
(
i
=
0
;
i
<
d
->
bucket_count
;
i
++
,
addr
+=
(
1
<<
d
->
bucket_shift
))
{
if
(
d
->
bucket
[
i
])
{
lua_push
number
(
L
,
addr
);
lua_push
number
(
L
,
d
->
bucket
[
i
]);
lua_push
unsigned
(
L
,
addr
);
lua_push
unsigned
(
L
,
d
->
bucket
[
i
]);
lua_settable
(
L
,
-
3
);
}
}
lua_push
number
(
L
,
1
<<
d
->
bucket_shift
);
lua_push
unsigned
(
L
,
1
<<
d
->
bucket_shift
);
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
d
->
ref
);
...
...
app/modules/rotary.c
View file @
606f9166
...
...
@@ -254,8 +254,8 @@ static int lrotary_getpos( lua_State* L )
return
0
;
}
lua_push
numb
er
(
L
,
(
pos
<<
1
)
>>
1
);
lua_push
numb
er
(
L
,
(
pos
&
0x80000000
)
?
MASK
(
PRESS
)
:
MASK
(
RELEASE
));
lua_push
integ
er
(
L
,
(
pos
<<
1
)
>>
1
);
lua_push
integ
er
(
L
,
(
pos
&
0x80000000
)
?
MASK
(
PRESS
)
:
MASK
(
RELEASE
));
return
2
;
}
...
...
app/modules/rtcfifo.c
View file @
606f9166
...
...
@@ -20,22 +20,22 @@ static int rtcfifo_prepare (lua_State *L)
#ifdef LUA_USE_MODULES_RTCTIME
lua_getfield
(
L
,
1
,
"interval_us"
);
if
(
lua_isnumber
(
L
,
-
1
))
interval_us
=
lua_to
numb
er
(
L
,
-
1
);
interval_us
=
lua_to
integ
er
(
L
,
-
1
);
lua_pop
(
L
,
1
);
#endif
lua_getfield
(
L
,
1
,
"sensor_count"
);
if
(
lua_isnumber
(
L
,
-
1
))
sensor_count
=
lua_to
numb
er
(
L
,
-
1
);
sensor_count
=
lua_to
integ
er
(
L
,
-
1
);
lua_pop
(
L
,
1
);
lua_getfield
(
L
,
1
,
"storage_begin"
);
if
(
lua_isnumber
(
L
,
-
1
))
first
=
lua_to
numb
er
(
L
,
-
1
);
first
=
lua_to
integ
er
(
L
,
-
1
);
lua_pop
(
L
,
1
);
lua_getfield
(
L
,
1
,
"storage_end"
);
if
(
lua_isnumber
(
L
,
-
1
))
last
=
lua_to
numb
er
(
L
,
-
1
);
last
=
lua_to
integ
er
(
L
,
-
1
);
lua_pop
(
L
,
1
);
}
else
if
(
!
lua_isnone
(
L
,
1
))
...
...
@@ -53,7 +53,7 @@ static int rtcfifo_prepare (lua_State *L)
// ready = rtcfifo.ready ()
static
int
rtcfifo_ready
(
lua_State
*
L
)
{
lua_push
numb
er
(
L
,
rtc_fifo_check_magic
());
lua_push
integ
er
(
L
,
rtc_fifo_check_magic
());
return
1
;
}
...
...
@@ -70,9 +70,9 @@ static int rtcfifo_put (lua_State *L)
check_fifo_magic
(
L
);
sample_t
s
;
s
.
timestamp
=
luaL_check
numb
er
(
L
,
1
);
s
.
value
=
luaL_check
numb
er
(
L
,
2
);
s
.
decimals
=
luaL_check
numb
er
(
L
,
3
);
s
.
timestamp
=
luaL_check
integ
er
(
L
,
1
);
s
.
value
=
luaL_check
integ
er
(
L
,
2
);
s
.
decimals
=
luaL_check
integ
er
(
L
,
3
);
size_t
len
;
const
char
*
str
=
luaL_checklstring
(
L
,
4
,
&
len
);
union
{
...
...
@@ -89,9 +89,9 @@ static int rtcfifo_put (lua_State *L)
static
int
extract_sample
(
lua_State
*
L
,
const
sample_t
*
s
)
{
lua_push
numb
er
(
L
,
s
->
timestamp
);
lua_push
numb
er
(
L
,
s
->
value
);
lua_push
numb
er
(
L
,
s
->
decimals
);
lua_push
integ
er
(
L
,
s
->
timestamp
);
lua_push
integ
er
(
L
,
s
->
value
);
lua_push
integ
er
(
L
,
s
->
decimals
);
union
{
uint32_t
u
;
char
s
[
4
];
...
...
@@ -125,7 +125,7 @@ static int rtcfifo_peek (lua_State *L)
sample_t
s
;
uint32_t
offs
=
0
;
if
(
lua_isnumber
(
L
,
1
))
offs
=
lua_to
numb
er
(
L
,
1
);
offs
=
lua_to
integ
er
(
L
,
1
);
if
(
!
rtc_fifo_peek_sample
(
&
s
,
offs
))
return
0
;
else
...
...
@@ -138,7 +138,7 @@ static int rtcfifo_drop (lua_State *L)
{
check_fifo_magic
(
L
);
rtc_fifo_drop_samples
(
luaL_check
numb
er
(
L
,
1
));
rtc_fifo_drop_samples
(
luaL_check
integ
er
(
L
,
1
));
return
0
;
}
...
...
@@ -148,7 +148,7 @@ static int rtcfifo_count (lua_State *L)
{
check_fifo_magic
(
L
);
lua_push
numb
er
(
L
,
rtc_fifo_get_count
());
lua_push
integ
er
(
L
,
rtc_fifo_get_count
());
return
1
;
}
...
...
@@ -159,7 +159,7 @@ static int rtcfifo_dsleep_until_sample (lua_State *L)
{
check_fifo_magic
(
L
);
uint32_t
min_us
=
luaL_check
numb
er
(
L
,
1
);
uint32_t
min_us
=
luaL_check
integ
er
(
L
,
1
);
rtc_fifo_deep_sleep_until_sample
(
min_us
);
// no return
return
0
;
}
...
...
app/modules/rtcmem.c
View file @
606f9166
...
...
@@ -6,13 +6,11 @@
static
int
rtcmem_read32
(
lua_State
*
L
)
{
int
idx
=
luaL_checknumber
(
L
,
1
);
int
n
=
1
;
if
(
lua_isnumber
(
L
,
2
))
n
=
lua_tonumber
(
L
,
2
);
if
(
!
lua_checkstack
(
L
,
n
))
return
0
;
int
idx
=
luaL_checkinteger
(
L
,
1
);
int
n
=
(
lua_gettop
(
L
)
<
2
)
?
1
:
lua_tointeger
(
L
,
2
);
if
(
n
==
0
||
!
lua_checkstack
(
L
,
n
))
{
return
0
;
}
int
ret
=
0
;
while
(
n
>
0
&&
idx
>=
0
&&
idx
<
RTC_USER_MEM_NUM_DWORDS
)
...
...
@@ -27,14 +25,14 @@ static int rtcmem_read32 (lua_State *L)
static
int
rtcmem_write32
(
lua_State
*
L
)
{
int
idx
=
luaL_check
numb
er
(
L
,
1
);
int
idx
=
luaL_check
integ
er
(
L
,
1
);
int
n
=
lua_gettop
(
L
)
-
1
;
luaL_argcheck
(
L
,
idx
+
n
<=
RTC_USER_MEM_NUM_DWORDS
,
1
,
"RTC mem would overrun"
);
int
src
=
2
;
while
(
n
--
>
0
)
{
rtc_mem_write
(
idx
++
,
lua_to
numb
er
(
L
,
src
++
));
rtc_mem_write
(
idx
++
,
(
uint32_t
)
lua_to
integ
er
(
L
,
src
++
));
}
return
0
;
}
...
...
app/modules/si7021.c
View file @
606f9166
...
...
@@ -183,13 +183,13 @@ static int si7021_lua_read(lua_State* L) {
read_reg
(
SI7021_CMD_MEASURE_RH_HOLD
,
buf_h
,
3
);
if
(
buf_h
[
2
]
!=
si7021_crc8
(
0
,
buf_h
,
2
))
//crc check
return
luaL_error
(
L
,
"crc error"
);
double
hum
=
(
uint16_t
)((
buf_h
[
0
]
<<
8
)
|
buf_h
[
1
]);
lua_Float
hum
=
(
uint16_t
)((
buf_h
[
0
]
<<
8
)
|
buf_h
[
1
]);
hum
=
((
hum
*
125
)
/
65536
-
6
);
int
humdec
=
(
int
)((
hum
-
(
int
)
hum
)
*
1000
);
uint8_t
buf_t
[
2
];
// two byte data, no crc on combined temp measurement
read_reg
(
SI7021_CMD_READ_PREV_TEMP
,
buf_t
,
2
);
double
temp
=
(
uint16_t
)((
buf_t
[
0
]
<<
8
)
|
buf_t
[
1
]);
lua_Float
temp
=
(
uint16_t
)((
buf_t
[
0
]
<<
8
)
|
buf_t
[
1
]);
temp
=
((
temp
*
175
.
72
)
/
65536
-
46
.
85
);
int
tempdec
=
(
int
)((
temp
-
(
int
)
temp
)
*
1000
);
...
...
app/modules/sjson.c
View file @
606f9166
...
...
@@ -152,21 +152,16 @@ create_new_element(jsonsl_t jsn,
}
static
void
push_number
(
JSN_DATA
*
data
,
struct
jsonsl_state_st
*
state
)
{
const
char
*
start
=
get_state_buffer
(
data
,
state
);
const
char
*
end
=
start
+
state
->
pos_cur
-
state
->
pos_begin
;
lua_pushlstring
(
data
->
L
,
start
,
end
-
start
);
#if LUA_VERSION_NUM >= 503
int
sz
=
lua_stringtonumber
(
data
->
L
,
lua_tostring
(
data
->
L
,
-
1
));
if
(
sz
)
{
lua_pop
(
data
->
L
,
1
);
}
else
{
lua_pushlstring
(
data
->
L
,
get_state_buffer
(
data
,
state
),
state
->
pos_cur
-
state
->
pos_begin
);
#if LUA_VERSION_NUM == 501
lua_pushnumber
(
data
->
L
,
lua_tonumber
(
data
->
L
,
-
1
));
#else
if
(
!
lua_stringtonumber
(
data
->
L
,
lua_tostring
(
data
->
L
,
-
1
)))
{
// In this case stringtonumber does not push a value
luaL_error
(
data
->
L
,
"Invalid number"
);
}
#else
lua_Number
result
=
lua_tonumber
(
data
->
L
,
-
1
);
lua_pop
(
data
->
L
,
1
);
lua_pushnumber
(
data
->
L
,
result
);
#endif
lua_remove
(
data
->
L
,
-
2
);
}
static
int
fromhex
(
char
c
)
{
...
...
app/modules/sntp.c
View file @
606f9166
...
...
@@ -272,16 +272,16 @@ static void sntp_handle_result(lua_State *L) {
{
lua_rawgeti
(
L
,
LUA_REGISTRYINDEX
,
state
->
sync_cb_ref
);
#ifdef LUA_USE_MODULES_RTCTIME
lua_push
numb
er
(
L
,
tv
.
tv_sec
);
lua_push
numb
er
(
L
,
tv
.
tv_usec
);
lua_push
integ
er
(
L
,
tv
.
tv_sec
);
lua_push
integ
er
(
L
,
tv
.
tv_usec
);
lua_pushstring
(
L
,
ipaddr_ntoa
(
&
state
->
best
.
server
));
lua_newtable
(
L
);
int
d40
=
state
->
best
.
delta
>>
40
;
if
(
d40
!=
0
&&
d40
!=
-
1
)
{
lua_push
numb
er
(
L
,
state
->
best
.
delta
>>
32
);
lua_push
integ
er
(
L
,
state
->
best
.
delta
>>
32
);
lua_setfield
(
L
,
-
2
,
"offset_s"
);
}
else
{
lua_push
numb
er
(
L
,
(
state
->
best
.
delta
*
MICROSECONDS
)
>>
32
);
lua_push
integ
er
(
L
,
(
state
->
best
.
delta
*
MICROSECONDS
)
>>
32
);
lua_setfield
(
L
,
-
2
,
"offset_us"
);
}
#else
...
...
@@ -292,26 +292,26 @@ static void sntp_handle_result(lua_State *L) {
tv_usec
-=
1000000
;
tv_sec
++
;
}
lua_push
numb
er
(
L
,
tv_sec
);
lua_push
numb
er
(
L
,
tv_usec
);
lua_push
integ
er
(
L
,
tv_sec
);
lua_push
integ
er
(
L
,
tv_usec
);
lua_pushstring
(
L
,
ipaddr_ntoa
(
&
state
->
best
.
server
));
lua_newtable
(
L
);
#endif
if
(
state
->
best
.
delay_frac
>
0
)
{
lua_push
numb
er
(
L
,
FRAC16_TO_US
(
state
->
best
.
delay_frac
));
lua_push
integ
er
(
L
,
FRAC16_TO_US
(
state
->
best
.
delay_frac
));
lua_setfield
(
L
,
-
2
,
"delay_us"
);
}
lua_push
numb
er
(
L
,
FRAC16_TO_US
(
state
->
best
.
root_delay
));
lua_push
integ
er
(
L
,
FRAC16_TO_US
(
state
->
best
.
root_delay
));
lua_setfield
(
L
,
-
2
,
"root_delay_us"
);
lua_push
numb
er
(
L
,
FRAC16_TO_US
(
state
->
best
.
root_dispersion
));
lua_push
integ
er
(
L
,
FRAC16_TO_US
(
state
->
best
.
root_dispersion
));
lua_setfield
(
L
,
-
2
,
"root_dispersion_us"
);
lua_push
numb
er
(
L
,
FRAC16_TO_US
(
state
->
best
.
root_maxerr
+
state
->
best
.
delay_frac
/
2
));
lua_push
integ
er
(
L
,
FRAC16_TO_US
(
state
->
best
.
root_maxerr
+
state
->
best
.
delay_frac
/
2
));
lua_setfield
(
L
,
-
2
,
"root_maxerr_us"
);
lua_push
numb
er
(
L
,
state
->
best
.
stratum
);
lua_push
integ
er
(
L
,
state
->
best
.
stratum
);
lua_setfield
(
L
,
-
2
,
"stratum"
);
lua_push
numb
er
(
L
,
state
->
best
.
LI
);
lua_push
integ
er
(
L
,
state
->
best
.
LI
);
lua_setfield
(
L
,
-
2
,
"leap"
);
lua_push
numb
er
(
L
,
pending_LI
);
lua_push
integ
er
(
L
,
pending_LI
);
lua_setfield
(
L
,
-
2
,
"pending_leap"
);
}
...
...
@@ -617,7 +617,7 @@ static int sntp_setoffset(lua_State *L)
static
int
sntp_getoffset
(
lua_State
*
L
)
{
update_offset
();
lua_push
numb
er
(
L
,
the_offset
);
lua_push
integ
er
(
L
,
the_offset
);
return
1
;
}
...
...
@@ -799,7 +799,7 @@ static int sntp_sync (lua_State *L)
/* Construct a singleton table containing the one server */
lua_newtable
(
L
);
lua_push
numb
er
(
L
,
1
);
lua_push
integ
er
(
L
,
1
);
lua_pushstring
(
L
,
hostname
);
lua_settable
(
L
,
-
3
);
}
...
...
@@ -808,14 +808,14 @@ static int sntp_sync (lua_State *L)
struct
netif
*
iface
=
(
struct
netif
*
)
eagle_lwip_getif
(
0x00
);
if
(
iface
->
dhcp
&&
iface
->
dhcp
->
offered_ntp_addr
.
addr
)
{
ip_addr_t
ntp_addr
=
iface
->
dhcp
->
offered_ntp_addr
;
lua_push
numb
er
(
L
,
1
);
lua_push
integ
er
(
L
,
1
);
lua_pushstring
(
L
,
inet_ntoa
(
ntp_addr
));
lua_settable
(
L
,
-
3
);
}
else
{
// default to ntp pool
int
i
;
for
(
i
=
0
;
i
<
4
;
i
++
)
{
lua_push
numb
er
(
L
,
i
+
1
);
lua_push
integ
er
(
L
,
i
+
1
);
char
buf
[
64
];
sprintf
(
buf
,
"%d.nodemcu.pool.ntp.org"
,
i
);
lua_pushstring
(
L
,
buf
);
...
...
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