| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /*
- @module ht1621
- @summary 液晶屏驱动(HT1621/HT1621B)
- @version 1.0
- @author wendal
- @date 2024.04.15
- @tag LUAT_USE_GPIO
- @usage
- -- 需要接3个GPIO引脚, 然后给ht1621接好供电
- -- 假设 CS脚接 模块的 GPIO4
- -- 假设 DATA脚接 模块的 GPIO5
- -- 假设 WR脚接 模块的 GPIO3
- local seg = ht1621.setup(4, 5, 3)
- ht1621.lcd(seg, true) -- 背光亮起
- ht1621.data(seg, 0, 0xeb) -- 位置0显示数字1
- */
- #include "luat_base.h"
- #include "luat_ht1621.h"
- #include "luat_gpio.h"
- /*
- 初始化ht1621
- @api ht1621.setup(pin_cs, pin_data, pin_wr, cmd_com_mode, cmd_rc, cmd_sysen)
- @int 片选引脚, 填模块的GPIO编码
- @int 数据引脚, 填模块的GPIO编码
- @int WR引脚, 填模块的GPIO编码
- @int 命令模式, 默认是0x52
- @int 内部RC振荡器,默认0x30
- @int 系统振荡器开,默认0x02
- @return userdata 返回ht1621对象
- @usage
- local seg = ht1621.setup(4, 5, 3)
- ht1621.data(seg, 0, 0xeb)
- */
- static int l_ht1621_setup(lua_State *L) {
- int pin_cs = luaL_checkinteger(L, 1);
- int pin_data = luaL_checkinteger(L, 2);
- int pin_wr = luaL_checkinteger(L, 3);
- luat_ht1621_conf_t* conf = lua_newuserdata(L, sizeof(luat_ht1621_conf_t));
- conf->pin_cs = pin_cs;
- conf->pin_data = pin_data;
- conf->pin_wr = pin_wr;
- if (lua_isinteger(L, 4)) {
- conf->cmd_com_mode = luaL_checkinteger(L, 4);
- }
- else {
- conf->cmd_com_mode = ComMode;
- }
-
- if (lua_isinteger(L, 5)) {
- conf->cmd_rc = luaL_checkinteger(L, 5);
- }
- else {
- conf->cmd_rc = RCosc;
- }
- if (lua_isinteger(L, 6)) {
- conf->cmd_sysen = luaL_checkinteger(L, 6);
- }
- else {
- conf->cmd_sysen = Sys_en;
- }
- luat_ht1621_init(conf);
- return 1;
- }
- /*
- LCD开关
- @api ht1621.lcd(seg, onoff)
- @userdata ht1621.setup返回的ht1621对象
- @boolean true开,false关
- @return nil 无返回值
- @usage
- local seg = ht1621.setup(4, 5, 3)
- ht1621.lcd(seg, true)
- */
- static int l_ht1621_lcd(lua_State *L) {
- luat_ht1621_conf_t* conf = lua_touserdata(L, 1);
- if (conf == NULL) return 0;
- int onoff = lua_toboolean(L, 2);
- luat_ht1621_lcd(conf, onoff);
- return 0;
- }
- /*
- 展示数据
- @api ht1621.data(seg, addr, sdat)
- @userdata ht1621.setup返回的ht1621对象
- @int 地址, 0-6, 超过6无效
- @int 数据, 0-255
- @return nil 无返回值
- @usage
- local seg = ht1621.setup(4, 5, 3)
- ht1621.lcd(seg, true)
- ht1621.data(seg, 0, 0xF1)
- -- 附数字0-9的值表
- -- 0,1,2,3,4,5,6,7,8,9
- -- 0xeb,0x0a,0xad,0x8f,0x4e,0xc7,0xe7,0x8a,0xef,0xcf
- */
- static int l_ht1621_data(lua_State *L) {
- luat_ht1621_conf_t* conf = lua_touserdata(L, 1);
- if (conf == NULL) return 0;
- int addr = luaL_checkinteger(L, 2);
- int sdat = luaL_checkinteger(L, 3);
- luat_ht1621_write_data(conf, addr, sdat);
- return 0;
- }
- /*
- 发送指令
- @api ht1621.cmd(seg, cmd)
- @userdata ht1621.setup返回的ht1621对象
- @int 指令, 0-255
- @return nil 无返回值
- @usage
- -- 具体指令请查阅硬件手册
- */
- static int l_ht1621_cmd(lua_State *L) {
- luat_ht1621_conf_t* conf = lua_touserdata(L, 1);
- if (conf == NULL) return 0;
- int cmd = luaL_checkinteger(L, 2);
- luat_ht1621_write_cmd(conf, cmd);
- return 0;
- }
- #include "rotable2.h"
- static const rotable_Reg_t reg_ht1621[] =
- {
- { "setup" , ROREG_FUNC(l_ht1621_setup)},
- { "lcd" , ROREG_FUNC(l_ht1621_lcd)},
- { "data" , ROREG_FUNC(l_ht1621_data)},
- { "cmd" , ROREG_FUNC(l_ht1621_cmd)},
- { NULL, ROREG_INT(0)}
- };
- LUAMOD_API int luaopen_ht1621( lua_State *L ) {
- luat_newlib2(L, reg_ht1621);
- return 1;
- }
|