Air101-LCD扩展板按键测试程序

 

目录

一、接口连接

 二、测试程序

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 = 4

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)

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

local x = 50
local y = 50
local updateflag = 0
local laststate = 0
local holdcnt = 0

local 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 = 0
    if flag & 0x0001 == 0x0001 then
        log.info("state", "Pass 上")
        XYOffsetUp(1)
        action = 1
    end
    if flag & 0x0002 == 0x0002 then
        log.info("state", "Pass 下")
        XYOffsetDown(1)
        action = 2
    end 
    if flag & 0x0004 == 0x0004 then
        log.info("state", "Pass 左")
        XYOffsetLeft(1)
        action = 3
    end
    if flag & 0x0008 == 0x0008 then
        log.info("state", "Pass 右")
        XYOffsetRight(1)
        action = 4
    end
    if flag & 0x0010 == 0x0010 then
        x = 50
        y = 50
        log.info("state", "Pass 中")
        lcd.clear()
        --显示demo
        lcd.drawLine(20,20,150,20,0x001F)
        lcd.drawRectangle(20,40,120,70,0xF800)
        lcd.drawCircle(50,50,20,0x0CE0)
        lcd.drawStr(20,20,"LuarOS!!")
    end

    if action > 0 then
        lcd.drawCircle(x,y,1,0x0CE0)
    end
end

5、长按操作函数

local function KeyPassHold(flag)
    local action = 0
    if flag & 0x0001 == 0x0001 then
        action = 1
        XYOffsetUp(1)
        log.info("state", "Hold 上")
    end
    if flag & 0x0002 == 0x0002 then
        action = 2
        XYOffsetDown(1)
        log.info("state", "Hold 下")
    end 
    if flag & 0x0004 == 0x0004 then
        action = 3
        XYOffsetLeft(1)
        log.info("state", "Hold 左")
    end
    if flag & 0x0008 == 0x0008 then
        action = 4
        XYOffsetRight(1)
        log.info("state", "Hold 右")
    end
    if flag & 0x0010 == 0x0010 then
        log.info("state", "Hold 中")
    end

    --有按键动作
    if action > 0 then
        lcd.drawCircle(x,y,1,0x0CE0)
    end
end

6、松开操作函数

local function KeyRelease(flag)
    local action = 0
    if flag & 0x0001 == 0x0001 then
        action = 1
        log.info("state", "Release 上")
    end
    if flag & 0x0002 == 0x0002 then
        action = 2
        log.info("state", "Release 下")
    end 
    if flag & 0x0004 == 0x0004 then
        action = 3
        log.info("state", "Release 左")
    end
    if flag & 0x0008 == 0x0008 then
        action = 4
        log.info("state", "Release 右")
    end
    if flag & 0x0010 == 0x0010 then
        action = 5
        log.info("state", "Release 中")
    end

    --有按键动作
    if action > 0 then

    end
end

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

sys.taskInit(function()
    while 1 do
        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
                KeyRelease(laststate)
                laststate = 0
            end
        else
            if laststate==0 then
                KeyPass(state)
            end

            holdcnt = holdcnt + 1
            if holdcnt > 50 then
                holdcnt = 45
            end

            --长按
            if state ~= laststate then
                holdcnt=0
            end
            if holdcnt==49 then
                KeyPassHold(state)
            end

            laststate = state
        end
        sys.wait(20)
    end
end)

-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!

三、演示效果

Air101-LCD扩展板

版权声明:本文为CSDN博主「Denuin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Denuin/article/details/121738704

 

目录

一、接口连接

 二、测试程序

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 = 4

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)

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

local x = 50
local y = 50
local updateflag = 0
local laststate = 0
local holdcnt = 0

local 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 = 0
    if flag & 0x0001 == 0x0001 then
        log.info("state", "Pass 上")
        XYOffsetUp(1)
        action = 1
    end
    if flag & 0x0002 == 0x0002 then
        log.info("state", "Pass 下")
        XYOffsetDown(1)
        action = 2
    end 
    if flag & 0x0004 == 0x0004 then
        log.info("state", "Pass 左")
        XYOffsetLeft(1)
        action = 3
    end
    if flag & 0x0008 == 0x0008 then
        log.info("state", "Pass 右")
        XYOffsetRight(1)
        action = 4
    end
    if flag & 0x0010 == 0x0010 then
        x = 50
        y = 50
        log.info("state", "Pass 中")
        lcd.clear()
        --显示demo
        lcd.drawLine(20,20,150,20,0x001F)
        lcd.drawRectangle(20,40,120,70,0xF800)
        lcd.drawCircle(50,50,20,0x0CE0)
        lcd.drawStr(20,20,"LuarOS!!")
    end

    if action > 0 then
        lcd.drawCircle(x,y,1,0x0CE0)
    end
end

5、长按操作函数

local function KeyPassHold(flag)
    local action = 0
    if flag & 0x0001 == 0x0001 then
        action = 1
        XYOffsetUp(1)
        log.info("state", "Hold 上")
    end
    if flag & 0x0002 == 0x0002 then
        action = 2
        XYOffsetDown(1)
        log.info("state", "Hold 下")
    end 
    if flag & 0x0004 == 0x0004 then
        action = 3
        XYOffsetLeft(1)
        log.info("state", "Hold 左")
    end
    if flag & 0x0008 == 0x0008 then
        action = 4
        XYOffsetRight(1)
        log.info("state", "Hold 右")
    end
    if flag & 0x0010 == 0x0010 then
        log.info("state", "Hold 中")
    end

    --有按键动作
    if action > 0 then
        lcd.drawCircle(x,y,1,0x0CE0)
    end
end

6、松开操作函数

local function KeyRelease(flag)
    local action = 0
    if flag & 0x0001 == 0x0001 then
        action = 1
        log.info("state", "Release 上")
    end
    if flag & 0x0002 == 0x0002 then
        action = 2
        log.info("state", "Release 下")
    end 
    if flag & 0x0004 == 0x0004 then
        action = 3
        log.info("state", "Release 左")
    end
    if flag & 0x0008 == 0x0008 then
        action = 4
        log.info("state", "Release 右")
    end
    if flag & 0x0010 == 0x0010 then
        action = 5
        log.info("state", "Release 中")
    end

    --有按键动作
    if action > 0 then

    end
end

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

sys.taskInit(function()
    while 1 do
        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
                KeyRelease(laststate)
                laststate = 0
            end
        else
            if laststate==0 then
                KeyPass(state)
            end

            holdcnt = holdcnt + 1
            if holdcnt > 50 then
                holdcnt = 45
            end

            --长按
            if state ~= laststate then
                holdcnt=0
            end
            if holdcnt==49 then
                KeyPassHold(state)
            end

            laststate = state
        end
        sys.wait(20)
    end
end)

-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!

三、演示效果

Air101-LCD扩展板

版权声明:本文为CSDN博主「Denuin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Denuin/article/details/121738704

生成海报
点赞 0

Denuin

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

基于8051单片机实现电子时钟+数字秒表设计

概述 电子时钟是一种利用数字电路来显示秒、分、时的计时装置,与传统的机械钟相比,它具有走时准确、显 示直观、无机械传动装置等优点,因而得到广泛应用。随着人们生活环境的不断改善和美化,在许

74HC138译码器的原理和使用

前言 译码器就是将每个输入的二进制代码译成对应的输出高低电平信号,和编码器互为逆过程。 百度百科 74HC138是一款高速CMOS器件,74HC138引脚兼容低功耗肖特基TTL(LSTTL&#xf