热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Air101LCD扩展板按键测试程序

目录一、接口连接二、测试程序1、LCD显示demo(官方demo)2、按键引脚定义3、变量及XY坐标偏移方法4、单击操作函数5、长按操作函数6、松开操作函数7、按键扫描

 

目录

一、接口连接

 二、测试程序

1、LCD显示demo (官方demo)

 2、按键引脚定义

3、变量及XY坐标偏移方法

4、单击操作函数

5、长按操作函数

6、松开操作函数

7、按键扫描(20ms扫描一次)

三、演示效果




一、接口连接


开发板引脚LCD / KEY 引脚GPIO编号备注
PB_00BL16
PB_04CS20
PB_01DC17
PB_03RES19
PB_05SDA21
PB_02SCL18
PA_00LKEY0代码里pin_key_right
PA_07UPKEY7代码里pin_key_up
PA_04CENTER4代码里pin_key_center
PA_01DWKEY1代码里pin_key_left
PB_11RKEY27代码里pin_key_down

 二、测试程序


1、LCD显示demo

log.info("lcd.init",
lcd.init("st7735s",{port = "device",pin_dc = 17, pin_pwr = 7,pin_rst = 19,direction = 2,w = 160,h = 80,xoffset = 1,yoffset = 26},spi_lcd))log.info("lcd.drawLine", lcd.drawLine(20,20,150,20,0x001F))
log.info("lcd.drawRectangle", lcd.drawRectangle(20,40,120,70,0xF800))
log.info("lcd.drawCircle", lcd.drawCircle(50,50,20,0x0CE0))--显示之前先设置为中文字体
lcd.setFont(lcd.font_opposansm12)
lcd.drawStr(20,20,"LuarOS!!")

 2、按键引脚定义

--按键引脚定义
local pin_key_up = 7
local pin_key_down = 27
local pin_key_left = 1
local pin_key_right = 0
local pin_key_center = 4gpio.setup(pin_key_up, nil, gpio.PULLUP)
gpio.setup(pin_key_down, nil, gpio.PULLUP)
gpio.setup(pin_key_left, nil, gpio.PULLUP)
gpio.setup(pin_key_right, nil, gpio.PULLUP)
gpio.setup(pin_key_center, nil, gpio.PULLUP)

3、变量及XY坐标偏移方法

local x = 50
local y = 50
local updateflag = 0
local laststate = 0
local holdcnt = 0local function XYOffsetUp(value)y = y - value
end
local function XYOffsetDown(value)y = y + value
end
local function XYOffsetLeft(value)x = x - value
end
local function XYOffsetRight(value)x = x + value
end

4、单击操作函数

local function KeyPass(flag)local action = 0if flag & 0x0001 == 0x0001 thenlog.info("state", "Pass 上")XYOffsetUp(1)action = 1endif flag & 0x0002 == 0x0002 thenlog.info("state", "Pass 下")XYOffsetDown(1)action = 2end if flag & 0x0004 == 0x0004 thenlog.info("state", "Pass 左")XYOffsetLeft(1)action = 3endif flag & 0x0008 == 0x0008 thenlog.info("state", "Pass 右")XYOffsetRight(1)action = 4endif flag & 0x0010 == 0x0010 thenx = 50y = 50log.info("state", "Pass 中")lcd.clear()--显示demolcd.drawLine(20,20,150,20,0x001F)lcd.drawRectangle(20,40,120,70,0xF800)lcd.drawCircle(50,50,20,0x0CE0)lcd.drawStr(20,20,"LuarOS!!")endif action > 0 thenlcd.drawCircle(x,y,1,0x0CE0)end
end

5、长按操作函数

local function KeyPassHold(flag)local action = 0if flag & 0x0001 == 0x0001 thenaction = 1XYOffsetUp(1)log.info("state", "Hold 上")endif flag & 0x0002 == 0x0002 thenaction = 2XYOffsetDown(1)log.info("state", "Hold 下")end if flag & 0x0004 == 0x0004 thenaction = 3XYOffsetLeft(1)log.info("state", "Hold 左")endif flag & 0x0008 == 0x0008 thenaction = 4XYOffsetRight(1)log.info("state", "Hold 右")endif flag & 0x0010 == 0x0010 thenlog.info("state", "Hold 中")end--有按键动作if action > 0 thenlcd.drawCircle(x,y,1,0x0CE0)end
end

6、松开操作函数

local function KeyRelease(flag)local action = 0if flag & 0x0001 == 0x0001 thenaction = 1log.info("state", "Release 上")endif flag & 0x0002 == 0x0002 thenaction = 2log.info("state", "Release 下")end if flag & 0x0004 == 0x0004 thenaction = 3log.info("state", "Release 左")endif flag & 0x0008 == 0x0008 thenaction = 4log.info("state", "Release 右")endif flag & 0x0010 == 0x0010 thenaction = 5log.info("state", "Release 中")end--有按键动作if action > 0 thenend
end

7、按键扫描(20ms扫描一次)

sys.taskInit(function()while 1 dolocal state = 0if gpio.get(pin_key_up)==0 thenstate = state | 0x0001endif gpio.get(pin_key_down)==0 thenstate = state | 0x0002endif gpio.get(pin_key_left)==0 thenstate = state | 0x0004endif gpio.get(pin_key_right)==0 thenstate = state | 0x0008endif gpio.get(pin_key_center)==0 thenstate = state | 0x0010endif state==0 thenholdcnt = 0if laststate ~= 0 thenKeyRelease(laststate)laststate = 0endelseif laststate==0 thenKeyPass(state)endholdcnt = holdcnt + 1if holdcnt > 50 thenholdcnt = 45end--长按if state ~= laststate thenholdcnt=0endif holdcnt==49 thenKeyPassHold(state)endlaststate = stateendsys.wait(20)end
end)-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!

三、演示效果


Air101-LCD扩展板



推荐阅读
author-avatar
絮尘飘雪_896
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有