Unverified Commit 310faf7f authored by Terry Ellison's avatar Terry Ellison Committed by GitHub
Browse files

Merge pull request #2886 from nodemcu/dev

Next master drop
parents 68c425c0 a08e74d9
#include <stdlib.h>
void *mbedtls_calloc(size_t n, size_t sz) { return calloc(n, sz); }
void mbedtls_free(void *p) { free(p); }
#include <stdlib.h>
void *mbedtls_calloc_wrap(size_t n, size_t sz) { return calloc(n, sz); }
void mbedtls_free_wrap(void *p) { free(p); }
...@@ -25,6 +25,7 @@ STD_CFLAGS=-std=gnu11 -Wimplicit ...@@ -25,6 +25,7 @@ STD_CFLAGS=-std=gnu11 -Wimplicit
# for a subtree within the makefile rooted therein # for a subtree within the makefile rooted therein
# #
#DEFINES += #DEFINES +=
CONFIGURATION_DEFINES += -DMBEDTLS_USER_CONFIG_FILE=\"user_mbedtls.h\"
############################################################# #############################################################
# Recursion Magic - Don't touch this!! # Recursion Magic - Don't touch this!!
...@@ -38,25 +39,8 @@ STD_CFLAGS=-std=gnu11 -Wimplicit ...@@ -38,25 +39,8 @@ STD_CFLAGS=-std=gnu11 -Wimplicit
# Required for each makefile to inherit from the parent # Required for each makefile to inherit from the parent
# #
INCLUDES := $(INCLUDES) -I $(PDIR)include INCLUDES += -I ../u8g2lib/u8g2/src/clib -I ../ucglib/ucg/src/clib
INCLUDES += -I ./
INCLUDES += -I ../libc
INCLUDES += -I ../coap
INCLUDES += -I ../mqtt
INCLUDES += -I ../u8g2lib/u8g2/src/clib
INCLUDES += -I ../ucglib/ucg/src/clib
INCLUDES += -I ../lua
INCLUDES += -I ../pcm
INCLUDES += -I ../platform
INCLUDES += -I ../spiffs
INCLUDES += -I ../smart
INCLUDES += -I ../dht
INCLUDES += -I ../fatfs
INCLUDES += -I ../http
INCLUDES += -I ../sjson
INCLUDES += -I ../websocket
INCLUDES += -I ../pm
INCLUDES += -I ../sqlite3
PDIR := ../$(PDIR) PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile sinclude $(PDIR)Makefile
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "c_types.h" #include <stdint.h>
#include "user_interface.h" #include "user_interface.h"
// Lua: read(id) , return system adc // Lua: read(id) , return system adc
...@@ -61,13 +61,13 @@ static int adc_init107( lua_State *L ) ...@@ -61,13 +61,13 @@ static int adc_init107( lua_State *L )
} }
// Module function map // Module function map
static const LUA_REG_TYPE adc_map[] = { LROT_BEGIN(adc)
{ LSTRKEY( "read" ), LFUNCVAL( adc_sample ) }, LROT_FUNCENTRY( read, adc_sample )
{ LSTRKEY( "readvdd33" ), LFUNCVAL( adc_readvdd33 ) }, LROT_FUNCENTRY( readvdd33, adc_readvdd33 )
{ LSTRKEY( "force_init_mode" ), LFUNCVAL( adc_init107 ) }, LROT_FUNCENTRY( force_init_mode, adc_init107 )
{ LSTRKEY( "INIT_ADC" ), LNUMVAL( 0x00 ) }, LROT_NUMENTRY( INIT_ADC, 0x00 )
{ LSTRKEY( "INIT_VDD33" ),LNUMVAL( 0xff ) }, LROT_NUMENTRY( INIT_VDD33, 0xff )
{ LNILKEY, LNILVAL } LROT_END( adc, NULL, 0 )
};
NODEMCU_MODULE(ADC, "adc", adc_map, NULL);
NODEMCU_MODULE(ADC, "adc", adc, NULL);
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "osapi.h" #include "osapi.h"
#include "c_stdlib.h" #include <stdlib.h>
//*************************************************************************** //***************************************************************************
...@@ -531,71 +531,68 @@ static int ads1115_lua_delete(lua_State *L) { ...@@ -531,71 +531,68 @@ static int ads1115_lua_delete(lua_State *L) {
return 0; return 0;
} }
static const LUA_REG_TYPE ads1115_map[] = { LROT_BEGIN(ads1115)
LROT_FUNCENTRY( ads1115, ads1115_lua_register_1115 )
{ LSTRKEY( "ads1115" ), LFUNCVAL(ads1115_lua_register_1115) }, LROT_FUNCENTRY( ads1015, ads1115_lua_register_1015 )
{ LSTRKEY( "ads1015" ), LFUNCVAL(ads1115_lua_register_1015) }, LROT_FUNCENTRY( reset, ads1115_lua_reset )
{ LSTRKEY( "reset" ), LFUNCVAL(ads1115_lua_reset) }, LROT_NUMENTRY( ADDR_GND, ADS1115_I2C_ADDR_GND )
{ LSTRKEY( "ADDR_GND" ), LNUMVAL(ADS1115_I2C_ADDR_GND) }, LROT_NUMENTRY( ADDR_VDD, ADS1115_I2C_ADDR_VDD )
{ LSTRKEY( "ADDR_VDD" ), LNUMVAL(ADS1115_I2C_ADDR_VDD) }, LROT_NUMENTRY( ADDR_SDA, ADS1115_I2C_ADDR_SDA )
{ LSTRKEY( "ADDR_SDA" ), LNUMVAL(ADS1115_I2C_ADDR_SDA) }, LROT_NUMENTRY( ADDR_SCL, ADS1115_I2C_ADDR_SCL )
{ LSTRKEY( "ADDR_SCL" ), LNUMVAL(ADS1115_I2C_ADDR_SCL) }, LROT_NUMENTRY( SINGLE_SHOT, ADS1115_MODE_SINGLE )
{ LSTRKEY( "SINGLE_SHOT" ), LNUMVAL(ADS1115_MODE_SINGLE) }, LROT_NUMENTRY( CONTINUOUS, ADS1115_MODE_CONTIN )
{ LSTRKEY( "CONTINUOUS" ), LNUMVAL(ADS1115_MODE_CONTIN) }, LROT_NUMENTRY( DIFF_0_1, ADS1115_MUX_DIFF_0_1 )
{ LSTRKEY( "DIFF_0_1" ), LNUMVAL(ADS1115_MUX_DIFF_0_1) }, LROT_NUMENTRY( DIFF_0_3, ADS1115_MUX_DIFF_0_3 )
{ LSTRKEY( "DIFF_0_3" ), LNUMVAL(ADS1115_MUX_DIFF_0_3) }, LROT_NUMENTRY( DIFF_1_3, ADS1115_MUX_DIFF_1_3 )
{ LSTRKEY( "DIFF_1_3" ), LNUMVAL(ADS1115_MUX_DIFF_1_3) }, LROT_NUMENTRY( DIFF_2_3, ADS1115_MUX_DIFF_2_3 )
{ LSTRKEY( "DIFF_2_3" ), LNUMVAL(ADS1115_MUX_DIFF_2_3) }, LROT_NUMENTRY( SINGLE_0, ADS1115_MUX_SINGLE_0 )
{ LSTRKEY( "SINGLE_0" ), LNUMVAL(ADS1115_MUX_SINGLE_0) }, LROT_NUMENTRY( SINGLE_1, ADS1115_MUX_SINGLE_1 )
{ LSTRKEY( "SINGLE_1" ), LNUMVAL(ADS1115_MUX_SINGLE_1) }, LROT_NUMENTRY( SINGLE_2, ADS1115_MUX_SINGLE_2 )
{ LSTRKEY( "SINGLE_2" ), LNUMVAL(ADS1115_MUX_SINGLE_2) }, LROT_NUMENTRY( SINGLE_3, ADS1115_MUX_SINGLE_3 )
{ LSTRKEY( "SINGLE_3" ), LNUMVAL(ADS1115_MUX_SINGLE_3) }, LROT_NUMENTRY( GAIN_6_144V, ADS1115_PGA_6_144V )
{ LSTRKEY( "GAIN_6_144V" ), LNUMVAL(ADS1115_PGA_6_144V) }, LROT_NUMENTRY( GAIN_4_096V, ADS1115_PGA_4_096V )
{ LSTRKEY( "GAIN_4_096V" ), LNUMVAL(ADS1115_PGA_4_096V) }, LROT_NUMENTRY( GAIN_2_048V, ADS1115_PGA_2_048V )
{ LSTRKEY( "GAIN_2_048V" ), LNUMVAL(ADS1115_PGA_2_048V) }, LROT_NUMENTRY( GAIN_1_024V, ADS1115_PGA_1_024V )
{ LSTRKEY( "GAIN_1_024V" ), LNUMVAL(ADS1115_PGA_1_024V) }, LROT_NUMENTRY( GAIN_0_512V, ADS1115_PGA_0_512V )
{ LSTRKEY( "GAIN_0_512V" ), LNUMVAL(ADS1115_PGA_0_512V) }, LROT_NUMENTRY( GAIN_0_256V, ADS1115_PGA_0_256V )
{ LSTRKEY( "GAIN_0_256V" ), LNUMVAL(ADS1115_PGA_0_256V) }, LROT_NUMENTRY( DR_8SPS, ADS1115_DR_8SPS )
{ LSTRKEY( "DR_8SPS" ), LNUMVAL(ADS1115_DR_8SPS) }, LROT_NUMENTRY( DR_16SPS, ADS1115_DR_16SPS )
{ LSTRKEY( "DR_16SPS" ), LNUMVAL(ADS1115_DR_16SPS) }, LROT_NUMENTRY( DR_32SPS, ADS1115_DR_32SPS )
{ LSTRKEY( "DR_32SPS" ), LNUMVAL(ADS1115_DR_32SPS) }, LROT_NUMENTRY( DR_64SPS, ADS1115_DR_64SPS )
{ LSTRKEY( "DR_64SPS" ), LNUMVAL(ADS1115_DR_64SPS) }, LROT_NUMENTRY( DR_128SPS, ADS1115_DR_128SPS )
{ LSTRKEY( "DR_128SPS" ), LNUMVAL(ADS1115_DR_128SPS) }, LROT_NUMENTRY( DR_250SPS, ADS1115_DR_250SPS )
{ LSTRKEY( "DR_250SPS" ), LNUMVAL(ADS1115_DR_250SPS) }, LROT_NUMENTRY( DR_475SPS, ADS1115_DR_475SPS )
{ LSTRKEY( "DR_475SPS" ), LNUMVAL(ADS1115_DR_475SPS) }, LROT_NUMENTRY( DR_490SPS, ADS1115_DR_490SPS )
{ LSTRKEY( "DR_490SPS" ), LNUMVAL(ADS1115_DR_490SPS) }, LROT_NUMENTRY( DR_860SPS, ADS1115_DR_860SPS )
{ LSTRKEY( "DR_860SPS" ), LNUMVAL(ADS1115_DR_860SPS) }, LROT_NUMENTRY( DR_920SPS, ADS1115_DR_920SPS )
{ LSTRKEY( "DR_920SPS" ), LNUMVAL(ADS1115_DR_920SPS) }, LROT_NUMENTRY( DR_1600SPS, ADS1115_DR_1600SPS )
{ LSTRKEY( "DR_1600SPS" ), LNUMVAL(ADS1115_DR_1600SPS) }, LROT_NUMENTRY( DR_2400SPS, ADS1115_DR_2400SPS )
{ LSTRKEY( "DR_2400SPS" ), LNUMVAL(ADS1115_DR_2400SPS) }, LROT_NUMENTRY( DR_3300SPS, ADS1115_DR_3300SPS )
{ LSTRKEY( "DR_3300SPS" ), LNUMVAL(ADS1115_DR_3300SPS) }, LROT_NUMENTRY( CONV_RDY_1, ADS1115_CQUE_1CONV )
{ LSTRKEY( "CONV_RDY_1" ), LNUMVAL(ADS1115_CQUE_1CONV) }, LROT_NUMENTRY( CONV_RDY_2, ADS1115_CQUE_2CONV )
{ LSTRKEY( "CONV_RDY_2" ), LNUMVAL(ADS1115_CQUE_2CONV) }, LROT_NUMENTRY( CONV_RDY_4, ADS1115_CQUE_4CONV )
{ LSTRKEY( "CONV_RDY_4" ), LNUMVAL(ADS1115_CQUE_4CONV) }, LROT_NUMENTRY( COMP_1CONV, ADS1115_CQUE_1CONV )
{ LSTRKEY( "COMP_1CONV" ), LNUMVAL(ADS1115_CQUE_1CONV) }, LROT_NUMENTRY( COMP_2CONV, ADS1115_CQUE_2CONV )
{ LSTRKEY( "COMP_2CONV" ), LNUMVAL(ADS1115_CQUE_2CONV) }, LROT_NUMENTRY( COMP_4CONV, ADS1115_CQUE_4CONV )
{ LSTRKEY( "COMP_4CONV" ), LNUMVAL(ADS1115_CQUE_4CONV) }, LROT_NUMENTRY( CMODE_TRAD, ADS1115_CMODE_TRAD )
{ LSTRKEY( "CMODE_TRAD"), LNUMVAL(ADS1115_CMODE_TRAD) }, LROT_NUMENTRY( CMODE_WINDOW, ADS1115_CMODE_WINDOW )
{ LSTRKEY( "CMODE_WINDOW"), LNUMVAL(ADS1115_CMODE_WINDOW) }, LROT_END(ads1115, NULL, 0 )
{ LNILKEY, LNILVAL }
}; LROT_BEGIN(ads1115_instance)
LROT_FUNCENTRY( setting, ads1115_lua_setting )
static const LUA_REG_TYPE ads1115_instance_map[] = { LROT_FUNCENTRY( startread, ads1115_lua_startread )
{ LSTRKEY( "setting" ), LFUNCVAL(ads1115_lua_setting) }, LROT_FUNCENTRY( read, ads1115_lua_read )
{ LSTRKEY( "startread" ), LFUNCVAL(ads1115_lua_startread) },
{ LSTRKEY( "read" ), LFUNCVAL(ads1115_lua_read) },
#ifdef ADS1115_INCLUDE_TEST_FUNCTION #ifdef ADS1115_INCLUDE_TEST_FUNCTION
{ LSTRKEY( "test_volt_conversion" ), LFUNCVAL(test_volt_conversion)}, LROT_FUNCENTRY( test_volt_conversion, test_volt_conversion )
#endif #endif
{ LSTRKEY( "__index" ), LROVAL(ads1115_instance_map) }, LROT_TABENTRY( __index, ads1115_instance )
{ LSTRKEY( "__gc" ), LFUNCVAL(ads1115_lua_delete) }, LROT_FUNCENTRY( __gc, ads1115_lua_delete )
{ LNILKEY, LNILVAL } LROT_END(ads1115_instance, ads1115_instance, LROT_MASK_GC_INDEX )
};
int luaopen_ads1115(lua_State *L) { int luaopen_ads1115(lua_State *L) {
luaL_rometatable(L, metatable_name, (void *)ads1115_instance_map); luaL_rometatable(L, metatable_name, LROT_TABLEREF(ads1115_instance));
return 0; return 0;
} }
NODEMCU_MODULE(ADS1115, "ads1115", ads1115_map, luaopen_ads1115); NODEMCU_MODULE(ADS1115, "ads1115", ads1115, luaopen_ads1115);
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "c_stdlib.h" #include <stdlib.h>
#include "c_string.h" #include <string.h>
static const uint32_t adxl345_i2c_id = 0; static const uint32_t adxl345_i2c_id = 0;
static const uint8_t adxl345_i2c_addr = 0x53; static const uint8_t adxl345_i2c_addr = 0x53;
...@@ -76,10 +76,10 @@ static int adxl345_read(lua_State* L) { ...@@ -76,10 +76,10 @@ static int adxl345_read(lua_State* L) {
return 3; return 3;
} }
static const LUA_REG_TYPE adxl345_map[] = { LROT_BEGIN(adxl345)
{ LSTRKEY( "read" ), LFUNCVAL( adxl345_read )}, LROT_FUNCENTRY( read, adxl345_read )
{ LSTRKEY( "setup" ), LFUNCVAL( adxl345_setup )}, LROT_FUNCENTRY( setup, adxl345_setup )
{ LNILKEY, LNILVAL} LROT_END( adxl345, NULL, 0 )
};
NODEMCU_MODULE(ADXL345, "adxl345", adxl345_map, NULL);
NODEMCU_MODULE(ADXL345, "adxl345", adxl345, NULL);
...@@ -129,10 +129,10 @@ static int am2320_read(lua_State* L) ...@@ -129,10 +129,10 @@ static int am2320_read(lua_State* L)
return 2; return 2;
} }
static const LUA_REG_TYPE am2320_map[] = { LROT_BEGIN(am2320)
{ LSTRKEY( "read" ), LFUNCVAL( am2320_read )}, LROT_FUNCENTRY( read, am2320_read )
{ LSTRKEY( "setup" ), LFUNCVAL( am2320_setup )}, LROT_FUNCENTRY( setup, am2320_setup )
{ LNILKEY, LNILVAL} LROT_END( am2320, NULL, 0 )
};
NODEMCU_MODULE(AM2320, "am2320", am2320_map, NULL);
NODEMCU_MODULE(AM2320, "am2320", am2320, NULL);
#include "c_stdlib.h" #include <stdlib.h>
#include "c_string.h" #include <string.h>
#include "lualib.h" #include "lualib.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lrotable.h" #include "lrotable.h"
...@@ -101,15 +101,14 @@ static int apa102_write(lua_State* L) { ...@@ -101,15 +101,14 @@ static int apa102_write(lua_State* L) {
} }
const LUA_REG_TYPE apa102_map[] = LROT_PUBLIC_BEGIN(apa102)
{ LROT_FUNCENTRY( write, apa102_write )
{ LSTRKEY( "write" ), LFUNCVAL( apa102_write )}, LROT_END( apa102, NULL, 0 )
{ LNILKEY, LNILVAL}
};
LUALIB_API int luaopen_apa102(lua_State *L) { LUALIB_API int luaopen_apa102(lua_State *L) {
LREGISTER(L, "apa102", apa102_map); LREGISTER(L, "apa102", apa102_map);
return 0; return 0;
} }
NODEMCU_MODULE(APA102, "apa102", apa102_map, luaopen_apa102); NODEMCU_MODULE(APA102, "apa102", apa102, luaopen_apa102);
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "module.h" #include "module.h"
#include "c_limits.h" #include <limits.h>
#include "lauxlib.h" #include "lauxlib.h"
...@@ -119,20 +119,20 @@ static int bit_clear( lua_State* L ) ...@@ -119,20 +119,20 @@ static int bit_clear( lua_State* L )
return 1; return 1;
} }
static const LUA_REG_TYPE bit_map[] = { LROT_BEGIN(bit)
{ LSTRKEY( "bnot" ), LFUNCVAL( bit_bnot ) }, LROT_FUNCENTRY( bnot, bit_bnot )
{ LSTRKEY( "band" ), LFUNCVAL( bit_band ) }, LROT_FUNCENTRY( band, bit_band )
{ LSTRKEY( "bor" ), LFUNCVAL( bit_bor ) }, LROT_FUNCENTRY( bor, bit_bor )
{ LSTRKEY( "bxor" ), LFUNCVAL( bit_bxor ) }, LROT_FUNCENTRY( bxor, bit_bxor )
{ LSTRKEY( "lshift" ), LFUNCVAL( bit_lshift ) }, LROT_FUNCENTRY( lshift, bit_lshift )
{ LSTRKEY( "rshift" ), LFUNCVAL( bit_rshift ) }, LROT_FUNCENTRY( rshift, bit_rshift )
{ LSTRKEY( "arshift" ), LFUNCVAL( bit_arshift ) }, LROT_FUNCENTRY( arshift, bit_arshift )
{ LSTRKEY( "bit" ), LFUNCVAL( bit_bit ) }, LROT_FUNCENTRY( bit, bit_bit )
{ LSTRKEY( "set" ), LFUNCVAL( bit_set ) }, LROT_FUNCENTRY( set, bit_set )
{ LSTRKEY( "clear" ), LFUNCVAL( bit_clear ) }, LROT_FUNCENTRY( clear, bit_clear )
{ LSTRKEY( "isset" ), LFUNCVAL( bit_isset ) }, LROT_FUNCENTRY( isset, bit_isset )
{ LSTRKEY( "isclear" ), LFUNCVAL( bit_isclear ) }, LROT_FUNCENTRY( isclear, bit_isclear )
{ LNILKEY, LNILVAL} LROT_END( bit, NULL, 0 )
};
NODEMCU_MODULE(BIT, "bit", bit_map, NULL); NODEMCU_MODULE(BIT, "bit", bit, NULL);
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "c_types.h" #include <string.h>
#include <stdint.h>
#include "../crypto/sha2.h" #include "../crypto/sha2.h"
#if defined(LUA_USE_MODULES_BLOOM) && !defined(SHA2_ENABLE) #if defined(LUA_USE_MODULES_BLOOM) && !defined(SHA2_ENABLE)
...@@ -169,24 +170,24 @@ static int bloom_create(lua_State *L) { ...@@ -169,24 +170,24 @@ static int bloom_create(lua_State *L) {
return 1; return 1;
} }
static const LUA_REG_TYPE bloom_filter_map[] = { LROT_BEGIN(bloom_filter)
{ LSTRKEY( "add" ), LFUNCVAL( bloom_filter_add ) }, LROT_FUNCENTRY( add, bloom_filter_add )
{ LSTRKEY( "check" ), LFUNCVAL( bloom_filter_check ) }, LROT_FUNCENTRY( check, bloom_filter_check )
{ LSTRKEY( "reset" ), LFUNCVAL( bloom_filter_reset ) }, LROT_FUNCENTRY( reset, bloom_filter_reset )
{ LSTRKEY( "info" ), LFUNCVAL( bloom_filter_info ) }, LROT_FUNCENTRY( info, bloom_filter_info )
{ LSTRKEY( "__index" ), LROVAL( bloom_filter_map ) }, LROT_TABENTRY( __index, bloom_filter )
{ LNILKEY, LNILVAL } LROT_END( bloom_filter, bloom_filter, LROT_MASK_INDEX )
};
// Module function map // Module function map
static const LUA_REG_TYPE bloom_map[] = { LROT_BEGIN(bloom)
{ LSTRKEY( "create" ), LFUNCVAL( bloom_create ) }, LROT_FUNCENTRY( create, bloom_create )
{ LNILKEY, LNILVAL } LROT_END( bloom, NULL, 0 )
};
LUALIB_API int bloom_open(lua_State *L) { LUALIB_API int bloom_open(lua_State *L) {
luaL_rometatable(L, "bloom.filter", (void *)bloom_filter_map); luaL_rometatable(L, "bloom.filter", LROT_TABLEREF(bloom_filter));
return 1; return 1;
} }
NODEMCU_MODULE(BLOOM, "bloom", bloom_map, bloom_open); NODEMCU_MODULE(BLOOM, "bloom", bloom, bloom_open);
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "c_math.h" #include <math.h>
/****************************************************/ /****************************************************/
/**\name registers definition */ /**\name registers definition */
...@@ -470,17 +470,17 @@ static int bme280_lua_dewpoint(lua_State* L) { ...@@ -470,17 +470,17 @@ static int bme280_lua_dewpoint(lua_State* L) {
return 1; return 1;
} }
static const LUA_REG_TYPE bme280_map[] = { LROT_BEGIN(bme280)
{ LSTRKEY( "setup" ), LFUNCVAL(bme280_lua_setup)}, LROT_FUNCENTRY( setup, bme280_lua_setup )
{ LSTRKEY( "temp" ), LFUNCVAL(bme280_lua_temp)}, LROT_FUNCENTRY( temp, bme280_lua_temp )
{ LSTRKEY( "baro" ), LFUNCVAL(bme280_lua_baro)}, LROT_FUNCENTRY( baro, bme280_lua_baro )
{ LSTRKEY( "humi" ), LFUNCVAL(bme280_lua_humi)}, LROT_FUNCENTRY( humi, bme280_lua_humi )
{ LSTRKEY( "startreadout" ), LFUNCVAL(bme280_lua_startreadout)}, LROT_FUNCENTRY( startreadout, bme280_lua_startreadout )
{ LSTRKEY( "qfe2qnh" ), LFUNCVAL(bme280_lua_qfe2qnh)}, LROT_FUNCENTRY( qfe2qnh, bme280_lua_qfe2qnh )
{ LSTRKEY( "altitude" ), LFUNCVAL(bme280_lua_altitude)}, LROT_FUNCENTRY( altitude, bme280_lua_altitude )
{ LSTRKEY( "dewpoint" ), LFUNCVAL(bme280_lua_dewpoint)}, LROT_FUNCENTRY( dewpoint, bme280_lua_dewpoint )
{ LSTRKEY( "read" ), LFUNCVAL(bme280_lua_read)}, LROT_FUNCENTRY( read, bme280_lua_read )
{ LNILKEY, LNILVAL} LROT_END( bme280, NULL, 0 )
};
NODEMCU_MODULE(BME280, "bme280", bme280_map, NULL); NODEMCU_MODULE(BME280, "bme280", bme280, NULL);
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "c_math.h" #include <math.h>
#include "bme680_defs.h" #include "bme680_defs.h"
...@@ -534,14 +534,14 @@ static int bme680_lua_dewpoint(lua_State* L) { ...@@ -534,14 +534,14 @@ static int bme680_lua_dewpoint(lua_State* L) {
return 1; return 1;
} }
static const LUA_REG_TYPE bme680_map[] = { LROT_BEGIN(bme680)
{ LSTRKEY( "setup" ), LFUNCVAL(bme680_lua_setup)}, LROT_FUNCENTRY( setup, bme680_lua_setup )
{ LSTRKEY( "startreadout" ), LFUNCVAL(bme680_lua_startreadout)}, LROT_FUNCENTRY( startreadout, bme680_lua_startreadout )
{ LSTRKEY( "qfe2qnh" ), LFUNCVAL(bme680_lua_qfe2qnh)}, LROT_FUNCENTRY( qfe2qnh, bme680_lua_qfe2qnh )
{ LSTRKEY( "altitude" ), LFUNCVAL(bme680_lua_altitude)}, LROT_FUNCENTRY( altitude, bme680_lua_altitude )
{ LSTRKEY( "dewpoint" ), LFUNCVAL(bme680_lua_dewpoint)}, LROT_FUNCENTRY( dewpoint, bme680_lua_dewpoint )
{ LSTRKEY( "read" ), LFUNCVAL(bme680_lua_read)}, LROT_FUNCENTRY( read, bme680_lua_read )
{ LNILKEY, LNILVAL} LROT_END( bme680, NULL, 0 )
};
NODEMCU_MODULE(BME680, "bme680", bme680_map, NULL); NODEMCU_MODULE(BME680, "bme680", bme680, NULL);
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "c_stdlib.h" #include <stdlib.h>
#include "c_string.h" #include <string.h>
static const uint32_t bmp085_i2c_id = 0; static const uint32_t bmp085_i2c_id = 0;
static const uint8_t bmp085_i2c_addr = 0x77; static const uint8_t bmp085_i2c_addr = 0x77;
...@@ -169,12 +169,12 @@ static int bmp085_lua_pressure(lua_State* L) { ...@@ -169,12 +169,12 @@ static int bmp085_lua_pressure(lua_State* L) {
return 1; return 1;
} }
static const LUA_REG_TYPE bmp085_map[] = { LROT_BEGIN(bmp085)
{ LSTRKEY( "temperature" ), LFUNCVAL( bmp085_lua_temperature )}, LROT_FUNCENTRY( temperature, bmp085_lua_temperature )
{ LSTRKEY( "pressure" ), LFUNCVAL( bmp085_lua_pressure )}, LROT_FUNCENTRY( pressure, bmp085_lua_pressure )
{ LSTRKEY( "pressure_raw" ), LFUNCVAL( bmp085_lua_pressure_raw )}, LROT_FUNCENTRY( pressure_raw, bmp085_lua_pressure_raw )
{ LSTRKEY( "setup" ), LFUNCVAL( bmp085_setup )}, LROT_FUNCENTRY( setup, bmp085_setup )
{ LNILKEY, LNILVAL} LROT_END( bmp085, NULL, 0 )
};
NODEMCU_MODULE(BMP085, "bmp085", bmp085_map, NULL);
NODEMCU_MODULE(BMP085, "bmp085", bmp085, NULL);
...@@ -4,21 +4,21 @@ ...@@ -4,21 +4,21 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "c_string.h" #include <string.h>
#include "c_stdlib.h" #include <stddef.h>
#include "c_types.h" #include <stdint.h>
#include "mem.h" #include "mem.h"
#include "lwip/ip_addr.h" #include "lwip/ip_addr.h"
#include "espconn.h" #include "espconn.h"
#include "driver/uart.h" #include "driver/uart.h"
#include "coap.h" #include "coap/coap.h"
#include "uri.h" #include "coap/uri.h"
#include "node.h" #include "coap/node.h"
#include "coap_timer.h" #include "coap/coap_timer.h"
#include "coap_io.h" #include "coap/coap_io.h"
#include "coap_server.h" #include "coap/coap_server.h"
coap_queue_t *gQueue = NULL; coap_queue_t *gQueue = NULL;
...@@ -36,13 +36,13 @@ static void coap_received(void *arg, char *pdata, unsigned short len) ...@@ -36,13 +36,13 @@ static void coap_received(void *arg, char *pdata, unsigned short len)
// static uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0' // static uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0'
uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0' uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0'
c_memset(buf, 0, sizeof(buf)); // wipe prev data memset(buf, 0, sizeof(buf)); // wipe prev data
if (len > MAX_MESSAGE_SIZE) { if (len > MAX_MESSAGE_SIZE) {
NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client... NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client...
return; return;
} }
// c_memcpy(buf, pdata, len); // memcpy(buf, pdata, len);
size_t rsplen = coap_server_respond(pdata, len, buf, MAX_MESSAGE_SIZE+1); size_t rsplen = coap_server_respond(pdata, len, buf, MAX_MESSAGE_SIZE+1);
...@@ -52,11 +52,11 @@ static void coap_received(void *arg, char *pdata, unsigned short len) ...@@ -52,11 +52,11 @@ static void coap_received(void *arg, char *pdata, unsigned short len)
return; return;
pesp_conn->proto.udp->remote_port = pr->remote_port; pesp_conn->proto.udp->remote_port = pr->remote_port;
os_memmove (pesp_conn->proto.udp->remote_ip, pr->remote_ip, 4); os_memmove (pesp_conn->proto.udp->remote_ip, pr->remote_ip, 4);
// The remot_info apparently should *not* be os_free()d, fyi // The remot_info apparently should *not* be free()d, fyi
espconn_sent(pesp_conn, (unsigned char *)buf, rsplen); espconn_sent(pesp_conn, (unsigned char *)buf, rsplen);
// c_memset(buf, 0, sizeof(buf)); // memset(buf, 0, sizeof(buf));
} }
static void coap_sent(void *arg) static void coap_sent(void *arg)
...@@ -83,7 +83,7 @@ static int coap_create( lua_State* L, const char* mt ) ...@@ -83,7 +83,7 @@ static int coap_create( lua_State* L, const char* mt )
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
// create the espconn struct // create the espconn struct
pesp_conn = (struct espconn *)c_zalloc(sizeof(struct espconn)); pesp_conn = (struct espconn *)calloc(1,sizeof(struct espconn));
if(!pesp_conn) if(!pesp_conn)
return luaL_error(L, "not enough memory"); return luaL_error(L, "not enough memory");
...@@ -93,9 +93,9 @@ static int coap_create( lua_State* L, const char* mt ) ...@@ -93,9 +93,9 @@ static int coap_create( lua_State* L, const char* mt )
pesp_conn->proto.tcp = NULL; pesp_conn->proto.tcp = NULL;
pesp_conn->proto.udp = NULL; pesp_conn->proto.udp = NULL;
pesp_conn->proto.udp = (esp_udp *)c_zalloc(sizeof(esp_udp)); pesp_conn->proto.udp = (esp_udp *)calloc(1,sizeof(esp_udp));
if(!pesp_conn->proto.udp){ if(!pesp_conn->proto.udp){
c_free(pesp_conn); free(pesp_conn);
cud->pesp_conn = pesp_conn = NULL; cud->pesp_conn = pesp_conn = NULL;
return luaL_error(L, "not enough memory"); return luaL_error(L, "not enough memory");
} }
...@@ -131,9 +131,9 @@ static int coap_delete( lua_State* L, const char* mt ) ...@@ -131,9 +131,9 @@ static int coap_delete( lua_State* L, const char* mt )
{ {
if(cud->pesp_conn->proto.udp->remote_port || cud->pesp_conn->proto.udp->local_port) if(cud->pesp_conn->proto.udp->remote_port || cud->pesp_conn->proto.udp->local_port)
espconn_delete(cud->pesp_conn); espconn_delete(cud->pesp_conn);
c_free(cud->pesp_conn->proto.udp); free(cud->pesp_conn->proto.udp);
cud->pesp_conn->proto.udp = NULL; cud->pesp_conn->proto.udp = NULL;
c_free(cud->pesp_conn); free(cud->pesp_conn);
cud->pesp_conn = NULL; cud->pesp_conn = NULL;
} }
...@@ -170,7 +170,7 @@ static int coap_start( lua_State* L, const char* mt ) ...@@ -170,7 +170,7 @@ static int coap_start( lua_State* L, const char* mt )
ip = "0.0.0.0"; ip = "0.0.0.0";
} }
ipaddr.addr = ipaddr_addr(ip); ipaddr.addr = ipaddr_addr(ip);
c_memcpy(pesp_conn->proto.udp->local_ip, &ipaddr.addr, 4); memcpy(pesp_conn->proto.udp->local_ip, &ipaddr.addr, 4);
NODE_DBG("UDP ip is set: "); NODE_DBG("UDP ip is set: ");
NODE_DBG(IPSTR, IP2STR(&ipaddr.addr)); NODE_DBG(IPSTR, IP2STR(&ipaddr.addr));
NODE_DBG("\n"); NODE_DBG("\n");
...@@ -235,7 +235,7 @@ static void coap_response_handler(void *arg, char *pdata, unsigned short len) ...@@ -235,7 +235,7 @@ static void coap_response_handler(void *arg, char *pdata, unsigned short len)
pkt.content.len = 0; pkt.content.len = 0;
// static uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0' // static uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0'
uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0' uint8_t buf[MAX_MESSAGE_SIZE+1] = {0}; // +1 for string '\0'
c_memset(buf, 0, sizeof(buf)); // wipe prev data memset(buf, 0, sizeof(buf)); // wipe prev data
int rc; int rc;
if( len > MAX_MESSAGE_SIZE ) if( len > MAX_MESSAGE_SIZE )
...@@ -243,7 +243,7 @@ static void coap_response_handler(void *arg, char *pdata, unsigned short len) ...@@ -243,7 +243,7 @@ static void coap_response_handler(void *arg, char *pdata, unsigned short len)
NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client... NODE_DBG("Request Entity Too Large.\n"); // NOTE: should response 4.13 to client...
return; return;
} }
c_memcpy(buf, pdata, len); memcpy(buf, pdata, len);
if (0 != (rc = coap_parse(&pkt, buf, len))){ if (0 != (rc = coap_parse(&pkt, buf, len))){
NODE_DBG("Bad packet rc=%d\n", rc); NODE_DBG("Bad packet rc=%d\n", rc);
...@@ -271,7 +271,7 @@ static void coap_response_handler(void *arg, char *pdata, unsigned short len) ...@@ -271,7 +271,7 @@ static void coap_response_handler(void *arg, char *pdata, unsigned short len)
uint32_t ip = 0, port = 0; uint32_t ip = 0, port = 0;
coap_tid_t id = COAP_INVALID_TID; coap_tid_t id = COAP_INVALID_TID;
c_memcpy(&ip, pesp_conn->proto.udp->remote_ip, sizeof(ip)); memcpy(&ip, pesp_conn->proto.udp->remote_ip, sizeof(ip));
port = pesp_conn->proto.udp->remote_port; port = pesp_conn->proto.udp->remote_port;
coap_transaction_id(ip, port, &pkt, &id); coap_transaction_id(ip, port, &pkt, &id);
...@@ -303,7 +303,7 @@ end: ...@@ -303,7 +303,7 @@ end:
if(pesp_conn->proto.udp->remote_port || pesp_conn->proto.udp->local_port) if(pesp_conn->proto.udp->remote_port || pesp_conn->proto.udp->local_port)
espconn_delete(pesp_conn); espconn_delete(pesp_conn);
} }
// c_memset(buf, 0, sizeof(buf)); // memset(buf, 0, sizeof(buf));
} }
// Lua: client:request( [CON], uri, [payload] ) // Lua: client:request( [CON], uri, [payload] )
...@@ -354,7 +354,7 @@ static int coap_request( lua_State* L, coap_method_t m ) ...@@ -354,7 +354,7 @@ static int coap_request( lua_State* L, coap_method_t m )
pesp_conn->proto.udp->local_port = espconn_port(); pesp_conn->proto.udp->local_port = espconn_port();
if(uri->host.length){ if(uri->host.length){
c_memcpy(host, uri->host.s, uri->host.length); memcpy(host, uri->host.s, uri->host.length);
host[uri->host.length] = '\0'; host[uri->host.length] = '\0';
ipaddr.addr = ipaddr_addr(host); ipaddr.addr = ipaddr_addr(host);
...@@ -362,7 +362,7 @@ static int coap_request( lua_State* L, coap_method_t m ) ...@@ -362,7 +362,7 @@ static int coap_request( lua_State* L, coap_method_t m )
NODE_DBG(host); NODE_DBG(host);
NODE_DBG("\n"); NODE_DBG("\n");
c_memcpy(pesp_conn->proto.udp->remote_ip, &ipaddr.addr, 4); memcpy(pesp_conn->proto.udp->remote_ip, &ipaddr.addr, 4);
NODE_DBG("UDP ip is set: "); NODE_DBG("UDP ip is set: ");
NODE_DBG(IPSTR, IP2STR(&ipaddr.addr)); NODE_DBG(IPSTR, IP2STR(&ipaddr.addr));
NODE_DBG("\n"); NODE_DBG("\n");
...@@ -371,7 +371,7 @@ static int coap_request( lua_State* L, coap_method_t m ) ...@@ -371,7 +371,7 @@ static int coap_request( lua_State* L, coap_method_t m )
coap_pdu_t *pdu = coap_new_pdu(); // should call coap_delete_pdu() somewhere coap_pdu_t *pdu = coap_new_pdu(); // should call coap_delete_pdu() somewhere
if(!pdu){ if(!pdu){
if(uri) if(uri)
c_free(uri); free(uri);
return luaL_error (L, "alloc fail"); return luaL_error (L, "alloc fail");
} }
...@@ -422,7 +422,7 @@ static int coap_request( lua_State* L, coap_method_t m ) ...@@ -422,7 +422,7 @@ static int coap_request( lua_State* L, coap_method_t m )
} }
if(uri) if(uri)
c_free((void *)uri); free((void *)uri);
NODE_DBG("coap_request is called.\n"); NODE_DBG("coap_request is called.\n");
return 0; return 0;
...@@ -447,13 +447,13 @@ static int coap_regist( lua_State* L, const char* mt, int isvar ) ...@@ -447,13 +447,13 @@ static int coap_regist( lua_State* L, const char* mt, int isvar )
h = function_entry; h = function_entry;
while(NULL!=h->next){ // goto the end of the list while(NULL!=h->next){ // goto the end of the list
if(h->name!= NULL && c_strcmp(h->name, name)==0) // key exist, override it if(h->name!= NULL && strcmp(h->name, name)==0) // key exist, override it
break; break;
h = h->next; h = h->next;
} }
if(h->name==NULL || c_strcmp(h->name, name)!=0){ // not exists. make a new one. if(h->name==NULL || strcmp(h->name, name)!=0){ // not exists. make a new one.
h->next = (coap_luser_entry *)c_zalloc(sizeof(coap_luser_entry)); h->next = (coap_luser_entry *)calloc(1,sizeof(coap_luser_entry));
h = h->next; h = h->next;
if(h == NULL) if(h == NULL)
return luaL_error(L, "not enough memory"); return luaL_error(L, "not enough memory");
...@@ -556,48 +556,47 @@ static int coap_client_delete( lua_State* L ) ...@@ -556,48 +556,47 @@ static int coap_client_delete( lua_State* L )
} }
// Module function map // Module function map
static const LUA_REG_TYPE coap_server_map[] = { LROT_BEGIN(coap_server)
{ LSTRKEY( "listen" ), LFUNCVAL( coap_server_listen ) }, LROT_FUNCENTRY( listen, coap_server_listen )
{ LSTRKEY( "close" ), LFUNCVAL( coap_server_close ) }, LROT_FUNCENTRY( close, coap_server_close )
{ LSTRKEY( "var" ), LFUNCVAL( coap_server_var ) }, LROT_FUNCENTRY( var, coap_server_var )
{ LSTRKEY( "func" ), LFUNCVAL( coap_server_func ) }, LROT_FUNCENTRY( func, coap_server_func )
{ LSTRKEY( "__gc" ), LFUNCVAL( coap_server_delete ) }, LROT_FUNCENTRY( __gc, coap_server_delete )
{ LSTRKEY( "__index" ), LROVAL( coap_server_map ) }, LROT_TABENTRY( __index, coap_server )
{ LNILKEY, LNILVAL } LROT_END( coap_server, coap_server, 0 )
};
static const LUA_REG_TYPE coap_client_map[] = { LROT_BEGIN(coap_client)
{ LSTRKEY( "get" ), LFUNCVAL( coap_client_get ) }, LROT_FUNCENTRY( get, coap_client_get )
{ LSTRKEY( "post" ), LFUNCVAL( coap_client_post ) }, LROT_FUNCENTRY( post, coap_client_post )
{ LSTRKEY( "put" ), LFUNCVAL( coap_client_put ) }, LROT_FUNCENTRY( put, coap_client_put )
{ LSTRKEY( "delete" ), LFUNCVAL( coap_client_delete ) }, LROT_FUNCENTRY( delete, coap_client_delete )
{ LSTRKEY( "__gc" ), LFUNCVAL( coap_client_gcdelete ) }, LROT_FUNCENTRY( __gc, coap_client_gcdelete )
{ LSTRKEY( "__index" ), LROVAL( coap_client_map ) }, LROT_TABENTRY( __index, coap_client )
{ LNILKEY, LNILVAL } LROT_END( coap_client, coap_client, 0 )
};
static const LUA_REG_TYPE coap_map[] = LROT_BEGIN(coap)
{ LROT_FUNCENTRY( Server, coap_createServer )
{ LSTRKEY( "Server" ), LFUNCVAL( coap_createServer ) }, LROT_FUNCENTRY( Client, coap_createClient )
{ LSTRKEY( "Client" ), LFUNCVAL( coap_createClient ) }, LROT_NUMENTRY( CON, COAP_TYPE_CON )
{ LSTRKEY( "CON" ), LNUMVAL( COAP_TYPE_CON ) }, LROT_NUMENTRY( NON, COAP_TYPE_NONCON )
{ LSTRKEY( "NON" ), LNUMVAL( COAP_TYPE_NONCON ) }, LROT_NUMENTRY( TEXT_PLAIN, COAP_CONTENTTYPE_TEXT_PLAIN )
{ LSTRKEY( "TEXT_PLAIN"), LNUMVAL( COAP_CONTENTTYPE_TEXT_PLAIN ) }, LROT_NUMENTRY( LINKFORMAT, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT )
{ LSTRKEY( "LINKFORMAT"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_LINKFORMAT ) }, LROT_NUMENTRY( XML, COAP_CONTENTTYPE_APPLICATION_XML )
{ LSTRKEY( "XML"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_XML ) }, LROT_NUMENTRY( OCTET_STREAM, COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM )
{ LSTRKEY( "OCTET_STREAM"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM ) }, LROT_NUMENTRY( EXI, COAP_CONTENTTYPE_APPLICATION_EXI )
{ LSTRKEY( "EXI"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_EXI ) }, LROT_NUMENTRY( JSON, COAP_CONTENTTYPE_APPLICATION_JSON )
{ LSTRKEY( "JSON"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_JSON) }, LROT_TABENTRY( __metatable, coap )
{ LSTRKEY( "__metatable" ), LROVAL( coap_map ) }, LROT_END( coap, coap, 0 )
{ LNILKEY, LNILVAL }
};
int luaopen_coap( lua_State *L ) int luaopen_coap( lua_State *L )
{ {
endpoint_setup(); endpoint_setup();
luaL_rometatable(L, "coap_server", (void *)coap_server_map); // create metatable for coap_server luaL_rometatable(L, "coap_server", LROT_TABLEREF(coap_server));
luaL_rometatable(L, "coap_client", (void *)coap_client_map); // create metatable for coap_client luaL_rometatable(L, "coap_client", LROT_TABLEREF(coap_client));
return 0; return 0;
} }
NODEMCU_MODULE(COAP, "coap", coap_map, luaopen_coap); NODEMCU_MODULE(COAP, "coap", coap, luaopen_coap);
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "lmem.h" #include "lmem.h"
#include "platform.h" #include "platform.h"
#include "c_stdlib.h" #include <stdlib.h>
#include "c_math.h" #include <math.h>
#include "c_string.h" #include <string.h>
#include "user_interface.h" #include "user_interface.h"
#include "osapi.h" #include "osapi.h"
...@@ -234,13 +234,12 @@ static int cu_grb2hsv(lua_State *L) { ...@@ -234,13 +234,12 @@ static int cu_grb2hsv(lua_State *L) {
} }
static const LUA_REG_TYPE color_utils_map[] = LROT_BEGIN(color_utils)
{ LROT_FUNCENTRY( hsv2grb, cu_hsv2grb )
{ LSTRKEY( "hsv2grb" ), LFUNCVAL( cu_hsv2grb )}, LROT_FUNCENTRY( hsv2grbw, cu_hsv2grbw )
{ LSTRKEY( "hsv2grbw" ), LFUNCVAL( cu_hsv2grbw )}, LROT_FUNCENTRY( colorWheel, cu_color_wheel )
{ LSTRKEY( "colorWheel" ), LFUNCVAL( cu_color_wheel )}, LROT_FUNCENTRY( grb2hsv, cu_grb2hsv )
{ LSTRKEY( "grb2hsv" ), LFUNCVAL( cu_grb2hsv )}, LROT_END( color_utils, NULL, 0 )
{ LNILKEY, LNILVAL}
};
NODEMCU_MODULE(COLOR_UTILS, "color_utils", color_utils, NULL);
NODEMCU_MODULE(COLOR_UTILS, "color_utils", color_utils_map, NULL);
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "lmem.h" #include "lmem.h"
#include "platform.h" #include "platform.h"
#include "c_stdlib.h" #include <stdlib.h>
#include "c_math.h" #include <math.h>
#include "c_string.h" #include <string.h>
#include "user_interface.h" #include "user_interface.h"
#include "osapi.h" #include "osapi.h"
......
...@@ -4,12 +4,12 @@ ...@@ -4,12 +4,12 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "lmem.h" #include "lmem.h"
#include "user_interface.h" #include "user_interface.h"
#include "c_types.h" #include <stdint.h>
#include "c_string.h" #include <stddef.h>
#include <string.h>
#include "ets_sys.h" #include "ets_sys.h"
#include "time.h" #include "time.h"
#include "rtc/rtctime.h" #include "rtc/rtctime.h"
#include "stdlib.h"
#include "mem.h" #include "mem.h"
struct cronent_desc { struct cronent_desc {
...@@ -176,7 +176,7 @@ static int lcron_reset(lua_State *L) { ...@@ -176,7 +176,7 @@ static int lcron_reset(lua_State *L) {
luaL_unref(L, LUA_REGISTRYINDEX, cronent_list[i]); luaL_unref(L, LUA_REGISTRYINDEX, cronent_list[i]);
} }
cronent_count = 0; cronent_count = 0;
os_free(cronent_list); free(cronent_list);
cronent_list = 0; cronent_list = 0;
return 0; return 0;
} }
...@@ -224,20 +224,20 @@ static void cron_handle_tmr() { ...@@ -224,20 +224,20 @@ static void cron_handle_tmr() {
cron_handle_time(tm.tm_mon + 1, tm.tm_mday, tm.tm_wday, tm.tm_hour, tm.tm_min); cron_handle_time(tm.tm_mon + 1, tm.tm_mday, tm.tm_wday, tm.tm_hour, tm.tm_min);
} }
static const LUA_REG_TYPE cronent_map[] = { LROT_BEGIN(cronent)
{ LSTRKEY( "schedule" ), LFUNCVAL( lcron_schedule ) }, LROT_FUNCENTRY( schedule, lcron_schedule )
{ LSTRKEY( "handler" ), LFUNCVAL( lcron_handler ) }, LROT_FUNCENTRY( handler, lcron_handler )
{ LSTRKEY( "unschedule" ), LFUNCVAL( lcron_unschedule ) }, LROT_FUNCENTRY( unschedule, lcron_unschedule )
{ LSTRKEY( "__gc" ), LFUNCVAL( lcron_delete ) }, LROT_FUNCENTRY( __gc, lcron_delete )
{ LSTRKEY( "__index" ), LROVAL( cronent_map ) }, LROT_TABENTRY( __index, cronent )
{ LNILKEY, LNILVAL } LROT_END( cronent, cronent, LROT_MASK_GC_INDEX )
};
LROT_BEGIN(cron)
LROT_FUNCENTRY( schedule, lcron_create )
LROT_FUNCENTRY( reset, lcron_reset )
LROT_END( cron, NULL, 0 )
static const LUA_REG_TYPE cron_map[] = {
{ LSTRKEY( "schedule" ), LFUNCVAL( lcron_create ) },
{ LSTRKEY( "reset" ), LFUNCVAL( lcron_reset ) },
{ LNILKEY, LNILVAL }
};
#include "pm/swtimer.h" #include "pm/swtimer.h"
int luaopen_cron( lua_State *L ) { int luaopen_cron( lua_State *L ) {
...@@ -247,8 +247,8 @@ int luaopen_cron( lua_State *L ) { ...@@ -247,8 +247,8 @@ int luaopen_cron( lua_State *L ) {
//cron_handle_tmr determines when to execute a scheduled cron job //cron_handle_tmr determines when to execute a scheduled cron job
//My guess: To be sure to give the other modules required by cron enough time to get to a ready state, restart cron_timer. //My guess: To be sure to give the other modules required by cron enough time to get to a ready state, restart cron_timer.
os_timer_arm(&cron_timer, 1000, 0); os_timer_arm(&cron_timer, 1000, 0);
luaL_rometatable(L, "cron.entry", (void *)cronent_map); luaL_rometatable(L, "cron.entry", LROT_TABLEREF(cronent));
return 0; return 0;
} }
NODEMCU_MODULE(CRON, "cron", cron_map, luaopen_cron); NODEMCU_MODULE(CRON, "cron", cron, luaopen_cron);
// Module for cryptography // Module for cryptography
#include <c_errno.h> #include <errno.h>
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "c_types.h" #include <stdint.h>
#include "c_stdlib.h" #include <stddef.h>
#include "vfs.h" #include "vfs.h"
#include "../crypto/digests.h" #include "../crypto/digests.h"
#include "../crypto/mech.h" #include "../crypto/mech.h"
...@@ -75,22 +75,21 @@ static const char* bytes64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx ...@@ -75,22 +75,21 @@ static const char* bytes64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx
*/ */
static int crypto_base64_encode( lua_State* L ) static int crypto_base64_encode( lua_State* L )
{ {
int len; int len, i;
const char* msg = luaL_checklstring(L, 1, &len); const char* msg = luaL_checklstring(L, 1, &len);
int blen = (len + 2) / 3 * 4; luaL_Buffer out;
char* out = (char*)c_malloc(blen);
int j = 0, i; luaL_buffinit(L, &out);
for (i = 0; i < len; i += 3) { for (i = 0; i < len; i += 3) {
int a = msg[i]; int a = msg[i];
int b = (i + 1 < len) ? msg[i + 1] : 0; int b = (i + 1 < len) ? msg[i + 1] : 0;
int c = (i + 2 < len) ? msg[i + 2] : 0; int c = (i + 2 < len) ? msg[i + 2] : 0;
out[j++] = bytes64[a >> 2]; luaL_addchar(&out, bytes64[a >> 2]);
out[j++] = bytes64[((a & 3) << 4) | (b >> 4)]; luaL_addchar(&out, bytes64[((a & 3) << 4) | (b >> 4)]);
out[j++] = (i + 1 < len) ? bytes64[((b & 15) << 2) | (c >> 6)] : 61; luaL_addchar(&out, (i + 1 < len) ? bytes64[((b & 15) << 2) | (c >> 6)] : 61);
out[j++] = (i + 2 < len) ? bytes64[(c & 63)] : 61; luaL_addchar(&out, (i + 2 < len) ? bytes64[(c & 63)] : 61);
} }
lua_pushlstring(L, out, j); luaL_pushresult(&out);
c_free(out);
return 1; return 1;
} }
...@@ -101,16 +100,16 @@ static int crypto_base64_encode( lua_State* L ) ...@@ -101,16 +100,16 @@ static int crypto_base64_encode( lua_State* L )
*/ */
static int crypto_hex_encode( lua_State* L) static int crypto_hex_encode( lua_State* L)
{ {
int len; int len, i;
const char* msg = luaL_checklstring(L, 1, &len); const char* msg = luaL_checklstring(L, 1, &len);
char* out = (char*)c_malloc(len * 2); luaL_Buffer out;
int i, j = 0;
luaL_buffinit(L, &out);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
out[j++] = crypto_hexbytes[msg[i] >> 4]; luaL_addchar(&out, crypto_hexbytes[msg[i] >> 4]);
out[j++] = crypto_hexbytes[msg[i] & 0xf]; luaL_addchar(&out, crypto_hexbytes[msg[i] & 0xf]);
} }
lua_pushlstring(L, out, len*2); luaL_pushresult(&out);
c_free(out);
return 1; return 1;
} }
#endif #endif
...@@ -121,22 +120,20 @@ static int crypto_hex_encode( lua_State* L) ...@@ -121,22 +120,20 @@ static int crypto_hex_encode( lua_State* L)
*/ */
static int crypto_mask( lua_State* L ) static int crypto_mask( lua_State* L )
{ {
int len, mask_len; int len, mask_len, i;
const char* msg = luaL_checklstring(L, 1, &len); const char* msg = luaL_checklstring(L, 1, &len);
const char* mask = luaL_checklstring(L, 2, &mask_len); const char* mask = luaL_checklstring(L, 2, &mask_len);
luaL_Buffer b;
if(mask_len <= 0) if(mask_len <= 0)
return luaL_error(L, "invalid argument: mask"); return luaL_error(L, "invalid argument: mask");
int i; luaL_buffinit(L, &b);
char* copy = (char*)c_malloc(len);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
copy[i] = msg[i] ^ mask[i % mask_len]; luaL_addchar(&b, msg[i] ^ mask[i % mask_len]);
} }
lua_pushlstring(L, copy, len);
c_free(copy);
luaL_pushresult(&b);
return 1; return 1;
} }
...@@ -175,29 +172,38 @@ static int crypto_lhash (lua_State *L) ...@@ -175,29 +172,38 @@ static int crypto_lhash (lua_State *L)
static int crypto_new_hash_hmac (lua_State *L, int what) static int crypto_new_hash_hmac (lua_State *L, int what)
{ {
// get pointer to relevant hash_mechs table entry in app/crypto/digest.c. Note that
// the size of the table needed is dependent on the the digest type
const digest_mech_info_t *mi = crypto_digest_mech (luaL_checkstring (L, 1)); const digest_mech_info_t *mi = crypto_digest_mech (luaL_checkstring (L, 1));
if (!mi) if (!mi)
return bad_mech (L); return bad_mech (L);
size_t len = 0; size_t len = 0, k_opad_len = 0, udlen;
const char *key = 0; const char *key = NULL;
uint8_t *k_opad = 0; uint8_t *k_opad = NULL;
if (what == WANT_HMAC) if (what == WANT_HMAC)
{ { // The key and k_opad are only used for HMAC; these default to NULLs for HASH
key = luaL_checklstring (L, 2, &len); key = luaL_checklstring (L, 2, &len);
k_opad = luaM_malloc (L, mi->block_size); k_opad_len = mi->block_size;
} }
void *ctx = luaM_malloc (L, mi->ctx_size);
mi->create (ctx); // create a userdatum with specific metatable. This comprises the ud header,
// the encrypto context block, and an optional HMAC block as a single allocation
// unit
udlen = sizeof(digest_user_datum_t) + mi->ctx_size + k_opad_len;
digest_user_datum_t *dudat = (digest_user_datum_t *)lua_newuserdata(L, udlen);
luaL_getmetatable(L, "crypto.hash"); // and set its metatable to the crypto.hash table
lua_setmetatable(L, -2);
if (what == WANT_HMAC) void *ctx = dudat + 1; // The context block immediately follows the digest_user_datum
mi->create (ctx);
if (what == WANT_HMAC) {
// The k_opad block immediately follows the context block
k_opad = (char *)ctx + mi->ctx_size;
crypto_hmac_begin (ctx, mi, key, len, k_opad); crypto_hmac_begin (ctx, mi, key, len, k_opad);
}
// create a userdataum with specific metatable
digest_user_datum_t *dudat = (digest_user_datum_t *)lua_newuserdata(L, sizeof(digest_user_datum_t));
luaL_getmetatable(L, "crypto.hash");
lua_setmetatable(L, -2);
// Set pointers to the mechanics and CTX // Set pointers to the mechanics and CTX
dudat->mech_info = mi; dudat->mech_info = mi;
...@@ -262,23 +268,6 @@ static int crypto_hash_finalize (lua_State *L) ...@@ -262,23 +268,6 @@ static int crypto_hash_finalize (lua_State *L)
return 1; return 1;
} }
/* Frees memory for the user datum and CTX hash state */
static int crypto_hash_gcdelete (lua_State *L)
{
NODE_DBG("enter crypto_hash_delete.\n");
digest_user_datum_t *dudat;
dudat = (digest_user_datum_t *)luaL_checkudata(L, 1, "crypto.hash");
// luaM_free() uses type info to obtain original size, so have to delve
// one level deeper and explicitly pass the size due to void*
luaM_realloc_ (L, dudat->ctx, dudat->mech_info->ctx_size, 0);
luaM_free (L, dudat->k_opad);
return 0;
}
static sint32_t vfs_read_wrap (int fd, void *ptr, size_t len) static sint32_t vfs_read_wrap (int fd, void *ptr, size_t len)
{ {
return vfs_read (fd, ptr, len); return vfs_read (fd, ptr, len);
...@@ -356,40 +345,31 @@ static const crypto_mech_t *get_mech (lua_State *L, int idx) ...@@ -356,40 +345,31 @@ static const crypto_mech_t *get_mech (lua_State *L, int idx)
static int crypto_encdec (lua_State *L, bool enc) static int crypto_encdec (lua_State *L, bool enc)
{ {
const crypto_mech_t *mech = get_mech (L, 1); const crypto_mech_t *mech = get_mech (L, 1);
size_t klen; size_t klen, dlen, ivlen, bs = mech->block_size;
const char *key = luaL_checklstring (L, 2, &klen); const char *key = luaL_checklstring (L, 2, &klen);
size_t dlen;
const char *data = luaL_checklstring (L, 3, &dlen); const char *data = luaL_checklstring (L, 3, &dlen);
size_t ivlen;
const char *iv = luaL_optlstring (L, 4, "", &ivlen); const char *iv = luaL_optlstring (L, 4, "", &ivlen);
size_t bs = mech->block_size;
size_t outlen = ((dlen + bs -1) / bs) * bs; size_t outlen = ((dlen + bs -1) / bs) * bs;
char *buf = (char *)os_zalloc (outlen);
if (!buf)
return luaL_error (L, "crypto init failed");
crypto_op_t op = char *buf = luaM_newvector(L, outlen, char);
{
crypto_op_t op = {
key, klen, key, klen,
iv, ivlen, iv, ivlen,
data, dlen, data, dlen,
buf, outlen, buf, outlen,
enc ? OP_ENCRYPT : OP_DECRYPT enc ? OP_ENCRYPT : OP_DECRYPT
}; };
if (!mech->run (&op))
{ int status = mech->run (&op);
os_free (buf);
return luaL_error (L, "crypto op failed"); lua_pushlstring (L, buf, outlen); /* discarded on error but what the hell */
} luaM_freearray(L, buf, outlen, char);
else
{ return status ? 1 : luaL_error (L, "crypto op failed");
lua_pushlstring (L, buf, outlen);
// note: if lua_pushlstring runs out of memory, we leak buf :(
os_free (buf);
return 1;
}
} }
static int lcrypto_encrypt (lua_State *L) static int lcrypto_encrypt (lua_State *L)
...@@ -403,35 +383,34 @@ static int lcrypto_decrypt (lua_State *L) ...@@ -403,35 +383,34 @@ static int lcrypto_decrypt (lua_State *L)
} }
// Hash function map // Hash function map
static const LUA_REG_TYPE crypto_hash_map[] = { LROT_BEGIN(crypto_hash)
{ LSTRKEY( "update" ), LFUNCVAL( crypto_hash_update ) }, LROT_FUNCENTRY( update, crypto_hash_update )
{ LSTRKEY( "finalize" ), LFUNCVAL( crypto_hash_finalize ) }, LROT_FUNCENTRY( finalize, crypto_hash_finalize )
{ LSTRKEY( "__gc" ), LFUNCVAL( crypto_hash_gcdelete ) }, LROT_TABENTRY( __index, crypto_hash )
{ LSTRKEY( "__index" ), LROVAL( crypto_hash_map ) }, LROT_END( crypto_hash, crypto_hash, LROT_MASK_INDEX )
{ LNILKEY, LNILVAL }
};
// Module function map // Module function map
static const LUA_REG_TYPE crypto_map[] = { LROT_BEGIN(crypto)
{ LSTRKEY( "sha1" ), LFUNCVAL( crypto_sha1 ) }, LROT_FUNCENTRY( sha1, crypto_sha1 )
{ LSTRKEY( "toBase64" ), LFUNCVAL( crypto_base64_encode ) }, LROT_FUNCENTRY( toBase64, crypto_base64_encode )
{ LSTRKEY( "toHex" ), LFUNCVAL( crypto_hex_encode ) }, LROT_FUNCENTRY( toHex, crypto_hex_encode )
{ LSTRKEY( "mask" ), LFUNCVAL( crypto_mask ) }, LROT_FUNCENTRY( mask, crypto_mask )
{ LSTRKEY( "hash" ), LFUNCVAL( crypto_lhash ) }, LROT_FUNCENTRY( hash, crypto_lhash )
{ LSTRKEY( "fhash" ), LFUNCVAL( crypto_flhash ) }, LROT_FUNCENTRY( fhash, crypto_flhash )
{ LSTRKEY( "new_hash" ), LFUNCVAL( crypto_new_hash ) }, LROT_FUNCENTRY( new_hash, crypto_new_hash )
{ LSTRKEY( "hmac" ), LFUNCVAL( crypto_lhmac ) }, LROT_FUNCENTRY( hmac, crypto_lhmac )
{ LSTRKEY( "new_hmac" ), LFUNCVAL( crypto_new_hmac ) }, LROT_FUNCENTRY( new_hmac, crypto_new_hmac )
{ LSTRKEY( "encrypt" ), LFUNCVAL( lcrypto_encrypt ) }, LROT_FUNCENTRY( encrypt, lcrypto_encrypt )
{ LSTRKEY( "decrypt" ), LFUNCVAL( lcrypto_decrypt ) }, LROT_FUNCENTRY( decrypt, lcrypto_decrypt )
{ LNILKEY, LNILVAL } LROT_END( crypto, NULL, 0 )
};
int luaopen_crypto ( lua_State *L ) int luaopen_crypto ( lua_State *L )
{ {
luaL_rometatable(L, "crypto.hash", (void *)crypto_hash_map); // create metatable for crypto.hash luaL_rometatable(L, "crypto.hash", LROT_TABLEREF(crypto_hash));
return 0; return 0;
} }
NODEMCU_MODULE(CRYPTO, "crypto", crypto_map, luaopen_crypto); NODEMCU_MODULE(CRYPTO, "crypto", crypto, luaopen_crypto);
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "cpu_esp8266.h" #include "cpu_esp8266.h"
#include "dht.h" #include "dht/dht.h"
#define NUM_DHT GPIO_PIN_NUM #define NUM_DHT GPIO_PIN_NUM
...@@ -99,14 +99,14 @@ static int dht_lapi_readxx( lua_State *L ) ...@@ -99,14 +99,14 @@ static int dht_lapi_readxx( lua_State *L )
// } // }
// Module function map // Module function map
static const LUA_REG_TYPE dht_map[] = { LROT_BEGIN(dht)
{ LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) }, LROT_FUNCENTRY( read, dht_lapi_read )
{ LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) }, LROT_FUNCENTRY( read11, dht_lapi_read11 )
{ LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) }, LROT_FUNCENTRY( readxx, dht_lapi_readxx )
{ LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) }, LROT_NUMENTRY( OK, DHTLIB_OK )
{ LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) }, LROT_NUMENTRY( ERROR_CHECKSUM, DHTLIB_ERROR_CHECKSUM )
{ LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) }, LROT_NUMENTRY( ERROR_TIMEOUT, DHTLIB_ERROR_TIMEOUT )
{ LNILKEY, LNILVAL } LROT_END( dht, NULL, 0 )
};
NODEMCU_MODULE(DHT, "dht", dht_map, NULL);
NODEMCU_MODULE(DHT, "dht", dht, NULL);
//***************************************************************************
// DS18B20 module for ESP8266 with nodeMCU
// fetchbot @github
// MIT license, http://opensource.org/licenses/MIT
//***************************************************************************
#include "module.h"
#include "lauxlib.h"
#include "platform.h"
#include "osapi.h"
#include "driver/onewire.h"
#include "c_stdio.h"
#include "c_stdlib.h"
//***************************************************************************
// OW ROM COMMANDS
//***************************************************************************
#define DS18B20_ROM_SEARCH (0xF0)
#define DS18B20_ROM_READ (0x33)
#define DS18B20_ROM_MATCH (0x55)
#define DS18B20_ROM_SKIP (0xCC)
#define DS18B20_ROM_SEARCH_ALARM (0xEC)
//***************************************************************************
// OW FUNCTION COMMANDS
//***************************************************************************
#define DS18B20_FUNC_CONVERT (0x44)
#define DS18B20_FUNC_SCRATCH_WRITE (0x4E)
#define DS18B20_FUNC_SCRATCH_READ (0xBE)
#define DS18B20_FUNC_SCRATCH_COPY (0x48)
#define DS18B20_FUNC_E2_RECALL (0xB8)
#define DS18B20_FUNC_POWER_READ (0xB4)
//***************************************************************************
// Initial EEPROM values
//***************************************************************************
#define DS18B20_EEPROM_TH (0x4B) // 75 degree
#define DS18B20_EEPROM_TL (0x46) // 70 degree
#define DS18B20_EEPROM_RES (0x7F) // 12 bit resolution
//***************************************************************************
static uint8_t ds18b20_bus_pin;
static uint8_t ds18b20_device_family;
static uint8_t ds18b20_device_search = 0;
static uint8_t ds18b20_device_index;
static uint8_t ds18b20_device_par;
static uint8_t ds18b20_device_conf[3];
static uint8_t ds18b20_device_rom[8];
static uint8_t ds18b20_device_scratchpad[9];
static double ds18b20_device_scratchpad_temp;
static int ds18b20_device_scratchpad_temp_dec;
static uint8_t ds18b20_device_scratchpad_conf;
static uint8_t ds18b20_device_res = 12; // 12 bit resolution (750ms conversion time)
os_timer_t ds18b20_timer; // timer for conversion delay
int ds18b20_timer_ref; // callback when readout is ready
int ds18b20_table_ref;
static int ds18b20_table_offset;
static int ds18b20_lua_readoutdone(void);
// Setup onewire bus for DS18B20 temperature sensors
// Lua: ds18b20.setup(OW_BUS_PIN)
static int ds18b20_lua_setup(lua_State *L) {
platform_print_deprecation_note("ds18b20 C module superseded by Lua implementation", "soon");
// check ow bus pin value
if (!lua_isnumber(L, 1) || lua_isnumber(L, 1) == 0) {
return luaL_error(L, "wrong 1-wire pin");
}
ds18b20_bus_pin = luaL_checkinteger(L, 1);
MOD_CHECK_ID(ow, ds18b20_bus_pin);
onewire_init(ds18b20_bus_pin);
}
static int ds18b20_set_device(uint8_t *ds18b20_device_rom) {
onewire_reset(ds18b20_bus_pin);
onewire_select(ds18b20_bus_pin, ds18b20_device_rom);
onewire_write(ds18b20_bus_pin, DS18B20_FUNC_SCRATCH_WRITE, 0);
onewire_write_bytes(ds18b20_bus_pin, ds18b20_device_conf, 3, 0);
}
// Change sensor settings
// Lua: ds18b20.setting(ROM, RES)
static int ds18b20_lua_setting(lua_State *L) {
// check rom table and resolution setting
if (!lua_istable(L, 1) || !lua_isnumber(L, 2)) {
return luaL_error(L, "wrong arg range");
}
ds18b20_device_res = luaL_checkinteger(L, 2);
if (!((ds18b20_device_res == 9) || (ds18b20_device_res == 10) || (ds18b20_device_res == 11) || (ds18b20_device_res == 12))) {
return luaL_error(L, "Invalid argument: resolution");
}
// no change to th and tl setting
ds18b20_device_conf[0] = DS18B20_EEPROM_TH;
ds18b20_device_conf[1] = DS18B20_EEPROM_TL;
ds18b20_device_conf[2] = ((ds18b20_device_res - 9) << 5) + 0x1F;
uint8_t table_len = lua_objlen(L, 1);
const char *str[table_len];
const char *sep = ":";
uint8_t string_index = 0;
lua_pushnil(L);
while (lua_next(L, -3)) {
str[string_index] = lua_tostring(L, -1);
lua_pop(L, 1);
string_index++;
}
lua_pop(L, 1);
for (uint8_t i = 0; i < string_index; i++) {
for (uint8_t j = 0; j < 8; j++) {
ds18b20_device_rom[j] = strtoul(str[i], NULL, 16);
str[i] = strchr(str[i], *sep);
if (str[i] == NULL || *str[i] == '\0') break;
str[i]++;
}
ds18b20_set_device(ds18b20_device_rom);
}
// set conversion delay once to max if sensors with higher resolution still on the bus
ds18b20_device_res = 12;
return 0;
}
#include "pm/swtimer.h"
// Reads sensor values from all devices
// Lua: ds18b20.read(function(INDEX, ROM, RES, TEMP, TEMP_DEC, PAR) print(INDEX, ROM, RES, TEMP, TEMP_DEC, PAR) end, ROM[, FAMILY])
static int ds18b20_lua_read(lua_State *L) {
luaL_argcheck(L, (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION), 1, "Must be function");
lua_pushvalue(L, 1);
ds18b20_timer_ref = luaL_ref(L, LUA_REGISTRYINDEX);
if (!lua_istable(L, 2)) {
return luaL_error(L, "wrong arg range");
}
if (lua_isnumber(L, 3)) {
ds18b20_device_family = luaL_checkinteger(L, 3);
onewire_target_search(ds18b20_bus_pin, ds18b20_device_family);
ds18b20_table_offset = -3;
} else {
ds18b20_table_offset = -2;
}
lua_pushvalue(L, 2);
ds18b20_table_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushnil(L);
if (lua_next(L, ds18b20_table_offset)) {
lua_pop(L, 2);
ds18b20_device_search = 0;
} else {
ds18b20_device_search = 1;
}
os_timer_disarm(&ds18b20_timer);
// perform a temperature conversion for all sensors and set timer
onewire_reset(ds18b20_bus_pin);
onewire_write(ds18b20_bus_pin, DS18B20_ROM_SKIP, 0);
onewire_write(ds18b20_bus_pin, DS18B20_FUNC_CONVERT, 1);
os_timer_setfn(&ds18b20_timer, (os_timer_func_t *)ds18b20_lua_readoutdone, NULL);
SWTIMER_REG_CB(ds18b20_lua_readoutdone, SWTIMER_DROP);
//The function ds18b20_lua_readoutdone reads the temperature from the sensor(s) after a set amount of time depending on temperature resolution
//MY guess: If this timer manages to get suspended before it fires and the temperature data is time sensitive then resulting data would be invalid and should be discarded
switch (ds18b20_device_res) {
case (9):
os_timer_arm(&ds18b20_timer, 95, 0);
break;
case (10):
os_timer_arm(&ds18b20_timer, 190, 0);
break;
case (11):
os_timer_arm(&ds18b20_timer, 380, 0);
break;
case (12):
os_timer_arm(&ds18b20_timer, 760, 0);
break;
}
}
static int ds18b20_read_device(uint8_t *ds18b20_device_rom) {
lua_State *L = lua_getstate();
int16_t ds18b20_raw_temp;
if (onewire_crc8(ds18b20_device_rom,7) == ds18b20_device_rom[7]) {
onewire_reset(ds18b20_bus_pin);
onewire_select(ds18b20_bus_pin, ds18b20_device_rom);
onewire_write(ds18b20_bus_pin, DS18B20_FUNC_POWER_READ, 0);
if (onewire_read(ds18b20_bus_pin)) ds18b20_device_par = 0;
else ds18b20_device_par = 1;
onewire_reset(ds18b20_bus_pin);
onewire_select(ds18b20_bus_pin, ds18b20_device_rom);
onewire_write(ds18b20_bus_pin, DS18B20_FUNC_SCRATCH_READ, 0);
onewire_read_bytes(ds18b20_bus_pin, ds18b20_device_scratchpad, 9);
if (onewire_crc8(ds18b20_device_scratchpad,8) == ds18b20_device_scratchpad[8]) {
lua_rawgeti(L, LUA_REGISTRYINDEX, ds18b20_timer_ref);
lua_pushinteger(L, ds18b20_device_index);
lua_pushfstring(L, "%d:%d:%d:%d:%d:%d:%d:%d", ds18b20_device_rom[0], ds18b20_device_rom[1], ds18b20_device_rom[2], ds18b20_device_rom[3], ds18b20_device_rom[4], ds18b20_device_rom[5], ds18b20_device_rom[6], ds18b20_device_rom[7]);
ds18b20_device_scratchpad_conf = (ds18b20_device_scratchpad[4] >> 5) + 9;
ds18b20_raw_temp = ((ds18b20_device_scratchpad[1] << 8) | ds18b20_device_scratchpad[0]);
ds18b20_device_scratchpad_temp = (double)ds18b20_raw_temp / 16;
ds18b20_device_scratchpad_temp_dec = (ds18b20_raw_temp - (ds18b20_raw_temp / 16 * 16)) * 1000 / 16;
if (ds18b20_device_scratchpad_conf >= ds18b20_device_res) {
ds18b20_device_res = ds18b20_device_scratchpad_conf;
}
lua_pushinteger(L, ds18b20_device_scratchpad_conf);
lua_pushnumber(L, ds18b20_device_scratchpad_temp);
lua_pushinteger(L, ds18b20_device_scratchpad_temp_dec);
lua_pushinteger(L, ds18b20_device_par);
lua_pcall(L, 6, 0, 0);
ds18b20_device_index++;
}
}
}
static int ds18b20_lua_readoutdone(void) {
lua_State *L = lua_getstate();
os_timer_disarm(&ds18b20_timer);
ds18b20_device_index = 1;
// set conversion delay to min and change it after finding the sensor with the highest resolution setting
ds18b20_device_res = 9;
if (ds18b20_device_search) {
// iterate through all sensors on the bus and read temperature, resolution and parasitc settings
while (onewire_search(ds18b20_bus_pin, ds18b20_device_rom)) {
ds18b20_read_device(ds18b20_device_rom);
}
} else {
lua_rawgeti(L, LUA_REGISTRYINDEX, ds18b20_table_ref);
uint8_t table_len = lua_objlen(L, -1);
const char *str[table_len];
const char *sep = ":";
uint8_t string_index = 0;
lua_pushnil(L);
while (lua_next(L, -2)) {
str[string_index] = lua_tostring(L, -1);
lua_pop(L, 1);
string_index++;
}
lua_pop(L, 1);
for (uint8_t i = 0; i < string_index; i++) {
for (uint8_t j = 0; j < 8; j++) {
ds18b20_device_rom[j] = strtoul(str[i], NULL, 16);
str[i] = strchr(str[i], *sep);
if (str[i] == NULL || *str[i] == '\0') break;
str[i]++;
}
ds18b20_read_device(ds18b20_device_rom);
}
}
luaL_unref(L, LUA_REGISTRYINDEX, ds18b20_table_ref);
ds18b20_table_ref = LUA_NOREF;
luaL_unref(L, LUA_REGISTRYINDEX, ds18b20_timer_ref);
ds18b20_timer_ref = LUA_NOREF;
}
static const LUA_REG_TYPE ds18b20_map[] = {
{ LSTRKEY( "read" ), LFUNCVAL(ds18b20_lua_read) },
{ LSTRKEY( "setting" ), LFUNCVAL(ds18b20_lua_setting) },
{ LSTRKEY( "setup" ), LFUNCVAL(ds18b20_lua_setup) },
{ LNILKEY, LNILVAL }
};
NODEMCU_MODULE(DS18B20, "ds18b20", ds18b20_map, NULL);
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