node.c 20 KB

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