-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
116 lines (96 loc) Β· 4.04 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
import os
import signal
import sys
import time
from inputs.midiPort import MidiPort
from inputs.midiRange import MidiRange
from inputs.midiToggle import MidiToggle
from inputs.sine import Sine
from inputs.sineClock import SineClock
from middleware.middleware import Middleware
from outputs.strip.canvas import Canvas
from outputs.strip.effects.chase import Chase
from outputs.strip.effects.flood import Flood
from outputs.strip.effects.shadow import Shadow
from outputs.strip.renderer.terminal import Terminal
from parameters.color import Color
from parameters.parameter import Parameter
# SIGINT handler
################################################################################
from parameters.constant import Constant
def signal_handler(sign, frame):
global app
app.shutdown()
sys.exit(0)
# App
################################################################################
class App:
def __init__(self, fps: int):
self.fps = fps
# Dimensions
terminal_size = os.get_terminal_size()
if len(sys.argv) == 3:
lines = int(sys.argv[1])
columns = int(sys.argv[2])
else:
default_lines = terminal_size.lines - 1
default_columns = terminal_size.columns
lines = int(input("Lines (" + str(default_lines) + "): ") or default_lines)
columns = int(input("Columns (" + str(default_columns) + "): ") or default_columns)
# Parameters
chase_speed = Parameter(vmax=1)
chase_gap = Parameter(vmin=3, vmax=100)
red = Parameter(vmin=0, vmax=255)
green = Parameter(vmin=0, vmax=255)
blue = Parameter(vmin=0, vmax=255)
brightness = Parameter(vmin=0, vmax=1)
color = Color(red=red, green=green, blue=blue, brightness=brightness)
pulse_period = Parameter(vmin=0.01, vmax=10, invert=True)
shadow_tail = Parameter(vmin=3, vmax=20)
# Inputs
midi_port = MidiPort()
slider1 = MidiRange(port=midi_port, initial=63, channel=0)
slider2 = MidiRange(port=midi_port, initial=63, channel=1)
slider3 = MidiRange(port=midi_port, initial=63, channel=2)
slider4 = MidiRange(port=midi_port, initial=63, channel=3)
slider5 = MidiRange(port=midi_port, initial=63, channel=4)
slider6 = MidiRange(port=midi_port, initial=63, channel=5)
slider7 = MidiRange(port=midi_port, initial=63, channel=6)
pulse_enabled = MidiToggle(port=midi_port, initial=False, channel=37)
pulse = SineClock(sine=Sine(period=pulse_period))
# Middleware
self.middleware = Middleware()
self.middleware.pipe(slider1, chase_speed)
self.middleware.pipe(slider2, chase_gap)
self.middleware.pipe(slider3, red)
self.middleware.pipe(slider4, green)
self.middleware.pipe(slider5, blue)
self.middleware.pipe_if_true(pulse_enabled, slider6, pulse_period)
self.middleware.pipe_if_true(pulse_enabled, pulse, brightness)
self.middleware.pipe_if_false(pulse_enabled, slider6, brightness)
self.middleware.pipe(slider7, shadow_tail)
# Output
self.canvas = Canvas(pixels=lines * columns, renderer=Terminal(columns=columns))
chase = Chase(speed=chase_speed, gap=chase_gap, color=color)
flood = Flood(color=Color(red=Constant(127), green=Constant(127), blue=Constant(255),
brightness=Constant(.4)))
shadow = Shadow(tail_length=shadow_tail)
self.canvas.add_layer(chase)
self.canvas.add_layer(shadow)
self.canvas.add_layer(flood)
def run(self):
while True:
self.middleware.run()
self.canvas.paint()
# Pause for a bit depending on app fps
time.sleep(1 / self.fps)
def shutdown(self):
os.system('clear')
print("bye")
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
# Run app
############################################################################
app = App(fps=120)
app.run()