device USB Samir

Dependents:   USBJoystick_HelloWorld3

Fork of USBDevice by Samuel Mokrani

Committer:
samux
Date:
Wed May 30 18:28:11 2012 +0000
Revision:
0:140cdf8e2d60

        

Who changed what in which revision?

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