-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedControl.py
More file actions
executable file
·227 lines (165 loc) · 5.55 KB
/
LedControl.py
File metadata and controls
executable file
·227 lines (165 loc) · 5.55 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Imports
from re import match
from time import sleep
from rpi_ws281x import Color, PixelStrip, ws
from copy import copy
#
# https://github.com/rpi-ws281x/rpi-ws281x-python/blob/f35d40a56c3a3ae854a6a601fecc9fa8bc92f5dc/examples/SK6812_strandtest.py#L47
#
# Variables
defaultColour = [255, 50, 100]
isOn = False
currentColour = {
"red": defaultColour[0],
"green": defaultColour[1],
"blue": defaultColour[2],
}
currentEffect = ""
currentEffectArguments = {}
currentEffectHelp = {}
strip = None
# LED Variables
LED_COUNT = 30 # Number of LED pixels.
LED_PIN = 13 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
# True to invert the signal (when using NPN transistor level shift)
LED_INVERT = False
LED_CHANNEL = 1
strip = PixelStrip(
LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL
)
strip.begin()
# Private
def __FindLargest(numbers):
largest = 0
for i in range(len(numbers)):
if largest == 0 or numbers[i] > largest:
largest = numbers[i]
return largest
def __StripSpaces(value):
return value.strip()
def __ValidateRGB(toCheck):
if isOn:
RGBArray = {}
toCheck = list(map(__StripSpaces, toCheck))
if len(toCheck) < 3:
return "RGB array does not include all three values!"
else:
for index, value in enumerate(toCheck):
if match(r"-?\d", str(value)) is not None:
valueAsInt = int(value)
if valueAsInt > 255:
RGBArray[index] = 255
elif valueAsInt < 0:
RGBArray[index] = 0
else:
RGBArray[index] = valueAsInt
else:
return f"Given value {value} is not a valid number!"
rgb = {"red": RGBArray[0], "green": RGBArray[1], "blue": RGBArray[2]}
return rgb
else:
return "LEDs are not currently on!"
def __FadeColour(r, g, b, fadeTime=0.01):
largest = 0
largestNumber = __FindLargest([r, g, b])
largestCurrent = __FindLargest(list(currentColour.values()))
initial = GetLEDColour()
internalR = initial["red"]
internalG = initial["green"]
internalB = initial["blue"]
if largestCurrent > largestNumber:
largest = largestCurrent
else:
largest = largestNumber
currentColour["red"] = r
currentColour["green"] = g
currentColour["blue"] = b
for i in range(largest):
# print(g < initial["green"],g,initial["green"])
if r > initial["red"] and internalR < r:
internalR += 1
if r < initial["red"] and internalR > r:
internalR -= 1
if g > initial["green"] and internalG < g:
internalG += 1
if g < initial["green"] and internalG > g:
internalG -= 1
if b > initial["blue"] and internalB < b:
internalB += 1
if b < initial["blue"] and internalB > b:
internalB -= 1
for x in range(strip.numPixels()):
strip.setPixelColor(x, Color(internalR, internalG, internalB))
sleep(fadeTime)
strip.show()
def __SetColour(r, g, b):
print(f"SETTING COLOUR TO: {r,g,b}")
currentColour["red"] = r
currentColour["green"] = g
currentColour["blue"] = b
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(r, g, b))
strip.show()
# Public
def SetLEDColour(colour, fade="false", fadeTime=0.01, override="false"):
if currentEffect == "" or override.lower() == "true":
RGB = __ValidateRGB(toCheck=colour)
if len(RGB) == 3:
if fade == "true":
__FadeColour(RGB["red"], RGB["green"], RGB["blue"], fadeTime)
else:
__SetColour(RGB["red"], RGB["green"], RGB["blue"])
return RGB
def DisableLEDs():
if currentEffect == "":
global isOn
isOn = False
print("TURNING ALL LEDS OFF")
__FadeColour(0, 0, 0, 0.001)
else:
print("Effect running, can not turn off LEDs")
def EnableLEDs():
if currentEffect == "":
global isOn
isOn = True
print("TURNING ALL LEDS ON")
__FadeColour(defaultColour[0], defaultColour[1], defaultColour[2])
else:
print("Effect running, can not turn LEDs on")
def SetEffect(effect, otherArguments):
global currentEffect, currentEffectArguments, currentEffectHelp
if isOn and currentEffect == "":
effectModule = __import__(effect, otherArguments)
if hasattr(effectModule, "Run"):
__SetColour(0, 0, 0)
currentEffect = effect
currentEffectArguments = otherArguments
if hasattr(effectModule, "help"):
currentEffectHelp = effectModule.help
else:
currentEffectHelp = "No help provided for effect"
effectModule.Run(otherArguments)
sleep(0.75)
currentEffect = ""
currentEffectArguments = {}
currentEffectHelp = {}
__FadeColour(defaultColour[0], defaultColour[1], defaultColour[2])
return f"Effect {effect} completed successfully!"
else:
return f"Effect {effect} doesn't have a Run function!"
else:
return "LEDs are not currently on!"
def GetLEDStatus():
return isOn
def GetLEDColour():
return copy(currentColour)
def GetLEDEffect():
return [currentEffect, currentEffectArguments, currentEffectHelp]
def ResetCurrentEffect():
global currentEffect, currentEffectArguments, currentEffectHelp
currentEffect = ""
currentEffectArguments = {}
currentEffectHelp = {}