def morph(self, end_color: RGB, duration: int = 1000, steps: int = 50) -> None: duration = RangeInt(duration, 1, self.MAX_DURATION, "duration") steps = RangeInt(steps, 1, self.MAX_STEPS, "steps") r_end, g_end, b_end = end_color start_color = self.getColor() r_start, g_start, b_start = start_color gradient: List[RGB] = [] steps += 1 for n in range(1, steps): d = 1.0 * n / float(steps) r = (r_start * (1 - d)) + (r_end * d) g = (g_start * (1 - d)) + (g_end * d) b = (b_start * (1 - d)) + (b_end * d) gradient.append(RGB(int(r), int(g), int(b))) gradient.append(end_color) ms_delay = float(duration) / float(1000 * steps) for grad in gradient: self.setColor(grad) time.sleep(ms_delay) self.setColor(end_color)
def blink(self, color: RGB, blinks: int = 1, delay: int = 500) -> None: blinks = RangeInt(blinks, 1, self.MAX_BLINKS, "blinks") delay = RangeInt(delay, 1, self.MAX_DELAY, "delay_ms") ms_delay = float(delay) / float(1000) for x in range(blinks): if x: time.sleep(ms_delay) self.setColor(color) time.sleep(ms_delay) self.turnOff()
def pulse(self, color: RGB, pulses: int = 1, duration: int = 1000, steps: int = 50) -> None: pulses = RangeInt(pulses, 1, self.MAX_PULSES, "pulses") duration = RangeInt(duration, 1, self.MAX_DURATION, "duration") steps = RangeInt(steps, 1, self.MAX_STEPS, "steps") self.turnOff() for x in range(pulses): self.morph(color, duration=duration, steps=steps) self.morph(OFF, duration=duration, steps=steps)
def blink(self, color: RGB, blinks: int = 1, delay_ms: int = 500) -> None: blinks = RangeInt(blinks, 1, self.MAX_BLINKS, "blinks") delay_ms = RangeInt(delay_ms, 1, self.MAX_DELAY, "delay_ms") pattern = Pattern() for x in range(blinks): if x: pattern.addColorAndDuration(OFF, delay_ms) pattern.addColorAndDuration(color, delay_ms) pattern.addColorAndDuration(OFF, 0) from core.FadeStickUSB import sendControlTransfer, R_USB_SEND, R_SET_CONFIG sendControlTransfer(self, R_USB_SEND, R_SET_CONFIG, FS_MODE_PATTERN, pattern.getBytePattern())
def test_range_too_small2(self): v = 100 mn = 10 mx = -10 with self.assertRaises(RangeIntException) as m: RangeInt(v, mn, mx, "test") self.assertEqual(f"test has a bad range [{mn}..{mx}]: {v}", str(m.exception))
def test_out_range2(self): v = 256 mn = 0 mx = 255 with self.assertRaises(RangeIntException) as m: RangeInt(v, mn, mx, "test") self.assertEqual(f"test is out of range [{mn}..{mx}]: {v}", str(m.exception))
def morph(self, end_color: RGB, duration: int = 1000, steps: int = MAX_STEPS) -> None: duration = RangeInt(duration, 1, self.MAX_DURATION, "duration") steps = RangeInt(steps, 1, self.MAX_STEPS, "steps") r_end, g_end, b_end = end_color start_color = self.getColor() r_start, g_start, b_start = start_color pattern: Pattern = Pattern() ms_delay = floor(float(duration) / float(steps)) for n in range(0, steps): # Range is exclusive d = 1.0 * (n + 1) / float(steps) r = (r_start * (1 - d)) + (r_end * d) g = (g_start * (1 - d)) + (g_end * d) b = (b_start * (1 - d)) + (b_end * d) pattern.addColorAndDuration(RGB(int(r), int(g), int(b)), ms_delay) from core.FadeStickUSB import sendControlTransfer, R_USB_SEND, R_SET_CONFIG sendControlTransfer(self, R_USB_SEND, R_SET_CONFIG, FS_MODE_PATTERN, pattern.getBytePattern())
def test_repr(self): self.assertEqual(f"{'RangeInt'}({'test'}, [{0}..{255}]: {1})", repr(RangeInt(1, 0, 255, "test")))
def test_str(self): self.assertEqual("1", str(RangeInt(1, 0, 255, "test")))
def test_simple(self): self.assertEqual(1, RangeInt(1, 0, 255, "test"))
def __init__(self, color: RGB, duration: int) -> None: self.color: Final[RGB] = color self.duration: Final[int] = RangeInt(duration, 0, self.MAX_DURATION, "duration")