-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilestone3.py
More file actions
executable file
·239 lines (208 loc) · 5.48 KB
/
Copy pathMilestone3.py
File metadata and controls
executable file
·239 lines (208 loc) · 5.48 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#! /usr/bin/python
from __future__ import division
import thread
import os
import time
import Adafruit_Trellis
import spidev
import RPi.GPIO as GPIO
MOMENTARY = 0
LATCHING = 1
MODE = LATCHING
def playSound(sound):
os.system("aplay -N %s &" % str(sound))
matrix0 = Adafruit_Trellis.Adafruit_Trellis()
trellis = Adafruit_Trellis.Adafruit_TrellisSet(matrix0)
NUMTRELLIS = 1
numKeys = NUMTRELLIS * 16
I2C_BUS = 1
cnt=0
bpm = 60
divisions = 60.0/bpm/16
b1 = [0] * 128
b2 = [0] * 128
b3 = [0] * 128
b4 = [0] * 128
b5 = [0] * 128
b6 = [0] * 128
b7 = [0] * 128
b8 = [0] * 128
b9 = [0] * 128
b10 = [0] * 128
b11 = [0] * 128
b12 = [0] * 128
b13 = [0] * 128
b14 = [0] * 128
b15 = [0] * 128
b16 = [0] * 128
print 'Milestone 3 Demo'
trellis.begin((0x70, I2C_BUS)) # only one
def readPad():
if trellis.readSwitches():
# go through every button
for i in range(numKeys):
# if it was pressed...
if trellis.justPressed(i):
if i==0:
b1[cnt%128] = 1
if i==1:
b2[cnt%128] = 1
if i==2:
b3[cnt%128] = 1
if i==3:
b4[cnt%128] = 1
if i==4:
b5[cnt%128] = 1
if i==5:
b6[cnt%128] = 1
if i==6:
b7[cnt%128] = 1
if i==7:
b8[cnt%128] = 1
if i==8:
b9[cnt%128] = 1
if i==9:
b10[cnt%128] = 1
if i==10:
b11[cnt%128] = 1
if i==11:
b12[cnt%128] = 1
if i==12:
b13[cnt%128] = 1
if i==13:
b14[cnt%128] = 1
if i==14:
b15[cnt%128] = 1
if i==15:
b16[cnt%128] = 1
def adjustVolume():
os.system("amixer -q -c 1 sset 'Headphone' %s%%" % int(read()*100))
def bitstring(n):
s = bin(n)[2:]
return '0'*(8-len(s)) + s
def read(adc_channel=0, spi_channel=0):
cmd = 128
if adc_channel:
cmd += 32
reply_bytes = conn.xfer2([cmd, 0])
reply_bitstring = ''.join(bitstring(n) for n in reply_bytes)
reply = reply_bitstring[5:15]
return int(reply, 2) / 2**10
pressed5last = False #metronome on/off
pressed6last = False #start/stop
pressed13last = False #tempo-up
pressed26last = False #tempo-down
metronome = True
stopped = False
def readButtons():
global pressed5last
global pressed6last
global pressed13last
global pressed26last
global metronome
global bpm
global stopped
global b1
global b2
global b3
global b4
global b5
global b6
global b7
global b8
global b9
global b10
global b11
global b12
global b13
global b14
global b15
global b16
while 1:
is5Pressed = GPIO.input(5)
is6Pressed = GPIO.input(6)
is13Pressed = GPIO.input(13)
is26Pressed = GPIO.input(26)
if is5Pressed == True and pressed5last == False:
pressed5last = True
elif is5Pressed == False and pressed5last == True:
pressed5last = False
metronome = not metronome
if is6Pressed == True and pressed6last == False:
pressed6last = True
elif is6Pressed == False and pressed6last == True:
pressed6last = False
stopped = not stopped
b1 = [0] * 128
b2 = [0] * 128
b3 = [0] * 128
b4 = [0] * 128
b5 = [0] * 128
b6 = [0] * 128
b7 = [0] * 128
b8 = [0] * 128
b9 = [0] * 128
b10 = [0] * 128
b11 = [0] * 128
b12 = [0] * 128
b13 = [0] * 128
b14 = [0] * 128
b15 = [0] * 128
b16 = [0] * 128
if is13Pressed == True and pressed13last == False:
pressed13last = True
elif is13Pressed == False and pressed13last == True:
pressed13last = False
bpm +=1
if is26Pressed == True and pressed26last == False:
pressed26last = True
elif is26Pressed == False and pressed26last == True:
pressed26last = False
bpm -=1
time.sleep(0.005)
conn = spidev.SpiDev(0, 0)
conn.max_speed_hz = 1200000 # 1.2 MHz
GPIO.setmode(GPIO.BCM)
GPIO.setup([5, 6, 13, 26], GPIO.IN)
thread.start_new_thread(readButtons, ())
while True:
thread.start_new_thread(adjustVolume, ())
if not stopped:
thread.start_new_thread(readPad, ())
if cnt%16 == 0 and metronome == True:
thread.start_new_thread(playSound, ('~/seniorproj/examples/tick.wav',))
if b1[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio1.wav',))
if b2[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio2.wav',))
if b3[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio3.wav',))
if b4[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio4.wav',))
if b5[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio5.wav',))
if b6[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio6.wav',))
if b7[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio7.wav',))
if b8[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio8.wav',))
if b9[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio9.wav',))
if b10[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio10.wav',))
if b11[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio11.wav',))
if b12[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio12.wav',))
if b13[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio13.wav',))
if b14[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio14.wav',))
if b15[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio15.wav',))
if b16[cnt%128] == 1:
thread.start_new_thread(playSound, ('~/seniorproj/examples/audio16.wav',))
cnt = cnt + 1
divisions = 60.0/bpm/16
time.sleep(divisions)