musicmouse/espmusicmouse/host_driver/led_cmds.py

89 lines
2.2 KiB
Python

from dataclasses import dataclass
import struct
@dataclass
class ColorRGBW:
r: float
g: float
b: float
w: float
def as_bytes(self) -> bytes:
return struct.pack("<BBBB", self.r * 255, self.g * 255, self.b * 255, self.w * 255)
def is_valid(self):
vals = (self.r, self.g, self.b, self.w)
return all(0 <= v <= 1 for v in vals)
@dataclass
class ColorHSV:
h: float
s: float
v: float
def as_bytes(self) -> bytes:
return struct.pack("<fff", self.h, self.s, self.v)
def is_valid(self):
if not 0 <= self.h <= 360:
return False
if not 0 <= self.s <= 1:
return False
if not 0 <= self.v <= 2:
return False
return True
@dataclass
class EffectStaticConfig:
color: ColorRGBW
def as_bytes(self) -> bytes:
return self.color.as_bytes()
@dataclass
class EffectAlexaSwipeConfig:
primary_color_width: float # in degrees
transition_width: float # in degrees
swipe_speed: float # in degrees per second
bell_curve_width_in_leds: float
start_position: float # in degrees
forward: bool
primary_color: ColorRGBW
secondary_color: ColorRGBW
def as_bytes(self) -> bytes:
return struct.pack(
"<fffff?", self.primary_color_width, self.transition_width, self.swipe_speed,
self.bell_curve_width_in_leds, self.start_position,
self.forward) + self.primary_color.as_bytes() + self.secondary_color.as_bytes()
@dataclass
class EffectRandomTwoColorInterpolationConfig:
cycle_durations_ms: int
start_with_existing: bool
num_segments: int
hue1_random: bool
hue2_random: bool
color1: ColorHSV
color2: ColorHSV
def as_bytes(self) -> bytes:
return struct.pack("<i?i??", self.cycle_durations_ms, self.start_with_existing,
self.num_segments, self.hue1_random,
self.hue2_random) + self.color1.as_bytes(), +self.color2.as_bytes()
@dataclass
class EffectCircularConfig:
speed: float # in degrees per second
width: float # in degrees
color: ColorRGBW
def as_bytes(self) -> bytes:
return struct.pack("<ff", self.speed, self.width) + self.color.as_bytes()