-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
166 lines (130 loc) · 3.86 KB
/
controller.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
from programcontroller import *
from pixel import Pixel
import time
import random
from signal_strength import StrengthController
from multiprocessing import Process
from functools import wraps
import sys
# LED strip configuration:
LED_COUNT = 300 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 120# 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
PIXEL_AMOUNT = 10
class Controller():
def __init__(self):
self.strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
self.strip.begin()
self.program = Program(self.strip, PIXEL_AMOUNT)
self.currentProcess = None
'''Create pixels'''
'''195 leds in the 8 pixels'''
self.pixels = []
ind = 0
for p in range(0, PIXEL_AMOUNT):
if p == 0 or p == 7:
amount = 26
elif p == 8:
amount = 40
elif p == 9:
amount = 41
else:
amount = 27
pixel = Pixel(ind, amount)
ind = ind + amount
self.pixels.append(pixel)
# Strength
# strength = StrengthController(self.strip)
# sproc = multiprocessing.Process(target=strength.start)
# sproc.start()
def terminate(func):
@wraps(func)
def terminateCurrentProcess(self, *args, **kwargs):
if self.currentProcess:
if self.currentProcess.is_alive():
self.currentProcess.terminate()
return func(self, *args, **kwargs)
return terminateCurrentProcess
@terminate
def fftRunner(self):
def startFFT():
self.program.beatMatching(self.pixels)
self.currentProcess = Process(target=startFFT)
self.currentProcess.start()
@terminate
def runWormChase(self):
def chase():
while (True):
color = randomColor()
wormPixels = []
i = 0
while i < len(wormPixels):
wormPixels.append(self.pixels[i])
i = i + 2
i = 1
while i < len(wormPixels):
wormPixels.append(self.pixels[i])
i = i + 2
self.program.wormChase(color, 1000, 2, wormPixels)
self.currentProcess = Process(target=chase)
self.currentProcess.start()
@terminate
def runCheckerPattern(self):
def checker():
while(True):
color = randomColor()
self.program.checkerPattern(color, 300, self.pixels)
self.currentProcess = Process(target=checker)
self.currentProcess.start()
@terminate
def runRandomColor(self):
def randColor():
self.program.randomColor(self.pixels)
self.currentProcess = Process(target=randColor)
self.currentProcess.start()
@terminate
def setFullColor(self, red, green, blue):
color = Color(red, green, blue)
self.program.setFullColor(self.pixels, color)
@terminate
def runPanicMode(self):
def panicMode():
while(True):
self.program.setFullColor(self.pixels, Color(255,0,0))
time.sleep(0.5)
self.program.shutPixels(self.pixels)
time.sleep(0.5)
self.currentProcess = Process(target=panicMode)
self.currentProcess.start()
@terminate
def stopLights(self):
self.program.shutPixels(self.pixels)
@terminate
def runRainbow(self):
def rainbow():
while True:
self.program.rainbow()
self.currentProcess = Process(target=rainbow)
self.currentProcess.start()
@terminate
def runRainbowCycle(self):
def rainbowCycle():
while True:
self.program.rainbowCycle()
self.currentProcess = Process(target=rainbowCycle)
self.currentProcess.start()
def randomColor():
blue = random.randint(0,255)
red = random.randint(0,255)
green = random.randint(0,255)
return Color(red, green, blue)
#controller = Controller()
#controller.runCheckerPattern()
#controller.runRandomColor()
#controller.setFullColor(Color(255,0,80))
#controller.runPanicMode()
#controller.runWormChase()
#controller.colorChooser()