Pinscape Controller version 1 fork. This is a fork to allow for ongoing bug fixes to the original controller version, from before the major changes for the expansion board project.

Dependencies:   FastIO FastPWM SimpleDMA mbed

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Thu Feb 11 18:12:52 2016 +0000
Revision:
52:63f0a9b45f0c
Convert FastAnalogIn and USBDevice libraries to folders

Who changed what in which revision?

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