Refactor guitar hero into modules
This commit is contained in:
parent
8573d30d02
commit
36eb0bb51c
57
guitarhero/fixtures.py
Normal file
57
guitarhero/fixtures.py
Normal file
@ -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
|
@ -3,73 +3,11 @@ import math
|
|||||||
import pygame
|
import pygame
|
||||||
import serial
|
import serial
|
||||||
|
|
||||||
|
import time
|
||||||
|
import colorsys
|
||||||
|
|
||||||
def clamp(x, ab):
|
from util import clamp, rescale
|
||||||
(a, b) = ab
|
from fixtures import MovingHead
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
@ -80,10 +18,13 @@ if __name__ == "__main__":
|
|||||||
MovingHead(43),
|
MovingHead(43),
|
||||||
]
|
]
|
||||||
|
|
||||||
movingheads[0].rgbw = (59, 130, 246, 0) # blue
|
# movingheads[0].rgbw = (59, 130, 246, 0) # blue
|
||||||
movingheads[1].rgbw = (245, 158, 11, 0) # yellow
|
# movingheads[1].rgbw = (245, 158, 11, 0) # yellow
|
||||||
movingheads[2].rgbw = (239, 68, 68, 0) # red
|
# movingheads[2].rgbw = (239, 68, 68, 0) # red
|
||||||
movingheads[3].rgbw = (16, 185, 129, 0) # green
|
# movingheads[3].rgbw = (16, 185, 129, 0) # green
|
||||||
|
|
||||||
|
for head in movingheads:
|
||||||
|
head.rgbw = (0, 0, 0xFF, 0)
|
||||||
|
|
||||||
for head in movingheads:
|
for head in movingheads:
|
||||||
head.pan = -0.5 * math.pi
|
head.pan = -0.5 * math.pi
|
||||||
@ -94,7 +35,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
pygame.init() # pylint: disable=no-member; wtf?
|
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.display.set_caption("meh")
|
||||||
|
|
||||||
pygame.joystick.init()
|
pygame.joystick.init()
|
||||||
@ -153,12 +94,14 @@ if __name__ == "__main__":
|
|||||||
# render
|
# render
|
||||||
|
|
||||||
for head_id, button_id in enumerate([3, 2, 1, 0]):
|
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
|
pitch = max(0, min(1, -1 * pitch)) # 0 horizontal, 1 up
|
||||||
tilt = rescale(pitch, (0, 1), (-0.5 * math.pi, 0))
|
tilt = rescale(pitch, (0, 1), (-0.5 * math.pi, 0))
|
||||||
for head in movingheads:
|
for head in movingheads:
|
||||||
head.tilt = tilt
|
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_HOME = -0.5 * math.pi
|
||||||
ANGLE_OUTER = 0.5235988
|
ANGLE_OUTER = 0.5235988
|
||||||
|
11
guitarhero/util.py
Normal file
11
guitarhero/util.py
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user