Lucas Lim / Mbed 2 deprecated HSP_Temperature_Barometer_CS3237

Dependencies:   mbed

Committer:
lucaslwl
Date:
Mon Aug 26 08:11:41 2019 +0000
Revision:
22:5c07298d3383
add library folder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lucaslwl 22:5c07298d3383 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
lucaslwl 22:5c07298d3383 2 *
lucaslwl 22:5c07298d3383 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
lucaslwl 22:5c07298d3383 4 * and associated documentation files (the "Software"), to deal in the Software without
lucaslwl 22:5c07298d3383 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
lucaslwl 22:5c07298d3383 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
lucaslwl 22:5c07298d3383 7 * Software is furnished to do so, subject to the following conditions:
lucaslwl 22:5c07298d3383 8 *
lucaslwl 22:5c07298d3383 9 * The above copyright notice and this permission notice shall be included in all copies or
lucaslwl 22:5c07298d3383 10 * substantial portions of the Software.
lucaslwl 22:5c07298d3383 11 *
lucaslwl 22:5c07298d3383 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
lucaslwl 22:5c07298d3383 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
lucaslwl 22:5c07298d3383 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
lucaslwl 22:5c07298d3383 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
lucaslwl 22:5c07298d3383 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
lucaslwl 22:5c07298d3383 17 */
lucaslwl 22:5c07298d3383 18
lucaslwl 22:5c07298d3383 19 #include "stdint.h"
lucaslwl 22:5c07298d3383 20 #include "USBMIDI.h"
lucaslwl 22:5c07298d3383 21
lucaslwl 22:5c07298d3383 22
lucaslwl 22:5c07298d3383 23 USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
lucaslwl 22:5c07298d3383 24 : USBDevice(vendor_id, product_id, product_release), cur_data(0), data_end(true)
lucaslwl 22:5c07298d3383 25 {
lucaslwl 22:5c07298d3383 26 midi_evt = NULL;
lucaslwl 22:5c07298d3383 27 USBDevice::connect();
lucaslwl 22:5c07298d3383 28 }
lucaslwl 22:5c07298d3383 29
lucaslwl 22:5c07298d3383 30 // write plain MIDIMessage that will be converted to USBMidi event packet
lucaslwl 22:5c07298d3383 31 void USBMIDI::write(MIDIMessage m) {
lucaslwl 22:5c07298d3383 32 // first byte keeped for retro-compatibility
lucaslwl 22:5c07298d3383 33 for(int p=1; p < m.length; p+=3) {
lucaslwl 22:5c07298d3383 34 uint8_t buf[4];
lucaslwl 22:5c07298d3383 35 // Midi message to USBMidi event packet
lucaslwl 22:5c07298d3383 36 buf[0]=m.data[1] >> 4;
lucaslwl 22:5c07298d3383 37 // SysEx
lucaslwl 22:5c07298d3383 38 if(buf[0] == 0xF) {
lucaslwl 22:5c07298d3383 39 if((m.length - p) > 3) {
lucaslwl 22:5c07298d3383 40 // SysEx start or continue
lucaslwl 22:5c07298d3383 41 buf[0]=0x4;
lucaslwl 22:5c07298d3383 42 } else {
lucaslwl 22:5c07298d3383 43 switch(m.length - p) {
lucaslwl 22:5c07298d3383 44 case 1:
lucaslwl 22:5c07298d3383 45 // SysEx end with one byte
lucaslwl 22:5c07298d3383 46 buf[0]=0x5;
lucaslwl 22:5c07298d3383 47 break;
lucaslwl 22:5c07298d3383 48 case 2:
lucaslwl 22:5c07298d3383 49 // SysEx end with two bytes
lucaslwl 22:5c07298d3383 50 buf[0]=0x6;
lucaslwl 22:5c07298d3383 51 break;
lucaslwl 22:5c07298d3383 52 case 3:
lucaslwl 22:5c07298d3383 53 // SysEx end with three bytes
lucaslwl 22:5c07298d3383 54 buf[0]=0x7;
lucaslwl 22:5c07298d3383 55 break;
lucaslwl 22:5c07298d3383 56 }
lucaslwl 22:5c07298d3383 57 }
lucaslwl 22:5c07298d3383 58 }
lucaslwl 22:5c07298d3383 59 buf[1]=m.data[p];
lucaslwl 22:5c07298d3383 60
lucaslwl 22:5c07298d3383 61 if(p+1 < m.length)
lucaslwl 22:5c07298d3383 62 buf[2]=m.data[p+1];
lucaslwl 22:5c07298d3383 63 else
lucaslwl 22:5c07298d3383 64 buf[2]=0;
lucaslwl 22:5c07298d3383 65
lucaslwl 22:5c07298d3383 66 if(p+2 < m.length)
lucaslwl 22:5c07298d3383 67 buf[3]=m.data[p+2];
lucaslwl 22:5c07298d3383 68 else
lucaslwl 22:5c07298d3383 69 buf[3]=0;
lucaslwl 22:5c07298d3383 70
lucaslwl 22:5c07298d3383 71 USBDevice::write(EPBULK_IN, buf, 4, MAX_PACKET_SIZE_EPBULK);
lucaslwl 22:5c07298d3383 72 }
lucaslwl 22:5c07298d3383 73 }
lucaslwl 22:5c07298d3383 74
lucaslwl 22:5c07298d3383 75
lucaslwl 22:5c07298d3383 76 void USBMIDI::attach(void (*fptr)(MIDIMessage)) {
lucaslwl 22:5c07298d3383 77 midi_evt = fptr;
lucaslwl 22:5c07298d3383 78 }
lucaslwl 22:5c07298d3383 79
lucaslwl 22:5c07298d3383 80 bool USBMIDI::EPBULK_OUT_callback() {
lucaslwl 22:5c07298d3383 81 uint8_t buf[64];
lucaslwl 22:5c07298d3383 82 uint32_t len;
lucaslwl 22:5c07298d3383 83 readEP(EPBULK_OUT, buf, &len, 64);
lucaslwl 22:5c07298d3383 84
lucaslwl 22:5c07298d3383 85 if (midi_evt != NULL) {
lucaslwl 22:5c07298d3383 86 for (uint32_t i=0; i<len; i+=4) {
lucaslwl 22:5c07298d3383 87 uint8_t data_read;
lucaslwl 22:5c07298d3383 88 data_end=true;
lucaslwl 22:5c07298d3383 89 switch(buf[i]) {
lucaslwl 22:5c07298d3383 90 case 0x2:
lucaslwl 22:5c07298d3383 91 // Two-bytes System Common Message - undefined in USBMidi 1.0
lucaslwl 22:5c07298d3383 92 data_read=2;
lucaslwl 22:5c07298d3383 93 break;
lucaslwl 22:5c07298d3383 94 case 0x4:
lucaslwl 22:5c07298d3383 95 // SysEx start or continue
lucaslwl 22:5c07298d3383 96 data_end=false;
lucaslwl 22:5c07298d3383 97 data_read=3;
lucaslwl 22:5c07298d3383 98 break;
lucaslwl 22:5c07298d3383 99 case 0x5:
lucaslwl 22:5c07298d3383 100 // Single-byte System Common Message or SysEx end with one byte
lucaslwl 22:5c07298d3383 101 data_read=1;
lucaslwl 22:5c07298d3383 102 break;
lucaslwl 22:5c07298d3383 103 case 0x6:
lucaslwl 22:5c07298d3383 104 // SysEx end with two bytes
lucaslwl 22:5c07298d3383 105 data_read=2;
lucaslwl 22:5c07298d3383 106 break;
lucaslwl 22:5c07298d3383 107 case 0xC:
lucaslwl 22:5c07298d3383 108 // Program change
lucaslwl 22:5c07298d3383 109 data_read=2;
lucaslwl 22:5c07298d3383 110 break;
lucaslwl 22:5c07298d3383 111 case 0xD:
lucaslwl 22:5c07298d3383 112 // Channel pressure
lucaslwl 22:5c07298d3383 113 data_read=2;
lucaslwl 22:5c07298d3383 114 break;
lucaslwl 22:5c07298d3383 115 case 0xF:
lucaslwl 22:5c07298d3383 116 // Single byte
lucaslwl 22:5c07298d3383 117 data_read=1;
lucaslwl 22:5c07298d3383 118 break;
lucaslwl 22:5c07298d3383 119 default:
lucaslwl 22:5c07298d3383 120 // Others three-bytes messages
lucaslwl 22:5c07298d3383 121 data_read=3;
lucaslwl 22:5c07298d3383 122 break;
lucaslwl 22:5c07298d3383 123 }
lucaslwl 22:5c07298d3383 124
lucaslwl 22:5c07298d3383 125 for(uint8_t j=1;j<data_read+1;j++) {
lucaslwl 22:5c07298d3383 126 data[cur_data]=buf[i+j];
lucaslwl 22:5c07298d3383 127 cur_data++;
lucaslwl 22:5c07298d3383 128 }
lucaslwl 22:5c07298d3383 129
lucaslwl 22:5c07298d3383 130 if(data_end) {
lucaslwl 22:5c07298d3383 131 midi_evt(MIDIMessage(data,cur_data));
lucaslwl 22:5c07298d3383 132 cur_data=0;
lucaslwl 22:5c07298d3383 133 }
lucaslwl 22:5c07298d3383 134 }
lucaslwl 22:5c07298d3383 135 }
lucaslwl 22:5c07298d3383 136
lucaslwl 22:5c07298d3383 137 // We reactivate the endpoint to receive next characters
lucaslwl 22:5c07298d3383 138 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
lucaslwl 22:5c07298d3383 139 return true;
lucaslwl 22:5c07298d3383 140 }
lucaslwl 22:5c07298d3383 141
lucaslwl 22:5c07298d3383 142 // Called in ISR context
lucaslwl 22:5c07298d3383 143 // Set configuration. Return false if the
lucaslwl 22:5c07298d3383 144 // configuration is not supported.
lucaslwl 22:5c07298d3383 145 bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {
lucaslwl 22:5c07298d3383 146 if (configuration != DEFAULT_CONFIGURATION) {
lucaslwl 22:5c07298d3383 147 return false;
lucaslwl 22:5c07298d3383 148 }
lucaslwl 22:5c07298d3383 149
lucaslwl 22:5c07298d3383 150 // Configure endpoints > 0
lucaslwl 22:5c07298d3383 151 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
lucaslwl 22:5c07298d3383 152 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
lucaslwl 22:5c07298d3383 153
lucaslwl 22:5c07298d3383 154 // We activate the endpoint to be able to receive data
lucaslwl 22:5c07298d3383 155 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
lucaslwl 22:5c07298d3383 156 return true;
lucaslwl 22:5c07298d3383 157 }
lucaslwl 22:5c07298d3383 158
lucaslwl 22:5c07298d3383 159
lucaslwl 22:5c07298d3383 160 uint8_t * USBMIDI::stringIinterfaceDesc() {
lucaslwl 22:5c07298d3383 161 static uint8_t stringIinterfaceDescriptor[] = {
lucaslwl 22:5c07298d3383 162 0x0c, //bLength
lucaslwl 22:5c07298d3383 163 STRING_DESCRIPTOR, //bDescriptorType 0x03
lucaslwl 22:5c07298d3383 164 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
lucaslwl 22:5c07298d3383 165 };
lucaslwl 22:5c07298d3383 166 return stringIinterfaceDescriptor;
lucaslwl 22:5c07298d3383 167 }
lucaslwl 22:5c07298d3383 168
lucaslwl 22:5c07298d3383 169 uint8_t * USBMIDI::stringIproductDesc() {
lucaslwl 22:5c07298d3383 170 static uint8_t stringIproductDescriptor[] = {
lucaslwl 22:5c07298d3383 171 0x16, //bLength
lucaslwl 22:5c07298d3383 172 STRING_DESCRIPTOR, //bDescriptorType 0x03
lucaslwl 22:5c07298d3383 173 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
lucaslwl 22:5c07298d3383 174 };
lucaslwl 22:5c07298d3383 175 return stringIproductDescriptor;
lucaslwl 22:5c07298d3383 176 }
lucaslwl 22:5c07298d3383 177
lucaslwl 22:5c07298d3383 178
lucaslwl 22:5c07298d3383 179 uint8_t * USBMIDI::configurationDesc() {
lucaslwl 22:5c07298d3383 180 static uint8_t configDescriptor[] = {
lucaslwl 22:5c07298d3383 181 // configuration descriptor
lucaslwl 22:5c07298d3383 182 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
lucaslwl 22:5c07298d3383 183
lucaslwl 22:5c07298d3383 184 // The Audio Interface Collection
lucaslwl 22:5c07298d3383 185 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
lucaslwl 22:5c07298d3383 186 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
lucaslwl 22:5c07298d3383 187 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
lucaslwl 22:5c07298d3383 188 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor
lucaslwl 22:5c07298d3383 189
lucaslwl 22:5c07298d3383 190 // MIDI IN JACKS
lucaslwl 22:5c07298d3383 191 0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
lucaslwl 22:5c07298d3383 192 0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
lucaslwl 22:5c07298d3383 193
lucaslwl 22:5c07298d3383 194 // MIDI OUT JACKS
lucaslwl 22:5c07298d3383 195 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
lucaslwl 22:5c07298d3383 196 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
lucaslwl 22:5c07298d3383 197
lucaslwl 22:5c07298d3383 198 // OUT endpoint descriptor
lucaslwl 22:5c07298d3383 199 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
lucaslwl 22:5c07298d3383 200 0x05, 0x25, 0x01, 0x01, 0x01,
lucaslwl 22:5c07298d3383 201
lucaslwl 22:5c07298d3383 202 // IN endpoint descriptor
lucaslwl 22:5c07298d3383 203 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
lucaslwl 22:5c07298d3383 204 0x05, 0x25, 0x01, 0x01, 0x03,
lucaslwl 22:5c07298d3383 205 };
lucaslwl 22:5c07298d3383 206 return configDescriptor;
lucaslwl 22:5c07298d3383 207 }