2014-12-22 12:35:05 +01:00
|
|
|
// Module for interfacing with system
|
|
|
|
|
2015-02-13 08:11:59 +01:00
|
|
|
#include "lua.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
#include "lauxlib.h"
|
2015-02-13 08:11:59 +01:00
|
|
|
|
|
|
|
#include "ldo.h"
|
|
|
|
#include "lfunc.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "lopcodes.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "lundump.h"
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
#include "platform.h"
|
|
|
|
#include "auxmods.h"
|
|
|
|
#include "lrotable.h"
|
|
|
|
|
|
|
|
#include "c_types.h"
|
|
|
|
#include "romfs.h"
|
|
|
|
#include "c_string.h"
|
|
|
|
#include "driver/uart.h"
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
//#include "spi_flash.h"
|
|
|
|
#include "user_interface.h"
|
2014-12-31 01:08:31 +01:00
|
|
|
#include "flash_api.h"
|
2015-02-13 08:11:59 +01:00
|
|
|
#include "flash_fs.h"
|
2015-03-06 04:59:04 +01:00
|
|
|
#include "user_version.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2015-03-17 10:23:45 +01:00
|
|
|
#define CPU80MHZ 80
|
|
|
|
#define CPU160MHZ 160
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Lua: restart()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_restart( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
system_restart();
|
2015-03-15 22:40:43 +01:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
// Lua: dsleep( us, option )
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_deepsleep( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
s32 us, option;
|
|
|
|
//us = luaL_checkinteger( L, 1 );
|
|
|
|
// Set deleep option, skip if nil
|
|
|
|
if ( lua_isnumber(L, 2) )
|
|
|
|
{
|
|
|
|
option = lua_tointeger(L, 2);
|
|
|
|
if ( option < 0 || option > 4)
|
|
|
|
return luaL_error( L, "wrong arg range" );
|
|
|
|
else
|
|
|
|
deep_sleep_set_option( option );
|
|
|
|
}
|
|
|
|
// Set deleep time, skip if nil
|
|
|
|
if ( lua_isnumber(L, 1) )
|
|
|
|
{
|
|
|
|
us = lua_tointeger(L, 1);
|
|
|
|
// if ( us <= 0 )
|
|
|
|
if ( us < 0 )
|
|
|
|
return luaL_error( L, "wrong arg range" );
|
|
|
|
else
|
|
|
|
system_deep_sleep( us );
|
|
|
|
}
|
2015-03-15 22:40:43 +01:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 17:13:19 +01:00
|
|
|
// Lua: dsleep_set_options
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
// Combined to dsleep( us, option )
|
|
|
|
// static int node_deepsleep_setoption( lua_State* L )
|
|
|
|
// {
|
|
|
|
// s32 option;
|
|
|
|
// option = luaL_checkinteger( L, 1 );
|
|
|
|
// if ( option < 0 || option > 4)
|
|
|
|
// return luaL_error( L, "wrong arg range" );
|
|
|
|
// else
|
|
|
|
// deep_sleep_set_option( option );
|
|
|
|
// return 0;
|
|
|
|
// }
|
2014-12-31 07:26:51 +01:00
|
|
|
// Lua: info()
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_info( lua_State* L )
|
2014-12-31 07:26:51 +01:00
|
|
|
{
|
|
|
|
lua_pushinteger(L, NODE_VERSION_MAJOR);
|
|
|
|
lua_pushinteger(L, NODE_VERSION_MINOR);
|
|
|
|
lua_pushinteger(L, NODE_VERSION_REVISION);
|
|
|
|
lua_pushinteger(L, system_get_chip_id()); // chip id
|
|
|
|
lua_pushinteger(L, spi_flash_get_id()); // flash id
|
2015-03-15 22:40:43 +01:00
|
|
|
#if defined(FLASH_SAFE_API)
|
2015-03-15 17:51:47 +01:00
|
|
|
lua_pushinteger(L, flash_safe_get_size_byte() / 1024); // flash size in KB
|
2015-03-15 22:40:43 +01:00
|
|
|
#else
|
2015-03-15 17:51:47 +01:00
|
|
|
lua_pushinteger(L, flash_rom_get_size_byte() / 1024); // flash size in KB
|
2015-03-15 22:40:43 +01:00
|
|
|
#endif // defined(FLASH_SAFE_API)
|
2015-03-15 17:48:28 +01:00
|
|
|
lua_pushinteger(L, flash_rom_get_mode());
|
|
|
|
lua_pushinteger(L, flash_rom_get_speed());
|
2015-03-15 22:40:43 +01:00
|
|
|
return 8;
|
2014-12-31 07:26:51 +01:00
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Lua: chipid()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_chipid( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
uint32_t id = system_get_chip_id();
|
|
|
|
lua_pushinteger(L, id);
|
2015-03-15 22:40:43 +01:00
|
|
|
return 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
2015-03-26 17:52:55 +01:00
|
|
|
|
|
|
|
// deprecated, moved to adc module
|
2015-01-26 11:17:15 +01:00
|
|
|
// Lua: readvdd33()
|
2015-03-26 17:52:55 +01:00
|
|
|
// static int node_readvdd33( lua_State* L )
|
|
|
|
// {
|
|
|
|
// uint32_t vdd33 = readvdd33();
|
|
|
|
// lua_pushinteger(L, vdd33);
|
|
|
|
// return 1;
|
|
|
|
// }
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2014-12-31 01:08:31 +01:00
|
|
|
// Lua: flashid()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_flashid( lua_State* L )
|
2014-12-31 01:08:31 +01:00
|
|
|
{
|
|
|
|
uint32_t id = spi_flash_get_id();
|
|
|
|
lua_pushinteger( L, id );
|
2015-03-15 22:40:43 +01:00
|
|
|
return 1;
|
2014-12-31 01:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: flashsize()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_flashsize( lua_State* L )
|
2014-12-31 01:08:31 +01:00
|
|
|
{
|
2015-03-15 22:40:43 +01:00
|
|
|
if (lua_type(L, 1) == LUA_TNUMBER)
|
|
|
|
{
|
|
|
|
flash_rom_set_size_byte(luaL_checkinteger(L, 1));
|
|
|
|
}
|
2015-03-15 17:51:47 +01:00
|
|
|
#if defined(FLASH_SAFE_API)
|
|
|
|
uint32_t sz = flash_safe_get_size_byte();
|
|
|
|
#else
|
|
|
|
uint32_t sz = flash_rom_get_size_byte();
|
|
|
|
#endif // defined(FLASH_SAFE_API)
|
2014-12-31 01:08:31 +01:00
|
|
|
lua_pushinteger( L, sz );
|
2015-03-15 22:40:43 +01:00
|
|
|
return 1;
|
2014-12-31 01:08:31 +01:00
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Lua: heap()
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_heap( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
uint32_t sz = system_get_free_heap_size();
|
|
|
|
lua_pushinteger(L, sz);
|
2015-03-15 22:40:43 +01:00
|
|
|
return 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 04:05:28 +01:00
|
|
|
static lua_State *gL = NULL;
|
|
|
|
|
|
|
|
#ifdef DEVKIT_VERSION_0_9
|
2015-10-08 05:14:51 +02:00
|
|
|
static int led_high_count = LED_HIGH_COUNT_DEFAULT;
|
|
|
|
static int led_low_count = LED_LOW_COUNT_DEFAULT;
|
|
|
|
static int led_count = 0;
|
|
|
|
static int key_press_count = 0;
|
|
|
|
static bool key_short_pressed = false;
|
|
|
|
static bool key_long_pressed = false;
|
|
|
|
static os_timer_t keyled_timer;
|
|
|
|
|
|
|
|
static void update_key_led (void *p)
|
|
|
|
{
|
|
|
|
(void)p;
|
|
|
|
uint8_t temp = 1, level = 1;
|
|
|
|
led_count++;
|
|
|
|
if(led_count>led_low_count+led_high_count){
|
|
|
|
led_count = 0; // reset led_count, the level still high
|
|
|
|
} else if(led_count>led_low_count && led_count <=led_high_count+led_low_count){
|
|
|
|
level = 1; // output high level
|
|
|
|
} else if(led_count<=led_low_count){
|
|
|
|
level = 0; // output low level
|
|
|
|
}
|
|
|
|
temp = platform_key_led(level);
|
|
|
|
if(temp == 0){ // key is pressed
|
|
|
|
key_press_count++;
|
|
|
|
if(key_press_count>=KEY_LONG_COUNT){
|
|
|
|
// key_long_press(NULL);
|
|
|
|
key_long_pressed = true;
|
|
|
|
key_short_pressed = false;
|
|
|
|
// key_press_count = 0;
|
|
|
|
} else if(key_press_count>=KEY_SHORT_COUNT){ // < KEY_LONG_COUNT
|
|
|
|
// key_short_press(NULL);
|
|
|
|
key_short_pressed = true;
|
|
|
|
}
|
|
|
|
}else{ // key is released
|
|
|
|
key_press_count = 0;
|
|
|
|
if(key_long_pressed){
|
|
|
|
key_long_press(NULL);
|
|
|
|
key_long_pressed = false;
|
|
|
|
}
|
|
|
|
if(key_short_pressed){
|
|
|
|
key_short_press(NULL);
|
|
|
|
key_short_pressed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void prime_keyled_timer (void)
|
|
|
|
{
|
|
|
|
os_timer_disarm (&keyled_timer);
|
|
|
|
os_timer_setfn (&keyled_timer, update_key_led, 0);
|
|
|
|
os_timer_arm (&keyled_timer, KEYLED_INTERVAL, 1);
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Lua: led(low, high)
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_led( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
int low, high;
|
|
|
|
if ( lua_isnumber(L, 1) )
|
|
|
|
{
|
|
|
|
low = lua_tointeger(L, 1);
|
2015-03-15 22:40:43 +01:00
|
|
|
if ( low < 0 ) {
|
2014-12-22 12:35:05 +01:00
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
low = LED_LOW_COUNT_DEFAULT; // default to LED_LOW_COUNT_DEFAULT
|
|
|
|
}
|
|
|
|
if ( lua_isnumber(L, 2) )
|
|
|
|
{
|
|
|
|
high = lua_tointeger(L, 2);
|
2015-03-15 22:40:43 +01:00
|
|
|
if ( high < 0 ) {
|
2014-12-22 12:35:05 +01:00
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
high = LED_HIGH_COUNT_DEFAULT; // default to LED_HIGH_COUNT_DEFAULT
|
|
|
|
}
|
|
|
|
led_high_count = (uint32_t)high / READLINE_INTERVAL;
|
|
|
|
led_low_count = (uint32_t)low / READLINE_INTERVAL;
|
2015-10-08 05:14:51 +02:00
|
|
|
prime_keyled_timer();
|
2015-03-15 22:40:43 +01:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int long_key_ref = LUA_NOREF;
|
|
|
|
static int short_key_ref = LUA_NOREF;
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
void default_long_press(void *arg) {
|
|
|
|
if (led_high_count == 12 && led_low_count == 12) {
|
2014-12-22 12:35:05 +01:00
|
|
|
led_low_count = led_high_count = 6;
|
|
|
|
} else {
|
|
|
|
led_low_count = led_high_count = 12;
|
|
|
|
}
|
|
|
|
// led_high_count = 1000 / READLINE_INTERVAL;
|
|
|
|
// led_low_count = 1000 / READLINE_INTERVAL;
|
|
|
|
// NODE_DBG("default_long_press is called. hc: %d, lc: %d\n", led_high_count, led_low_count);
|
|
|
|
}
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
void default_short_press(void *arg) {
|
2014-12-22 12:35:05 +01:00
|
|
|
system_restart();
|
|
|
|
}
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
void key_long_press(void *arg) {
|
2014-12-22 12:35:05 +01:00
|
|
|
NODE_DBG("key_long_press is called.\n");
|
2015-03-15 22:40:43 +01:00
|
|
|
if (long_key_ref == LUA_NOREF) {
|
2014-12-22 12:35:05 +01:00
|
|
|
default_long_press(arg);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-15 22:40:43 +01:00
|
|
|
if (!gL)
|
2014-12-22 12:35:05 +01:00
|
|
|
return;
|
|
|
|
lua_rawgeti(gL, LUA_REGISTRYINDEX, long_key_ref);
|
|
|
|
lua_call(gL, 0, 0);
|
|
|
|
}
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
void key_short_press(void *arg) {
|
2014-12-22 12:35:05 +01:00
|
|
|
NODE_DBG("key_short_press is called.\n");
|
2015-03-15 22:40:43 +01:00
|
|
|
if (short_key_ref == LUA_NOREF) {
|
2014-12-22 12:35:05 +01:00
|
|
|
default_short_press(arg);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-15 22:40:43 +01:00
|
|
|
if (!gL)
|
2014-12-22 12:35:05 +01:00
|
|
|
return;
|
|
|
|
lua_rawgeti(gL, LUA_REGISTRYINDEX, short_key_ref);
|
2015-03-15 22:40:43 +01:00
|
|
|
lua_call(gL, 0, 0);
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: key(type, function)
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_key( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
int *ref = NULL;
|
|
|
|
size_t sl;
|
2015-03-15 22:40:43 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
const char *str = luaL_checklstring( L, 1, &sl );
|
|
|
|
if (str == NULL)
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
if (sl == 5 && c_strcmp(str, "short") == 0) {
|
2014-12-22 12:35:05 +01:00
|
|
|
ref = &short_key_ref;
|
2015-03-15 22:40:43 +01:00
|
|
|
} else if (sl == 4 && c_strcmp(str, "long") == 0) {
|
2014-12-22 12:35:05 +01:00
|
|
|
ref = &long_key_ref;
|
2015-03-15 22:40:43 +01:00
|
|
|
} else {
|
2014-12-22 12:35:05 +01:00
|
|
|
ref = &short_key_ref;
|
|
|
|
}
|
|
|
|
gL = L;
|
|
|
|
// luaL_checkanyfunction(L, 2);
|
2015-03-15 22:40:43 +01:00
|
|
|
if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) {
|
2014-12-22 12:35:05 +01:00
|
|
|
lua_pushvalue(L, 2); // copy argument (func) to the top of stack
|
2015-03-15 22:40:43 +01:00
|
|
|
if (*ref != LUA_NOREF)
|
2014-12-22 12:35:05 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, *ref);
|
|
|
|
*ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
} else { // unref the key press function
|
2015-03-15 22:40:43 +01:00
|
|
|
if (*ref != LUA_NOREF)
|
2014-12-22 12:35:05 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, *ref);
|
2015-03-15 22:40:43 +01:00
|
|
|
*ref = LUA_NOREF;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
2015-10-08 05:14:51 +02:00
|
|
|
prime_keyled_timer();
|
2015-03-15 22:40:43 +01:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
2015-02-12 04:05:28 +01:00
|
|
|
#endif
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
extern lua_Load gLoad;
|
|
|
|
// Lua: input("string")
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_input( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
2015-03-15 22:40:43 +01:00
|
|
|
size_t l = 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
const char *s = luaL_checklstring(L, 1, &l);
|
|
|
|
if (s != NULL && l > 0 && l < LUA_MAXINPUT - 1)
|
|
|
|
{
|
|
|
|
lua_Load *load = &gLoad;
|
2015-03-15 22:40:43 +01:00
|
|
|
if (load->line_position == 0) {
|
2014-12-22 12:35:05 +01:00
|
|
|
c_memcpy(load->line, s, l);
|
2015-03-15 22:40:43 +01:00
|
|
|
load->line[l + 1] = '\0';
|
|
|
|
load->line_position = c_strlen(load->line) + 1;
|
2014-12-22 12:35:05 +01:00
|
|
|
load->done = 1;
|
|
|
|
NODE_DBG("Get command:\n");
|
|
|
|
NODE_DBG(load->line); // buggy here
|
|
|
|
NODE_DBG("\nResult(if any):\n");
|
2015-10-08 05:14:51 +02:00
|
|
|
system_os_post (LUA_TASK_PRIO, LUA_PROCESS_LINE_SIG, 0);
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int output_redir_ref = LUA_NOREF;
|
|
|
|
static int serial_debug = 1;
|
2015-03-15 22:40:43 +01:00
|
|
|
void output_redirect(const char *str) {
|
2014-12-22 12:35:05 +01:00
|
|
|
// if(c_strlen(str)>=TX_BUFF_SIZE){
|
|
|
|
// NODE_ERR("output too long.\n");
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
if (output_redir_ref == LUA_NOREF || !gL) {
|
2014-12-22 12:35:05 +01:00
|
|
|
uart0_sendStr(str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
if (serial_debug != 0) {
|
2014-12-22 12:35:05 +01:00
|
|
|
uart0_sendStr(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_rawgeti(gL, LUA_REGISTRYINDEX, output_redir_ref);
|
|
|
|
lua_pushstring(gL, str);
|
|
|
|
lua_call(gL, 1, 0); // this call back function should never user output.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: output(function(c), debug)
|
2015-01-05 03:09:51 +01:00
|
|
|
static int node_output( lua_State* L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
gL = L;
|
|
|
|
// luaL_checkanyfunction(L, 1);
|
2015-03-15 22:40:43 +01:00
|
|
|
if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION) {
|
2014-12-22 12:35:05 +01:00
|
|
|
lua_pushvalue(L, 1); // copy argument (func) to the top of stack
|
2015-03-15 22:40:43 +01:00
|
|
|
if (output_redir_ref != LUA_NOREF)
|
2014-12-22 12:35:05 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, output_redir_ref);
|
|
|
|
output_redir_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
} else { // unref the key press function
|
2015-03-15 22:40:43 +01:00
|
|
|
if (output_redir_ref != LUA_NOREF)
|
2014-12-22 12:35:05 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, output_redir_ref);
|
2015-03-15 22:40:43 +01:00
|
|
|
output_redir_ref = LUA_NOREF;
|
2014-12-22 12:35:05 +01:00
|
|
|
serial_debug = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lua_isnumber(L, 2) )
|
|
|
|
{
|
|
|
|
serial_debug = lua_tointeger(L, 2);
|
2015-03-15 22:40:43 +01:00
|
|
|
if (serial_debug != 0)
|
2014-12-22 12:35:05 +01:00
|
|
|
serial_debug = 1;
|
|
|
|
} else {
|
|
|
|
serial_debug = 1; // default to 1
|
|
|
|
}
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
return 0;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
2015-02-13 08:11:59 +01:00
|
|
|
static int writer(lua_State* L, const void* p, size_t size, void* u)
|
|
|
|
{
|
|
|
|
UNUSED(L);
|
|
|
|
int file_fd = *( (int *)u );
|
2015-03-15 22:40:43 +01:00
|
|
|
if ((FS_OPEN_OK - 1) == file_fd)
|
2015-02-13 08:11:59 +01:00
|
|
|
return 1;
|
2015-03-15 22:40:43 +01:00
|
|
|
NODE_DBG("get fd:%d,size:%d\n", file_fd, size);
|
|
|
|
|
|
|
|
if (size != 0 && (size != fs_write(file_fd, (const char *)p, size)) )
|
2015-02-13 08:11:59 +01:00
|
|
|
return 1;
|
2015-03-15 22:40:43 +01:00
|
|
|
NODE_DBG("write fd:%d,size:%d\n", file_fd, size);
|
2015-02-13 08:11:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define toproto(L,i) (clvalue(L->top+(i))->l.p)
|
|
|
|
// Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc
|
|
|
|
static int node_compile( lua_State* L )
|
|
|
|
{
|
|
|
|
Proto* f;
|
|
|
|
int file_fd = FS_OPEN_OK - 1;
|
|
|
|
size_t len;
|
|
|
|
const char *fname = luaL_checklstring( L, 1, &len );
|
2015-03-15 22:40:43 +01:00
|
|
|
if ( len > FS_NAME_MAX_LENGTH )
|
2015-02-13 08:11:59 +01:00
|
|
|
return luaL_error(L, "filename too long");
|
|
|
|
|
|
|
|
char output[FS_NAME_MAX_LENGTH];
|
|
|
|
c_strcpy(output, fname);
|
|
|
|
// check here that filename end with ".lua".
|
2015-03-15 22:40:43 +01:00
|
|
|
if (len < 4 || (c_strcmp( output + len - 4, ".lua") != 0) )
|
2015-02-13 08:11:59 +01:00
|
|
|
return luaL_error(L, "not a .lua file");
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
output[c_strlen(output) - 2] = 'c';
|
|
|
|
output[c_strlen(output) - 1] = '\0';
|
2015-02-13 08:11:59 +01:00
|
|
|
NODE_DBG(output);
|
|
|
|
NODE_DBG("\n");
|
2015-03-15 22:40:43 +01:00
|
|
|
if (luaL_loadfsfile(L, fname) != 0) {
|
|
|
|
return luaL_error(L, lua_tostring(L, -1));
|
2015-02-13 08:11:59 +01:00
|
|
|
}
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
f = toproto(L, -1);
|
2015-02-13 08:11:59 +01:00
|
|
|
|
|
|
|
int stripping = 1; /* strip debug information? */
|
|
|
|
|
|
|
|
file_fd = fs_open(output, fs_mode2flag("w+"));
|
2015-03-15 22:40:43 +01:00
|
|
|
if (file_fd < FS_OPEN_OK)
|
2015-02-13 08:11:59 +01:00
|
|
|
{
|
|
|
|
return luaL_error(L, "cannot open/write to file");
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_lock(L);
|
2015-03-15 22:40:43 +01:00
|
|
|
int result = luaU_dump(L, f, writer, &file_fd, stripping);
|
2015-02-13 08:11:59 +01:00
|
|
|
lua_unlock(L);
|
|
|
|
|
2015-08-11 00:08:49 +02:00
|
|
|
if (fs_flush(file_fd) < 0) { // result codes aren't propagated by flash_fs.h
|
|
|
|
// overwrite Lua error, like writer() does in case of a file io error
|
|
|
|
result = 1;
|
|
|
|
}
|
2015-02-13 08:11:59 +01:00
|
|
|
fs_close(file_fd);
|
|
|
|
file_fd = FS_OPEN_OK - 1;
|
|
|
|
|
2015-03-15 22:40:43 +01:00
|
|
|
if (result == LUA_ERR_CC_INTOVERFLOW) {
|
2015-02-13 08:11:59 +01:00
|
|
|
return luaL_error(L, "value too big or small for target integer type");
|
|
|
|
}
|
2015-03-15 22:40:43 +01:00
|
|
|
if (result == LUA_ERR_CC_NOTINTEGER) {
|
2015-02-13 08:11:59 +01:00
|
|
|
return luaL_error(L, "target lua_Number is integral but fractional value found");
|
|
|
|
}
|
2015-08-11 00:08:49 +02:00
|
|
|
if (result == 1) { // result status generated by writer() or fs_flush() fail
|
|
|
|
return luaL_error(L, "writing to file failed");
|
|
|
|
}
|
2015-02-13 08:11:59 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-17 10:23:45 +01:00
|
|
|
// Lua: setcpufreq(mhz)
|
|
|
|
// mhz is either CPU80MHZ od CPU160MHZ
|
|
|
|
static int node_setcpufreq(lua_State* L)
|
|
|
|
{
|
|
|
|
// http://www.esp8266.com/viewtopic.php?f=21&t=1369
|
|
|
|
uint32_t new_freq = luaL_checkinteger(L, 1);
|
|
|
|
if (new_freq == CPU160MHZ){
|
|
|
|
REG_SET_BIT(0x3ff00014, BIT(0));
|
2015-10-01 07:20:53 +02:00
|
|
|
ets_update_cpu_frequency(CPU160MHZ);
|
2015-03-17 10:23:45 +01:00
|
|
|
} else {
|
|
|
|
REG_CLR_BIT(0x3ff00014, BIT(0));
|
2015-10-01 07:20:53 +02:00
|
|
|
ets_update_cpu_frequency(CPU80MHZ);
|
2015-03-17 10:23:45 +01:00
|
|
|
}
|
|
|
|
new_freq = ets_get_cpu_frequency();
|
|
|
|
lua_pushinteger(L, new_freq);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-27 04:34:03 +02:00
|
|
|
// Lua: code = bootreason()
|
|
|
|
static int node_bootreason (lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushnumber (L, rtc_get_reset_reason ());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-07-03 21:54:22 +02:00
|
|
|
// Lua: restore()
|
|
|
|
static int node_restore (lua_State *L)
|
|
|
|
{
|
2015-07-03 22:27:59 +02:00
|
|
|
flash_init_data_default();
|
|
|
|
flash_init_data_blank();
|
2015-07-03 21:54:22 +02:00
|
|
|
system_restore();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
// Module function map
|
|
|
|
#define MIN_OPT_LEVEL 2
|
|
|
|
#include "lrodefs.h"
|
2015-03-15 22:40:43 +01:00
|
|
|
const LUA_REG_TYPE node_map[] =
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
{ LSTRKEY( "restart" ), LFUNCVAL( node_restart ) },
|
|
|
|
{ LSTRKEY( "dsleep" ), LFUNCVAL( node_deepsleep ) },
|
2014-12-31 07:26:51 +01:00
|
|
|
{ LSTRKEY( "info" ), LFUNCVAL( node_info ) },
|
2014-12-22 12:35:05 +01:00
|
|
|
{ LSTRKEY( "chipid" ), LFUNCVAL( node_chipid ) },
|
2014-12-31 01:08:31 +01:00
|
|
|
{ LSTRKEY( "flashid" ), LFUNCVAL( node_flashid ) },
|
|
|
|
{ LSTRKEY( "flashsize" ), LFUNCVAL( node_flashsize) },
|
2014-12-22 12:35:05 +01:00
|
|
|
{ LSTRKEY( "heap" ), LFUNCVAL( node_heap ) },
|
2015-02-12 04:05:28 +01:00
|
|
|
#ifdef DEVKIT_VERSION_0_9
|
2014-12-22 12:35:05 +01:00
|
|
|
{ LSTRKEY( "key" ), LFUNCVAL( node_key ) },
|
|
|
|
{ LSTRKEY( "led" ), LFUNCVAL( node_led ) },
|
2015-02-12 04:05:28 +01:00
|
|
|
#endif
|
2014-12-22 12:35:05 +01:00
|
|
|
{ LSTRKEY( "input" ), LFUNCVAL( node_input ) },
|
|
|
|
{ LSTRKEY( "output" ), LFUNCVAL( node_output ) },
|
2015-03-26 17:52:55 +01:00
|
|
|
// Moved to adc module, use adc.readvdd33()
|
|
|
|
// { LSTRKEY( "readvdd33" ), LFUNCVAL( node_readvdd33) },
|
2015-02-13 08:11:59 +01:00
|
|
|
{ LSTRKEY( "compile" ), LFUNCVAL( node_compile) },
|
2015-03-17 10:23:45 +01:00
|
|
|
{ LSTRKEY( "CPU80MHZ" ), LNUMVAL( CPU80MHZ ) },
|
|
|
|
{ LSTRKEY( "CPU160MHZ" ), LNUMVAL( CPU160MHZ ) },
|
|
|
|
{ LSTRKEY( "setcpufreq" ), LFUNCVAL( node_setcpufreq) },
|
2015-06-27 04:34:03 +02:00
|
|
|
{ LSTRKEY( "bootreason" ), LFUNCVAL( node_bootreason) },
|
2015-07-03 21:54:22 +02:00
|
|
|
{ LSTRKEY( "restore" ), LFUNCVAL( node_restore) },
|
2015-03-15 22:40:43 +01:00
|
|
|
// Combined to dsleep(us, option)
|
Combined dsleep_set_options(option) to dsleep( us, option )
* dsleep( us, option )
Hardware has to support deep-sleep wake up (XPD_DCDC connects to EXT_RSTB with 0R). system_deep_sleep(0) ,set no wake up timer,connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
** us: Integer
time to sleep.
if us = 0, it will sleep forever.
** option: Integer
option=0, init data byte 108 is valuable;
option>0, init data byte 108 is valueless.
More details as follows:
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
1, RF_CAL after deep-sleep wake up, there will belarge current.
2, no RF_CAL after deep-sleep wake up, there will only be small current.
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
2015-01-29 07:21:38 +01:00
|
|
|
// { LSTRKEY( "dsleepsetoption" ), LFUNCVAL( node_deepsleep_setoption) },
|
2014-12-22 12:35:05 +01:00
|
|
|
#if LUA_OPTIMIZE_MEMORY > 0
|
|
|
|
|
|
|
|
#endif
|
|
|
|
{ LNILKEY, LNILVAL }
|
|
|
|
};
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
LUALIB_API int luaopen_node( lua_State *L )
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
#if LUA_OPTIMIZE_MEMORY > 0
|
|
|
|
return 0;
|
|
|
|
#else // #if LUA_OPTIMIZE_MEMORY > 0
|
|
|
|
luaL_register( L, AUXLIB_NODE, node_map );
|
|
|
|
// Add constants
|
|
|
|
|
|
|
|
return 1;
|
2015-03-17 10:23:45 +01:00
|
|
|
#endif // #if LUA_OPTIMIZE_MEMORY > 0
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|