-
Notifications
You must be signed in to change notification settings - Fork 0
/
midifoot.c
301 lines (272 loc) · 11.2 KB
/
midifoot.c
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
/* Name: midifoot.c
* Project: Single button midi controller
* Author: Bill Peterson
*/
#include <avr/io.h>
#include <avr/interrupt.h> /* for sei() */
#include <avr/pgmspace.h> /* required by usbdrv.h */
#include <util/delay.h> /* for _delay_ms() */
#include <avr/wdt.h>
#include "usbdrv.h"
#if USE_INCLUDE
#include "usbdrv.c"
#endif
// This descriptor is based on http://www.usb.org/developers/devclass_docs/midi10.pdf
//
// Appendix B. Example: Simple MIDI Adapter (Informative)
// B.1 Device Descriptor
const static PROGMEM char deviceDescrMIDI[] = { /* USB device descriptor */
18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */
USBDESCR_DEVICE, /* descriptor type */
0x10, 0x01, /* USB version supported */
0, /* device class: defined at interface level */
0, /* subclass */
0, /* protocol */
8, /* max packet size */
USB_CFG_VENDOR_ID, /* 2 bytes */
USB_CFG_DEVICE_ID, /* 2 bytes */
USB_CFG_DEVICE_VERSION, /* 2 bytes */
1, /* manufacturer string index */
2, /* product string index */
0, /* serial number string index */
1, /* number of configurations */
};
// B.2 Configuration Descriptor
const static PROGMEM char configDescrMIDI[] = { /* USB configuration descriptor */
9, /* sizeof(usbDescrConfig): length of descriptor in bytes */
USBDESCR_CONFIG, /* descriptor type */
101, 0, /* total length of data returned (including inlined descriptors) */
2, /* number of interfaces in this configuration */
1, /* index of this configuration */
0, /* configuration name string index */
#if USB_CFG_IS_SELF_POWERED
USBATTR_SELFPOWER, /* attributes */
#else
USBATTR_BUSPOWER, /* attributes */
#endif
USB_CFG_MAX_BUS_POWER / 2, /* max USB current in 2mA units */
// B.3 AudioControl Interface Descriptors
// The AudioControl interface describes the device structure (audio function topology)
// and is used to manipulate the Audio Controls. This device has no audio function
// incorporated. However, the AudioControl interface is mandatory and therefore both
// the standard AC interface descriptor and the classspecific AC interface descriptor
// must be present. The class-specific AC interface descriptor only contains the header
// descriptor.
// B.3.1 Standard AC Interface Descriptor
// The AudioControl interface has no dedicated endpoints associated with it. It uses the
// default pipe (endpoint 0) for all communication purposes. Class-specific AudioControl
// Requests are sent using the default pipe. There is no Status Interrupt endpoint provided.
/* AC interface descriptor follows inline: */
9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
USBDESCR_INTERFACE, /* descriptor type */
0, /* index of this interface */
0, /* alternate setting for this interface */
0, /* endpoints excl 0: number of endpoint descriptors to follow */
1, /* */
1, /* */
0, /* */
0, /* string index for interface */
// B.3.2 Class-specific AC Interface Descriptor
// The Class-specific AC interface descriptor is always headed by a Header descriptor
// that contains general information about the AudioControl interface. It contains all
// the pointers needed to describe the Audio Interface Collection, associated with the
// described audio function. Only the Header descriptor is present in this device
// because it does not contain any audio functionality as such.
/* AC Class-Specific descriptor */
9, /* sizeof(usbDescrCDC_HeaderFn): length of descriptor in bytes */
36, /* descriptor type */
1, /* header functional descriptor */
0x0, 0x01, /* bcdADC */
9, 0, /* wTotalLength */
1, /* */
1, /* */
// B.4 MIDIStreaming Interface Descriptors
// B.4.1 Standard MS Interface Descriptor
/* interface descriptor follows inline: */
9, /* length of descriptor in bytes */
USBDESCR_INTERFACE, /* descriptor type */
1, /* index of this interface */
0, /* alternate setting for this interface */
2, /* endpoints excl 0: number of endpoint descriptors to follow */
1, /* AUDIO */
3, /* MS */
0, /* unused */
0, /* string index for interface */
// B.4.2 Class-specific MS Interface Descriptor
/* MS Class-Specific descriptor */
7, /* length of descriptor in bytes */
36, /* descriptor type */
1, /* header functional descriptor */
0x0, 0x01, /* bcdADC */
65, 0, /* wTotalLength */
// B.4.3 MIDI IN Jack Descriptor
6, /* bLength */
36, /* descriptor type */
2, /* MIDI_IN_JACK desc subtype */
1, /* EMBEDDED bJackType */
1, /* bJackID */
0, /* iJack */
6, /* bLength */
36, /* descriptor type */
2, /* MIDI_IN_JACK desc subtype */
2, /* EXTERNAL bJackType */
2, /* bJackID */
0, /* iJack */
//B.4.4 MIDI OUT Jack Descriptor
9, /* length of descriptor in bytes */
36, /* descriptor type */
3, /* MIDI_OUT_JACK descriptor */
1, /* EMBEDDED bJackType */
3, /* bJackID */
1, /* No of input pins */
2, /* BaSourceID */
1, /* BaSourcePin */
0, /* iJack */
9, /* bLength of descriptor in bytes */
36, /* bDescriptorType */
3, /* MIDI_OUT_JACK bDescriptorSubtype */
2, /* EXTERNAL bJackType */
4, /* bJackID */
1, /* bNrInputPins */
1, /* baSourceID (0) */
1, /* baSourcePin (0) */
0, /* iJack */
// B.5 Bulk OUT Endpoint Descriptors
//B.5.1 Standard Bulk OUT Endpoint Descriptor
9, /* bLenght */
USBDESCR_ENDPOINT, /* bDescriptorType = endpoint */
0x1, /* bEndpointAddress OUT endpoint number 1 */
3, /* bmAttributes: 2:Bulk, 3:Interrupt endpoint */
8, 0, /* wMaxPacketSize */
10, /* bIntervall in ms */
0, /* bRefresh */
0, /* bSyncAddress */
// B.5.2 Class-specific MS Bulk OUT Endpoint Descriptor
5, /* bLength of descriptor in bytes */
37, /* bDescriptorType */
1, /* bDescriptorSubtype */
1, /* bNumEmbMIDIJack */
1, /* baAssocJackID (0) */
//B.6 Bulk IN Endpoint Descriptors
//B.6.1 Standard Bulk IN Endpoint Descriptor
9, /* bLenght */
USBDESCR_ENDPOINT, /* bDescriptorType = endpoint */
0x81, /* bEndpointAddress IN endpoint number 1 */
3, /* bmAttributes: 2: Bulk, 3: Interrupt endpoint */
8, 0, /* wMaxPacketSize */
10, /* bIntervall in ms */
0, /* bRefresh */
0, /* bSyncAddress */
// B.6.2 Class-specific MS Bulk IN Endpoint Descriptor
5, /* bLength of descriptor in bytes */
37, /* bDescriptorType */
1, /* bDescriptorSubtype */
1, /* bNumEmbMIDIJack (0) */
3, /* baAssocJackID (0) */
};
// provide the custom descriptor
usbMsgLen_t usbFunctionDescriptor(usbRequest_t * rq)
{
if (rq->wValue.bytes[1] == USBDESCR_DEVICE) {
usbMsgPtr = (uchar *) deviceDescrMIDI;
return sizeof(deviceDescrMIDI);
} else { /* must be config descriptor */
usbMsgPtr = (uchar *) configDescrMIDI;
return sizeof(configDescrMIDI);
}
}
// no special setup needed
usbMsgLen_t usbFunctionSetup(uchar data[8])
{
return 0;
}
// midi packets
// byte 0: packet header - cable number (always 0), code index (msg type)
// midi byte 1 - msg type, channel
// midi byte 2 - note or CC number
// midi byte 3 - velocity or value
// msg types:
// 8 - note-on
// 9 - note-off
// A - poly-keypress
// B - control change
// C - program change
// D - channel pressure
// E - pitch bend
uchar midiPkt[16][4] = {
{0x0B, 0xBE, 0x40, 0x46}, // [0] ch15 cc64 (hold pedal) = 70 (on)
{0x0B, 0xBE, 0x40, 0x00}, // [1] ch15 cc64 (hold pedal) = 0 (off)
{0x0B, 0xBE, 0x40, 0x64}, // [2] ch15 cc64 (hold pedal) = 100 (on)
{0x0B, 0xBE, 0x40, 0x1e}, // [3] ch15 cc64 (hold pedal) = 30 (off)
{0x0B, 0xBE, 0x40, 0x55}, // [4] ch15 cc64 (hold pedal) = 85 (on)
{0x0B, 0xBE, 0x40, 0x0f}, // [5] ch15 cc64 (hold pedal) = 15 (off)
{0x0B, 0xBE, 0x40, 0x73}, // [6] ch15 cc64 (hold pedal) = 115 (on)
{0x0B, 0xBE, 0x40, 0x2d}, // [7] ch15 cc64 (hold pedal) = 45 (off)
{0x0B, 0xBE, 0x40, 0x4b}, // [8] ch15 cc64 (hold pedal) = 75 (on)
{0x0B, 0xBE, 0x40, 0x05}, // [9] ch15 cc64 (hold pedal) = 5 (off)
{0x0B, 0xBE, 0x40, 0x69}, // [10] ch15 cc64 (hold pedal) = 105 (on)
{0x0B, 0xBE, 0x40, 0x23}, // [11] ch15 cc64 (hold pedal) = 35 (off)
{0x0B, 0xBE, 0x40, 0x50}, // [12] ch15 cc64 (hold pedal) = 80 (on)
{0x0B, 0xBE, 0x40, 0x0a}, // [13] ch15 cc64 (hold pedal) = 10 (off)
{0x0B, 0xBE, 0x40, 0x6e}, // [14] ch15 cc64 (hold pedal) = 110 (on)
{0x0B, 0xBE, 0x40, 0x28}, // [15] ch15 cc64 (hold pedal) = 40 (off)
};
#define MSG_COUNT 16
uint8_t msgNum = 0;
uint8_t lastReading = 1;
uint8_t buttonState = 1;
uint8_t buttonChanged = 0;
int main(void)
{
MCUSR = 0;
wdt_disable();
usbInit();
usbDeviceDisconnect(); // enforce re-enumeration
uint8_t i = 0;
while(--i) // fake USB disconnect for > 250 ms
{
_delay_ms(1);
}
usbDeviceConnect();
wdt_enable(WDTO_500MS);
sei();
DDRB &= ~(1 << PB0); // set PB0 as input (default)
PORTB |= (1 << PB0); // enable pullup on PB0
TCCR1 |= (1 << CTC1); // clear timer on compare match
TCCR1 |= (1 << CS13); // clock prescaler 128
OCR1C = 5; // reset timer every 80 ms ([1 / (16E6 / 128)] * 5 = 40us)
for(;;) // main event loop
{
wdt_reset(); // reset the watchdog timer
usbPoll();
if (!buttonChanged)
{
buttonState = PINB & (1 << PB0);
if (buttonState != lastReading)
{
TCNT1 = 0x00;
TCCR1 &= ~(1 << CTC1); // cancel timer restart on compare
lastReading = buttonState;
}
if (TCNT1 > 25) // 200ms and no button change
{
TCNT1 = 0x00;
TCCR1 |= (1 << CTC1); // restart timer if > 40ms
buttonChanged = 1;
}
}
else
{
if (usbInterruptIsReady())
{
if ((msgNum % 2) && !buttonState) msgNum++;
else if (!(msgNum % 2) && buttonState) msgNum++;
usbSetInterrupt(midiPkt[msgNum], 4);
msgNum++;
if (msgNum >= MSG_COUNT) msgNum = 0;
buttonChanged = 0;
}
}
}
return 0;
}