Slingshot Controller

Dependencies:   ADXL345 DebounceIn USBDevice mbed

Committer:
Brandon
Date:
Sun Oct 14 18:58:38 2012 +0000
Revision:
0:cf17ea89fd09
lab3

Who changed what in which revision?

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