luat_lib_ht1621.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. @module ht1621
  3. @summary 液晶屏驱动(HT1621/HT1621B)
  4. @version 1.0
  5. @author wendal
  6. @date 2024.04.15
  7. @tag LUAT_USE_GPIO
  8. @usage
  9. -- 需要接3个GPIO引脚, 然后给ht1621接好供电
  10. -- 假设 CS脚接 模块的 GPIO4
  11. -- 假设 DATA脚接 模块的 GPIO5
  12. -- 假设 WR脚接 模块的 GPIO3
  13. local seg = ht1621.setup(4, 5, 3)
  14. ht1621.lcd(seg, true) -- 背光亮起
  15. ht1621.data(seg, 0, 0xeb) -- 位置0显示数字1
  16. */
  17. #include "luat_base.h"
  18. #include "luat_ht1621.h"
  19. #include "luat_gpio.h"
  20. /*
  21. 初始化ht1621
  22. @api ht1621.setup(pin_cs, pin_data, pin_wr, cmd_com_mode, cmd_rc, cmd_sysen)
  23. @int 片选引脚, 填模块的GPIO编码
  24. @int 数据引脚, 填模块的GPIO编码
  25. @int WR引脚, 填模块的GPIO编码
  26. @int 命令模式, 默认是0x52
  27. @int 内部RC振荡器,默认0x30
  28. @int 系统振荡器开,默认0x02
  29. @return userdata 返回ht1621对象
  30. @usage
  31. local seg = ht1621.setup(4, 5, 3)
  32. ht1621.data(seg, 0, 0xeb)
  33. */
  34. static int l_ht1621_setup(lua_State *L) {
  35. int pin_cs = luaL_checkinteger(L, 1);
  36. int pin_data = luaL_checkinteger(L, 2);
  37. int pin_wr = luaL_checkinteger(L, 3);
  38. luat_ht1621_conf_t* conf = lua_newuserdata(L, sizeof(luat_ht1621_conf_t));
  39. conf->pin_cs = pin_cs;
  40. conf->pin_data = pin_data;
  41. conf->pin_wr = pin_wr;
  42. if (lua_isinteger(L, 4)) {
  43. conf->cmd_com_mode = luaL_checkinteger(L, 4);
  44. }
  45. else {
  46. conf->cmd_com_mode = ComMode;
  47. }
  48. if (lua_isinteger(L, 5)) {
  49. conf->cmd_rc = luaL_checkinteger(L, 5);
  50. }
  51. else {
  52. conf->cmd_rc = RCosc;
  53. }
  54. if (lua_isinteger(L, 6)) {
  55. conf->cmd_sysen = luaL_checkinteger(L, 6);
  56. }
  57. else {
  58. conf->cmd_sysen = Sys_en;
  59. }
  60. luat_ht1621_init(conf);
  61. return 1;
  62. }
  63. /*
  64. LCD开关
  65. @api ht1621.lcd(seg, onoff)
  66. @userdata ht1621.setup返回的ht1621对象
  67. @boolean true开,false关
  68. @return nil 无返回值
  69. @usage
  70. local seg = ht1621.setup(4, 5, 3)
  71. ht1621.lcd(seg, true)
  72. */
  73. static int l_ht1621_lcd(lua_State *L) {
  74. luat_ht1621_conf_t* conf = lua_touserdata(L, 1);
  75. if (conf == NULL) return 0;
  76. int onoff = lua_toboolean(L, 2);
  77. luat_ht1621_lcd(conf, onoff);
  78. return 0;
  79. }
  80. /*
  81. 展示数据
  82. @api ht1621.data(seg, addr, sdat)
  83. @userdata ht1621.setup返回的ht1621对象
  84. @int 地址, 0-6, 超过6无效
  85. @int 数据, 0-255
  86. @return nil 无返回值
  87. @usage
  88. local seg = ht1621.setup(4, 5, 3)
  89. ht1621.lcd(seg, true)
  90. ht1621.data(seg, 0, 0xF1)
  91. -- 附数字0-9的值表
  92. -- 0,1,2,3,4,5,6,7,8,9
  93. -- 0xeb,0x0a,0xad,0x8f,0x4e,0xc7,0xe7,0x8a,0xef,0xcf
  94. */
  95. static int l_ht1621_data(lua_State *L) {
  96. luat_ht1621_conf_t* conf = lua_touserdata(L, 1);
  97. if (conf == NULL) return 0;
  98. int addr = luaL_checkinteger(L, 2);
  99. int sdat = luaL_checkinteger(L, 3);
  100. luat_ht1621_write_data(conf, addr, sdat);
  101. return 0;
  102. }
  103. /*
  104. 发送指令
  105. @api ht1621.cmd(seg, cmd)
  106. @userdata ht1621.setup返回的ht1621对象
  107. @int 指令, 0-255
  108. @return nil 无返回值
  109. @usage
  110. -- 具体指令请查阅硬件手册
  111. */
  112. static int l_ht1621_cmd(lua_State *L) {
  113. luat_ht1621_conf_t* conf = lua_touserdata(L, 1);
  114. if (conf == NULL) return 0;
  115. int cmd = luaL_checkinteger(L, 2);
  116. luat_ht1621_write_cmd(conf, cmd);
  117. return 0;
  118. }
  119. #include "rotable2.h"
  120. static const rotable_Reg_t reg_ht1621[] =
  121. {
  122. { "setup" , ROREG_FUNC(l_ht1621_setup)},
  123. { "lcd" , ROREG_FUNC(l_ht1621_lcd)},
  124. { "data" , ROREG_FUNC(l_ht1621_data)},
  125. { "cmd" , ROREG_FUNC(l_ht1621_cmd)},
  126. { NULL, ROREG_INT(0)}
  127. };
  128. LUAMOD_API int luaopen_ht1621( lua_State *L ) {
  129. luat_newlib2(L, reg_ht1621);
  130. return 1;
  131. }