动手学树莓派第9章:小小按键在我手,我的触碰你知道

SAKSHAT提供的按键使用方法

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

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

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

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

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

1
is_on(self, index)

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

1
row_status(self)

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

1
items(self)

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#输出按键状态
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("任务被终止了")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#输出按键组状态
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("任务被终止了")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#按键事件触发方式操作
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

返回课程目录

课程 gitee 地址:https://gitee.com/shirf_taste_raspi/shirf_serial_share