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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
import time
#Declare the SAKS Board
SAKS = SAKSHAT()
#当前开关状态
__light_status = False
#在检测到拨码开关状态被修改时自动执行此函数
def dip_switch_status_changed_handler(status):
'''
called while the status of dip switch changed
:param status: current status
:return: void
'''
global __light_status
#在小灯状态开着时执行
if __light_status:
#拨码开关第1位状态为ON
if status[0]:
#点亮第3个LED
SAKS.ledrow.items[2].on()
else:
SAKS.ledrow.items[2].off()
#拨码开关第2位状态为ON
if status[1]:
#点亮第4个LED
SAKS.ledrow.items[3].on()
else:
SAKS.ledrow.items[3].off()
#print(status)
#在检测到轻触开关触发时自动执行此函数
def tact_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
'''
global __light_status
#判断是否是右边的轻触开关被触发,并且是在被按下
if pin == PINS.TACT_RIGHT and status == True:
#在小灯当前状态关着时将它们点亮并修改小灯当前状态为开; 在小灯当前状态开着时将它们灭掉并修改小灯当前状态为关
if not __light_status:
SAKS.ledrow.items[0].on()
SAKS.ledrow.items[1].on()
#检测第1位拨码开关状态是否为ON
if SAKS.dip_switch.is_on[0] == True:
#点亮第3个LED
SAKS.ledrow.items[2].on()
#检测第2位拨码开关状态是否为ON
if SAKS.dip_switch.is_on[1] == True:
#点亮第4个LED
SAKS.ledrow.items[3].on()
else:
SAKS.ledrow.items[0].off()
SAKS.ledrow.items[1].off()
SAKS.ledrow.items[2].off()
SAKS.ledrow.items[3].off()
__light_status = not __light_status
#print("%d - %s" % (pin, status))
if __name__ == "__main__":
#设定拨码开关回调函数
SAKS.dip_switch_status_changed_handler = dip_switch_status_changed_handler
#设定轻触开关回调函数
SAKS.tact_event_handler = tact_event_handler
input("Enter any keys to exit...")
|