-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.py
51 lines (41 loc) · 1.1 KB
/
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
import RPi.GPIO as GPIO
import time
import sys
import threading
lock = threading.Lock()
current_pattern = [0.1, 0]
next_pattern = None
def set_led_pattern(pattern):
lock.acquire()
global next_pattern
next_pattern = pattern
lock.release()
def drive_led():
index = 0
global current_pattern, next_pattern
GPIO.setup(18, GPIO.OUT)
while True:
#print("LED on")
GPIO.output(18, GPIO.HIGH)
time.sleep(current_pattern[index])
index += 1
#print("LED off")
GPIO.output(18, GPIO.LOW)
time.sleep(current_pattern[index])
index += 1
if index >= len(current_pattern):
index = 0
lock.acquire()
if not next_pattern is None:
current_pattern = next_pattern
index = 0
next_pattern = None
lock.release()
def start_led():
led_thread = threading.Thread(target=drive_led, args=())
led_thread.start()
if __name__ == '__main__':
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
start_led();
set_led_pattern([0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.9]);