-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit.py
115 lines (97 loc) · 2.21 KB
/
circuit.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import CHIP_IO.GPIO as GPIO
from time import sleep
# --- leds --- #
led_init = False
def __led_init():
global led_init
if not led_init:
GPIO.setup("XIO-P0", GPIO.OUT)
GPIO.setup("XIO-P2", GPIO.OUT)
GPIO.setup("XIO-P4", GPIO.OUT)
GPIO.output("XIO-P0", GPIO.LOW)
GPIO.output("XIO-P2", GPIO.LOW)
GPIO.output("XIO-P4", GPIO.LOW)
led_init = True
def __led(led, state):
# led
if led == "green":
led = "XIO-P0"
elif led == "yellow":
led = "XIO-P2"
elif led == "orange":
led = "XIO-P4"
elif led == "all":
__led("green", state)
__led("yellow", state)
__led("orange", state)
return True
else:
return False
# state
if state == "off":
state = GPIO.LOW
elif state == "on":
state = GPIO.HIGH
else:
return False
# execute
return GPIO.output(led, state)
def led_off(led):
__led_init()
return __led(led, "off")
def led_on(led):
__led_init()
return __led(led, "on")
# --- buttons --- #
button_init = False
def __button_init():
global button_init
if not button_init:
GPIO.setup("XIO-P6", GPIO.IN)
GPIO.setup("XIO-P7", GPIO.IN)
button_init = True
def button(button):
__button_init()
if button == "one":
button = "XIO-P6"
elif button == "two":
button = "XIO-P7"
else:
return False
return GPIO.input(button)
# --- general functions --- #
def cleanup():
GPIO.cleanup()
def test():
print "led test"
for i in range(3):
led_on("green")
sleep(0.5)
led_on("yellow")
sleep(0.5)
led_on("orange")
sleep(0.5)
led_off("green")
sleep(0.5)
led_off("yellow")
sleep(0.5)
led_off("orange")
sleep(0.5)
print "press button 1"
while True:
if button("one"):
print "perfect!"
break
print "press button 2"
while True:
if button("two"):
print "perfect!"
break
sleep(0.25)
print "all tests done"
# --- main --- #
if __name__ == '__main__':
print "test"
test()
print "cleanup"
cleanup()