-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb_led.py
143 lines (97 loc) · 2.35 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
136
137
138
139
140
141
142
143
import logging
import threading
import time
import RPi.GPIO as GPIO
from config import LED
log = logging.getLogger(__name__)
red_pin = LED.RED_PIN
green_pin = LED.GREEN_PIN
blue_pin = LED.BLUE_PIN
ready_pulse_event = None
conf_blink_event = None
def ready_led_on():
off()
green()
def ready_pause_led_on():
off()
magenta()
def config_led_on():
off()
blue()
def copy_led_on():
off()
cyan()
def power_off_led_on():
off()
red()
def config_led_blink():
global conf_blink_event
conf_blink_event = threading.Event()
blinker = threading.Thread(target=blink, args=(blue, conf_blink_event))
blinker.start()
def config_led_stop_blink():
global conf_blink_event
if conf_blink_event is not None:
conf_blink_event.set()
off()
time.sleep(2 * LED.BLINK_SLEEP)
def blink(color_func, e):
log.debug('start blink')
while not e.is_set():
color_func()
time.sleep(LED.BLINK_SLEEP)
off()
time.sleep(LED.BLINK_SLEEP)
log.debug('end blink')
def ready_led_pulse():
global ready_pulse_event
ready_pulse_event = threading.Event()
pulsing = threading.Thread(target=pulse, args=(green_pin, ready_pulse_event))
pulsing.start()
def ready_led_stop_pulse():
global ready_pulse_event
if ready_pulse_event is not None:
ready_pulse_event.set()
ready_led_on()
def pulse(channel, e):
frequency = 300
speed = 0.08
step = 5
p = GPIO.PWM(channel, frequency)
p.start(30)
while not e.is_set():
for duty_cycle in range(30, 101, step):
p.ChangeDutyCycle(duty_cycle)
time.sleep(speed)
for duty_cycle in range(100, 29, -step):
p.ChangeDutyCycle(duty_cycle)
time.sleep(speed)
p.stop()
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(red_pin, GPIO.OUT)
GPIO.setup(green_pin, GPIO.OUT)
GPIO.setup(blue_pin, GPIO.OUT)
def off():
GPIO.output(red_pin, GPIO.LOW)
GPIO.output(green_pin, GPIO.LOW)
GPIO.output(blue_pin, GPIO.LOW)
def red():
GPIO.output(red_pin, GPIO.HIGH)
def green():
GPIO.output(green_pin, GPIO.HIGH)
def blue():
GPIO.output(blue_pin, GPIO.HIGH)
def yellow():
red()
green()
def cyan():
blue()
green()
def magenta():
red()
blue()
def white():
red()
green()
blue()