luat_lib_yhm27xx.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. @module yhm27xx
  3. @summary yhm27xx充电芯片
  4. @version 1.0
  5. @date 2025.04.2
  6. @tag LUAT_USE_GPIO
  7. @demo yhm27xxx
  8. @usage
  9. -- 请查阅demo/yhm27xx
  10. */
  11. #include "luat_base.h"
  12. #include "luat_timer.h"
  13. #include "luat_mem.h"
  14. #include "luat_gpio.h"
  15. #include "luat_zbuff.h"
  16. #define LUAT_LOG_TAG "yhm27xx"
  17. #include "luat_log.h"
  18. /*
  19. 单总线命令读写YHM27XX
  20. @api yhm27xx.cmd(pin, chip_id, reg, data)
  21. @int gpio端口号
  22. @int 芯片ID
  23. @int 寄存器地址
  24. @int 要写入的数据,如果没填,则表示从寄存器读取数据
  25. @return boolean 成功返回true,失败返回false
  26. @return int 读取成功返回寄存器值,写入成功无返回
  27. @usage
  28. while 1 do
  29. sys.wait(1000)
  30. local result, data = yhm27xx.cmd(15, 0x04, 0x05)
  31. log.info("yhm27xx", result, data)
  32. end
  33. */
  34. static int l_yhm27xx_cmd(lua_State *L)
  35. {
  36. uint8_t pin = luaL_checkinteger(L, 1);
  37. uint8_t chip_id = luaL_checkinteger(L, 2);
  38. uint8_t reg = luaL_checkinteger(L, 3);
  39. uint8_t data = 0;
  40. uint8_t is_read = 1;
  41. if (!lua_isnone(L, 4))
  42. {
  43. is_read = 0;
  44. data = luaL_checkinteger(L, 4);
  45. }
  46. if(luat_gpio_driver_yhm27xx(pin, chip_id, reg, is_read, &data))
  47. {
  48. lua_pushboolean(L, 0);
  49. return 1;
  50. }
  51. lua_pushboolean(L, 1);
  52. if (is_read)
  53. {
  54. lua_pushinteger(L, data);
  55. return 2;
  56. }
  57. return 1;
  58. }
  59. #include "rotable2.h"
  60. static const rotable_Reg_t reg_yhm27xx[] = {
  61. {"cmd", ROREG_FUNC(l_yhm27xx_cmd)},
  62. {NULL, ROREG_INT(0)}
  63. };
  64. LUAMOD_API int luaopen_yhm27xx(lua_State *L)
  65. {
  66. luat_newlib2(L, reg_yhm27xx);
  67. return 1;
  68. }