rtcmem.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Module for RTC user memory access
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "rtc/rtcaccess.h"
  5. static int rtcmem_read32 (lua_State *L)
  6. {
  7. int idx = luaL_checknumber (L, 1);
  8. int n = 1;
  9. if (lua_isnumber (L, 2))
  10. n = lua_tonumber (L, 2);
  11. if (!lua_checkstack (L, n))
  12. return 0;
  13. int ret = 0;
  14. while (n > 0 && idx >= 0 && idx < RTC_USER_MEM_NUM_DWORDS)
  15. {
  16. lua_pushinteger (L, rtc_mem_read (idx++));
  17. --n;
  18. ++ret;
  19. }
  20. return ret;
  21. }
  22. static int rtcmem_write32 (lua_State *L)
  23. {
  24. int idx = luaL_checknumber (L, 1);
  25. int n = lua_gettop (L) - 1;
  26. luaL_argcheck (
  27. L, idx + n <= RTC_USER_MEM_NUM_DWORDS, 1, "RTC mem would overrun");
  28. int src = 2;
  29. while (n-- > 0)
  30. {
  31. rtc_mem_write (idx++, lua_tonumber (L, src++));
  32. }
  33. return 0;
  34. }
  35. // Module function map
  36. static const LUA_REG_TYPE rtcmem_map[] = {
  37. { LSTRKEY("read32"), LFUNCVAL(rtcmem_read32) },
  38. { LSTRKEY("write32"), LFUNCVAL(rtcmem_write32) },
  39. { LNILKEY, LNILVAL }
  40. };
  41. NODEMCU_MODULE(RTCMEM, "rtcmem", rtcmem_map, NULL);