forked from masapasa/VoiceBotChatGPT-RaspberryPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_service.py
85 lines (76 loc) · 2.44 KB
/
led_service.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
import time
from typing import Tuple
from apa102 import APA102
import gpiozero
# LED Settings
NUM_LEDS = 3
LEDS_GPIO = 12
_BLACK = (0, 0, 0)
_WHITE = (255, 255, 255)
_RED = (255, 0, 0)
_YELLOW = (255, 255, 0)
_BLUE = (0, 0, 255)
_GREEN = (0, 255, 0)
_PURPLE = (255, 0, 255)
_CYAN = (0, 255, 255)
_PINK = (255, 105, 180)
_ORANGE = (255, 165, 0)
class LEDService:
def __init__(self, led_brightness = None):
self.led_power = gpiozero.LED(LEDS_GPIO, active_high=False)
self.led_brightness = led_brightness
self.turn_on()
def set_color(self, rgb: Tuple[int, int, int]):
if self.current_color == rgb:
return
# get the variable name of the color
# color_name = [k for k, v in globals().items() if v == rgb][0]
# if self.leds is None:
# print(f"{self.event}: {color_name}")
# return
for i in range(NUM_LEDS):
self.leds.set_pixel(i, rgb[0], rgb[1], rgb[2])
self.leds.show()
self.current_color = rgb
def handle_event(self, event):
self.event = event
self.set_color(_BLACK)
if "StreamingStarted" == event:
self.set_color(_CYAN)
elif "NoInternet" == event:
self.set_color(_BLUE)
elif "Processing" == event:
self.set_color(_PINK)
elif "VoiceStarted" == event:
self.set_color(_YELLOW)
elif "Transcript" == event:
self.set_color(_WHITE)
elif "Starting" == event:
self.set_color(_PURPLE)
elif "Running" == event:
self.set_color(_RED)
elif "Connected" == event:
self.set_color(_GREEN)
elif "Shutdown" == event:
self.set_color(_ORANGE)
elif "Paused" == event:
self.set_color(_ORANGE)
elif "Off" == event:
self.set_color(_BLACK)
def turn_on(self):
self.led_power.on()
if self.led_brightness is not None:
self.leds = APA102(num_led=NUM_LEDS, global_brightness=self.led_brightness)
else:
self.leds = APA102(num_led=NUM_LEDS)
self.current_color = _BLACK
def turn_off(self):
self.handle_event("Off")
self.leds.cleanup()
self.led_power.off()
def blink(self, rgb, duration):
for _ in range(duration):
self.set_color(rgb)
time.sleep(0.3)
self.set_color(_BLACK)
time.sleep(0.3)