kuukar-rpi/kuukar/kuukar_leds.py

84 lines
2.8 KiB
Python
Raw Normal View History

2022-10-26 08:10:08 +00:00
import threading
from time import perf_counter
from telemetrix_rpi_pico import telemetrix_rpi_pico
2022-10-26 08:10:08 +00:00
import time
2022-10-30 08:57:00 +00:00
from kuukar.kuukar_config import LEDS_DATA_PIN, LEDS_NUM, HEADLIGHT_LEDS, LEFT_SIGNAL_LEDS, RIGHT_SIGNAL_LEDS, REVERSE_LEDS
2022-10-25 15:01:26 +00:00
2022-10-22 12:39:10 +00:00
class leds:
2022-10-26 08:10:08 +00:00
flashing = False
2022-10-28 07:14:00 +00:00
headlight = False
reverse_signal = False
left_signal = False
right_signal = False
ambient_light = [0, 0, 0]
2022-10-26 08:10:08 +00:00
start_time = perf_counter()
2022-10-28 07:14:00 +00:00
2022-11-04 04:58:20 +00:00
def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.mcu = mcu
self.mcu.set_pin_mode_neopixel(LEDS_DATA_PIN, LEDS_NUM)
2022-11-10 15:33:12 +00:00
self.ambient_light = [2, 2, 10] #Keke Image Color
2022-11-09 15:59:21 +00:00
self.__update_leds()
2022-10-28 07:14:00 +00:00
def set_headlights(self, state: bool):
2022-11-09 15:59:21 +00:00
self.headlight = state
2022-10-28 07:14:00 +00:00
self.__update_leds()
def set_left_signal_led(self, state: bool):
self.left_signal = state
self.__update_leds()
def set_right_signal_led(self, state: bool):
self.right_signal = state
self.__update_leds()
def set_ambient_led(self, r: int, g: int, b: int):
self.ambient_light = [r, g, b]
self.__update_leds()
def set_reverse_led(self, state: bool):
2022-11-10 15:28:12 +00:00
self.reverse_signal = state
2022-10-28 07:14:00 +00:00
self.__update_leds()
def __update_leds(self):
2022-11-09 15:59:21 +00:00
self.mcu.neopixel_fill(
2022-11-10 15:40:24 +00:00
r=self.ambient_light[0], g=self.ambient_light[1], b=self.ambient_light[2], auto_show=False)
2022-10-28 07:14:00 +00:00
if self.headlight:
for led in HEADLIGHT_LEDS:
2022-11-04 04:58:20 +00:00
self.mcu.neo_pixel_set_value(led, self.headlight * 255, self.headlight * 255,
2022-11-09 15:59:21 +00:00
self.headlight * 255)
2022-10-28 07:14:00 +00:00
if self.left_signal:
for led in LEFT_SIGNAL_LEDS:
2022-11-04 04:58:20 +00:00
self.mcu.neo_pixel_set_value(led, 255, 0, 0)
2022-10-28 07:14:00 +00:00
if self.right_signal:
for led in RIGHT_SIGNAL_LEDS:
2022-11-04 04:58:20 +00:00
self.mcu.neo_pixel_set_value(led, 255, 0, 0)
2022-10-28 07:14:00 +00:00
if self.reverse_signal:
for led in REVERSE_LEDS:
2022-11-04 04:58:20 +00:00
self.mcu.neo_pixel_set_value(led, 255, 0, 0)
self.mcu.neopixel_show()
2022-10-28 07:14:00 +00:00
def flash(self, r: int, g: int, b: int, duration: int):
2022-10-26 08:10:08 +00:00
if not self.flashing:
self.flashing = True
self.start_time = perf_counter()
2022-10-28 07:14:00 +00:00
threading.Thread(target=self.__flasher, args=(r, g, b, duration))
2022-10-26 08:10:08 +00:00
@staticmethod
2022-10-28 07:14:00 +00:00
def __blink_func(t: float, dur: float):
if t > dur:
2022-10-26 08:10:08 +00:00
return 0
2022-10-28 07:14:00 +00:00
return (-1 / (dur / 2.0) ** 2) * (t - dur / 2.0) ** 2 + 1
2022-10-26 08:10:08 +00:00
2022-10-28 07:14:00 +00:00
def __get_time(self) -> float:
return perf_counter() - self.start_time
2022-10-26 08:10:08 +00:00
2022-10-28 07:14:00 +00:00
def __flasher(self, r: int, g: int, b: int, duration: int):
2022-10-26 08:10:08 +00:00
while True:
2022-10-28 07:14:00 +00:00
print(leds.__blink_func(self.__get_time(), duration))
2022-10-26 08:10:08 +00:00
time.sleep(0.05)
2022-11-04 04:58:20 +00:00
if self.__get_time() > duration:
2022-11-11 15:01:38 +00:00
self.flashing = False
2022-10-28 07:14:00 +00:00
break