From 36eb0bb51c645cbc4ce0c80a7057620a8b0a0977 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Sat, 25 Sep 2021 22:40:28 +0200 Subject: [PATCH] Refactor guitar hero into modules --- guitarhero/fixtures.py | 57 +++++++++++++++++++++++++++ guitarhero/main.py | 87 ++++++++---------------------------------- guitarhero/util.py | 11 ++++++ 3 files changed, 83 insertions(+), 72 deletions(-) create mode 100644 guitarhero/fixtures.py create mode 100644 guitarhero/util.py diff --git a/guitarhero/fixtures.py b/guitarhero/fixtures.py new file mode 100644 index 0000000..0e2c600 --- /dev/null +++ b/guitarhero/fixtures.py @@ -0,0 +1,57 @@ +import math + +from util import clamp, rescale + + +class MovingHead: + def __init__(self, start_addr): + self.start_addr = start_addr + + self.pan = 0 # -pi/2 to pi/2 + self.tilt = 0 # -3pi/2 to 3pi/2 + self.speed = 0 + self.dimmer = 0 # 0 to 1 + self.rgbw = (0, 0, 0, 0) + + def __str__(self): + + return ( + f"MovingHead({self.start_addr}): pan={self.pan!r}, " + f"tilt={self.tilt!r}, speed={self.speed!r}, " + f"dimmer={self.dimmer!r}, rgbw={self.rgbw!r}" + ) + + def render(self, dst): + + pan = rescale(self.pan, (-1.5 * math.pi, 1.5 * math.pi), (255, 0)) + pan = clamp(int(pan), (0, 255)) + pan_fine = 0 + + tilt = rescale(self.tilt, (-0.5 * math.pi, 0.5 * math.pi), (0, 255)) + tilt = clamp(int(tilt), (0, 255)) + tilt_fine = 0 + + dimmer = clamp(7 + int(127 * self.dimmer), (7, 134)) + + (r, g, b, w) = self.rgbw + + channels = [ + pan, + pan_fine, + tilt, + tilt_fine, + self.speed, + dimmer, + r, + g, + b, + w, + 0, # color mode + 0, # auto jump speed + 0, # control mode + 0, # reset + ] + + offset = self.start_addr - 1 + + dst[offset : offset + len(channels)] = channels diff --git a/guitarhero/main.py b/guitarhero/main.py index ac6351f..f5c2254 100644 --- a/guitarhero/main.py +++ b/guitarhero/main.py @@ -3,73 +3,11 @@ import math import pygame import serial +import time +import colorsys -def clamp(x, ab): - (a, b) = ab - return max(a, min(b, x)) - - -def rescale(x, from_limits, to_limits): - (a, b) = from_limits - x_0_1 = (x - a) / (b - a) - - (c, d) = to_limits - return c + (d - c) * x_0_1 - - -class MovingHead: - def __init__(self, start_addr): - self.start_addr = start_addr - - self.pan = 0 # -pi/2 to pi/2 - self.tilt = 0 # -3pi/2 to 3pi/2 - self.speed = 0 - self.dimmer = 0 # 0 to 1 - self.rgbw = (0, 0, 0, 0) - - def __str__(self): - - return ( - f"MovingHead({self.start_addr}): pan={self.pan!r}, " - f"tilt={self.tilt!r}, speed={self.speed!r}, " - f"dimmer={self.dimmer!r}, rgbw={self.rgbw!r}" - ) - - def render(self, dst): - - pan = rescale(self.pan, (-1.5 * math.pi, 1.5 * math.pi), (255, 0)) - pan = clamp(int(pan), (0, 255)) - pan_fine = 0 - - tilt = rescale(self.tilt, (-0.5 * math.pi, 0.5 * math.pi), (0, 255)) - tilt = clamp(int(tilt), (0, 255)) - tilt_fine = 0 - - dimmer = clamp(7 + int(127 * self.dimmer), (7, 134)) - - (r, g, b, w) = self.rgbw - - channels = [ - pan, - pan_fine, - tilt, - tilt_fine, - self.speed, - dimmer, - r, - g, - b, - w, - 0, # color mode - 0, # auto jump speed - 0, # control mode - 0, # reset - ] - - offset = self.start_addr - 1 - - dst[offset : offset + len(channels)] = channels - +from util import clamp, rescale +from fixtures import MovingHead if __name__ == "__main__": @@ -80,10 +18,13 @@ if __name__ == "__main__": MovingHead(43), ] - movingheads[0].rgbw = (59, 130, 246, 0) # blue - movingheads[1].rgbw = (245, 158, 11, 0) # yellow - movingheads[2].rgbw = (239, 68, 68, 0) # red - movingheads[3].rgbw = (16, 185, 129, 0) # green + # movingheads[0].rgbw = (59, 130, 246, 0) # blue + # movingheads[1].rgbw = (245, 158, 11, 0) # yellow + # movingheads[2].rgbw = (239, 68, 68, 0) # red + # movingheads[3].rgbw = (16, 185, 129, 0) # green + + for head in movingheads: + head.rgbw = (0, 0, 0xFF, 0) for head in movingheads: head.pan = -0.5 * math.pi @@ -94,7 +35,7 @@ if __name__ == "__main__": pygame.init() # pylint: disable=no-member; wtf? - screen = pygame.display.set_mode((420, 69)) + screen = pygame.display.set_mode((420, 69), pygame.RESIZABLE) pygame.display.set_caption("meh") pygame.joystick.init() @@ -153,12 +94,14 @@ if __name__ == "__main__": # render for head_id, button_id in enumerate([3, 2, 1, 0]): - movingheads[head_id].dimmer = 1 if button_state[button_id] else 0.2 + movingheads[head_id].dimmer = 0 if button_state[button_id] else 1 pitch = max(0, min(1, -1 * pitch)) # 0 horizontal, 1 up tilt = rescale(pitch, (0, 1), (-0.5 * math.pi, 0)) for head in movingheads: head.tilt = tilt + r, g, b = colorsys.hls_to_rgb((time.time() / 12) % 1, 0.5, 1) + head.rgbw = (int(255 * r), int(255 * g), int(255 * b), 0) ANGLE_HOME = -0.5 * math.pi ANGLE_OUTER = 0.5235988 diff --git a/guitarhero/util.py b/guitarhero/util.py new file mode 100644 index 0000000..d1d5bac --- /dev/null +++ b/guitarhero/util.py @@ -0,0 +1,11 @@ +def clamp(x, ab): + (a, b) = ab + return max(a, min(b, x)) + + +def rescale(x, from_limits, to_limits): + (a, b) = from_limits + x_0_1 = (x - a) / (b - a) + + (c, d) = to_limits + return c + (d - c) * x_0_1 \ No newline at end of file