动手学树莓派Python篇第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

- 本文内容来自网络,如有侵权,请联系本站处理。

2022-12   阅读(11)   评论(0)
 标签: 编程

涨知识
伺服电机

伺服电机(servo motor )是指在伺服系统中控制机械元件运转的发动机,是一种补助马达间接变速装置。

评论:
相关文章
Blockly开始使用

本文档面向希望创建自己的应用程序的开发人员,这些应用程序将Blockly集成为代码编辑器。


App Inventor 编程实例及指南-第8章 总统测验

“总统测验”是一个关于美国前总统的问答游戏。虽然测验的内容与总统有关,但你可以把它当作模板,来实现对任何题目的测验。


App Inventor 编程实例及指南-第11章 广播中心

本章将创建一个与FrontlineSMS功能类似的广播中心,不过是运行在Android手机上。


Scratch积木指令详解

由于Scratch软件界面显示和国内等级考试大纲用词不同,部分名词出现了两种称呼。例如指令模块(积木)、程序区(代码区),其实表达的是一个意思。


App Inventor 编程实例及指南-第5章 飘虫快跑

游戏是移动应用中最令人兴奋的部分,无论是玩游戏,还是做游戏。



PIL(Python Imagimg Library)简明教程

Python图像处理库,该库支持多种文件格式,提供强大的图像处理功能。


machine.UART 类——双工串行通信总线

UART 实现标准的 UART/USART 双工串行通信协议。在物理层,它由 2 条线组成:RX 和 TX。通信单位是一个字符(不要与字符串字符混淆),它可以是 8 位或 9 位宽。