目录
本文整理了之前Air101-LCD扩展板按键的测试程序,写成模块方便以后直接拿来使用。
Air101-LCD扩展板按键测试程序_Denuin的博客-优快云博客_air101开发板
keyscan.lua
--- 模块功能:Air101-LCD扩展板按键程序
-- @module keyscan
-- @author Denuin
-- @release 2022.03.19
local keyscan = {}
local updateflag = 0
local laststate = 0
local holdcnt = 0
-- 默认:ESP32-C3 板子的gpio引脚号,可通过keyscan.gpio来修改
local pin_key_up = 8 -- LCD_UPKEY
local pin_key_down = 13 -- LCD_RKEY
local pin_key_left = 5 -- LCD_DWKEY
local pin_key_right = 9 -- LCD_LKEY
local pin_key_center = 4 -- LCD_CENTER
local pass_left_fun = nil
local pass_up_fun = nil
local pass_right_fun = nil
local pass_down_fun = nil
local pass_center_fun = nil
local release_left_fun = nil
local release_up_fun = nil
local release_right_fun = nil
local release_down_fun = nil
local release_center_fun = nil
local hold_left_fun = nil
local hold_up_fun = nil
local hold_right_fun = nil
local hold_down_fun = nil
local hold_center_fun = nil
local function key_pass_action(flag)
if flag & 0x0001 == 0x0001 then
if pass_up_fun ~= nil then pass_up_fun() end
end
if flag & 0x0002 == 0x0002 then
if pass_down_fun ~= nil then pass_down_fun() end
end
if flag & 0x0004 == 0x0004 then
if pass_left_fun ~= nil then pass_left_fun() end
end
if flag & 0x0008 == 0x0008 then
if pass_right_fun ~= nil then pass_right_fun() end
end
if flag & 0x0010 == 0x0010 then
if pass_center_fun ~= nil then pass_center_fun() end
end
end
local function key_hold_action(flag)
if flag & 0x0001 == 0x0001 then
if hold_up_fun ~= nil then hold_up_fun() end
end
if flag & 0x0002 == 0x0002 then
if hold_down_fun ~= nil then hold_down_fun() end
end
if flag & 0x0004 == 0x0004 then
if hold_left_fun ~= nil then hold_left_fun() end
end
if flag & 0x0008 == 0x0008 then
if hold_right_fun ~= nil then hold_right_fun() end
end
if flag & 0x0010 == 0x0010 then
if hold_center_fun ~= nil then hold_center_fun() end
end
end
local function key_release_action(flag)
if flag & 0x0001 == 0x0001 then
if release_up_fun ~= nil then release_up_fun() end
end
if flag & 0x0002 == 0x0002 then
if release_down_fun ~= nil then release_down_fun() end
end
if flag & 0x0004 == 0x0004 then
if release_left_fun ~= nil then release_left_fun() end
end
if flag & 0x0008 == 0x0008 then
if release_right_fun ~= nil then release_right_fun() end
end
if flag & 0x0010 == 0x0010 then
if release_center_fun ~= nil then release_center_fun() end
end
end
function keyscan.gpio(pin_left, pin_up, pin_right, pin_down, pin_center)
pin_key_up = pin_up
pin_key_down = pin_down
pin_key_left = pin_left
pin_key_right = pin_right
pin_key_center = pin_center
end
function keyscan.pass(...)
local arg = {...}
for i, v in ipairs(arg) do
if i == 1 then pass_left_fun = v end
if i == 2 then pass_up_fun = v end
if i == 3 then pass_right_fun = v end
if i == 4 then pass_down_fun = v end
if i == 5 then pass_center_fun = v end
end
end
function keyscan.release(...)
local arg = {...}
for i, v in ipairs(arg) do
if i == 1 then release_left_fun = v end
if i == 2 then release_up_fun = v end
if i == 3 then release_right_fun = v end
if i == 4 then release_down_fun = v end
if i == 5 then release_center_fun = v end
end
end
function keyscan.hold(...)
local arg = {...}
for i, v in ipairs(arg) do
if i == 1 then hold_left_fun = v end
if i == 2 then hold_up_fun = v end
if i == 3 then hold_right_fun = v end
if i == 4 then hold_down_fun = v end
if i == 5 then hold_center_fun = v end
end
end
function keyscan.setup()
gpio.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)
end
-- 20ms进行一次扫描
function keyscan.run()
local state = 0
if gpio.get(pin_key_up) == 0 then state = state | 0x0001 end
if gpio.get(pin_key_down) == 0 then state = state | 0x0002 end
if gpio.get(pin_key_left) == 0 then state = state | 0x0004 end
if gpio.get(pin_key_right) == 0 then state = state | 0x0008 end
if gpio.get(pin_key_center) == 0 then state = state | 0x0010 end
if state == 0 then
holdcnt = 0
if laststate ~= 0 then
key_release_action(laststate)
laststate = 0
end
else
if laststate == 0 then key_pass_action(state) end
holdcnt = holdcnt + 1
if holdcnt > 50 then holdcnt = 45 end
-- 长按
if state ~= laststate then holdcnt = 0 end
if holdcnt == 49 then key_hold_action(state) end
laststate = state
end
end
return keyscan
使用方法
-- (其它代码略)
local keyscan = require("keyscan")
-- 引脚设置
-- 按键顺序:key_left, key_up, key_right, key_down, key_center
keyscan.gpio(5, 8, 9, 13, 4) -- ESP32-C3 板子的gpio引脚号
keyscan.setup() -- 初始化gpio引脚
-- 单击操作
-- 按键顺序:key_left, key_up, key_right, key_down, key_center
keyscan.pass(function() log.info("keyscan.pass", "left") end,
function() log.info("keyscan.pass", "up") end,
function() log.info("keyscan.pass", "right") end,
function() log.info("keyscan.pass", "down") end,
function() log.info("keyscan.pass", "center") end)
-- 松开操作
-- 按键顺序:key_left, key_up, key_right, key_down, key_center
keyscan.release(function() log.info("keyscan.release", "left") end,
function() log.info("keyscan.release", "up") end,
function() log.info("keyscan.release", "right") end,
function() log.info("keyscan.release", "down") end,
function() log.info("keyscan.release", "center") end)
-- 长按操作
-- 按键顺序:key_left, key_up, key_right, key_down, key_center
keyscan.hold(function() log.info("keyscan.hold", "left") end,
function() log.info("keyscan.hold", "up") end,
function() log.info("keyscan.hold", "right") end,
function() log.info("keyscan.hold", "down") end,
function() log.info("keyscan.hold", "center") end)
-- 按键扫描(20ms扫描一次)
sys.taskInit(function()
while 1 do
keyscan.run()
sys.wait(20)
end
end)
-- (其它代码略)
参考: