-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy path_16n_faderbank_firmware.ino
409 lines (330 loc) · 8.55 KB
/
_16n_faderbank_firmware.ino
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* 16n Faderbank Firmware
* (c) 2017,2018 by Brian Crabtree, Sean Hellfritsch, Tom Armitage, and Brendon Cassidy
* MIT License
*/
/*
* NOTES:
* - Hardware MIDI is on pin 1
* - You **must** also compile this with Tools->USB type set to MIDI or MIDI/Serial (for debugging)
* - You also should overclock to 120MHz to make it as snappy as possible
*/
/*
* ALL configuration should take place in config.h.
* You can disable/enable flags, and configure MIDI channels in there.
*/
#include "config.h"
#include <i2c_t3.h>
#include <MIDI.h>
#include <ResponsiveAnalogRead.h>
#include <CD74HC4067.h>
#include "TxHelper.h"
MIDI_CREATE_DEFAULT_INSTANCE();
// loop helpers
int i, temp;
// midi write helpers
int q, shiftyTemp, notShiftyTemp;
// the storage of the values; current is in the main loop; last value is for midi output
int volatile currentValue[channelCount];
int lastMidiValue[channelCount];
#ifdef MASTER
// memory of the last unshifted value
int lastValue[channelCount];
// the i2c message buffer we are sending
uint8_t messageBuffer[4];
// temporary values
uint16_t valueTemp;
uint8_t device = 0;
uint8_t port = 0;
#endif
// the thing that smartly smooths the input
ResponsiveAnalogRead *analog[channelCount];
// mux config
CD74HC4067 mux(8, 7, 6, 5);
#ifdef REV
const int muxMapping[16] = {8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0};
#else
const int muxMapping[16] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8};
#endif
// MIDI timers
IntervalTimer midiWriteTimer;
IntervalTimer midiReadTimer;
int midiInterval = 1000; // 1ms
bool shouldDoMidiRead = false;
bool shouldDoMidiWrite = false;
// helper values for i2c reading and future expansion
int activeInput = 0;
int activeMode = 0;
/*
* The function that sets up the application
*/
void setup()
{
#ifdef DEBUG
while (!Serial)
;
Serial.print("16n Firmware Debug Mode\n");
#endif
// initialize the TX Helper
#ifdef V125
TxHelper::UseWire1(true);
#else
TxHelper::UseWire1(false);
#endif
TxHelper::SetPorts(16);
TxHelper::SetModes(4);
// set read resolution to teensy's 13 usable bits
analogReadResolution(13);
// initialize the value storage
for (i = 0; i < channelCount; i++)
{
// analog[i] = new ResponsiveAnalogRead(0, false);
analog[i] = new ResponsiveAnalogRead(0, true, .0001);
analog[i]->setAnalogResolution(1 << 13);
currentValue[i] = 0;
lastMidiValue[i] = 0;
#ifdef MASTER
lastValue[i] = 0;
#endif
}
// i2c using the default I2C pins on a Teensy 3.2
#ifdef MASTER
#ifdef DEBUG
Serial.println("Enabling i2c in MASTER mode");
#endif
#ifdef V125
Wire1.begin(I2C_MASTER, I2C_ADDRESS, I2C_PINS_29_30, I2C_PULLUP_EXT, 400000);
#else
Wire.begin(I2C_MASTER, I2C_ADDRESS, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
#endif
#else
// non-master mode
#ifdef DEBUG
Serial.println("Enabling i2c enabled in SLAVE mode");
#endif
#ifdef V125
Wire1.begin(I2C_SLAVE, I2C_ADDRESS, I2C_PINS_29_30, I2C_PULLUP_EXT, 400000);
Wire1.onReceive(i2cWrite);
Wire1.onRequest(i2cReadRequest);
#else
Wire.begin(I2C_SLAVE, I2C_ADDRESS, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000);
Wire.onReceive(i2cWrite);
Wire.onRequest(i2cReadRequest);
#endif
#endif
// turn on the MIDI party
MIDI.begin();
midiWriteTimer.begin(writeMidi, midiInterval);
midiReadTimer.begin(readMidi, midiInterval);
#ifdef LED
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
#endif
}
/*
* The main read loop that goes through all of the sliders
*/
void loop()
{
// read loop using the i counter
for (i = 0; i < channelCount; i++)
{
#ifdef V125
temp = analogRead(ports[i]); // mux goes into A0
#else
// set mux to appropriate channel
mux.channel(muxMapping[i]);
// read the value
temp = analogRead(0); // mux goes into A0
#endif
// put the value into the smoother
analog[i]->update(temp);
// read from the smoother, constrain (to account for tolerances), and map it
temp = analog[i]->getValue();
#ifdef FLIP
temp = MAXFADER - temp;
#endif
temp = constrain(temp, MINFADER, MAXFADER);
temp = map(temp, MINFADER, MAXFADER, 0, 16383);
// map and update the value
currentValue[i] = temp;
}
if (shouldDoMidiRead)
{
doMidiRead();
noInterrupts();
shouldDoMidiRead = false;
interrupts();
}
if (shouldDoMidiWrite)
{
doMidiWrite();
noInterrupts();
shouldDoMidiWrite = false;
interrupts();
}
}
/*
* Tiny function called via interrupt
* (it's important to catch inbound MIDI messages even if we do nothing with
* them.)
*/
void readMidi()
{
shouldDoMidiRead = true;
}
/*
* Function called when shouldDoMidiRead flag is HIGH
*/
void doMidiRead()
{
MIDI.read();
usbMIDI.read();
}
/*
* Tiny function called via interrupt
*/
void writeMidi()
{
shouldDoMidiWrite = true;
}
/*
* The function that writes changes in slider positions out the midi ports
* Called when shouldDoMidiWrite flag is HIGH
*/
void doMidiWrite()
{
// write loop using the q counter (
// (can't use i or temp cuz this might interrupt the reads)
for (q = 0; q < channelCount; q++)
{
notShiftyTemp = currentValue[q];
// shift for MIDI precision (0-127)
shiftyTemp = notShiftyTemp >> 7;
// if there was a change in the midi value
if (shiftyTemp != lastMidiValue[q])
{
// send the message over USB and physical MIDI
usbMIDI.sendControlChange(usb_ccs[q], shiftyTemp, usb_channels[q]);
MIDI.sendControlChange(trs_ccs[q], shiftyTemp, trs_channels[q]);
// store the shifted value for future comparison
lastMidiValue[q] = shiftyTemp;
#ifdef DEBUG
Serial.printf("MIDI[%d]: %d\n", q, shiftyTemp);
#endif
}
#ifdef MASTER
// we send out to all three supported i2c slave devices
// keeps the firmware simple :)
if (notShiftyTemp != lastValue[q])
{
#ifdef DEBUG
Serial.printf("i2c Master[%d]: %d\n", q, notShiftyTemp);
#endif
// for 4 output devices
port = q % 4;
device = q / 4;
// TXo
sendi2c(0x60, device, 0x11, port, notShiftyTemp);
// ER-301
sendi2c(0x31, 0, 0x11, q, notShiftyTemp);
// ANSIBLE
sendi2c(0x20, device << 1, 0x06, port, notShiftyTemp);
lastValue[q] = notShiftyTemp;
}
#endif
}
}
#ifdef MASTER
/*
* Sends an i2c command out to a slave when running in master mode
*/
void sendi2c(uint8_t model, uint8_t deviceIndex, uint8_t cmd, uint8_t devicePort, int value)
{
valueTemp = (uint16_t)value;
messageBuffer[2] = valueTemp >> 8;
messageBuffer[3] = valueTemp & 0xff;
#ifdef V125
Wire1.beginTransmission(model + deviceIndex);
messageBuffer[0] = cmd;
messageBuffer[1] = (uint8_t)devicePort;
Wire1.write(messageBuffer, 4);
Wire1.endTransmission();
#else
Wire.beginTransmission(model + deviceIndex);
messageBuffer[0] = cmd;
messageBuffer[1] = (uint8_t)devicePort;
Wire.write(messageBuffer, 4);
Wire.endTransmission();
#endif
}
#else
/*
* The function that responds to a command from i2c.
* In the first version, this simply sets the port to be read from.
*/
void i2cWrite(size_t len)
{
#ifdef DEBUG
Serial.printf("i2c Write (%d)\n", len);
#endif
// parse the response
TxResponse response = TxHelper::Parse(len);
// true command our setting of the input for a read?
if (len == 1)
{
// use a helper to decode the command
TxIO io = TxHelper::DecodeIO(response.Command);
#ifdef DEBUG
Serial.printf("Port: %d; Mode: %d [%d]\n", io.Port, io.Mode, response.Command);
#endif
// this is the single byte that sets the active input
activeInput = io.Port;
activeMode = io.Mode;
}
else
{
// act on the command
actOnCommand(response.Command, response.Output, response.Value);
}
}
/*
* The function that responds to read requests over i2c.
* This uses the port from the write request to determine which slider to send.
*/
void i2cReadRequest()
{
#ifdef DEBUG
Serial.print("i2c Read\n");
#endif
// get and cast the value
uint16_t shiftReady = 0;
switch (activeMode)
{
case 1:
shiftReady = (uint16_t)currentValue[activeInput];
break;
case 2:
shiftReady = (uint16_t)currentValue[activeInput];
break;
default:
shiftReady = (uint16_t)currentValue[activeInput];
break;
}
#ifdef DEBUG
Serial.printf("delivering: %d; value: %d [%d]\n", activeInput, currentValue[activeInput], shiftReady);
#endif
// send the puppy as a pair of bytes
#ifdef V125
Wire1.write(shiftReady >> 8);
Wire1.write(shiftReady & 255);
#else
Wire.write(shiftReady >> 8);
Wire.write(shiftReady & 255);
#endif
}
/*
* Future function if we add more i2c capabilities beyond reading values.
*/
void actOnCommand(byte cmd, byte out, int value) {}
#endif