作者:絮尘飘雪_896 | 来源:互联网 | 2023-08-22 02:27
目录一、接口连接二、测试程序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_00 | BL | 16 | |
PB_04 | CS | 20 | |
PB_01 | DC | 17 | |
PB_03 | RES | 19 | |
PB_05 | SDA | 21 | |
PB_02 | SCL | 18 | |
PA_00 | LKEY | 0 | 代码里pin_key_right |
PA_07 | UPKEY | 7 | 代码里pin_key_up |
PA_04 | CENTER | 4 | 代码里pin_key_center |
PA_01 | DWKEY | 1 | 代码里pin_key_left |
PB_11 | RKEY | 27 | 代码里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()之后后面不要加任何语句!!!!!
三、演示效果