-
Notifications
You must be signed in to change notification settings - Fork 21
/
rgb_led.py
135 lines (108 loc) · 3.16 KB
/
rgb_led.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
#! /usr/bin/python3
import time
import threading
import RPi.GPIO as GPIO
RED_PIN = 22
GREEN_PIN = 23
BLUE_PIN = 24
COLOURS = {
"OFF": None,
"RED": [RED_PIN],
"GREEN": [GREEN_PIN],
"BLUE": [BLUE_PIN],
"CYAN": [GREEN_PIN, BLUE_PIN],
"MAGENTA": [RED_PIN, BLUE_PIN],
"YELLOW": [RED_PIN, GREEN_PIN],
"WHITE": [RED_PIN, GREEN_PIN, BLUE_PIN]
}
class RGB_LED (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
# BCM pin numbering!
GPIO.setmode(GPIO.BCM)
GPIO.setup([RED_PIN, GREEN_PIN, BLUE_PIN], direction=GPIO.OUT, initial=GPIO.HIGH)
self.state = 1
self.colour_0 = "OFF"
self.colour_1 = "OFF"
self.colour_0_mem = None
self.colour_1_mem = None
self.timer = None
def __del__(self):
GPIO.cleanup()
def set_static(self, colour:str=None, timeout_sec:float=None, restore_previous_on_timeout:bool=False):
if timeout_sec:
if self.timer:
return
self.timer = timeout_sec + 0.5
if restore_previous_on_timeout:
self.colour_0_mem = self.colour_0
self.colour_1_mem = self.colour_1
else:
self.colour_0_mem = None
self.colour_1_mem = None
self.colour_0 = colour
self.colour_1 = colour
pins = COLOURS[colour]
# All pins off (perhaps momentarily)
GPIO.output([RED_PIN, GREEN_PIN, BLUE_PIN], GPIO.LOW)
# Write required pins high
if pins != None:
GPIO.output(pins, GPIO.HIGH)
def set_blink(self, colour_0:str=None, colour_1:str="OFF", timeout_sec:float=None, restore_previous_on_timeout:bool=False):
if timeout_sec:
if self.timer:
return
self.timer = timeout_sec + 0.5
if restore_previous_on_timeout:
self.colour_0_mem = self.colour_0
self.colour_1_mem = self.colour_1
else:
self.colour_0_mem = None
self.colour_1_mem = None
self.colour_0 = colour_0
self.colour_1 = colour_1
def run(self):
while True:
if self.state == 0:
self.state = 1
pins = COLOURS[self.colour_1]
else:
self.state = 0
pins = COLOURS[self.colour_0]
# All pins off (perhaps momentarily)
GPIO.output([RED_PIN, GREEN_PIN, BLUE_PIN], GPIO.LOW)
# Write required pins high
if pins != None:
GPIO.output(pins, GPIO.HIGH)
# Flash/wait period
time.sleep(0.5)
# Turn off the light when the timer expires
if self.timer:
self.timer -= 0.5
if self.timer <= 0:
self.timer = None
if self.colour_0_mem:
self.colour_0 = self.colour_0_mem;
self.colour_0_mem = None;
else:
self.colour_0 = "OFF"
if self.colour_1_mem:
self.colour_1 = self.colour_1_mem;
self.colour_1_mem = None;
else:
self.colour_1 = "OFF"
if __name__ == "__main__":
led = RGB_LED(1, "LED")
led.start()
try:
while True:
led.set_blink("YELLOW", "BLUE")
time.sleep(5)
for colour in COLOURS:
led.set_static(colour)
time.sleep(0.5)
except:
GPIO.cleanup()
exit()