|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
-
-
- #include "usb_dev.h"
- #include "usb_midi.h"
- #include "core_pins.h"
- #include "HardwareSerial.h"
-
- #ifdef MIDI_INTERFACE
-
-
- uint8_t usb_midi_msg_channel;
- uint8_t usb_midi_msg_type;
- uint8_t usb_midi_msg_data1;
- uint8_t usb_midi_msg_data2;
- uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
- uint8_t usb_midi_msg_sysex_len;
- void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
- void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
- void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
- void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value) = NULL;
- void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program) = NULL;
- void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure) = NULL;
- void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch) = NULL;
- void (*usb_midi_handleRealTimeSystem)(uint8_t rtb) = NULL;
-
-
- #define TX_PACKET_LIMIT 6
- static usb_packet_t *rx_packet=NULL;
- static usb_packet_t *tx_packet=NULL;
- static uint8_t transmit_previous_timeout=0;
- static uint8_t tx_noautoflush=0;
-
-
-
- #define TX_TIMEOUT_MSEC 40
-
- #if F_CPU == 96000000
- #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
- #elif F_CPU == 48000000
- #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
- #elif F_CPU == 24000000
- #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
- #endif
-
-
- void usb_midi_write_packed(uint32_t n)
- {
- uint32_t index, wait_count=0;
-
- tx_noautoflush = 1;
- if (!tx_packet) {
- while (1) {
- if (!usb_configuration) {
-
- return;
- }
- if (usb_tx_packet_count(MIDI_TX_ENDPOINT) < TX_PACKET_LIMIT) {
- tx_packet = usb_malloc();
- if (tx_packet) break;
- }
- if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
- transmit_previous_timeout = 1;
-
- return;
- }
- yield();
- }
- }
- transmit_previous_timeout = 0;
- index = tx_packet->index;
- *((uint32_t *)(tx_packet->buf) + index++) = n;
- if (index < MIDI_TX_SIZE/4) {
- tx_packet->index = index;
- } else {
- tx_packet->len = MIDI_TX_SIZE;
- usb_tx(MIDI_TX_ENDPOINT, tx_packet);
- tx_packet = usb_malloc();
- }
- tx_noautoflush = 0;
- }
-
- void usb_midi_send_sysex(const uint8_t *data, uint32_t length)
- {
-
- while (length > 3) {
- usb_midi_write_packed(0x04 | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
- data += 3;
- length -= 3;
- }
- if (length == 3) {
- usb_midi_write_packed(0x07 | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
- } else if (length == 2) {
- usb_midi_write_packed(0x06 | (data[0] << 8) | (data[1] << 16));
- } else if (length == 1) {
- usb_midi_write_packed(0x05 | (data[0] << 8));
- }
- }
-
- void usb_midi_flush_output(void)
- {
- if (tx_noautoflush == 0 && tx_packet && tx_packet->index > 0) {
- tx_packet->len = tx_packet->index * 4;
- usb_tx(MIDI_TX_ENDPOINT, tx_packet);
- tx_packet = usb_malloc();
- }
- }
-
- void static sysex_byte(uint8_t b)
- {
- if (usb_midi_msg_sysex_len < USB_MIDI_SYSEX_MAX) {
- usb_midi_msg_sysex[usb_midi_msg_sysex_len++] = b;
- }
- }
-
-
- int usb_midi_read(uint32_t channel)
- {
- uint32_t n, index, ch, type1, type2;
-
- if (!rx_packet) {
- if (!usb_configuration) return 0;
- rx_packet = usb_rx(MIDI_RX_ENDPOINT);
- if (!rx_packet) return 0;
- if (rx_packet->len == 0) {
- usb_free(rx_packet);
- rx_packet = NULL;
- return 0;
- }
- }
- index = rx_packet->index;
- n = *(uint32_t *)(rx_packet->buf + index);
-
-
-
- index += 4;
- if (index < rx_packet->len) {
- rx_packet->index = index;
- } else {
- usb_free(rx_packet);
- rx_packet = usb_rx(MIDI_RX_ENDPOINT);
- }
- type1 = n & 15;
- type2 = (n >> 12) & 15;
- ch = ((n >> 8) & 15) + 1;
- if (type1 >= 0x08 && type1 <= 0x0E) {
- if (channel && channel != ch) {
-
- return 0;
- }
- if (type1 == 0x08 && type2 == 0x08) {
- usb_midi_msg_type = 0;
- if (usb_midi_handleNoteOff)
- (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
- } else
- if (type1 == 0x09 && type2 == 0x09) {
- if ((n >> 24) > 0) {
- usb_midi_msg_type = 1;
- if (usb_midi_handleNoteOn)
- (*usb_midi_handleNoteOn)(ch, (n >> 16), (n >> 24));
- } else {
- usb_midi_msg_type = 0;
- if (usb_midi_handleNoteOff)
- (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
- }
- } else
- if (type1 == 0x0A && type2 == 0x0A) {
- usb_midi_msg_type = 2;
- if (usb_midi_handleVelocityChange)
- (*usb_midi_handleVelocityChange)(ch, (n >> 16), (n >> 24));
- } else
- if (type1 == 0x0B && type2 == 0x0B) {
- usb_midi_msg_type = 3;
- if (usb_midi_handleControlChange)
- (*usb_midi_handleControlChange)(ch, (n >> 16), (n >> 24));
- } else
- if (type1 == 0x0C && type2 == 0x0C) {
- usb_midi_msg_type = 4;
- if (usb_midi_handleProgramChange)
- (*usb_midi_handleProgramChange)(ch, (n >> 16));
- } else
- if (type1 == 0x0D && type2 == 0x0D) {
- usb_midi_msg_type = 5;
- if (usb_midi_handleAfterTouch)
- (*usb_midi_handleAfterTouch)(ch, (n >> 16));
- } else
- if (type1 == 0x0E && type2 == 0x0E) {
- usb_midi_msg_type = 6;
- if (usb_midi_handlePitchChange)
- (*usb_midi_handlePitchChange)(ch,
- ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
- } else {
- return 0;
- }
- return_message:
- usb_midi_msg_channel = ch;
- usb_midi_msg_data1 = (n >> 16);
- usb_midi_msg_data2 = (n >> 24);
- return 1;
- }
- if (type1 == 0x04) {
- sysex_byte(n >> 8);
- sysex_byte(n >> 16);
- sysex_byte(n >> 24);
- return 0;
- }
- if (type1 >= 0x05 && type1 <= 0x07) {
- sysex_byte(n >> 8);
- if (type1 >= 0x06) sysex_byte(n >> 16);
- if (type1 == 0x07) sysex_byte(n >> 24);
- usb_midi_msg_data1 = usb_midi_msg_sysex_len;
- usb_midi_msg_sysex_len = 0;
- usb_midi_msg_type = 7;
- return 1;
- }
- if (type1 == 0x0F) {
-
-
- if (usb_midi_msg_sysex_len > 0) {
-
-
-
- sysex_byte(n >> 8);
- } else {
-
-
- usb_midi_msg_type = 8;
- if (usb_midi_handleRealTimeSystem)
- (*usb_midi_handleRealTimeSystem)(n >> 8);
- goto return_message;
- }
- }
- return 0;
- }
-
-
- #endif
|