Unverified Commit 606f9166 authored by Philip Gladstone's avatar Philip Gladstone Committed by GitHub
Browse files

First phase of number to integer conversion (#3221)

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