-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitches_and_led.py
More file actions
executable file
·86 lines (71 loc) · 2.19 KB
/
Copy pathswitches_and_led.py
File metadata and controls
executable file
·86 lines (71 loc) · 2.19 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python3
import time
PUSHSW_PATH_LIST = [
'/dev/frootspi_pushsw0',
'/dev/frootspi_pushsw1',
'/dev/frootspi_pushsw2',
'/dev/frootspi_pushsw3'
]
DIPSW_PATH_LIST = [
'/dev/frootspi_dipsw0',
'/dev/frootspi_dipsw1',
]
LED_PATH = '/dev/frootspi_led0'
# 指定されたプッシュスイッチが押されたらTrue
def pushsw_has_pressed(path):
retval = False
with open(path, 'r') as f:
# 負論理回路のため、押されると0を返す
if f.readline() == "0\n":
retval = True
return retval
# プッシュスイッチのうち1つでも押されたらTrue
def any_pushsw_has_pressed():
for path in PUSHSW_PATH_LIST:
if pushsw_has_pressed(path):
return True
return False
# 指定されたDIPスイッチの状態を1/0で返す
def get_dipsw_state(path):
retval = 0
with open(path, 'r') as f:
# 負論理回路のため、ONになると0を返す
if f.readline() == "0\n":
retval = 1
return retval
# DIPスイッチの状態が変わったらTrue
g_dipsw_state = 0
def dipsw_state_has_changed():
global g_dipsw_state
current_dipsw_state = get_dipsw_state(DIPSW_PATH_LIST[0])
current_dipsw_state += 2 * get_dipsw_state(DIPSW_PATH_LIST[1])
if current_dipsw_state != g_dipsw_state:
g_dipsw_state = current_dipsw_state
return True
return False
# LEDを点灯・消灯する
def turn_on_led(turn_on = False):
with open(LED_PATH, 'w') as f:
if turn_on:
f.write('1')
print("LED ON")
else:
f.write('0')
print("LED OFF")
# LEDの点灯・消灯をトグルする
g_led_turn_on = False
def toggle_led():
global g_led_turn_on
g_led_turn_on = not g_led_turn_on
turn_on_led(g_led_turn_on)
### main ###
if __name__ == '__main__':
print("プッシュスイッチかDIPSWを押してね ([Ctrl-C]で終了)")
dipsw_state_has_changed() # DIPSW状態の初期化のため実行
while 1:
if any_pushsw_has_pressed():
toggle_led()
time.sleep(0.1)
if dipsw_state_has_changed():
toggle_led()
time.sleep(0.1)