Kalibriersoftware Stromwerte

Dependencies:   Matrix mbed

Committer:
Racer01014
Date:
Mon Nov 23 16:09:54 2015 +0000
Revision:
0:5e35c180ed4a
-

Who changed what in which revision?

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