luat_lib_repl.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. @module repl
  3. @summary "读取-求值-输出" 循环
  4. @date 2023.06.16
  5. @tag LUAT_USE_REPL
  6. @usage
  7. --[[
  8. 本功能支持的模块及对应的端口
  9. 模块/芯片 端口 波特率及其他参数
  10. Air101/Air103 UART0 921600 8 None 1
  11. Air105 UART0 1500000 8 None 1
  12. ESP32C3 UART0 921600 8 None 1 -- 注意, 简约版(无CH343)不支持
  13. ESP32C2 UART0 921600 8 None 1
  14. ESP32S2 UART0 921600 8 None 1
  15. Air780EXXX 虚拟串口 任意 -- 暂不支持从物理UART调用
  16. 使用方法:
  17. 1. 非Air780EXXX系列可以使用任意串口工具, 打开对应的串口, 记得勾选"回车换行"
  18. 2. Air780EXXX请配合LuaTools使用, 菜单里有 "简易串口工具" 可发送, 记得勾选"回车换行"
  19. 2. 发送lua语句, 并以回车换行结束
  20. 语句支持情况:
  21. 1. 单行lua语句, 以回车换行结束即可
  22. 2. 多行语句, 用以下格式包裹起来发送, 例如
  23. <<EOF
  24. for k,v in pairs(_G) do
  25. print(k, v)
  26. end
  27. EOF
  28. 注意事项:
  29. 1. 可通过repl.enable(false)语句禁用REPL
  30. 2. 使用uart.setup/uart.close指定UART端口后, REPL自动失效
  31. 3. 单行语句一般支持到510字节,更长的语句请使用"多行语句"的方式使用
  32. 4. 若需要定义全局变量, 请使用 _G.xxx = yyy 形式
  33. 若有任何疑问, 请到 chat.openluat.com 发帖反馈
  34. ]]
  35. */
  36. #include "luat_base.h"
  37. #include "luat_shell.h"
  38. #include "luat_repl.h"
  39. #define LUAT_LOG_TAG "repl"
  40. #include "luat_log.h"
  41. /*
  42. 代理打印, 暂未实现
  43. */
  44. static int l_repl_print(lua_State* L) {
  45. return 0;
  46. }
  47. /*
  48. 启用或禁用REPL功能
  49. @api repl.enable(re)
  50. @bool 启用与否,默认是启用
  51. @return 之前的设置状态
  52. @usage
  53. -- 若固件支持REPL,即编译时启用了REPL,是默认启用REPL功能的
  54. -- 本函数是提供关闭REPL的途径
  55. repl.enable(false)
  56. */
  57. static int l_repl_enable(lua_State* L) {
  58. int ret = 0;
  59. if (lua_isboolean(L, 1))
  60. ret = luat_repl_enable(lua_toboolean(L, 1));
  61. else
  62. ret = luat_repl_enable(-1);
  63. lua_pushboolean(L, ret == 0 ? 1 : 0);
  64. return 1;
  65. }
  66. /*
  67. 主动推送待处理的数据到底层
  68. @api repl.push(data)
  69. @string 待处理的数据,通常从串口来
  70. @return nil 无返回值
  71. @usage
  72. -- 虚拟串口的设备才需要这个函数
  73. */
  74. static int l_repl_push(lua_State* L) {
  75. if (lua_isstring(L, 1)) {
  76. size_t len = 0;
  77. const char* buff = luaL_checklstring(L, 1, &len);
  78. if (len > 0) {
  79. luat_shell_push(buff, len);
  80. }
  81. }
  82. return 0;
  83. }
  84. #include "rotable2.h"
  85. static const rotable_Reg_t reg_repl[] =
  86. {
  87. { "print" , ROREG_FUNC(l_repl_print)},
  88. { "enable" , ROREG_FUNC(l_repl_enable)},
  89. { "push" , ROREG_FUNC(l_repl_push)},
  90. { NULL, ROREG_INT(0) }
  91. };
  92. LUAMOD_API int luaopen_repl( lua_State *L ) {
  93. luat_newlib2(L, reg_repl);
  94. return 1;
  95. }