-
Notifications
You must be signed in to change notification settings - Fork 3
/
HybridMidiAttiny.h
executable file
·376 lines (317 loc) · 7.68 KB
/
HybridMidiAttiny.h
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
/*
HybridMidiAttiny
V-USB / I2C Slave hybrid MIDI Arduino Library for mi:muz:expr
based :
[1] V-USB by Objective Development Software GmbH
http://www.obdev.at/products/vusb/index.html
[2] vusb-for-arduino
https://code.google.com/p/vusb-for-arduino/downloads/list
[3] Atmel Attiny45 / Attiny85 based USB MIDI controller by Thorsten
http://electronicsodyssey.blogspot.jp/2011/10/atmel-attiny45-attiny85-based-usb-midi.html
[4] TinyWireS
http://playground.arduino.cc/Code/USIi2c
https://github.com/rambo/TinyWire
[5] VUSBMidiATtiny
https://github.com/tadfmac/mi-muz/tree/master/arduino/libraries/VUSBMidiATtiny
[6] TWISlave for ATtiny841
[1]->[2]
[3]+[2]->[5]
[5]+[4]+[6]->
Licenses:
Under the GPLv2 License.
*/
#ifndef __hybridMidiAttiny_h__
#define __hybridMidiAttiny_h__
#include <Arduino.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include <avr/wdt.h>
#include "./utility/usbdrv.h"
#if defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny841__)
#include "./utility/i2c841s.h"
#include "pins_arduino.h"
#else
#include "./utility/TinyWireS.h"
#endif
#include "MimuzHelper.h"
#include "commonfunc.h"
#include "usbdef.h"
#include "queue2.h"
typedef uint8_t byte;
///////////
// queue
///////////
stB4Arrq txq;
stB4Arrq rxq;
///////////////////////////////////////////////////////////////////
// I2C
///////////////////////////////////////////////////////////////////
#ifdef __cplusplus
extern "C"{
#endif
// I2C
#if defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny841__)
void onRcv(uint8_t *pbuf, uint8_t size)
{
handleMidiMessage(pbuf,size);
}
#else
volatile byte rcvbuf[8];
void receiveEvent(uint8_t howMany)
{
byte cnt;
byte cnt2 = 1;
byte kind;
byte *p;
for(cnt = 0;cnt < howMany;cnt++){
*(rcvbuf+cnt) = TinyWireS.receive();
}
handleMidiMessage((uchar *)rcvbuf,howMany);
}
volatile byte sendpos = 0;
volatile byte sendBuffer[] = {0xff,0xff,0xff,0xff};
volatile byte isDataReady = false;
void requestEvent(void)
{
if(isDataReady){
TinyWireS.send(sendBuffer[sendpos]);
sendpos++;
if (sendpos >= 4){
sendpos = 0;
sendBuffer[0]= 0xff;
isDataReady = false;
}
}else{
sendpos = 0;
TinyWireS.send(0xff);
}
}
#endif
///////////////////////////////////////////////////////////////////
// COMMON
///////////////////////////////////////////////////////////////////
void (*cbNoteOff)(byte ch, byte note, byte vel);
void (*cbNoteOn)(byte ch, byte note, byte vel);
void (*cbCtlChange)(byte ch, byte num, byte value);
byte checkMidiMessage(byte *pMidi){
if(((*(pMidi + 1) & 0xf0)== 0x90)&&(*(pMidi + 3) != 0)){
return 2;
}else if(((*(pMidi + 1) & 0xf0)== 0x90)&&(*(pMidi + 3) == 0)){
return 1;
}else if((*(pMidi + 1) & 0xf0)== 0x80){
return 1;
}else if((*(pMidi + 1) & 0xf0)== 0xb0){
return 3;
}else{
return 0;
}
}
void handleMidiMessage(uchar *data, uchar len)
{
b4arrq_push(&rxq,data);
if(len > 4){
b4arrq_push(&rxq,data+4);
}
}
void processMidiMessage(){
uint8_t *pbuf;
uint8_t kindmessage;
if(rxq.num > 0){
pbuf = b4arrq_pop(&rxq);
kindmessage = checkMidiMessage(pbuf);
if(kindmessage == 1){
if(cbNoteOff != NULL){
(*cbNoteOff)(*(pbuf+1)&0x0f,*(pbuf+2)&0x7f,*(pbuf+3)&0x7f);
}
}else if(kindmessage == 2){
if(cbNoteOn != NULL){
(*cbNoteOn)(*(pbuf+1)&0x0f,*(pbuf+2)&0x7f,*(pbuf+3)&0x7f);
}
}else if(kindmessage == 3){
if(cbCtlChange != NULL){
(*cbCtlChange)(*(pbuf+1)&0x0f,*(pbuf+2)&0x7f,*(pbuf+3)&0x7f);
}
}
}
}
#ifdef __cplusplus
} // extern "C"
#endif
///////////////////////////////////////////////////////////////////
// HybridMidiDevice Class
///////////////////////////////////////////////////////////////////
#define MODE_I2C 0
#define MODE_USB 1
#define setI2CAddr(x) ((x)<<1)
#define setI2CAddrGCE(x) (((x)<<1) | 0x01)
#define HMMAddr_None 0x26
#define HMMAddr_0 0x30
#define HMMAddr_1 0x31
#define HMMAddr_2 0x32
#define HMMAddr_3 0x33
#define HMMAddr_4 0x34
#define HMMAddr_5 0x35
#define HMMAddr_6 0x36
#define HMMAddr_7 0x37
class HybridMidiDevice {
public:
HybridMidiDevice () {
}
void init(uint8_t addr){
slaveaddr = addr;
ledInit();
modeRead();
b4arrq_init(&rxq);
if(mode){
#if defined (__AVR_ATtiny24__) || defined (__AVR_ATtiny44__) || defined (__AVR_ATtiny84__)
ACSR |= (1<<ACD); // Disable analog comparator
#elif defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny841__)
ACSR0A |= (1<<ACD0); // Disable analog comparator
ACSR1A |= (1<<ACD1); // Disable analog comparator
#endif
cli();
#if defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__)
calibrateOscillator();
#endif
usbInit();
usbDeviceDisconnect();
delayMs(250);
usbDeviceConnect();
cbNoteOff = NULL;
cbNoteOn = NULL;
cbCtlChange = NULL;
sei();
}else{
#if defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny841__)
if(slaveaddr == 0x26){
i2cs.init(setI2CAddrGCE(slaveaddr));
}else{
i2cs.init(setI2CAddr(slaveaddr));
}
i2cs.setOnReceived(onRcv);
#else
TinyWireS.begin(slaveaddr); // init I2C Slave mode
TinyWireS.onReceive(receiveEvent);
TinyWireS.onRequest(requestEvent);
#endif
}
#if defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny841__)
T841_TIMER_PINS_DEFAULT;
#endif
}
void update() {
processMidiMessage();
if(mode){ // USB
usbPoll();
}else{ // I2C
#if !(defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny841__))
TinyWireS_stop_check();
#endif
}
}
void check() {
while (!usbInterruptIsReady()) {
usbPoll();
}
}
void sendNoteOn(byte ch, byte note, byte vel){
buffer[0] = 0x09;
buffer[1] = 0x90 | ch;
buffer[2] = 0x7f & note;
buffer[3] = 0x7f & vel;
sendMidiMessage(buffer,4);
}
void sendNoteOff(byte ch, byte note){
buffer[0] = 0x08;
buffer[1] = 0x80 | ch;
buffer[2] = 0x7f & note;
buffer[3] = 0;
sendMidiMessage(buffer,4);
}
void sendCtlChange(byte ch, byte num, byte value){
buffer[0] = 0x0b;
buffer[1] = 0xb0 | ch;
buffer[2] = 0x7f & num;
buffer[3] = 0x7f & value;
sendMidiMessage(buffer,4);
}
void setHdlNoteOff(void (*fptr)(byte ch, byte note, byte vel)){
cbNoteOff = fptr;
}
void setHdlNoteOn(void (*fptr)(byte ch, byte note, byte vel)){
cbNoteOn = fptr;
}
void setHdlCtlChange(void (*fptr)(byte ch, byte num, byte value)){
cbCtlChange = fptr;
}
void delayMs(unsigned int ms){
for( int i=0; i<ms; i++ ) {
#if !(defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny841__))
if(!mode){
TinyWireS_stop_check();
}
#endif
delayMicroseconds(1000);
}
}
private:
byte buffer[4];
uint8_t mode;
uint8_t slaveaddr;
void modeRead(){
int p;
USBPLUS_DDR &= ~USBPLUS_MASK;
delayMs(500);
p = USBPLUS_PIN;
if(p & USBPLUS_MASK){
mode = MODE_I2C;
ledOn();
delayMs(300);
ledOff();
delayMs(300);
ledOn();
delayMs(300);
ledOff();
delayMs(300);
}else{
mode = MODE_USB;
ledOn();
delayMs(300);
ledOff();
delayMs(300);
}
}
void sendMidiMessage(byte *data, byte size) {
if(size != 4){
return; // This function is now support only 4 byte data.
}
if(mode){ // USB
while (!usbInterruptIsReady()) {
usbPoll();
}
usbSetInterrupt(data, size);
}else{ // I2C
#if defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny841__)
i2cs.setSendData(data,size);
#else
setSendData(data,size);
#endif
}
}
#if !(defined (__AVR_ATtiny441__) || defined (__AVR_ATtiny841__))
void setSendData(uint8_t *data, uint8_t size){
cli();
sendBuffer[0] = data[0];
sendBuffer[1] = data[1];
sendBuffer[2] = data[2];
sendBuffer[3] = data[3];
sendpos = 0;
isDataReady = true;
sei();
}
#endif
};
HybridMidiDevice HybridMidi;
#endif // __hybridMidiAttiny_h__