第9章:小小按键在我手,我的触碰你知道

SAKSHAT提供的按键使用方法

当前SAKSHAT为我们提供的方法参考:http://wiki.nxez.com/saks:sdk:libraries:tactrow

这里就直接摘录了,以下是创百科的内容:

TactRow
轻触开关组类,配置一组轻触开关的触发事件并随时读取开关即时状态。 方法

init(self, pin, real_true=GPIO.HIGH)

初始化对象,设置引脚和触发电平(高电平或低电平触发)。pins 为 IO 引脚数组。

is_on(self, index)

返回当前轻触开关的按下状态。true 或 false。index 为 LED 编号,从 0 开始。

row_status(self)

返回当前轻触开关组的工作状态的数组。

items(self)

返回当前轻触开关组的对象数组。
对于按键可以安装按键触发时的回调函数,回调函数复制给tact_event_handler方法


#输出按键状态
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        while True:
            #获取按键状态
            blue_button = SAKS.tactrow.is_on(0)
            yello_button = SAKS.tactrow.is_on(1)
             
            if blue_button == False:
                print("蓝色未按下")
            else:
                print("蓝色已按下")
                 
            if yello_button == False:
                print("黄色未按下")
            else:
                print("黄色已按下")
 
            time.sleep(1)
             
    except KeyboardInterrupt:
        print("任务被终止了")
#输出按键组状态
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        while True:
            #获取按键组状态
            key_status = SAKS.tactrow.row_status
             
            #打印安装状态
            print("按键组状态为" + str(key_status))
 
            time.sleep(1)
             
    except KeyboardInterrupt:
        print("任务被终止了")
#按键事件触发方式操作
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
#在检测到轻触开关触发时自动执行此函数
def key_event_handler(pin, status):
    '''
    called while the status of tacts changed
    :param pin: pin number which stauts of tact is changed
    :param status: current status
    :return: void
    '''
     
    #判断是否是右边的轻触开关被触发,并且是在被按下
    if pin == PINS.TACT_RIGHT and status == True:   
        print("黄色按键被按下")
 
    #判断是否是右边的轻触开关被触发,并且是在被按下
    if pin == PINS.TACT_RIGHT and status == False:   
        print("黄色按键被释放")
         
    #判断是否是右边的轻触开关被触发,并且是在被按下
    if pin == PINS.TACT_LEFT and status == True:   
        print("蓝色按键被按下")
 
    #判断是否是右边的轻触开关被触发,并且是在被按下
    if pin == PINS.TACT_LEFT and status == False:   
        print("蓝色按键被释放")
 
if __name__ == "__main__":
    try:
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        #设定轻触开关回调函数
        SAKS.tact_event_handler = key_event_handler
             
    except KeyboardInterrupt:
        print("任务被终止了")


课程 bilibili 视频地址:https://www.bilibili.com/video/av71878718/?p=20

 


评论:
相关文章
树莓派Build HAT

Raspberry Pi Build HAT 是一个扩展板,可连接到 Raspberry Pi 的 40 针 GPIO 接头,该接头是与 LEGO® Education 合作设计的,旨在使用 Raspberry Pi 计算机轻松控制 LEGO® Technic™ 电机和传感器。


树莓派超算和科学计算教程(全)

我们将用 Raspberry Pi 开始我们探索超级计算和科学编程科学领域的激动人心的旅程。


全球首款桌面级树莓派双轮足机器人问世!哈工大创业团队出品

近日,全球规模最大、最具影响力的众筹平台Kickstarter就发售了这样一种全新形态的桌宠机器人——全球首款桌面级树莓派双轮足机器人XGO-Rider。


树莓派如何设计3D打印外壳

这篇文章将为大家讲解有关树莓派如何设计3D打印外壳。


树莓派 5 发布,新特性细节公布

树莓派5 具有一些新特性,速度是树莓派 4B 的两倍多,并且是英国剑桥第一台自主设计芯片的树莓派计算机。

动手学树莓派 Python 篇

作者:大飞品树莓   共13讲

本课程使用树莓派配合 NXEZ 树莓派瑞士军刀扩展板卡,进行动手学树莓派。教程和教程示例源码均托管在 git 上,同时正在不断完善和更新,欢迎各位一线教师在自己的授课中参考。