MIDI Stop Controller V1.0 suitable for driving Hauptwerk digital organ software. Written for the KL25Z uses BusInOut and will drive up to 16 illuminated push buttons each switch uses a single I/O pin to both drive the LED and act as a switch input. Pressing a button will alternately send MIDI note on / off messages and turn the LED on or off. If corresponding MIDI note on/off messages are received these will be used to drive the LEDs in preference to the locally generated LED signals. The MIDI channel used to send can be selected by jumpers on 4 pins of the J2 header.
USBMIDI/USBMIDI.cpp@0:aac55e1fc12f, 2013-09-14 (annotated)
- Committer:
- djbottrill
- Date:
- Sat Sep 14 21:32:06 2013 +0000
- Revision:
- 0:aac55e1fc12f
V1.0 MIDI Stop Controller
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
djbottrill | 0:aac55e1fc12f | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
djbottrill | 0:aac55e1fc12f | 2 | * |
djbottrill | 0:aac55e1fc12f | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
djbottrill | 0:aac55e1fc12f | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
djbottrill | 0:aac55e1fc12f | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
djbottrill | 0:aac55e1fc12f | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
djbottrill | 0:aac55e1fc12f | 7 | * Software is furnished to do so, subject to the following conditions: |
djbottrill | 0:aac55e1fc12f | 8 | * |
djbottrill | 0:aac55e1fc12f | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
djbottrill | 0:aac55e1fc12f | 10 | * substantial portions of the Software. |
djbottrill | 0:aac55e1fc12f | 11 | * |
djbottrill | 0:aac55e1fc12f | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
djbottrill | 0:aac55e1fc12f | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
djbottrill | 0:aac55e1fc12f | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
djbottrill | 0:aac55e1fc12f | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
djbottrill | 0:aac55e1fc12f | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
djbottrill | 0:aac55e1fc12f | 17 | */ |
djbottrill | 0:aac55e1fc12f | 18 | |
djbottrill | 0:aac55e1fc12f | 19 | #include "stdint.h" |
djbottrill | 0:aac55e1fc12f | 20 | #include "USBMIDI.h" |
djbottrill | 0:aac55e1fc12f | 21 | |
djbottrill | 0:aac55e1fc12f | 22 | |
djbottrill | 0:aac55e1fc12f | 23 | USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) { |
djbottrill | 0:aac55e1fc12f | 24 | midi_evt = NULL; |
djbottrill | 0:aac55e1fc12f | 25 | USBDevice::connect(); |
djbottrill | 0:aac55e1fc12f | 26 | } |
djbottrill | 0:aac55e1fc12f | 27 | |
djbottrill | 0:aac55e1fc12f | 28 | void USBMIDI::write(MIDIMessage m) { |
djbottrill | 0:aac55e1fc12f | 29 | USBDevice::write(EPBULK_IN, m.data, 4, MAX_PACKET_SIZE_EPBULK); |
djbottrill | 0:aac55e1fc12f | 30 | } |
djbottrill | 0:aac55e1fc12f | 31 | |
djbottrill | 0:aac55e1fc12f | 32 | |
djbottrill | 0:aac55e1fc12f | 33 | void USBMIDI::attach(void (*fptr)(MIDIMessage)) { |
djbottrill | 0:aac55e1fc12f | 34 | midi_evt = fptr; |
djbottrill | 0:aac55e1fc12f | 35 | } |
djbottrill | 0:aac55e1fc12f | 36 | |
djbottrill | 0:aac55e1fc12f | 37 | |
djbottrill | 0:aac55e1fc12f | 38 | bool USBMIDI::EP2_OUT_callback() { |
djbottrill | 0:aac55e1fc12f | 39 | uint8_t buf[64]; |
djbottrill | 0:aac55e1fc12f | 40 | uint32_t len; |
djbottrill | 0:aac55e1fc12f | 41 | readEP(EPBULK_OUT, buf, &len, 64); |
djbottrill | 0:aac55e1fc12f | 42 | |
djbottrill | 0:aac55e1fc12f | 43 | if (midi_evt != NULL) { |
djbottrill | 0:aac55e1fc12f | 44 | for (int i=0; i<len; i+=4) { |
djbottrill | 0:aac55e1fc12f | 45 | midi_evt(MIDIMessage(buf+i)); |
djbottrill | 0:aac55e1fc12f | 46 | } |
djbottrill | 0:aac55e1fc12f | 47 | } |
djbottrill | 0:aac55e1fc12f | 48 | |
djbottrill | 0:aac55e1fc12f | 49 | // We reactivate the endpoint to receive next characters |
djbottrill | 0:aac55e1fc12f | 50 | readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
djbottrill | 0:aac55e1fc12f | 51 | return true; |
djbottrill | 0:aac55e1fc12f | 52 | } |
djbottrill | 0:aac55e1fc12f | 53 | |
djbottrill | 0:aac55e1fc12f | 54 | |
djbottrill | 0:aac55e1fc12f | 55 | |
djbottrill | 0:aac55e1fc12f | 56 | // Called in ISR context |
djbottrill | 0:aac55e1fc12f | 57 | // Set configuration. Return false if the |
djbottrill | 0:aac55e1fc12f | 58 | // configuration is not supported. |
djbottrill | 0:aac55e1fc12f | 59 | bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) { |
djbottrill | 0:aac55e1fc12f | 60 | if (configuration != DEFAULT_CONFIGURATION) { |
djbottrill | 0:aac55e1fc12f | 61 | return false; |
djbottrill | 0:aac55e1fc12f | 62 | } |
djbottrill | 0:aac55e1fc12f | 63 | |
djbottrill | 0:aac55e1fc12f | 64 | // Configure endpoints > 0 |
djbottrill | 0:aac55e1fc12f | 65 | addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK); |
djbottrill | 0:aac55e1fc12f | 66 | addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
djbottrill | 0:aac55e1fc12f | 67 | |
djbottrill | 0:aac55e1fc12f | 68 | // We activate the endpoint to be able to receive data |
djbottrill | 0:aac55e1fc12f | 69 | readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
djbottrill | 0:aac55e1fc12f | 70 | return true; |
djbottrill | 0:aac55e1fc12f | 71 | } |
djbottrill | 0:aac55e1fc12f | 72 | |
djbottrill | 0:aac55e1fc12f | 73 | |
djbottrill | 0:aac55e1fc12f | 74 | uint8_t * USBMIDI::stringIinterfaceDesc() { |
djbottrill | 0:aac55e1fc12f | 75 | static uint8_t stringIinterfaceDescriptor[] = { |
djbottrill | 0:aac55e1fc12f | 76 | 0x0c, //bLength |
djbottrill | 0:aac55e1fc12f | 77 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
djbottrill | 0:aac55e1fc12f | 78 | 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio |
djbottrill | 0:aac55e1fc12f | 79 | }; |
djbottrill | 0:aac55e1fc12f | 80 | return stringIinterfaceDescriptor; |
djbottrill | 0:aac55e1fc12f | 81 | } |
djbottrill | 0:aac55e1fc12f | 82 | |
djbottrill | 0:aac55e1fc12f | 83 | uint8_t * USBMIDI::stringIproductDesc() { |
djbottrill | 0:aac55e1fc12f | 84 | static uint8_t stringIproductDescriptor[] = { |
djbottrill | 0:aac55e1fc12f | 85 | 0x16, //bLength |
djbottrill | 0:aac55e1fc12f | 86 | STRING_DESCRIPTOR, //bDescriptorType 0x03 |
djbottrill | 0:aac55e1fc12f | 87 | 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio |
djbottrill | 0:aac55e1fc12f | 88 | }; |
djbottrill | 0:aac55e1fc12f | 89 | return stringIproductDescriptor; |
djbottrill | 0:aac55e1fc12f | 90 | } |
djbottrill | 0:aac55e1fc12f | 91 | |
djbottrill | 0:aac55e1fc12f | 92 | |
djbottrill | 0:aac55e1fc12f | 93 | uint8_t * USBMIDI::configurationDesc() { |
djbottrill | 0:aac55e1fc12f | 94 | static uint8_t configDescriptor[] = { |
djbottrill | 0:aac55e1fc12f | 95 | // configuration descriptor |
djbottrill | 0:aac55e1fc12f | 96 | 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50, |
djbottrill | 0:aac55e1fc12f | 97 | |
djbottrill | 0:aac55e1fc12f | 98 | // The Audio Interface Collection |
djbottrill | 0:aac55e1fc12f | 99 | 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor |
djbottrill | 0:aac55e1fc12f | 100 | 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor |
djbottrill | 0:aac55e1fc12f | 101 | 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors |
djbottrill | 0:aac55e1fc12f | 102 | 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor |
djbottrill | 0:aac55e1fc12f | 103 | |
djbottrill | 0:aac55e1fc12f | 104 | // MIDI IN JACKS |
djbottrill | 0:aac55e1fc12f | 105 | 0x06, 0x24, 0x02, 0x01, 0x01, 0x00, |
djbottrill | 0:aac55e1fc12f | 106 | 0x06, 0x24, 0x02, 0x02, 0x02, 0x00, |
djbottrill | 0:aac55e1fc12f | 107 | |
djbottrill | 0:aac55e1fc12f | 108 | // MIDI OUT JACKS |
djbottrill | 0:aac55e1fc12f | 109 | 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, |
djbottrill | 0:aac55e1fc12f | 110 | 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00, |
djbottrill | 0:aac55e1fc12f | 111 | |
djbottrill | 0:aac55e1fc12f | 112 | // OUT endpoint descriptor |
djbottrill | 0:aac55e1fc12f | 113 | 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, |
djbottrill | 0:aac55e1fc12f | 114 | 0x05, 0x25, 0x01, 0x01, 0x01, |
djbottrill | 0:aac55e1fc12f | 115 | |
djbottrill | 0:aac55e1fc12f | 116 | // IN endpoint descriptor |
djbottrill | 0:aac55e1fc12f | 117 | 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, |
djbottrill | 0:aac55e1fc12f | 118 | 0x05, 0x25, 0x01, 0x01, 0x03, |
djbottrill | 0:aac55e1fc12f | 119 | }; |
djbottrill | 0:aac55e1fc12f | 120 | return configDescriptor; |
djbottrill | 0:aac55e1fc12f | 121 | } |