-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·184 lines (151 loc) · 6 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python3
from board import SCL, SDA
import busio
import time
from gpiozero import Button, LED
from big_red_button import big_red_button
import argparse
from adafruit_pca9685 import PCA9685
import json
import sh
import atexit
import signal
###############################
# #
# Attach to PCA9685 Controller#
# #
###############################
# Create the I2C bus interface.
i2c_bus = busio.I2C(SCL, SDA)
# Create PCA9685 class instance.
pca = PCA9685(i2c_bus)
# Set the PWM frequency to 60hz.
pca.frequency = 60
processKeeper = {}
thread = None
###############################
# #
# Animations #
# #
###############################
stopLightAnimationPattern = [ [[ 0, 0], [ 1, 1]], [[0.5, 0], [0.2,0.2]], [[ 1,0.5], [ 1, 1]], [[ 1, 1], [0.2,0.2]], [[0.5, 1], [ 1, 1]], [[ 0,0.5], [0.2,0.2]] ]
#Old High-Low Animation
#highLightAnimationPattern = [ [[0.5, 1], [0.5, 1]], [[ 1,0.5], [0.5, 1]], [[0.5, 1], [ 1,0.5]], [[ 1,0.5], [ 1,0.5]]]
animationList = []
for counter in range(1,11):
firstNumber = counter * 0.2
secondNumber = counter * 0.1
if firstNumber > 1: firstNumber = firstNumber - 1
animationList.append([[firstNumber,firstNumber],[secondNumber,secondNumber]])
highLightAnimationPattern = animationList
###############################
# #
# AssignLights and Buttons #
# #
###############################
smallRedButton = Button(pin=25)
keySwitch = [Button(pin=23),Button(pin=24)]
bigRedButton = Button(pin=18)
ledArray = []
ledArray.append(LED(5))
ledArray.append(LED(6))
ledArray.append(LED(13))
ledArray.append(LED(19))
ledArray.append(LED(26))
lightButtons = {
'leftHighButton' : big_red_button([[pca.channels[12],pca.channels[13]],[pca.channels[14],pca.channels[15]]],Button(pin=12), highLightAnimationPattern, 'leftHighButton'),
'leftRunButton' : big_red_button([[pca.channels[8],pca.channels[9]],[pca.channels[10],pca.channels[11]]],Button(pin=16), stopLightAnimationPattern, 'leftRunButton'),
'rightHighButton' : big_red_button([[pca.channels[4],pca.channels[5]],[pca.channels[6],pca.channels[7]]],Button(pin=20), highLightAnimationPattern, 'rightHighButton'),
'rightRunButton' : big_red_button([[pca.channels[0],pca.channels[1]],[pca.channels[2],pca.channels[3]]],Button(pin=21), stopLightAnimationPattern, 'rightRunButton')
}
###############################
# #
# Command Line Arguments #
# #
###############################
parser = argparse.ArgumentParser(description='Set Parameters for Runing lights')
parser.add_argument('--ButtonToRun', choices=['leftHighButton','leftRunButton','rightHighButton', 'rightRunButton'], default=None,required=False, help='Which Button to Run')
parser.add_argument('--AnimationPattern', type=str, default=None,required=False, help='Animation Pattern to run on button')
args = parser.parse_args()
###############################
# #
# Functions #
# #
###############################
def runButton(button, animation):
lightButtons[button].setAnimationPattern(animation)
lightButtons[button].setOutput()
thread = lightButtons[button].runLights()
def process_line(line, stdin, process):
values = line.split(",")
lightButtons[values[0]].setToggle(bool(values[1]))
###############################
# #
# Self Test Integration #
# #
###############################
def testLights(lights, lightBank):
lightArray = list(lights.keys())
lightsBlank = [0,0]
lightsFirst = [0.5,0]
lightsSecond = [1,0.5]
lightsFull = [1,1]
#Blank the lights
blankLights(lights,lightBank)
#Crawl the top
for light in lightArray:
lights[light].setLights([lightsFirst,lightsBlank])
time.sleep(0.2)
lights[light].setLights([lightsSecond,lightsBlank])
time.sleep(0.2)
lights[light].setLights([lightsFull,lightsBlank])
#Crawl the Bottom
for light in reversed(lightArray):
lights[light].setLights([lightsFull, list(reversed(lightsFirst))])
time.sleep(0.2)
lights[light].setLights([lightsFull, list(reversed(lightsSecond))])
time.sleep(0.2)
lights[light].setLights([lightsFull, list(reversed(lightsFull))])
for light in lightBank:
light.on()
time.sleep(1)
light.off()
return True
def blankLights(lights, lightBank):
lightArray = list(lights.keys())
lightsBlank = [0,0]
for light in lightArray:
lights[light].setLights([lightsBlank,lightsBlank])
###############################
# #
# Termination Routines #
# #
###############################
def killSubprocesses():
for key in processKeeper:
processKeeper[key].kill()
blankLights(lightButtons, ledArray)
def killThreads(signal = None, other = None):
for button in lightButtons:
lightButtons[button].end()
killSubprocesses()
#Catch attempts to terminate so we can kill our threads and Subprocesses
signal.signal(signal.SIGINT, killThreads)
signal.signal(signal.SIGTERM, killThreads)
atexit.register(killSubprocesses)
###############################
# #
# Main #
# #
###############################
def main():
testLights(lightButtons, ledArray)
for key in lightButtons:
processKeeper[key] = sh.python3("main.py","--ButtonToRun", key ,"--AnimationPattern" ,json.dumps(lightButtons[key].getAnimationPattern()), _out=process_line, _bg=True)
processKeeper['leftHighButton'].wait()
if __name__ == "__main__":
if args.ButtonToRun == None:
main()
else:
animationPattern = json.loads(args.AnimationPattern)
runButton(args.ButtonToRun, animationPattern)