Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

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