-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOPA.cpp
456 lines (404 loc) · 11.4 KB
/
OPA.cpp
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
fredslab.net : OPA Shield library for Arduino
The MIT License (MIT)
Source code copyright (c) 2013-2016 Frédéric Meslin / Thomas Hopper
Email: [email protected]
Website: www.fredslab.net
Twitter: @marzacdev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Version 1.0, 30/07/2016
*/
#include "OPA.h"
#include "Arduino.h"
/*****************************************************************************/
#define OPA_CS1_STATE(a) (a & 0x01)
#define OPA_CS2_STATE(a) ((a >> 1) & 0x01)
/*****************************************************************************/
#if defined ARDUINO_LEONARDO
#define SerialPort Serial1
#elif defined ARDUINO_MEGA
#define SerialPort Serial1
#elif defined ARDUINO_UNO
#define SerialPort Serial
#elif defined ARDUINO_OTHER
#define SerialPort Serial
#else
#error "No Arduino board has been defined, please configure the opa.h file!"
#define SerialPort Serial
#endif
/*****************************************************************************/
OPA::OPA() :
address(OPA_ADDRESS_0),
error(OPA_ERROR_NONE)
{
initPins();
}
OPA::OPA(OPA_ADDRESSES address) :
address(address),
error(OPA_ERROR_NONE)
{
initPins();
}
/*****************************************************************************/
void OPA::initPins()
{
pinMode(OPA_TX_PIN, INPUT);
pinMode(OPA_RX_PIN, INPUT);
pinMode(OPA_CS1_PIN, OUTPUT);
pinMode(OPA_CS2_PIN, OUTPUT);
pinMode(OPA_SWAP_PIN, OUTPUT);
pinMode(OPA_RESET_PIN, OUTPUT);
digitalWrite(OPA_CS1_PIN, 1);
digitalWrite(OPA_CS2_PIN, 1);
digitalWrite(OPA_SWAP_PIN, 0);
digitalWrite(OPA_RESET_PIN, 0);
}
/*****************************************************************************/
void OPA::enable()
{
digitalWrite(OPA_CS1_PIN, OPA_CS1_STATE(address));
digitalWrite(OPA_CS2_PIN, OPA_CS2_STATE(address));
SerialPort.begin(OPA_BAUDRATE);
SerialPort.setTimeout(OPA_SERIAL_TIMEOUT);
clearErrors();
}
void OPA::disable()
{
SerialPort.end();
digitalWrite(OPA_CS1_PIN, 1);
digitalWrite(OPA_CS2_PIN, 1);
}
/*****************************************************************************/
void OPA::reset()
{
digitalWrite(OPA_RESET_PIN, 1);
delay(2);
digitalWrite(OPA_RESET_PIN, 0);
delay(2);
error = OPA_ERROR_NONE;
}
/*****************************************************************************/
char * OPA::readVersion()
{
static char version[24];
char buffer[1];
buffer[0] = OPA_CODE_VERSION;
SerialPort.write(buffer, 1);
if (SerialPort.readBytes(version, 24) == 24)
return version;
error = OPA_ERROR_TIMEOUT;
return 0;
}
/*****************************************************************************/
void OPA::noteOn(OPA_PROGRAMS program, uint8_t note, uint8_t fraction, uint8_t nuance)
{
char buffer[5];
buffer[0] = OPA_CODE_NOTEON;
buffer[1] = program;
buffer[2] = note;
buffer[3] = fraction;
buffer[4] = nuance;
SerialPort.write(buffer, 5);
}
void OPA::noteOff(OPA_PROGRAMS program, uint8_t note, uint8_t fraction, uint8_t nuance)
{
char buffer[5];
buffer[0] = OPA_CODE_NOTEOFF;
buffer[1] = program;
buffer[2] = note;
buffer[3] = fraction;
buffer[4] = nuance;
SerialPort.write(buffer, 5);
}
void OPA::allNotesOff(OPA_PROGRAMS program)
{
char buffer[2];
buffer[0] = OPA_CODE_ALLNOTESOFF;
buffer[1] = program;
SerialPort.write(buffer, 2);
}
void OPA::allSoundsOff()
{
char buffer[2];
buffer[0] = OPA_CODE_ALLSOUNDSOFF;
SerialPort.write(buffer, 1);
}
/*****************************************************************************/
void OPA::pitchBend(OPA_PROGRAMS program, int8_t coarse, int8_t fine)
{
char buffer[4];
buffer[0] = OPA_CODE_PITCHBEND;
buffer[1] = program;
buffer[2] = coarse;
buffer[3] = fine;
SerialPort.write(buffer, 4);
}
/*****************************************************************************/
void OPA::setMemoryProtection(bool protection)
{
int flags = readGlobalParam(OPA_GLOBAL_FLAGS);
flags &= ~OPA_GLOBAL_PROTECT;
if (protection) flags |= OPA_GLOBAL_PROTECT;
writeGlobalParam(OPA_GLOBAL_FLAGS, flags);
}
/*****************************************************************************/
void OPA::writeOperatorParam(OPA_PROGRAMS program, OPA_OPERATORS op, OPA_OP_PARAMETERS param, uint8_t value)
{
uint8_t p = op * OPA_OP_PARAMS_NB + OPA_PROGS_PARAMS_NB + param;
writeFMParam(program, p, value);
}
uint8_t OPA::readOperatorParam(OPA_PROGRAMS program, OPA_OPERATORS op, OPA_OP_PARAMETERS param)
{
uint8_t p = op * OPA_OP_PARAMS_NB + OPA_PROGS_PARAMS_NB + param;
return readFMParam(program, p);
}
/*****************************************************************************/
void OPA::writeGlobalParam(OPA_GLOBAL_PARAMETERS param, uint8_t value)
{
/** Send a parameter */
char buffer[3];
buffer[0] = OPA_CODE_GLOBALSPARAMWRITE;
buffer[1] = param;
buffer[2] = value;
SerialPort.write(buffer, 6);
}
void OPA::writeFMParam(OPA_PROGRAMS program, uint8_t param, uint8_t value)
{
/** Send a parameter */
char buffer[4];
buffer[0] = OPA_CODE_FMPARAMWRITE;
buffer[1] = program;
buffer[2] = param;
buffer[3] = value;
SerialPort.write(buffer, 4);
}
uint8_t OPA::writeKitParam(int sample, OPA_KIT_PARAMETERS param, uint8_t value)
{
/** Send a parameter */
char buffer[4];
buffer[0] = OPA_CODE_KITPARAMWRITE;
buffer[1] = sample;
buffer[2] = param;
buffer[3] = value;
SerialPort.write(buffer, 4);
}
uint8_t OPA::readGlobalParam(OPA_GLOBAL_PARAMETERS param)
{
/** Send a parameter request */
char buffer[3];
buffer[0] = OPA_CODE_GLOBALSPARAMREAD;
buffer[1] = param;
buffer[2] = 0;
SerialPort.write(buffer, 3);
/** Check the reply */
if (SerialPort.readBytes(buffer, 3) == 3) {
if (buffer[0] == OPA_CODE_GLOBALSPARAMWRITE)
return buffer[2];
error = OPA_ERROR_BADREPLY;
return 0;
}
error = OPA_ERROR_TIMEOUT;
return 0;
}
uint8_t OPA::readFMParam(OPA_PROGRAMS program, uint8_t param)
{
/** Send a parameter request */
char buffer[4];
buffer[0] = OPA_CODE_FMPARAMREAD;
buffer[1] = program;
buffer[2] = param;
buffer[3] = 0;
SerialPort.write(buffer, 4);
/** Check the reply */
if (SerialPort.readBytes(buffer, 4) == 4) {
if (buffer[0] == OPA_CODE_FMPARAMWRITE)
return buffer[3];
error = OPA_ERROR_BADREPLY;
return 0;
}
error = OPA_ERROR_TIMEOUT;
return 0;
}
uint8_t OPA::readKitParam(int sample, OPA_KIT_PARAMETERS param)
{
/** Send a parameter request */
char buffer[4];
buffer[0] = OPA_CODE_KITPARAMREAD;
buffer[1] = sample;
buffer[2] = param;
buffer[3] = 0;
SerialPort.write(buffer, 4);
/** Check the reply */
if (SerialPort.readBytes(buffer, 4) == 4) {
if (buffer[0] == OPA_CODE_KITPARAMWRITE)
return buffer[3];
error = OPA_ERROR_BADREPLY;
return 0;
}
error = OPA_ERROR_TIMEOUT;
return 0;
}
/*****************************************************************************/
void OPA::writeProgram(OPA_PROGRAMS program, OpaProgram &programData)
{
char buffer[4];
if (program >= OPA_PROGS_NB) {
error = OPA_ERROR_BADPARAMETER;
return;
}
buffer[0] = OPA_CODE_PROGRAMWRITE;
buffer[1] = program;
buffer[2] = sizeof(OpaProgram);
buffer[3] = 0;
SerialPort.write(buffer, 4);
SerialPort.write((char *) &programData, sizeof(OpaProgram));
}
void OPA::readProgram(OPA_PROGRAMS program, OpaProgram &programData)
{
char buffer[4];
if (program >= OPA_PROGS_NB) {
error = OPA_ERROR_BADPARAMETER;
return;
}
/** Clear the program */
memset(&programData, 0, sizeof(OpaProgram));
/** Send a program request */
buffer[0] = OPA_CODE_PROGRAMREAD;
buffer[1] = program;
buffer[2] = sizeof(OpaProgram);
buffer[3] = 0;
SerialPort.write(buffer, 4);
/** Check the reply */
if (SerialPort.readBytes(buffer, 4) != 4) {
error = OPA_ERROR_TIMEOUT;
return;
}
if (buffer[0] != OPA_CODE_PROGRAMWRITE) {
error = OPA_ERROR_BADREPLY;
return;
}
char * data = (char *) &programData;
if (SerialPort.readBytes(data, sizeof(OpaProgram)) != sizeof(OpaProgram)) {
error = OPA_ERROR_TIMEOUT;
return;
}
}
/*****************************************************************************/
void OPA::writeKit(OpaKit &kitData)
{
char buffer[3];
buffer[0] = OPA_CODE_KITWRITE;
buffer[1] = sizeof(OpaKit);
buffer[2] = 0;
SerialPort.write(buffer, 3);
SerialPort.write((char *) &kitData, sizeof(OpaKit));
}
void OPA::readKit(OpaKit &kitData)
{
/** Clear the kit */
char buffer[3];
memset(&kitData, 0, sizeof(OpaKit));
/** Send a program request */
buffer[0] = OPA_CODE_KITREAD;
buffer[1] = sizeof(OpaKit);
buffer[2] = 0;
SerialPort.write(buffer, 3);
/** Check the reply */
if (SerialPort.readBytes(buffer, 3) != 3) {
error = OPA_ERROR_TIMEOUT;
return;
}
if (buffer[0] != OPA_CODE_KITWRITE) {
error = OPA_ERROR_BADREPLY;
return;
}
char * data = (char *) &kitData;
if (SerialPort.readBytes(data, sizeof(OpaKit)) != sizeof(OpaKit)) {
error = OPA_ERROR_TIMEOUT;
return;
}
}
/*****************************************************************************/
void OPA::storeInternal(OPA_PROGRAMS program, uint8_t slot)
{
char buffer[3];
if (slot >= OPA_MAX_SLOTS) {
error = OPA_ERROR_BADPARAMETER;
return;
}
buffer[0] = OPA_CODE_INTERNALSTORE;
buffer[1] = program;
buffer[2] = slot;
SerialPort.write(buffer, 3);
}
void OPA::loadInternal(OPA_PROGRAMS program, uint8_t slot)
{
char buffer[3];
if (slot >= OPA_MAX_SLOTS) {
error = OPA_ERROR_BADPARAMETER;
return;
}
buffer[0] = OPA_CODE_INTERNALLOAD;
buffer[1] = program;
buffer[2] = slot;
SerialPort.write(buffer, 3);
}
/*****************************************************************************/
void OPA::writeInternal(uint8_t slot, OpaProgram &programData)
{
char buffer[4];
if (slot >= OPA_MAX_SLOTS) {
error = OPA_ERROR_BADPARAMETER;
return;
}
buffer[0] = OPA_CODE_INTERNALWRITE;
buffer[1] = slot;
buffer[2] = sizeof(OpaProgram);
buffer[3] = 0;
SerialPort.write(buffer, 4);
SerialPort.write((char *) &programData, sizeof(OpaProgram));
}
void OPA::readInternal(uint8_t slot, OpaProgram &programData)
{
char buffer[4];
if (slot >= OPA_MAX_SLOTS) {
error = OPA_ERROR_BADPARAMETER;
return;
}
/** Clear the program */
memset(&programData, 0, sizeof(OpaProgram));
/** Send a program request */
buffer[0] = OPA_CODE_INTERNALREAD;
buffer[1] = slot;
buffer[2] = sizeof(OpaProgram);
buffer[3] = 0;
SerialPort.write(buffer, 4);
/** Check the reply */
if (SerialPort.readBytes(buffer, 4) != 4) {
error = OPA_ERROR_TIMEOUT;
return;
}
if (buffer[0] != OPA_CODE_INTERNALWRITE) {
error = OPA_ERROR_BADREPLY;
return;
}
char * data = (char *) &programData;
if (SerialPort.readBytes(data, sizeof(OpaProgram)) != sizeof(OpaProgram)) {
error = OPA_ERROR_TIMEOUT;
return;
}
}