diff options
Diffstat (limited to 'protocol/lufa/midi/midi_device.c')
| -rwxr-xr-x | protocol/lufa/midi/midi_device.c | 291 |
1 files changed, 291 insertions, 0 deletions
diff --git a/protocol/lufa/midi/midi_device.c b/protocol/lufa/midi/midi_device.c new file mode 100755 index 000000000..3215a007d --- /dev/null +++ b/protocol/lufa/midi/midi_device.c | |||
| @@ -0,0 +1,291 @@ | |||
| 1 | //midi for embedded chips, | ||
| 2 | //Copyright 2010 Alex Norman | ||
| 3 | // | ||
| 4 | //This file is part of avr-midi. | ||
| 5 | // | ||
| 6 | //avr-midi is free software: you can redistribute it and/or modify | ||
| 7 | //it under the terms of the GNU General Public License as published by | ||
| 8 | //the Free Software Foundation, either version 3 of the License, or | ||
| 9 | //(at your option) any later version. | ||
| 10 | // | ||
| 11 | //avr-midi is distributed in the hope that it will be useful, | ||
| 12 | //but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | //GNU General Public License for more details. | ||
| 15 | // | ||
| 16 | //You should have received a copy of the GNU General Public License | ||
| 17 | //along with avr-midi. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | #include "midi_device.h" | ||
| 20 | #include "midi.h" | ||
| 21 | |||
| 22 | #ifndef NULL | ||
| 23 | #define NULL 0 | ||
| 24 | #endif | ||
| 25 | |||
| 26 | //forward declarations, internally used to call the callbacks | ||
| 27 | void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2); | ||
| 28 | void midi_process_byte(MidiDevice * device, uint8_t input); | ||
| 29 | |||
| 30 | void midi_device_init(MidiDevice * device){ | ||
| 31 | device->input_state = IDLE; | ||
| 32 | device->input_count = 0; | ||
| 33 | bytequeue_init(&device->input_queue, device->input_queue_data, MIDI_INPUT_QUEUE_LENGTH); | ||
| 34 | |||
| 35 | //three byte funcs | ||
| 36 | device->input_cc_callback = NULL; | ||
| 37 | device->input_noteon_callback = NULL; | ||
| 38 | device->input_noteoff_callback = NULL; | ||
| 39 | device->input_aftertouch_callback = NULL; | ||
| 40 | device->input_pitchbend_callback = NULL; | ||
| 41 | device->input_songposition_callback = NULL; | ||
| 42 | |||
| 43 | //two byte funcs | ||
| 44 | device->input_progchange_callback = NULL; | ||
| 45 | device->input_chanpressure_callback = NULL; | ||
| 46 | device->input_songselect_callback = NULL; | ||
| 47 | device->input_tc_quarterframe_callback = NULL; | ||
| 48 | |||
| 49 | //one byte funcs | ||
| 50 | device->input_realtime_callback = NULL; | ||
| 51 | device->input_tunerequest_callback = NULL; | ||
| 52 | |||
| 53 | //var byte functions | ||
| 54 | device->input_sysex_callback = NULL; | ||
| 55 | device->input_fallthrough_callback = NULL; | ||
| 56 | device->input_catchall_callback = NULL; | ||
| 57 | |||
| 58 | device->pre_input_process_callback = NULL; | ||
| 59 | } | ||
| 60 | |||
| 61 | void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input) { | ||
| 62 | uint8_t i; | ||
| 63 | for (i = 0; i < cnt; i++) | ||
| 64 | bytequeue_enqueue(&device->input_queue, input[i]); | ||
| 65 | } | ||
| 66 | |||
| 67 | void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_func){ | ||
| 68 | device->send_func = send_func; | ||
| 69 | } | ||
| 70 | |||
| 71 | void midi_device_set_pre_input_process_func(MidiDevice * device, midi_no_byte_func_t pre_process_func){ | ||
| 72 | device->pre_input_process_callback = pre_process_func; | ||
| 73 | } | ||
| 74 | |||
| 75 | void midi_device_process(MidiDevice * device) { | ||
| 76 | //call the pre_input_process_callback if there is one | ||
| 77 | if(device->pre_input_process_callback) | ||
| 78 | device->pre_input_process_callback(device); | ||
| 79 | |||
| 80 | //pull stuff off the queue and process | ||
| 81 | byteQueueIndex_t len = bytequeue_length(&device->input_queue); | ||
| 82 | uint16_t i; | ||
| 83 | //TODO limit number of bytes processed? | ||
| 84 | for(i = 0; i < len; i++) { | ||
| 85 | uint8_t val = bytequeue_get(&device->input_queue, 0); | ||
| 86 | midi_process_byte(device, val); | ||
| 87 | bytequeue_remove(&device->input_queue, 1); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | void midi_process_byte(MidiDevice * device, uint8_t input) { | ||
| 92 | if (midi_is_realtime(input)) { | ||
| 93 | //call callback, store and restore state | ||
| 94 | input_state_t state = device->input_state; | ||
| 95 | device->input_state = ONE_BYTE_MESSAGE; | ||
| 96 | midi_input_callbacks(device, 1, input, 0, 0); | ||
| 97 | device->input_state = state; | ||
| 98 | } else if (midi_is_statusbyte(input)) { | ||
| 99 | //store the byte | ||
| 100 | if (device->input_state != SYSEX_MESSAGE) { | ||
| 101 | device->input_buffer[0] = input; | ||
| 102 | device->input_count = 1; | ||
| 103 | } | ||
| 104 | switch (midi_packet_length(input)) { | ||
| 105 | case ONE: | ||
| 106 | device->input_state = ONE_BYTE_MESSAGE;; | ||
| 107 | midi_input_callbacks(device, 1, input, 0, 0); | ||
| 108 | device->input_state = IDLE; | ||
| 109 | break; | ||
| 110 | case TWO: | ||
| 111 | device->input_state = TWO_BYTE_MESSAGE; | ||
| 112 | break; | ||
| 113 | case THREE: | ||
| 114 | device->input_state = THREE_BYTE_MESSAGE; | ||
| 115 | break; | ||
| 116 | case UNDEFINED: | ||
| 117 | switch(input) { | ||
| 118 | case SYSEX_BEGIN: | ||
| 119 | device->input_state = SYSEX_MESSAGE; | ||
| 120 | device->input_buffer[0] = input; | ||
| 121 | device->input_count = 1; | ||
| 122 | break; | ||
| 123 | case SYSEX_END: | ||
| 124 | //send what is left in the input buffer, set idle | ||
| 125 | device->input_buffer[device->input_count % 3] = input; | ||
| 126 | device->input_count += 1; | ||
| 127 | //call the callback | ||
| 128 | midi_input_callbacks(device, device->input_count, | ||
| 129 | device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); | ||
| 130 | device->input_state = IDLE; | ||
| 131 | break; | ||
| 132 | default: | ||
| 133 | device->input_state = IDLE; | ||
| 134 | device->input_count = 0; | ||
| 135 | } | ||
| 136 | |||
| 137 | break; | ||
| 138 | default: | ||
| 139 | device->input_state = IDLE; | ||
| 140 | device->input_count = 0; | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | } else { | ||
| 144 | if (device->input_state != IDLE) { | ||
| 145 | //store the byte | ||
| 146 | device->input_buffer[device->input_count % 3] = input; | ||
| 147 | //increment count | ||
| 148 | uint16_t prev = device->input_count; | ||
| 149 | device->input_count += 1; | ||
| 150 | |||
| 151 | switch(prev % 3) { | ||
| 152 | case 2: | ||
| 153 | //call callback | ||
| 154 | midi_input_callbacks(device, device->input_count, | ||
| 155 | device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); | ||
| 156 | if (device->input_state != SYSEX_MESSAGE) { | ||
| 157 | //set to 1, keeping status byte, allowing for running status | ||
| 158 | device->input_count = 1; | ||
| 159 | } | ||
| 160 | break; | ||
| 161 | case 1: | ||
| 162 | if (device->input_state == TWO_BYTE_MESSAGE) { | ||
| 163 | //call callback | ||
| 164 | midi_input_callbacks(device, device->input_count, | ||
| 165 | device->input_buffer[0], device->input_buffer[1], 0); | ||
| 166 | if (device->input_state != SYSEX_MESSAGE) { | ||
| 167 | //set to 1, keeping status byte, allowing for running status | ||
| 168 | device->input_count = 1; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | break; | ||
| 172 | case 0: | ||
| 173 | default: | ||
| 174 | //one byte messages are dealt with directly | ||
| 175 | break; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { | ||
| 182 | //did we end up calling a callback? | ||
| 183 | bool called = false; | ||
| 184 | if (device->input_state == SYSEX_MESSAGE) { | ||
| 185 | if (device->input_sysex_callback) { | ||
| 186 | const uint16_t start = ((cnt - 1) / 3) * 3; | ||
| 187 | const uint8_t length = (cnt - start); | ||
| 188 | uint8_t data[3]; | ||
| 189 | data[0] = byte0; | ||
| 190 | data[1] = byte1; | ||
| 191 | data[2] = byte2; | ||
| 192 | device->input_sysex_callback(device, start, length, data); | ||
| 193 | called = true; | ||
| 194 | } | ||
| 195 | } else { | ||
| 196 | switch (cnt) { | ||
| 197 | case 3: | ||
| 198 | { | ||
| 199 | midi_three_byte_func_t func = NULL; | ||
| 200 | switch (byte0 & 0xF0) { | ||
| 201 | case MIDI_CC: | ||
| 202 | func = device->input_cc_callback; | ||
| 203 | break; | ||
| 204 | case MIDI_NOTEON: | ||
| 205 | func = device->input_noteon_callback; | ||
| 206 | break; | ||
| 207 | case MIDI_NOTEOFF: | ||
| 208 | func = device->input_noteoff_callback; | ||
| 209 | break; | ||
| 210 | case MIDI_AFTERTOUCH: | ||
| 211 | func = device->input_aftertouch_callback; | ||
| 212 | break; | ||
| 213 | case MIDI_PITCHBEND: | ||
| 214 | func = device->input_pitchbend_callback; | ||
| 215 | break; | ||
| 216 | case 0xF0: | ||
| 217 | if (byte0 == MIDI_SONGPOSITION) | ||
| 218 | func = device->input_songposition_callback; | ||
| 219 | break; | ||
| 220 | default: | ||
| 221 | break; | ||
| 222 | } | ||
| 223 | if(func) { | ||
| 224 | //mask off the channel for non song position functions | ||
| 225 | if (byte0 == MIDI_SONGPOSITION) | ||
| 226 | func(device, byte0, byte1, byte2); | ||
| 227 | else | ||
| 228 | func(device, byte0 & 0x0F, byte1, byte2); | ||
| 229 | called = true; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | break; | ||
| 233 | case 2: | ||
| 234 | { | ||
| 235 | midi_two_byte_func_t func = NULL; | ||
| 236 | switch (byte0 & 0xF0) { | ||
| 237 | case MIDI_PROGCHANGE: | ||
| 238 | func = device->input_progchange_callback; | ||
| 239 | break; | ||
| 240 | case MIDI_CHANPRESSURE: | ||
| 241 | func = device->input_chanpressure_callback; | ||
| 242 | break; | ||
| 243 | case 0xF0: | ||
| 244 | if (byte0 == MIDI_SONGSELECT) | ||
| 245 | func = device->input_songselect_callback; | ||
| 246 | else if (byte0 == MIDI_TC_QUARTERFRAME) | ||
| 247 | func = device->input_tc_quarterframe_callback; | ||
| 248 | break; | ||
| 249 | default: | ||
| 250 | break; | ||
| 251 | } | ||
| 252 | if(func) { | ||
| 253 | //mask off the channel | ||
| 254 | if (byte0 == MIDI_SONGSELECT || byte0 == MIDI_TC_QUARTERFRAME) | ||
| 255 | func(device, byte0, byte1); | ||
| 256 | else | ||
| 257 | func(device, byte0 & 0x0F, byte1); | ||
| 258 | called = true; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | break; | ||
| 262 | case 1: | ||
| 263 | { | ||
| 264 | midi_one_byte_func_t func = NULL; | ||
| 265 | if (midi_is_realtime(byte0)) | ||
| 266 | func = device->input_realtime_callback; | ||
| 267 | else if (byte0 == MIDI_TUNEREQUEST) | ||
| 268 | func = device->input_tunerequest_callback; | ||
| 269 | if (func) { | ||
| 270 | func(device, byte0); | ||
| 271 | called = true; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | break; | ||
| 275 | default: | ||
| 276 | //just in case | ||
| 277 | if (cnt > 3) | ||
| 278 | cnt = 0; | ||
| 279 | break; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | //if there is fallthrough default callback and we haven't called a more specific one, | ||
| 284 | //call the fallthrough | ||
| 285 | if (!called && device->input_fallthrough_callback) | ||
| 286 | device->input_fallthrough_callback(device, cnt, byte0, byte1, byte2); | ||
| 287 | //always call the catch all if it exists | ||
| 288 | if (device->input_catchall_callback) | ||
| 289 | device->input_catchall_callback(device, cnt, byte0, byte1, byte2); | ||
| 290 | } | ||
| 291 | |||
