aboutsummaryrefslogtreecommitdiff
path: root/Software/LGL/PacketMidi.c
blob: b29f5bf8d798b0b778e2624fb741a79b78e01380 (plain)
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
/*-------------------------------------------------------------------------------------PacketMidi.c
 * 
 * Draft of Suspect Devices "PacketMidi" midi library. 
 * I started this because I needed a library that I could embed in client projects
 * Some of the better libraries are gpl only and therefor could not be used where
 * the client wanted control over their end product and code. This library is there
 * for bsd liscensed. 
 *
 * The philosophy of PacketMidi is based on the model presented in the usb spec
 * where all midi events are 4 bytes long, serialization (sic) and deserialialization
 * is handled by the uart interupts and uart output routines. 
 * events are queued and handled in the main loop.
 *
 *  
 * Created by Donald D Davis on 9/9/11.
 *
 * Copyright (c) 2011 Donald Delmar Davis, Suspect Devices
 *
 * 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.
 * 
 */
#include <avr/io.h>
#include <avr/interrupt.h>
#include "PacketMidi.h"
// should use fancy use if __AVR_ATMEGA168__
#if defined(__AVR_ATmega328__) || defined(__AVR_ATmega168__)
#define MIDI_USES_UART0 1
#endif

#ifndef DEFAULT_MIDI_CHANNEL
#define DEFAULT_MIDI_CHANNEL	0
#define DEFAULT_MIDI_CABLE_NO	0
#endif


volatile uint8_t Channel = DEFAULT_MIDI_CHANNEL;



/*---------------------------------------dispatchMidiEvent(MIDI_EVENT_PACKET_t e)
 * 
 */


void dispatchMidiEvent(MIDI_EVENT_PACKET_t e) {
	
    if (CIN_IS_SYSEX(e.cin)) {
		sysexInterpreter(e);
    } else {
        doLocalMidiEvent(e);
		routeToUart(e);
    }
}


#define MAX_EVENT_QUEUE_LENGTH 16
/*------------------------------------------------------------------------------
 * uart event queue routines. 
 * these are brutally rudimentary at the moment.
 *----------------------------------------------------------------------------*/

MIDI_EVENT_PACKET_t UARTMIDIEventQueue[MAX_EVENT_QUEUE_LENGTH];
uint8_t UARTMIDIEventQueueHead=0;
uint8_t UARTMIDIEventQueueTail=0;
/*------------------------------------------queueMidiEvent(MIDI_EVENT_PACKET_t e)
 * add packet to queue 
 */

void queueMidiEvent(MIDI_EVENT_PACKET_t e) {
	UARTMIDIEventQueue[UARTMIDIEventQueueTail]=e;
	if (++UARTMIDIEventQueueTail>=MAX_EVENT_QUEUE_LENGTH) {
		UARTMIDIEventQueueTail=0;
	} // some bounds checking should be here!!!
}
/*------------------------------------------------------------midiEventInQueue() 
 * 
 */

bool midiEventInQueue() {
	return (UARTMIDIEventQueueHead!=UARTMIDIEventQueueTail);
}

/*------------------------------------------------------------------------------
 * uart event queue routines. 
 *----------------------------------------------------------------------------*/
MIDI_EVENT_PACKET_t nextMidiEvent() {
	// some bounds checking should be here!!!
	MIDI_EVENT_PACKET_t e = UARTMIDIEventQueue[UARTMIDIEventQueueHead];
	if (++UARTMIDIEventQueueHead>=MAX_EVENT_QUEUE_LENGTH) {
		UARTMIDIEventQueueHead=0;
	} 
	return (e);
}

/*------------------------------------------------------------------------------
 * uart setup and handling
 *----------------------------------------------------------------------------*/

/*------------------------------------------------ISR(USARTx_RX_vect, ISR_BLOCK)
 * parse incoming bytes into midi Events. 
 */

#ifndef MIDI_USES_UART0
ISR(USART1_RX_vect, ISR_BLOCK)
{
    uint8_t b = UDR1;
#else 
ISR(USART_RX_vect, ISR_BLOCK)
{
    uint8_t b = UDR0;
#endif
        
	static uint8_t needBytes;
	static uint8_t bytesIndex;
	static bool inSysex;
	static MIDI_EVENT_PACKET_t realTimeEvent;
	static MIDI_EVENT_PACKET_t pendingEvent;
	
	if (inSysex) {
		if (b == MIDIv1_SYSEX_END){
			inSysex=0;
			if (bytesIndex==3) {
				pendingEvent.cin=CIN_SYSEX_ENDS_IN_3;
				pendingEvent.midi2=b;
			} else if (bytesIndex==2) {
				pendingEvent.cin=CIN_SYSEX_ENDS_IN_2;
				pendingEvent.midi1=b;
			} else {
				pendingEvent.cin=CIN_SYSEX_ENDS_IN_1;
				pendingEvent.midi0=b;
			}
			bytesIndex=0;
			needBytes=0;
			queueMidiEvent(pendingEvent);
			
		} else {
			if (bytesIndex==1){
				pendingEvent.midi0=b;
				bytesIndex++;
			} else if (bytesIndex==2) {
				pendingEvent.midi1=b;
				bytesIndex++;
			} else {
				pendingEvent.midi2=b;
				queueMidiEvent(pendingEvent);
				pendingEvent.cin=CIN_SYSEX;
				bytesIndex=1;
			}
			
		}
		return;
	}
	
	if (needBytes) { // if still parsing last command.
		if (bytesIndex==1){
			pendingEvent.midi0=b;
			bytesIndex++;
		} else if (bytesIndex==2) {
			pendingEvent.midi1=b;
			bytesIndex++;
		} else {
			pendingEvent.midi2=b;
		}
		needBytes--;
		if (needBytes < 1) { // if that was the last Byte			
			needBytes=0;
			queueMidiEvent(pendingEvent);
		} 
		return;	
	}
	
	
	if (MIDIv1_IS_REALTIME(b)){
		realTimeEvent.cable=DEFAULT_MIDI_CABLE_NO;
		realTimeEvent.cin=CIN_1BYTE;
		realTimeEvent.midi0=b;
		queueMidiEvent(realTimeEvent);
		return ;
	}
	if (MIDIv1_IS_VOICE(b)){
        
		pendingEvent.cable = DEFAULT_MIDI_CABLE_NO;
		pendingEvent.cin = (b>>4)&0x0f;
		pendingEvent.midi0 = b;
		if((MIDIv1_VOICE_COMMAND(b) == MIDIv1_PROGRAM_CHANGE)
		   || (MIDIv1_VOICE_COMMAND(b) == MIDIv1_CHANNEL_PRESSURE)
		   ) {
			needBytes = 1;
		} else {
			needBytes = 2;
		}
		bytesIndex = 2;
		return;
	}
	if (MIDIv1_IS_SYSCOMMON(b)){
		pendingEvent.cable = DEFAULT_MIDI_CABLE_NO;
		switch (b) {
			case MIDIv1_SYSEX_START :
				inSysex = 1;
				needBytes = 2; 
				bytesIndex = 2;
				pendingEvent.cin = CIN_SYSEX;
				pendingEvent.midi0 = b;
				break;
			case MIDIv1_SONG_POSITION_PTR :
				pendingEvent.cin = CIN_3BYTE_SYS_COMMON;
				pendingEvent.midi0 = b;
				needBytes=2;
				bytesIndex=2;
				break;
			case MIDIv1_TUNE_REQUEST :
				pendingEvent.cin = CIN_1BYTE;
				pendingEvent.midi0 = b;
				queueMidiEvent(pendingEvent);
				break;
			case MIDIv1_MTC_QUARTER_FRAME :
			case MIDIv1_SONG_SELECT :
				pendingEvent.cin = CIN_2BYTE_SYS_COMMON;
				pendingEvent.midi0 = b;
				needBytes = 1;
				bytesIndex = 2;
				break;
                // case MIDIv1_SYSEX_END handled up top
		}
		return;
	}
	
}

/*---------------------------------------------routeToUart(MIDI_EVENT_PACKET_t E)
 * 
 */
void routeToUart(MIDI_EVENT_PACKET_t E) {
	// we will handle these when they are defined in the standard.
	//	if (E.cin<CIN_2BYTE_SYS_COMMON ) { 
	//		return;
	//	}
	sendUartMidiByte(E.midi0); // we always send at least data1
	if ((E.cin==CIN_1BYTE)
		||(E.cin==CIN_SYSEX_ENDS_IN_1)
		) {
		return;
	}
	sendUartMidiByte(E.midi1); // we sometimes send data2
	if ((E.cin==CIN_2BYTE_SYS_COMMON)
		|| (E.cin==CIN_SYSEX_ENDS_IN_2)
		|| (E.cin==CIN_PROGRAM_CHANGE)
		|| (E.cin==CIN_CHANNEL_PRESSURE)
		) {
		return;
	}
    sendUartMidiByte(E.midi2); // the rest of the time we send data3
}

/*---------------------------------------------------sendUartMidiByte(uint8_t b)
 * 
 */
void sendUartMidiByte(uint8_t b) {	
#ifndef MIDI_USES_UART0
	while (!(UCSR1A & (1 << UDRE1)));
	UDR1 = b;	
#else 
	while (!(UCSR0A & (1 << UDRE0)));
	UDR0 = b;	
#endif
}

/*---------------------------------------------------------------setupMidiUart()
 * 
 */
#define UART_USE_u2x 1

void setupMidiUart()   {                
    uint16_t baud;
#ifndef MIDI_USES_UART0    
#ifdef	UART_USE_u2x 
    UCSR1A = _BV(U2X1);
    baud = (F_CPU / 4 / MIDIv1_BAUD_RATE - 1) / 2;
#else 
    UCSR1A = 0;
    baud = (F_CPU / 8 / MIDIv1_BAUD_RATE - 1) / 2;
#endif
    UBRR1 = baud;
	UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
#else
#ifdef	UART_USE_u2x 
    UCSR0A = _BV(U2X0);
    baud = (F_CPU / 4 / MIDIv1_BAUD_RATE - 1) / 2;
#else 
    UCSR0A = 0;
    baud = (F_CPU / 8 / MIDIv1_BAUD_RATE - 1) / 2;
#endif
    UBRR0 = baud;
	UCSR0B = ((1 << RXCIE0) | (1 << TXEN0) | (1 << RXEN0));
#endif
}