node.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. // Module for interfacing with system
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "ldebug.h"
  5. #include "ldo.h"
  6. #include "lfunc.h"
  7. #include "lmem.h"
  8. #include "lobject.h"
  9. #include "lstate.h"
  10. #include "legc.h"
  11. #include "lopcodes.h"
  12. #include "lstring.h"
  13. #include "lundump.h"
  14. #include "platform.h"
  15. #include "lrodefs.h"
  16. #include "c_types.h"
  17. #include "c_string.h"
  18. #include "driver/uart.h"
  19. #include "user_interface.h"
  20. #include "flash_api.h"
  21. #include "flash_fs.h"
  22. #include "user_version.h"
  23. #include "rom.h"
  24. #include "task/task.h"
  25. #define CPU80MHZ 80
  26. #define CPU160MHZ 160
  27. // Lua: restart()
  28. static int node_restart( lua_State* L )
  29. {
  30. system_restart();
  31. return 0;
  32. }
  33. // Lua: dsleep( us, option )
  34. static int node_deepsleep( lua_State* L )
  35. {
  36. uint32 us;
  37. uint8 option;
  38. //us = luaL_checkinteger( L, 1 );
  39. // Set deleep option, skip if nil
  40. if ( lua_isnumber(L, 2) )
  41. {
  42. option = lua_tointeger(L, 2);
  43. if ( option < 0 || option > 4)
  44. return luaL_error( L, "wrong arg range" );
  45. else
  46. system_deep_sleep_set_option( option );
  47. }
  48. // Set deleep time, skip if nil
  49. if ( lua_isnumber(L, 1) )
  50. {
  51. us = luaL_checknumber(L, 1);
  52. // if ( us <= 0 )
  53. if ( us < 0 )
  54. return luaL_error( L, "wrong arg range" );
  55. else
  56. system_deep_sleep( us );
  57. }
  58. return 0;
  59. }
  60. // Lua: dsleep_set_options
  61. // Combined to dsleep( us, option )
  62. // static int node_deepsleep_setoption( lua_State* L )
  63. // {
  64. // s32 option;
  65. // option = luaL_checkinteger( L, 1 );
  66. // if ( option < 0 || option > 4)
  67. // return luaL_error( L, "wrong arg range" );
  68. // else
  69. // deep_sleep_set_option( option );
  70. // return 0;
  71. // }
  72. // Lua: info()
  73. static int node_info( lua_State* L )
  74. {
  75. lua_pushinteger(L, NODE_VERSION_MAJOR);
  76. lua_pushinteger(L, NODE_VERSION_MINOR);
  77. lua_pushinteger(L, NODE_VERSION_REVISION);
  78. lua_pushinteger(L, system_get_chip_id()); // chip id
  79. lua_pushinteger(L, spi_flash_get_id()); // flash id
  80. #if defined(FLASH_SAFE_API)
  81. lua_pushinteger(L, flash_safe_get_size_byte() / 1024); // flash size in KB
  82. #else
  83. lua_pushinteger(L, flash_rom_get_size_byte() / 1024); // flash size in KB
  84. #endif // defined(FLASH_SAFE_API)
  85. lua_pushinteger(L, flash_rom_get_mode());
  86. lua_pushinteger(L, flash_rom_get_speed());
  87. return 8;
  88. }
  89. // Lua: chipid()
  90. static int node_chipid( lua_State* L )
  91. {
  92. uint32_t id = system_get_chip_id();
  93. lua_pushinteger(L, id);
  94. return 1;
  95. }
  96. // deprecated, moved to adc module
  97. // Lua: readvdd33()
  98. // static int node_readvdd33( lua_State* L )
  99. // {
  100. // uint32_t vdd33 = readvdd33();
  101. // lua_pushinteger(L, vdd33);
  102. // return 1;
  103. // }
  104. // Lua: flashid()
  105. static int node_flashid( lua_State* L )
  106. {
  107. uint32_t id = spi_flash_get_id();
  108. lua_pushinteger( L, id );
  109. return 1;
  110. }
  111. // Lua: flashsize()
  112. static int node_flashsize( lua_State* L )
  113. {
  114. if (lua_type(L, 1) == LUA_TNUMBER)
  115. {
  116. flash_rom_set_size_byte(luaL_checkinteger(L, 1));
  117. }
  118. #if defined(FLASH_SAFE_API)
  119. uint32_t sz = flash_safe_get_size_byte();
  120. #else
  121. uint32_t sz = flash_rom_get_size_byte();
  122. #endif // defined(FLASH_SAFE_API)
  123. lua_pushinteger( L, sz );
  124. return 1;
  125. }
  126. // Lua: heap()
  127. static int node_heap( lua_State* L )
  128. {
  129. uint32_t sz = system_get_free_heap_size();
  130. lua_pushinteger(L, sz);
  131. return 1;
  132. }
  133. #ifdef DEVKIT_VERSION_0_9
  134. static int led_high_count = LED_HIGH_COUNT_DEFAULT;
  135. static int led_low_count = LED_LOW_COUNT_DEFAULT;
  136. static int led_count = 0;
  137. static int key_press_count = 0;
  138. static bool key_short_pressed = false;
  139. static bool key_long_pressed = false;
  140. static os_timer_t keyled_timer;
  141. static int long_key_ref = LUA_NOREF;
  142. static int short_key_ref = LUA_NOREF;
  143. static void default_long_press(void *arg) {
  144. if (led_high_count == 12 && led_low_count == 12) {
  145. led_low_count = led_high_count = 6;
  146. } else {
  147. led_low_count = led_high_count = 12;
  148. }
  149. // led_high_count = 1000 / READLINE_INTERVAL;
  150. // led_low_count = 1000 / READLINE_INTERVAL;
  151. // NODE_DBG("default_long_press is called. hc: %d, lc: %d\n", led_high_count, led_low_count);
  152. }
  153. static void default_short_press(void *arg) {
  154. system_restart();
  155. }
  156. static void key_long_press(void *arg) {
  157. lua_State *L = lua_getstate();
  158. NODE_DBG("key_long_press is called.\n");
  159. if (long_key_ref == LUA_NOREF) {
  160. default_long_press(arg);
  161. return;
  162. }
  163. lua_rawgeti(L, LUA_REGISTRYINDEX, long_key_ref);
  164. lua_call(L, 0, 0);
  165. }
  166. static void key_short_press(void *arg) {
  167. lua_State *L = lua_getstate();
  168. NODE_DBG("key_short_press is called.\n");
  169. if (short_key_ref == LUA_NOREF) {
  170. default_short_press(arg);
  171. return;
  172. }
  173. lua_rawgeti(L, LUA_REGISTRYINDEX, short_key_ref);
  174. lua_call(L, 0, 0);
  175. }
  176. static void update_key_led (void *p)
  177. {
  178. (void)p;
  179. uint8_t temp = 1, level = 1;
  180. led_count++;
  181. if(led_count>led_low_count+led_high_count){
  182. led_count = 0; // reset led_count, the level still high
  183. } else if(led_count>led_low_count && led_count <=led_high_count+led_low_count){
  184. level = 1; // output high level
  185. } else if(led_count<=led_low_count){
  186. level = 0; // output low level
  187. }
  188. temp = platform_key_led(level);
  189. if(temp == 0){ // key is pressed
  190. key_press_count++;
  191. if(key_press_count>=KEY_LONG_COUNT){
  192. // key_long_press(NULL);
  193. key_long_pressed = true;
  194. key_short_pressed = false;
  195. // key_press_count = 0;
  196. } else if(key_press_count>=KEY_SHORT_COUNT){ // < KEY_LONG_COUNT
  197. // key_short_press(NULL);
  198. key_short_pressed = true;
  199. }
  200. }else{ // key is released
  201. key_press_count = 0;
  202. if(key_long_pressed){
  203. key_long_press(NULL);
  204. key_long_pressed = false;
  205. }
  206. if(key_short_pressed){
  207. key_short_press(NULL);
  208. key_short_pressed = false;
  209. }
  210. }
  211. }
  212. static void prime_keyled_timer (void)
  213. {
  214. os_timer_disarm (&keyled_timer);
  215. os_timer_setfn (&keyled_timer, update_key_led, 0);
  216. os_timer_arm (&keyled_timer, KEYLED_INTERVAL, 1);
  217. }
  218. // Lua: led(low, high)
  219. static int node_led( lua_State* L )
  220. {
  221. int low, high;
  222. if ( lua_isnumber(L, 1) )
  223. {
  224. low = lua_tointeger(L, 1);
  225. if ( low < 0 ) {
  226. return luaL_error( L, "wrong arg type" );
  227. }
  228. } else {
  229. low = LED_LOW_COUNT_DEFAULT; // default to LED_LOW_COUNT_DEFAULT
  230. }
  231. if ( lua_isnumber(L, 2) )
  232. {
  233. high = lua_tointeger(L, 2);
  234. if ( high < 0 ) {
  235. return luaL_error( L, "wrong arg type" );
  236. }
  237. } else {
  238. high = LED_HIGH_COUNT_DEFAULT; // default to LED_HIGH_COUNT_DEFAULT
  239. }
  240. led_high_count = (uint32_t)high / READLINE_INTERVAL;
  241. led_low_count = (uint32_t)low / READLINE_INTERVAL;
  242. prime_keyled_timer();
  243. return 0;
  244. }
  245. // Lua: key(type, function)
  246. static int node_key( lua_State* L )
  247. {
  248. int *ref = NULL;
  249. size_t sl;
  250. const char *str = luaL_checklstring( L, 1, &sl );
  251. if (str == NULL)
  252. return luaL_error( L, "wrong arg type" );
  253. if (sl == 5 && c_strcmp(str, "short") == 0) {
  254. ref = &short_key_ref;
  255. } else if (sl == 4 && c_strcmp(str, "long") == 0) {
  256. ref = &long_key_ref;
  257. } else {
  258. ref = &short_key_ref;
  259. }
  260. // luaL_checkanyfunction(L, 2);
  261. if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) {
  262. lua_pushvalue(L, 2); // copy argument (func) to the top of stack
  263. if (*ref != LUA_NOREF)
  264. luaL_unref(L, LUA_REGISTRYINDEX, *ref);
  265. *ref = luaL_ref(L, LUA_REGISTRYINDEX);
  266. } else { // unref the key press function
  267. if (*ref != LUA_NOREF)
  268. luaL_unref(L, LUA_REGISTRYINDEX, *ref);
  269. *ref = LUA_NOREF;
  270. }
  271. prime_keyled_timer();
  272. return 0;
  273. }
  274. #endif
  275. extern lua_Load gLoad;
  276. extern bool user_process_input(bool force);
  277. // Lua: input("string")
  278. static int node_input( lua_State* L )
  279. {
  280. size_t l = 0;
  281. const char *s = luaL_checklstring(L, 1, &l);
  282. if (s != NULL && l > 0 && l < LUA_MAXINPUT - 1)
  283. {
  284. lua_Load *load = &gLoad;
  285. if (load->line_position == 0) {
  286. c_memcpy(load->line, s, l);
  287. load->line[l + 1] = '\0';
  288. load->line_position = c_strlen(load->line) + 1;
  289. load->done = 1;
  290. NODE_DBG("Get command:\n");
  291. NODE_DBG(load->line); // buggy here
  292. NODE_DBG("\nResult(if any):\n");
  293. user_process_input(true);
  294. }
  295. }
  296. return 0;
  297. }
  298. static int output_redir_ref = LUA_NOREF;
  299. static int serial_debug = 1;
  300. void output_redirect(const char *str) {
  301. lua_State *L = lua_getstate();
  302. // if(c_strlen(str)>=TX_BUFF_SIZE){
  303. // NODE_ERR("output too long.\n");
  304. // return;
  305. // }
  306. if (output_redir_ref == LUA_NOREF || !L) {
  307. uart0_sendStr(str);
  308. return;
  309. }
  310. if (serial_debug != 0) {
  311. uart0_sendStr(str);
  312. }
  313. lua_rawgeti(L, LUA_REGISTRYINDEX, output_redir_ref);
  314. lua_pushstring(L, str);
  315. lua_call(L, 1, 0); // this call back function should never user output.
  316. }
  317. // Lua: output(function(c), debug)
  318. static int node_output( lua_State* L )
  319. {
  320. // luaL_checkanyfunction(L, 1);
  321. if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION) {
  322. lua_pushvalue(L, 1); // copy argument (func) to the top of stack
  323. if (output_redir_ref != LUA_NOREF)
  324. luaL_unref(L, LUA_REGISTRYINDEX, output_redir_ref);
  325. output_redir_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  326. } else { // unref the key press function
  327. if (output_redir_ref != LUA_NOREF)
  328. luaL_unref(L, LUA_REGISTRYINDEX, output_redir_ref);
  329. output_redir_ref = LUA_NOREF;
  330. serial_debug = 1;
  331. return 0;
  332. }
  333. if ( lua_isnumber(L, 2) )
  334. {
  335. serial_debug = lua_tointeger(L, 2);
  336. if (serial_debug != 0)
  337. serial_debug = 1;
  338. } else {
  339. serial_debug = 1; // default to 1
  340. }
  341. return 0;
  342. }
  343. static int writer(lua_State* L, const void* p, size_t size, void* u)
  344. {
  345. UNUSED(L);
  346. int file_fd = *( (int *)u );
  347. if ((FS_OPEN_OK - 1) == file_fd)
  348. return 1;
  349. NODE_DBG("get fd:%d,size:%d\n", file_fd, size);
  350. if (size != 0 && (size != fs_write(file_fd, (const char *)p, size)) )
  351. return 1;
  352. NODE_DBG("write fd:%d,size:%d\n", file_fd, size);
  353. return 0;
  354. }
  355. #define toproto(L,i) (clvalue(L->top+(i))->l.p)
  356. // Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc
  357. static int node_compile( lua_State* L )
  358. {
  359. Proto* f;
  360. int file_fd = FS_OPEN_OK - 1;
  361. size_t len;
  362. const char *fname = luaL_checklstring( L, 1, &len );
  363. if ( len >= FS_NAME_MAX_LENGTH )
  364. return luaL_error(L, "filename too long");
  365. char output[FS_NAME_MAX_LENGTH];
  366. c_strcpy(output, fname);
  367. // check here that filename end with ".lua".
  368. if (len < 4 || (c_strcmp( output + len - 4, ".lua") != 0) )
  369. return luaL_error(L, "not a .lua file");
  370. output[c_strlen(output) - 2] = 'c';
  371. output[c_strlen(output) - 1] = '\0';
  372. NODE_DBG(output);
  373. NODE_DBG("\n");
  374. if (luaL_loadfsfile(L, fname) != 0) {
  375. return luaL_error(L, lua_tostring(L, -1));
  376. }
  377. f = toproto(L, -1);
  378. int stripping = 1; /* strip debug information? */
  379. file_fd = fs_open(output, fs_mode2flag("w+"));
  380. if (file_fd < FS_OPEN_OK)
  381. {
  382. return luaL_error(L, "cannot open/write to file");
  383. }
  384. lua_lock(L);
  385. int result = luaU_dump(L, f, writer, &file_fd, stripping);
  386. lua_unlock(L);
  387. if (fs_flush(file_fd) < 0) { // result codes aren't propagated by flash_fs.h
  388. // overwrite Lua error, like writer() does in case of a file io error
  389. result = 1;
  390. }
  391. fs_close(file_fd);
  392. file_fd = FS_OPEN_OK - 1;
  393. if (result == LUA_ERR_CC_INTOVERFLOW) {
  394. return luaL_error(L, "value too big or small for target integer type");
  395. }
  396. if (result == LUA_ERR_CC_NOTINTEGER) {
  397. return luaL_error(L, "target lua_Number is integral but fractional value found");
  398. }
  399. if (result == 1) { // result status generated by writer() or fs_flush() fail
  400. return luaL_error(L, "writing to file failed");
  401. }
  402. return 0;
  403. }
  404. // Task callback handler for node.task.post()
  405. static task_handle_t do_node_task_handle;
  406. static void do_node_task (task_param_t task_fn_ref, uint8_t prio)
  407. {
  408. lua_State* L = lua_getstate();
  409. lua_rawgeti(L, LUA_REGISTRYINDEX, (int)task_fn_ref);
  410. luaL_unref(L, LUA_REGISTRYINDEX, (int)task_fn_ref);
  411. lua_pushinteger(L, prio);
  412. lua_call(L, 1, 0);
  413. }
  414. // Lua: node.task.post([priority],task_cb) -- schedule a task for execution next
  415. static int node_task_post( lua_State* L )
  416. {
  417. int n = 1, Ltype = lua_type(L, 1);
  418. unsigned priority = TASK_PRIORITY_MEDIUM;
  419. if (Ltype == LUA_TNUMBER) {
  420. priority = (unsigned) luaL_checkint(L, 1);
  421. luaL_argcheck(L, priority <= TASK_PRIORITY_HIGH, 1, "invalid priority");
  422. Ltype = lua_type(L, ++n);
  423. }
  424. luaL_argcheck(L, Ltype == LUA_TFUNCTION || Ltype == LUA_TLIGHTFUNCTION, n, "invalid function");
  425. lua_pushvalue(L, n);
  426. int task_fn_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  427. if (!do_node_task_handle) // bind the task handle to do_node_task on 1st call
  428. do_node_task_handle = task_get_id(do_node_task);
  429. if(!task_post(priority, do_node_task_handle, (task_param_t)task_fn_ref)) {
  430. luaL_unref(L, LUA_REGISTRYINDEX, task_fn_ref);
  431. luaL_error(L, "Task queue overflow. Task not posted");
  432. }
  433. return 0;
  434. }
  435. // Lua: setcpufreq(mhz)
  436. // mhz is either CPU80MHZ od CPU160MHZ
  437. static int node_setcpufreq(lua_State* L)
  438. {
  439. // http://www.esp8266.com/viewtopic.php?f=21&t=1369
  440. uint32_t new_freq = luaL_checkinteger(L, 1);
  441. if (new_freq == CPU160MHZ){
  442. REG_SET_BIT(0x3ff00014, BIT(0));
  443. ets_update_cpu_frequency(CPU160MHZ);
  444. } else {
  445. REG_CLR_BIT(0x3ff00014, BIT(0));
  446. ets_update_cpu_frequency(CPU80MHZ);
  447. }
  448. new_freq = ets_get_cpu_frequency();
  449. lua_pushinteger(L, new_freq);
  450. return 1;
  451. }
  452. // Lua: code, reason [, exccause, epc1, epc2, epc3, excvaddr, depc ] = bootreason()
  453. static int node_bootreason (lua_State *L)
  454. {
  455. const struct rst_info *ri = system_get_rst_info ();
  456. uint32_t arr[8] = {
  457. rtc_get_reset_reason(),
  458. ri->reason,
  459. ri->exccause, ri->epc1, ri->epc2, ri->epc3, ri->excvaddr, ri->depc
  460. };
  461. int i, n = ((ri->reason != REASON_EXCEPTION_RST) ? 2 : 8);
  462. for (i = 0; i < n; ++i)
  463. lua_pushinteger (L, arr[i]);
  464. return n;
  465. }
  466. // Lua: restore()
  467. static int node_restore (lua_State *L)
  468. {
  469. system_restore();
  470. return 0;
  471. }
  472. #ifdef LUA_OPTIMIZE_DEBUG
  473. /* node.stripdebug([level[, function]]). 
  474. * level: 1 don't discard debug
  475. * 2 discard Local and Upvalue debug info
  476. * 3 discard Local, Upvalue and lineno debug info.
  477. * function: Function to be stripped as per setfenv except 0 not permitted.
  478. * If no arguments then the current default setting is returned.
  479. * If function is omitted, this is the default setting for future compiles
  480. * The function returns an estimated integer count of the bytes stripped.
  481. */
  482. static int node_stripdebug (lua_State *L) {
  483. int level;
  484. if (L->top == L->base) {
  485. lua_pushlightuserdata(L, &luaG_stripdebug );
  486. lua_gettable(L, LUA_REGISTRYINDEX);
  487. if (lua_isnil(L, -1)) {
  488. lua_pop(L, 1);
  489. lua_pushinteger(L, LUA_OPTIMIZE_DEBUG);
  490. }
  491. return 1;
  492. }
  493. level = luaL_checkint(L, 1);
  494. if ((level <= 0) || (level > 3)) luaL_argerror(L, 1, "must in range 1-3");
  495. if (L->top == L->base + 1) {
  496. /* Store the default level in the registry if no function parameter */
  497. lua_pushlightuserdata(L, &luaG_stripdebug);
  498. lua_pushinteger(L, level);
  499. lua_settable(L, LUA_REGISTRYINDEX);
  500. lua_settop(L,0);
  501. return 0;
  502. }
  503. if (level == 1) {
  504. lua_settop(L,0);
  505. lua_pushinteger(L, 0);
  506. return 1;
  507. }
  508. if (!lua_isfunction(L, 2)) {
  509. int scope = luaL_checkint(L, 2);
  510. if (scope > 0) {
  511. /* if the function parameter is a +ve integer then climb to find function */
  512. lua_Debug ar;
  513. lua_pop(L, 1); /* pop level as getinfo will replace it by the function */
  514. if (lua_getstack(L, scope, &ar)) {
  515. lua_getinfo(L, "f", &ar);
  516. }
  517. }
  518. }
  519. if(!lua_isfunction(L, 2) || lua_iscfunction(L, -1)) luaL_argerror(L, 2, "must be a Lua Function");
  520. // lua_lock(L);
  521. Proto *f = clvalue(L->base + 1)->l.p;
  522. // lua_unlock(L);
  523. lua_settop(L,0);
  524. lua_pushinteger(L, luaG_stripdebug(L, f, level, 1));
  525. return 1;
  526. }
  527. #endif
  528. // Lua: node.egc.setmode( mode, [param])
  529. // where the mode is one of the node.egc constants NOT_ACTIVE , ON_ALLOC_FAILURE,
  530. // ON_MEM_LIMIT, ALWAYS. In the case of ON_MEM_LIMIT an integer parameter is reqired
  531. // See legc.h and lecg.c.
  532. static int node_egc_setmode(lua_State* L) {
  533. unsigned mode = luaL_checkinteger(L, 1);
  534. unsigned limit = luaL_optinteger (L, 2, 0);
  535. luaL_argcheck(L, mode <= (EGC_ON_ALLOC_FAILURE | EGC_ON_MEM_LIMIT | EGC_ALWAYS), 1, "invalid mode");
  536. luaL_argcheck(L, !(mode & EGC_ON_MEM_LIMIT) || limit>0, 1, "limit must be non-zero");
  537. legc_set_mode( L, mode, limit );
  538. return 0;
  539. }
  540. //
  541. // Lua: osprint(true/false)
  542. // Allows you to turn on the native Espressif SDK printing
  543. static int node_osprint( lua_State* L )
  544. {
  545. if (lua_toboolean(L, 1)) {
  546. system_set_os_print(1);
  547. } else {
  548. system_set_os_print(0);
  549. }
  550. return 0;
  551. }
  552. // Module function map
  553. static const LUA_REG_TYPE node_egc_map[] = {
  554. { LSTRKEY( "setmode" ), LFUNCVAL( node_egc_setmode ) },
  555. { LSTRKEY( "NOT_ACTIVE" ), LNUMVAL( EGC_NOT_ACTIVE ) },
  556. { LSTRKEY( "ON_ALLOC_FAILURE" ), LNUMVAL( EGC_ON_ALLOC_FAILURE ) },
  557. { LSTRKEY( "ON_MEM_LIMIT" ), LNUMVAL( EGC_ON_MEM_LIMIT ) },
  558. { LSTRKEY( "ALWAYS" ), LNUMVAL( EGC_ALWAYS ) },
  559. { LNILKEY, LNILVAL }
  560. };
  561. static const LUA_REG_TYPE node_task_map[] = {
  562. { LSTRKEY( "post" ), LFUNCVAL( node_task_post ) },
  563. { LSTRKEY( "LOW_PRIORITY" ), LNUMVAL( TASK_PRIORITY_LOW ) },
  564. { LSTRKEY( "MEDIUM_PRIORITY" ), LNUMVAL( TASK_PRIORITY_MEDIUM ) },
  565. { LSTRKEY( "HIGH_PRIORITY" ), LNUMVAL( TASK_PRIORITY_HIGH ) },
  566. { LNILKEY, LNILVAL }
  567. };
  568. static const LUA_REG_TYPE node_map[] =
  569. {
  570. { LSTRKEY( "restart" ), LFUNCVAL( node_restart ) },
  571. { LSTRKEY( "dsleep" ), LFUNCVAL( node_deepsleep ) },
  572. { LSTRKEY( "info" ), LFUNCVAL( node_info ) },
  573. { LSTRKEY( "chipid" ), LFUNCVAL( node_chipid ) },
  574. { LSTRKEY( "flashid" ), LFUNCVAL( node_flashid ) },
  575. { LSTRKEY( "flashsize" ), LFUNCVAL( node_flashsize) },
  576. { LSTRKEY( "heap" ), LFUNCVAL( node_heap ) },
  577. #ifdef DEVKIT_VERSION_0_9
  578. { LSTRKEY( "key" ), LFUNCVAL( node_key ) },
  579. { LSTRKEY( "led" ), LFUNCVAL( node_led ) },
  580. #endif
  581. { LSTRKEY( "input" ), LFUNCVAL( node_input ) },
  582. { LSTRKEY( "output" ), LFUNCVAL( node_output ) },
  583. // Moved to adc module, use adc.readvdd33()
  584. // { LSTRKEY( "readvdd33" ), LFUNCVAL( node_readvdd33) },
  585. { LSTRKEY( "compile" ), LFUNCVAL( node_compile) },
  586. { LSTRKEY( "CPU80MHZ" ), LNUMVAL( CPU80MHZ ) },
  587. { LSTRKEY( "CPU160MHZ" ), LNUMVAL( CPU160MHZ ) },
  588. { LSTRKEY( "setcpufreq" ), LFUNCVAL( node_setcpufreq) },
  589. { LSTRKEY( "bootreason" ), LFUNCVAL( node_bootreason) },
  590. { LSTRKEY( "restore" ), LFUNCVAL( node_restore) },
  591. #ifdef LUA_OPTIMIZE_DEBUG
  592. { LSTRKEY( "stripdebug" ), LFUNCVAL( node_stripdebug ) },
  593. #endif
  594. { LSTRKEY( "egc" ), LROVAL( node_egc_map ) },
  595. { LSTRKEY( "task" ), LROVAL( node_task_map ) },
  596. #ifdef DEVELOPMENT_TOOLS
  597. { LSTRKEY( "osprint" ), LFUNCVAL( node_osprint ) },
  598. #endif
  599. // Combined to dsleep(us, option)
  600. // { LSTRKEY( "dsleepsetoption" ), LFUNCVAL( node_deepsleep_setoption) },
  601. { LNILKEY, LNILVAL }
  602. };
  603. NODEMCU_MODULE(NODE, "node", node_map, NULL);