USB CDC (serial) and USB MSC (strage) Composite Device. http://mbed.org/users/okini3939/notebook/USB_Device/

Dependencies:   ChaNFSSD mbed ChaNFS

Committer:
okini3939
Date:
Fri Dec 23 16:37:58 2011 +0000
Revision:
2:5db90410bb90
Parent:
0:9b1d17d54055

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:9b1d17d54055 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
okini3939 0:9b1d17d54055 2 *
okini3939 0:9b1d17d54055 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
okini3939 0:9b1d17d54055 4 * and associated documentation files (the "Software"), to deal in the Software without
okini3939 0:9b1d17d54055 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
okini3939 0:9b1d17d54055 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
okini3939 0:9b1d17d54055 7 * Software is furnished to do so, subject to the following conditions:
okini3939 0:9b1d17d54055 8 *
okini3939 0:9b1d17d54055 9 * The above copyright notice and this permission notice shall be included in all copies or
okini3939 0:9b1d17d54055 10 * substantial portions of the Software.
okini3939 0:9b1d17d54055 11 *
okini3939 0:9b1d17d54055 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
okini3939 0:9b1d17d54055 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
okini3939 0:9b1d17d54055 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
okini3939 0:9b1d17d54055 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
okini3939 0:9b1d17d54055 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
okini3939 0:9b1d17d54055 17 */
okini3939 0:9b1d17d54055 18
okini3939 0:9b1d17d54055 19 #ifndef MIDIMESSAGE_H
okini3939 0:9b1d17d54055 20 #define MIDIMESSAGE_H
okini3939 0:9b1d17d54055 21
okini3939 0:9b1d17d54055 22 #include "mbed.h"
okini3939 0:9b1d17d54055 23
okini3939 0:9b1d17d54055 24 // MIDI Message Format
okini3939 0:9b1d17d54055 25 //
okini3939 0:9b1d17d54055 26 // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ]
okini3939 0:9b1d17d54055 27 //
okini3939 0:9b1d17d54055 28 // MIDI Data Messages (Channel Specific)
okini3939 0:9b1d17d54055 29 //
okini3939 0:9b1d17d54055 30 // Message msg n m
okini3939 0:9b1d17d54055 31 // ---------------------------------------------
okini3939 0:9b1d17d54055 32 // Note Off 0x8 Key Velocity
okini3939 0:9b1d17d54055 33 // Note On 0x9 Key Velocity
okini3939 0:9b1d17d54055 34 // Polyphonic Aftertouch 0xA Key Pressure
okini3939 0:9b1d17d54055 35 // Control Change 0xB Controller Value
okini3939 0:9b1d17d54055 36 // Program Change 0xC Program -
okini3939 0:9b1d17d54055 37 // Channel Aftertouch 0xD Pressure -
okini3939 0:9b1d17d54055 38 // Pitch Wheel 0xE LSB MSB
okini3939 0:9b1d17d54055 39
okini3939 0:9b1d17d54055 40 #define CABLE_NUM (0<<4)
okini3939 0:9b1d17d54055 41
okini3939 0:9b1d17d54055 42 /** A MIDI message container */
okini3939 0:9b1d17d54055 43 class MIDIMessage {
okini3939 0:9b1d17d54055 44 public:
okini3939 0:9b1d17d54055 45 MIDIMessage() {}
okini3939 0:9b1d17d54055 46
okini3939 0:9b1d17d54055 47 MIDIMessage(uint8_t *buf) {
okini3939 0:9b1d17d54055 48 *((uint32_t *)data) = *((uint32_t *)buf);
okini3939 0:9b1d17d54055 49 }
okini3939 0:9b1d17d54055 50
okini3939 0:9b1d17d54055 51 // create messages
okini3939 0:9b1d17d54055 52
okini3939 0:9b1d17d54055 53 /** Create a NoteOff message
okini3939 0:9b1d17d54055 54 * @param key Key ID
okini3939 0:9b1d17d54055 55 * @param velocity Key velocity (0-127, default = 127)
okini3939 0:9b1d17d54055 56 * @param channel Key channel (0-15, default 0)
okini3939 0:9b1d17d54055 57 * @returns A MIDIMessage
okini3939 0:9b1d17d54055 58 */
okini3939 0:9b1d17d54055 59 static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) {
okini3939 0:9b1d17d54055 60 MIDIMessage msg;
okini3939 0:9b1d17d54055 61 msg.data[0] = CABLE_NUM | 0x08;
okini3939 0:9b1d17d54055 62 msg.data[1] = 0x80 | (channel & 0x0F);
okini3939 0:9b1d17d54055 63 msg.data[2] = key & 0x7F;
okini3939 0:9b1d17d54055 64 msg.data[3] = velocity & 0x7F;
okini3939 0:9b1d17d54055 65 return msg;
okini3939 0:9b1d17d54055 66 }
okini3939 0:9b1d17d54055 67
okini3939 0:9b1d17d54055 68 /** Create a NoteOn message
okini3939 0:9b1d17d54055 69 * @param key Key ID
okini3939 0:9b1d17d54055 70 * @param velocity Key velocity (0-127, default = 127)
okini3939 0:9b1d17d54055 71 * @param channel Key channel (0-15, default 0)
okini3939 0:9b1d17d54055 72 * @returns A MIDIMessage
okini3939 0:9b1d17d54055 73 */
okini3939 0:9b1d17d54055 74 static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) {
okini3939 0:9b1d17d54055 75 MIDIMessage msg;
okini3939 0:9b1d17d54055 76 msg.data[0] = CABLE_NUM | 0x09;
okini3939 0:9b1d17d54055 77 msg.data[1] = 0x90 | (channel & 0x0F);
okini3939 0:9b1d17d54055 78 msg.data[2] = key & 0x7F;
okini3939 0:9b1d17d54055 79 msg.data[3] = velocity & 0x7F;
okini3939 0:9b1d17d54055 80 return msg;
okini3939 0:9b1d17d54055 81 }
okini3939 0:9b1d17d54055 82
okini3939 0:9b1d17d54055 83 /** Create a PolyPhonic Aftertouch message
okini3939 0:9b1d17d54055 84 * @param key Key ID
okini3939 0:9b1d17d54055 85 * @param pressure Aftertouch pressure (0-127)
okini3939 0:9b1d17d54055 86 * @param channel Key channel (0-15, default 0)
okini3939 0:9b1d17d54055 87 * @returns A MIDIMessage
okini3939 0:9b1d17d54055 88 */
okini3939 0:9b1d17d54055 89 static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) {
okini3939 0:9b1d17d54055 90 MIDIMessage msg;
okini3939 0:9b1d17d54055 91 msg.data[0] = CABLE_NUM | 0x0A;
okini3939 0:9b1d17d54055 92 msg.data[1] = 0xA0 | (channel & 0x0F);
okini3939 0:9b1d17d54055 93 msg.data[2] = key & 0x7F;
okini3939 0:9b1d17d54055 94 msg.data[3] = pressure & 0x7F;
okini3939 0:9b1d17d54055 95 return msg;
okini3939 0:9b1d17d54055 96 }
okini3939 0:9b1d17d54055 97
okini3939 0:9b1d17d54055 98 /** Create a Control Change message
okini3939 0:9b1d17d54055 99 * @param control Controller ID
okini3939 0:9b1d17d54055 100 * @param value Controller value (0-127)
okini3939 0:9b1d17d54055 101 * @param channel Controller channel (0-15, default 0)
okini3939 0:9b1d17d54055 102 * @returns A MIDIMessage
okini3939 0:9b1d17d54055 103 */
okini3939 0:9b1d17d54055 104 static MIDIMessage ControlChange(int control, int value, int channel = 0) {
okini3939 0:9b1d17d54055 105 MIDIMessage msg;
okini3939 0:9b1d17d54055 106 msg.data[0] = CABLE_NUM | 0x0B;
okini3939 0:9b1d17d54055 107 msg.data[1] = 0xB0 | (channel & 0x0F);
okini3939 0:9b1d17d54055 108 msg.data[2] = control & 0x7F;
okini3939 0:9b1d17d54055 109 msg.data[3] = value & 0x7F;
okini3939 0:9b1d17d54055 110 return msg;
okini3939 0:9b1d17d54055 111 }
okini3939 0:9b1d17d54055 112
okini3939 0:9b1d17d54055 113 /** Create a Program Change message
okini3939 0:9b1d17d54055 114 * @param program Program ID
okini3939 0:9b1d17d54055 115 * @param channel Channel (0-15, default 0)
okini3939 0:9b1d17d54055 116 * @returns A MIDIMessage
okini3939 0:9b1d17d54055 117 */
okini3939 0:9b1d17d54055 118 static MIDIMessage ProgramChange(int program, int channel = 0) {
okini3939 0:9b1d17d54055 119 MIDIMessage msg;
okini3939 0:9b1d17d54055 120 msg.data[0] = CABLE_NUM | 0x0C;
okini3939 0:9b1d17d54055 121 msg.data[1] = 0xC0 | (channel & 0x0F);
okini3939 0:9b1d17d54055 122 msg.data[2] = program & 0x7F;
okini3939 0:9b1d17d54055 123 msg.data[3] = 0x00;
okini3939 0:9b1d17d54055 124 return msg;
okini3939 0:9b1d17d54055 125 }
okini3939 0:9b1d17d54055 126
okini3939 0:9b1d17d54055 127 /** Create a Channel Aftertouch message
okini3939 0:9b1d17d54055 128 * @param pressure Pressure
okini3939 0:9b1d17d54055 129 * @param channel Key channel (0-15, default 0)
okini3939 0:9b1d17d54055 130 * @returns A MIDIMessage
okini3939 0:9b1d17d54055 131 */
okini3939 0:9b1d17d54055 132 static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) {
okini3939 0:9b1d17d54055 133 MIDIMessage msg;
okini3939 0:9b1d17d54055 134 msg.data[0] = CABLE_NUM | 0x0D;
okini3939 0:9b1d17d54055 135 msg.data[1] = 0xD0 | (channel & 0x0F);
okini3939 0:9b1d17d54055 136 msg.data[2] = pressure & 0x7F;
okini3939 0:9b1d17d54055 137 msg.data[3] = 0x00;
okini3939 0:9b1d17d54055 138 return msg;
okini3939 0:9b1d17d54055 139 }
okini3939 0:9b1d17d54055 140
okini3939 0:9b1d17d54055 141 /** Create a Pitch Wheel message
okini3939 0:9b1d17d54055 142 * @param pitch Pitch (-8192 - 8191, default = 0)
okini3939 0:9b1d17d54055 143 * @param channel Channel (0-15, default 0)
okini3939 0:9b1d17d54055 144 * @returns A MIDIMessage
okini3939 0:9b1d17d54055 145 */
okini3939 0:9b1d17d54055 146 static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) {
okini3939 0:9b1d17d54055 147 MIDIMessage msg;
okini3939 0:9b1d17d54055 148 int p = pitch + 8192; // 0 - 16383, 8192 is center
okini3939 0:9b1d17d54055 149 msg.data[0] = CABLE_NUM | 0x0E;
okini3939 0:9b1d17d54055 150 msg.data[1] = 0xE0 | (channel & 0x0F);
okini3939 0:9b1d17d54055 151 msg.data[2] = p & 0x7F;
okini3939 0:9b1d17d54055 152 msg.data[3] = (p >> 7) & 0x7F;
okini3939 0:9b1d17d54055 153 return msg;
okini3939 0:9b1d17d54055 154 }
okini3939 0:9b1d17d54055 155
okini3939 0:9b1d17d54055 156 /** Create an All Notes Off message
okini3939 0:9b1d17d54055 157 * @param channel Channel (0-15, default 0)
okini3939 0:9b1d17d54055 158 * @returns A MIDIMessage
okini3939 0:9b1d17d54055 159 */
okini3939 0:9b1d17d54055 160 static MIDIMessage AllNotesOff(int channel = 0) {
okini3939 0:9b1d17d54055 161 return ControlChange(123, 0, channel);
okini3939 0:9b1d17d54055 162 }
okini3939 0:9b1d17d54055 163
okini3939 0:9b1d17d54055 164 // decode messages
okini3939 0:9b1d17d54055 165
okini3939 0:9b1d17d54055 166 /** MIDI Message Types */
okini3939 0:9b1d17d54055 167 enum MIDIMessageType {
okini3939 0:9b1d17d54055 168 ErrorType,
okini3939 0:9b1d17d54055 169 NoteOffType,
okini3939 0:9b1d17d54055 170 NoteOnType,
okini3939 0:9b1d17d54055 171 PolyphonicAftertouchType,
okini3939 0:9b1d17d54055 172 ControlChangeType,
okini3939 0:9b1d17d54055 173 ProgramChangeType,
okini3939 0:9b1d17d54055 174 ChannelAftertouchType,
okini3939 0:9b1d17d54055 175 PitchWheelType,
okini3939 0:9b1d17d54055 176 AllNotesOffType
okini3939 0:9b1d17d54055 177 };
okini3939 0:9b1d17d54055 178
okini3939 0:9b1d17d54055 179 /** Read the message type
okini3939 0:9b1d17d54055 180 * @returns MIDIMessageType
okini3939 0:9b1d17d54055 181 */
okini3939 0:9b1d17d54055 182 MIDIMessageType type() {
okini3939 0:9b1d17d54055 183 switch((data[1] >> 4) & 0xF) {
okini3939 0:9b1d17d54055 184 case 0x8: return NoteOffType;
okini3939 0:9b1d17d54055 185 case 0x9: return NoteOnType;
okini3939 0:9b1d17d54055 186 case 0xA: return PolyphonicAftertouchType;
okini3939 0:9b1d17d54055 187 case 0xB:
okini3939 0:9b1d17d54055 188 if(controller() < 120) { // standard controllers
okini3939 0:9b1d17d54055 189 return ControlChangeType;
okini3939 0:9b1d17d54055 190 } else if(controller() == 123) {
okini3939 0:9b1d17d54055 191 return AllNotesOffType;
okini3939 0:9b1d17d54055 192 } else {
okini3939 0:9b1d17d54055 193 return ErrorType; // unsupported atm
okini3939 0:9b1d17d54055 194 }
okini3939 0:9b1d17d54055 195 case 0xC: return ProgramChangeType;
okini3939 0:9b1d17d54055 196 case 0xD: return ChannelAftertouchType;
okini3939 0:9b1d17d54055 197 case 0xE: return PitchWheelType;
okini3939 0:9b1d17d54055 198 default: return ErrorType;
okini3939 0:9b1d17d54055 199 }
okini3939 0:9b1d17d54055 200 }
okini3939 0:9b1d17d54055 201
okini3939 0:9b1d17d54055 202 /** Read the channel number */
okini3939 0:9b1d17d54055 203 int channel() {
okini3939 0:9b1d17d54055 204 return (data[1] & 0x0F);
okini3939 0:9b1d17d54055 205 }
okini3939 0:9b1d17d54055 206
okini3939 0:9b1d17d54055 207 /** Read the key ID */
okini3939 0:9b1d17d54055 208 int key() {
okini3939 0:9b1d17d54055 209 return (data[2] & 0x7F);
okini3939 0:9b1d17d54055 210 }
okini3939 0:9b1d17d54055 211
okini3939 0:9b1d17d54055 212 /** Read the velocity */
okini3939 0:9b1d17d54055 213 int velocity() {
okini3939 0:9b1d17d54055 214 return (data[3] & 0x7F);
okini3939 0:9b1d17d54055 215 }
okini3939 0:9b1d17d54055 216
okini3939 0:9b1d17d54055 217 /** Read the controller value */
okini3939 0:9b1d17d54055 218 int value() {
okini3939 0:9b1d17d54055 219 return (data[3] & 0x7F);
okini3939 0:9b1d17d54055 220 }
okini3939 0:9b1d17d54055 221
okini3939 0:9b1d17d54055 222 /** Read the aftertouch pressure */
okini3939 0:9b1d17d54055 223 int pressure() {
okini3939 0:9b1d17d54055 224 if(type() == PolyphonicAftertouchType) {
okini3939 0:9b1d17d54055 225 return (data[3] & 0x7F);
okini3939 0:9b1d17d54055 226 } else {
okini3939 0:9b1d17d54055 227 return (data[2] & 0x7F);
okini3939 0:9b1d17d54055 228 }
okini3939 0:9b1d17d54055 229 }
okini3939 0:9b1d17d54055 230
okini3939 0:9b1d17d54055 231 /** Read the controller number */
okini3939 0:9b1d17d54055 232 int controller() {
okini3939 0:9b1d17d54055 233 return (data[2] & 0x7F);
okini3939 0:9b1d17d54055 234 }
okini3939 0:9b1d17d54055 235
okini3939 0:9b1d17d54055 236 /** Read the program number */
okini3939 0:9b1d17d54055 237 int program() {
okini3939 0:9b1d17d54055 238 return (data[2] & 0x7F);
okini3939 0:9b1d17d54055 239 }
okini3939 0:9b1d17d54055 240
okini3939 0:9b1d17d54055 241 /** Read the pitch value */
okini3939 0:9b1d17d54055 242 int pitch() {
okini3939 0:9b1d17d54055 243 int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F);
okini3939 0:9b1d17d54055 244 return p - 8192; // 0 - 16383, 8192 is center
okini3939 0:9b1d17d54055 245 }
okini3939 0:9b1d17d54055 246
okini3939 0:9b1d17d54055 247 uint8_t data[4];
okini3939 0:9b1d17d54055 248 };
okini3939 0:9b1d17d54055 249
okini3939 0:9b1d17d54055 250 #endif