-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·181 lines (138 loc) · 5.89 KB
/
test.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
#!/usr/bin/python3
from time import sleep
from rpi_ws281x import *
from threading import Thread
class FutevTorchMode():
NONE = -1
BLINK_SHORT = 0
BLINK_LONG = 1
PULSE = 2
RAINBOW = 3
class FutevTorch():
def __init__(self, pin=18, frequency=800000, dma=5, brightness=255):
self.__pin = pin
self.__frequency = frequency
self.__dma = dma
self.__brightness = brightness
def __init_torch(self):
self.__torch = FutevTorchThread(self.__pin, self.__frequency, self.__dma, self.__brightness)
def run_pulse(self, red, green, blue, intervall=0.005):
self.__init_torch()
self.__torch.set_color(red, green, blue)
self.__torch.set_mode(FutevTorchMode.PULSE)
self.__torch.set_intervall(intervall)
self.__torch.start()
def run_rainbow(self, intervall=0.02):
self.__init_torch()
self.__torch.set_intervall(intervall)
self.__torch.set_mode(FutevTorchMode.RAINBOW)
self.__torch.start()
def run_blink_short(self, red, green, blue, intervall=3):
self.__init_torch()
self.__torch.set_color(red, green, blue)
self.__torch.set_intervall(intervall)
self.__torch.set_mode(FutevTorchMode.BLINK_SHORT)
self.__torch.start()
def run_blink_long(self, red, green, blue, intervall=3):
self.__init_torch()
self.__torch.set_color(red, green, blue)
self.__torch.set_intervall(intervall)
self.__torch.set_mode(FutevTorchMode.BLINK_LONG)
self.__torch.start()
def stop(self):
self.__torch.stop()
self.__torch.join()
class FutevTorchThread(Thread):
def __init__(self, pin=18, frequency=800000, dma=5, brihtness=255):
Thread.__init__(self)
self.__strip = PixelStrip(1, pin, frequency, dma, False, brihtness)
self.__strip.begin()
self.__red = 0
self.__green = 0
self.__blue = 0
self.__mode = FutevTorchMode.NONE
self.__intervall = 0
def set_color(self, red, green, blue):
self.__red = red
self.__green = green
self.__blue = blue
def set_mode(self, mode):
self.__mode = mode
def set_intervall(self, intervall):
self.__intervall = intervall
def __show_pulse(self):
while not self.__stop:
self.__smooth_start(self.__intervall)
self.__smooth_stop(self.__intervall)
def __show_rainbow(self):
self.set_color(0, 255, 0)
self.__smooth_start(0.005)
while not self.__stop:
for i in range(255):
red, green, blue = self.__wheel((i) & 255)
self.set_color(red, green, blue)
self.__show()
sleep(self.__intervall)
self.__smooth_stop(0.005)
def __wheel(self, position):
if position < 85:
return position * 3, 255 - position * 3, 0
elif position < 170:
position -= 85
return 255 - position * 3, 0, position * 3
else:
position -= 170
return 0, position * 3, 255 - position * 3
def __show_blink(self, time):
for i in range(self.__intervall):
self.__strip.setBrightness(255)
self.__show()
sleep(time)
self.__strip.setBrightness(0)
self.__show()
sleep(time)
def run(self):
self.__stop = False
if self.__mode == FutevTorchMode.PULSE:
self.__show_pulse()
elif self.__mode == FutevTorchMode.RAINBOW:
self.__show_rainbow()
elif self.__mode == FutevTorchMode.BLINK_SHORT:
self.__show_blink(0.5)
elif self.__mode == FutevTorchMode.BLINK_LONG:
self.__show_blink(1)
self.__strip._cleanup()
def __show(self):
self.__strip.setPixelColorRGB(0, self.__red, self.__green, self.__blue)
self.__strip.show()
def stop(self):
self.__stop = True
def __smooth_start(self, intervall = 0.002):
self.__strip.setPixelColorRGB(0, self.__red, self.__green, self.__blue)
for i in range(0, 255, 1):
self.__strip.setBrightness(i)
self.__strip.show()
sleep(intervall)
def __smooth_stop(self, intervall = 0.002):
for i in range(255, 0, -1):
self.__strip.setBrightness(i)
self.__strip.show()
sleep(intervall)
if __name__ == '__main__':
torch = FutevTorch()
#torch.run_pulse(255,0,255)
#print("Pulse")
#sleep(3)
#torch.stop()
torch.run_rainbow()
print("Rainbow")
sleep(30)
torch.stop()
torch.run_blink_short(255,0,0, intervall=10)
print("blink short")
sleep(3)
torch.stop()
torch.run_blink_long(0,0,255)
print("blink short")
sleep(3)
torch.stop()