|
-
-
- #include <avr/io.h>
- #include <avr/pgmspace.h>
- #include <stdint.h>
- #include "usb_common.h"
- #include "usb_private.h"
- #include "usb_api.h"
- #include "wiring.h"
-
-
- void usb_midi_class::sendSysEx_BufferHasTerm(uint16_t length, const uint8_t *data)
- {
- while (length > 3) {
- send_raw(0x04, data[0], data[1], data[2]);
- data += 3;
- length -= 3;
- }
- if (length == 3) {
- send_raw(0x07, data[0], data[1], data[2]);
- } else if (length == 2) {
- send_raw(0x06, data[0], data[1], 0);
- } else if (length == 1) {
- send_raw(0x05, data[0], 0, 0);
- }
- }
-
- void usb_midi_class::sendSysEx_AddTermBytes(uint16_t length, const uint8_t *data)
- {
- if (length == 0) {
- send_raw(0x06, 0xF0, 0xF7, 0);
- return;
- } else if (length == 1) {
- send_raw(0x07, 0xF0, data[0], 0xF7);
- return;
- } else {
- send_raw(0x04, 0xF0, data[0], data[1]);
- data += 2;
- length -= 2;
- }
- while (length >= 3) {
- send_raw(0x04, data[0], data[1], data[2]);
- data += 3;
- length -= 3;
- }
- if (length == 2) {
- send_raw(0x07, data[0], data[1], 0xF7);
- } else if (length == 1) {
- send_raw(0x06, data[0], 0xF7, 0);
- } else {
- send_raw(0x05, 0xF7, 0, 0);
- }
- }
-
- void usb_midi_class::send_raw(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
- {
- uint8_t intr_state, timeout;
-
- if (!usb_configuration) return;
- intr_state = SREG;
- cli();
- UENUM = MIDI_TX_ENDPOINT;
- timeout = UDFNUML + 2;
- while (1) {
-
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
- if (UDFNUML == timeout) return;
- if (!usb_configuration) return;
- intr_state = SREG;
- cli();
- UENUM = MIDI_TX_ENDPOINT;
- }
- UEDATX = b0;
- UEDATX = b1;
- UEDATX = b2;
- UEDATX = b3;
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
- SREG = intr_state;
- }
-
- void usb_midi_class::send_now(void)
- {
- uint8_t intr_state;
-
- if (!usb_configuration) return;
- intr_state = SREG;
- cli();
- UENUM = MIDI_TX_ENDPOINT;
- if (UEBCLX != MIDI_TX_SIZE) {
- UEINTX = 0x3A;
- }
- SREG = intr_state;
- }
-
-
-
-
-
-
-
-
-
-
- uint8_t usb_midi_class::analog2velocity(uint16_t val, uint8_t range)
- {
- #if 0
- if (val == 0) return 0;
- float scale = 1.0 + (20.0 / (float)range) * log10((float)val / 1023.0);
- if (scale < 0) return 0;
- return 127 * scale;
- #else
- uint8_t i, e, b;
- uint16_t s=0;
- static const uint8_t PROGMEM table[] = {225,124,65,34,17,9,4,2,1};
-
- if (val == 0) return 0;
- if (val >= 1023) return 127;
- for (e=0; (val & 512) == 0; e++) val <<= 1;
- for (i=0; i<9; i++) {
- uint16_t x = val + (val >> (i + 1));
- if (x < 1024) {
- val = x;
- s += pgm_read_byte(table + i);
- }
- }
- s += e * 385;
- s <<= 4;
- s += (range >> 1);
- s /= range;
- if (s >= 1024) return 0;
- s = 1024 - s;
- if (s > 511) {
- s -= 512;
- b = 64;
- } else if (s > 255) {
- s -= 256;
- b = 32;
- } else {
- b = 0;
- }
- return b + ((s * 127) >> 10);
- #endif
- }
-
-
-
-
- uint32_t usb_midi_class::midiusb_available()
- {
- uint8_t c, intr_state;
-
- intr_state = SREG;
- cli();
- if (!usb_configuration) {
- SREG = intr_state;
- return 0;
- }
- UENUM = MIDI_RX_ENDPOINT;
- retry:
- c = UEINTX;
- if (!(c & (1<<RWAL))) {
- if (c & (1<<RXOUTI)) {
- UEINTX = 0x6B;
- goto retry;
- }
- SREG = intr_state;
- return 0;
- }
- SREG = intr_state;
- return 4;
- }
-
- void usb_midi_class::midiusb_read(uint8_t *buf)
- {
- uint8_t c, intr_state;
-
- intr_state = SREG;
- cli();
- if (!usb_configuration) {
- SREG = intr_state;
- buf[0] = 0;
- buf[1] = 0;
- buf[2] = 0;
- buf[3] = 0;
- return;
- }
- UENUM = MIDI_RX_ENDPOINT;
- retry:
- c = UEINTX;
- if (!(c & (1<<RWAL))) {
- if (c & (1<<RXOUTI)) {
- UEINTX = 0x6B;
- goto retry;
- }
- SREG = intr_state;
- buf[0] = 0;
- buf[1] = 0;
- buf[2] = 0;
- buf[3] = 0;
- return;
- }
- buf[0] = UEDATX;
- buf[1] = UEDATX;
- buf[2] = UEDATX;
- buf[3] = UEDATX;
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
- SREG = intr_state;
- }
-
-
- bool usb_midi_class::read(uint8_t channel)
- {
- uint8_t c, intr_state;
- uint8_t b0, b1, b2, b3, type1, type2;
-
- intr_state = SREG;
- cli();
- if (!usb_configuration) {
- SREG = intr_state;
- return false;
- }
- UENUM = MIDI_RX_ENDPOINT;
- retry:
- c = UEINTX;
- if (!(c & (1<<RWAL))) {
- if (c & (1<<RXOUTI)) {
- UEINTX = 0x6B;
- goto retry;
- }
- SREG = intr_state;
- return false;
- }
- b0 = UEDATX;
- b1 = UEDATX;
- b2 = UEDATX;
- b3 = UEDATX;
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
- SREG = intr_state;
-
- type1 = b0 & 0x0F;
- type2 = b1 & 0xF0;
- c = (b1 & 0x0F) + 1;
- if (type1 >= 0x08 && type1 <= 0x0E) {
- if (channel && channel != c) {
-
- return false;
- }
- if (type1 == 0x08 && type2 == 0x80) {
- msg_type = 0x80;
- if (handleNoteOff) (*handleNoteOff)(c, b2, b3);
- } else
- if (type1 == 0x09 && type2 == 0x90) {
- if (b3) {
- msg_type = 0x90;
- if (handleNoteOn) (*handleNoteOn)(c, b2, b3);
- } else {
- msg_type = 0x80;
- if (handleNoteOff) (*handleNoteOff)(c, b2, b3);
- }
- } else
- if (type1 == 0x0A && type2 == 0xA0) {
- msg_type = 0xA0;
- if (handleVelocityChange) (*handleVelocityChange)(c, b2, b3);
- } else
- if (type1 == 0x0B && type2 == 0xB0) {
- msg_type = 0xB0;
- if (handleControlChange) (*handleControlChange)(c, b2, b3);
- } else
- if (type1 == 0x0C && type2 == 0xC0) {
- msg_type = 0xC0;
- if (handleProgramChange) (*handleProgramChange)(c, b2);
- } else
- if (type1 == 0x0D && type2 == 0xD0) {
- msg_type = 0xD0;
- if (handleAfterTouch) (*handleAfterTouch)(c, b2);
- } else
- if (type1 == 0x0E && type2 == 0xE0) {
- msg_type = 0xE0;
- if (handlePitchChange) {
- int value = (b2 & 0x7F) | ((int)(b3 & 0x7F) << 7);
- value -= 8192;
- (*handlePitchChange)(c, value);
- }
- } else {
- return false;
- }
- return_message:
-
-
- msg_channel = c;
- msg_data1 = b2;
- msg_data2 = b3;
- return true;
- }
- if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && type2 == 0x0F)) {
-
- system_common_or_realtime:
- switch (b1) {
- case 0xF1:
- if (handleTimeCodeQuarterFrame) {
- (*handleTimeCodeQuarterFrame)(b2);
- }
- break;
- case 0xF2:
- if (handleSongPosition) {
- (*handleSongPosition)(
- (uint16_t)(b2 & 0x7F) | (uint16_t)(b3 & 0x7F) << 7);
- }
- break;
- case 0xF3:
- if (handleSongSelect) {
- (*handleSongSelect)(b2);
- }
- break;
- case 0xF6:
- if (handleTuneRequest) {
- (*handleTuneRequest)();
- }
- break;
- case 0xF8:
- if (handleClock) {
- (*handleClock)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xF8);
- }
- break;
- case 0xFA:
- if (handleStart) {
- (*handleStart)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFA);
- }
- break;
- case 0xFB:
- if (handleContinue) {
- (*handleContinue)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFB);
- }
- break;
- case 0xFC:
- if (handleStop) {
- (*handleStop)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFC);
- }
- break;
- case 0xFE:
- if (handleActiveSensing) {
- (*handleActiveSensing)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFE);
- }
- break;
- case 0xFF:
- if (handleSystemReset) {
- (*handleSystemReset)();
- } else if (handleRealTimeSystem) {
- (*handleRealTimeSystem)(0xFF);
- }
- break;
- default:
- return false;
- }
- msg_type = b1;
- goto return_message;
- }
- if (type1 == 0x04) {
- read_sysex_byte(b1);
- read_sysex_byte(b2);
- read_sysex_byte(b3);
- return false;
- }
- if (type1 >= 0x05 && type1 <= 0x07) {
- read_sysex_byte(b1);
- if (type1 >= 0x06) read_sysex_byte(b2);
- if (type1 == 0x07) read_sysex_byte(b3);
- uint16_t len = msg_sysex_len;
- msg_data1 = len;
- msg_data2 = len >> 8;
- msg_sysex_len = 0;
- msg_type = 0xF0;
- if (handleSysExPartial) {
- (*handleSysExPartial)(msg_sysex, len, 1);
- } else if (handleSysExComplete) {
- (*handleSysExComplete)(msg_sysex, len);
- }
- return true;
- }
- if (type1 == 0x0F) {
- if (b1 >= 0xF8) {
-
-
- goto system_common_or_realtime;
- }
- if (msg_sysex_len > 0) {
-
-
-
- read_sysex_byte(b1);
- }
- }
- return false;
- }
-
- void usb_midi_class::read_sysex_byte(uint8_t b)
- {
- if (handleSysExPartial && msg_sysex_len >= USB_MIDI_SYSEX_MAX) {
-
- (*handleSysExPartial)(msg_sysex, msg_sysex_len, 0);
- msg_sysex_len = 0;
- }
- if (msg_sysex_len < USB_MIDI_SYSEX_MAX) {
- msg_sysex[msg_sysex_len++] = b;
- }
- }
-
-
-
-
-
- static volatile uint8_t prev_byte=0;
-
- void usb_serial_class::begin(long speed)
- {
-
- usb_init();
- uint16_t begin_wait = (uint16_t)millis();
- while (1) {
- if (usb_configuration) {
- delay(200);
- return;
- }
- if (usb_suspended) {
- uint16_t begin_suspend = (uint16_t)millis();
- while (usb_suspended) {
-
-
-
- if ((uint16_t)millis() - begin_suspend > 250) {
- return;
- }
- }
- }
-
-
- if ((uint16_t)millis() - begin_wait > 2500) return;
- }
- prev_byte = 0;
- }
-
- void usb_serial_class::end()
- {
- usb_shutdown();
- delay(25);
- }
-
-
-
-
- int usb_serial_class::available()
- {
- uint8_t c;
-
- c = prev_byte;
- if (c) return 1;
- c = readnext();
- if (c) {
- prev_byte = c;
- return 1;
- }
- return 0;
- }
-
-
- int usb_serial_class::read()
- {
- uint8_t c;
-
- c = prev_byte;
- if (c) {
- prev_byte = 0;
- return c;
- }
- c = readnext();
- if (c) return c;
- return -1;
- }
-
- int usb_serial_class::peek()
- {
- uint8_t c;
-
- c = prev_byte;
- if (c) return c;
- c = readnext();
- if (c) {
- prev_byte = c;
- return c;
- }
- return -1;
- }
-
-
- uint8_t usb_serial_class::readnext(void)
- {
- uint8_t c, intr_state;
-
-
-
-
- intr_state = SREG;
- cli();
- if (!usb_configuration) {
- SREG = intr_state;
- return 0;
- }
- UENUM = DEBUG_RX_ENDPOINT;
- try_again:
- if (!(UEINTX & (1<<RWAL))) {
-
- SREG = intr_state;
- return 0;
- }
-
- c = UEDATX;
- if (c == 0) {
-
-
- UEINTX = 0x6B;
- goto try_again;
- }
-
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
- SREG = intr_state;
- return c;
- }
-
-
- void usb_serial_class::flush()
- {
- uint8_t intr_state;
-
- if (usb_configuration) {
- intr_state = SREG;
- cli();
- UENUM = DEBUG_RX_ENDPOINT;
- while ((UEINTX & (1<<RWAL))) {
- UEINTX = 0x6B;
- }
- SREG = intr_state;
- }
- prev_byte = 0;
- }
-
-
- #if ARDUINO >= 100
- size_t usb_serial_class::write(uint8_t c)
- #else
- #define setWriteError()
- void usb_serial_class::write(uint8_t c)
- #endif
- {
-
- uint8_t timeout, intr_state;
-
-
- if (!usb_configuration) goto error;
-
-
-
- intr_state = SREG;
- cli();
- UENUM = DEBUG_TX_ENDPOINT;
-
- #if 0
-
- if (previous_timeout) {
- if (!(UEINTX & (1<<RWAL))) {
- SREG = intr_state;
- return;
- }
- previous_timeout = 0;
- }
- #endif
-
- timeout = UDFNUML + TRANSMIT_TIMEOUT;
- while (1) {
-
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
-
-
- if (UDFNUML == timeout) {
-
- goto error;
- }
-
- if (!usb_configuration) goto error;
-
- intr_state = SREG;
- cli();
- UENUM = DEBUG_TX_ENDPOINT;
- }
-
- UEDATX = c;
-
- if (!(UEINTX & (1<<RWAL))) {
- UEINTX = 0x3A;
- debug_flush_timer = 0;
- } else {
- debug_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
- }
- SREG = intr_state;
- #if ARDUINO >= 100
- return 1;
- #endif
- error:
- #if ARDUINO >= 100
- setWriteError();
- return 0;
- #else
- return;
- #endif
- }
-
-
-
-
-
-
-
-
- void usb_serial_class::send_now(void)
- {
- uint8_t intr_state;
-
- intr_state = SREG;
- cli();
- if (debug_flush_timer) {
- UENUM = DEBUG_TX_ENDPOINT;
- while ((UEINTX & (1<<RWAL))) {
- UEDATX = 0;
- }
- UEINTX = 0x3A;
- debug_flush_timer = 0;
- }
- SREG = intr_state;
- }
-
- uint32_t usb_serial_class::baud(void)
- {
- return ((uint32_t)DEBUG_TX_SIZE * 10000 / DEBUG_TX_INTERVAL);
- }
-
- uint8_t usb_serial_class::stopbits(void)
- {
- return 1;
- }
-
- uint8_t usb_serial_class::paritytype(void)
- {
- return 0;
- }
-
- uint8_t usb_serial_class::numbits(void)
- {
- return 8;
- }
-
- uint8_t usb_serial_class::dtr(void)
- {
- return 1;
- }
-
- uint8_t usb_serial_class::rts(void)
- {
- return 1;
- }
-
- usb_serial_class::operator bool()
- {
- if (usb_configuration) return true;
- return false;
- }
-
-
-
-
-
- usb_serial_class Serial = usb_serial_class();
- usb_midi_class usbMIDI = usb_midi_class();
|