/
cosmo-hat.py
executable file
·104 lines (87 loc) · 2.7 KB
/
cosmo-hat.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
#!/usr/bin/python
from __future__ import print_function
from cosmoavr.cosmohat import CosmoHat
from time import sleep, time
import csnd6
PRINT = False
TOLERANCE = 0.001
def safesleep(amt):
if amt > 0:
sleep(amt)
def run(c, cs, perf):
switch_state = c.switches()
switch_last = switch_state
last_knobs = c.knobs()
first = True
count = 0
now = time()
while perf.GetStatus() == 0:
knobs = c.knobs()
changed = [abs(n-l) > TOLERANCE for n,l in zip(knobs, last_knobs)]
last_knobs = knobs
for i, (knob, change) in enumerate(zip(knobs, changed)):
if change or first:
cs.SetChannel("P"+str(i), knob)
if PRINT:
print("{}: ".format(i) + "=" * int(knob*80))
switches = c.switches()
posedge = [s and not l for s,l in zip(switches, switch_last)]
changed = [s != l for s,l in zip(switches, switch_last)]
switch_last = switches
for i, edge in enumerate(posedge):
if edge or first:
switch_state[i] = not switch_state[i]
cs.SetChannel("T"+str(i), switch_state[i])
for i, change in enumerate(changed):
if change or first:
cs.SetChannel("M"+str(i), switches[i])
leds = {}
blink = 0
# If Csound doesnt run - blink with leds
for i in range(c.nleds):
leds[i] = cs.GetChannel("L"+str(i)) != 0
c.set_leds(leds)
now += 0.005
safesleep(now-time())
count += 1
first = False
def main(csound_file):
c = CosmoHat()
try:
while True:
cs = csnd6.Csound()
res = cs.Compile(csound_file)
if res == 0:
perf = csnd6.CsoundPerformanceThread(cs)
perf.Play()
try:
run(c, cs, perf)
finally:
perf.Stop()
perf.Join()
break
# Script stopped or didn't compile. Blink leds a couple of times
on = {}
off = {}
for i in range(c.nleds):
on[i] = True
off[i] = False
for i in range(5):
for j in range(2):
c.set_leds(on)
sleep(0.1)
c.set_leds(off)
sleep(0.05)
sleep(0.5)
finally:
c.stop()
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
print("Usage: {} <file.csd>".format(sys.argv[0]))
sys.exit(1)
csound_file = sys.argv[1]
try:
main(csound_file)
except KeyboardInterrupt:
pass