luat_lib_hmeta.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. @module hmeta
  3. @summary 硬件元数据
  4. @version 1.0
  5. @date 2022.01.11
  6. @demo hmeta
  7. @tag LUAT_USE_HMETA
  8. @usage
  9. -- 本库开发中
  10. --[[
  11. 这个库的作用是展示当前硬件的能力, 例如:
  12. 1. 有多少GPIO, 各GPIO默认模式是什么, 是否支持上拉/下拉
  13. 2. 有多少I2C,支持哪些速率
  14. 3. 有多少SPI,支持哪些速率和模式
  15. 4. 扩展属性, 例如区分Air780E和Air600E
  16. ]]
  17. */
  18. #include "luat_base.h"
  19. #include "luat_hmeta.h"
  20. /*
  21. 获取模组名称
  22. @api hmeta.model()
  23. @return string 若能识别到,返回模组类型, 否则会是nil
  24. @usage
  25. sys.taskInit(function()
  26. while 1 do
  27. sys.wait(3000)
  28. -- hmeta识别底层模组类型的
  29. -- 不同的模组可以使用相同的bsp,但根据封装的不同,根据内部数据仍可识别出具体模块
  30. log.info("hmeta", hmeta.model())
  31. log.info("bsp", rtos.bsp())
  32. end
  33. end)
  34. */
  35. static int l_hmeta_model(lua_State *L) {
  36. char buff[40] = {0};
  37. luat_hmeta_model_name(buff);
  38. if (strlen(buff)) {
  39. lua_pushstring(L, buff);
  40. }
  41. else {
  42. lua_pushnil(L);
  43. }
  44. return 1;
  45. }
  46. /*
  47. 获取模组的硬件版本号
  48. @api hmeta.hwver()
  49. @return string 若能识别到,返回模组类型, 否则会是nil
  50. @usage
  51. sys.taskInit(function()
  52. while 1 do
  53. sys.wait(3000)
  54. -- hmeta识别底层模组类型的
  55. -- 不同的模组可以使用相同的bsp,但根据封装的不同,根据内部数据仍可识别出具体模块
  56. log.info("hmeta", hmeta.model(), hmeta.hwver())
  57. log.info("bsp", rtos.bsp())
  58. end
  59. end)
  60. */
  61. static int l_hmeta_hwver(lua_State *L) {
  62. char buff[40] = {0};
  63. luat_hmeta_hwversion(buff);
  64. if (strlen(buff)) {
  65. lua_pushstring(L, buff);
  66. }
  67. else {
  68. lua_pushnil(L);
  69. }
  70. return 1;
  71. }
  72. /*
  73. 获取原始芯片型号
  74. @api hmeta.chip()
  75. @return string 若能识别到,返回芯片类型, 否则会是nil
  76. @usage
  77. -- 若底层正确实现, 这个函数总会返回值
  78. -- 本函数于 2024.12.5 新增
  79. */
  80. static int l_hmeta_chip(lua_State *L) {
  81. char buff[16] = {0};
  82. luat_hmeta_chip(buff);
  83. if (strlen(buff)) {
  84. lua_pushstring(L, buff);
  85. }
  86. else {
  87. lua_pushnil(L);
  88. }
  89. return 1;
  90. }
  91. // static int l_hmeta_gpio(lua_State *L) {
  92. // return 0;
  93. // }
  94. // static int l_hmeta_uart(lua_State *L) {
  95. // return 0;
  96. // }
  97. #include "rotable2.h"
  98. static const rotable_Reg_t reg_hmeta[] =
  99. {
  100. { "model" , ROREG_FUNC(l_hmeta_model)},
  101. { "hwver" , ROREG_FUNC(l_hmeta_hwver)},
  102. { "chip" , ROREG_FUNC(l_hmeta_chip)},
  103. // { "gpio" , ROREG_FUNC(l_hmeta_gpio)},
  104. // { "uart" , ROREG_FUNC(l_hmeta_uart)},
  105. { NULL, ROREG_INT(0)}
  106. };
  107. LUAMOD_API int luaopen_hmeta( lua_State *L ) {
  108. luat_newlib2(L, reg_hmeta);
  109. return 1;
  110. }