 uint8_t laststate;
 uint8_t rcode;
 USB_DEVICE_DESCRIPTOR buf;
+OutParserState prsState; /* Parser */
 
 void MIDI_poll();
-void doDelay(unsigned long t1, unsigned long t2, unsigned long delayTime);
 
 boolean bFirst;
 boolean isConnected;
+boolean isSysex;
 uint16_t pid, vid;
 
 

#include <Usb.h>
#include <usbh_midi.h>
#define __CORE_CM0_H_GENERIC
#define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
#define BLE_UUID_TX_CHARACTERISTIC       0x0001 /**< The UUID of the TX Characteristic. */
#define BLE_UUID_RX_CHARACTERISTIC      0x0002 /**< The UUID of the RX Characteristic. */

#define TXRX_BUF_LEN                     100
#define UART_RX_TIME                     APP_TIMER_TICKS(10, 0)


#define STATUS_CHECK_TIME                APP_TIMER_TICKS(20, 0)


// Activate to debug
// this increases delay significantly due to increased I/Os
#define DEBUG 1

#ifdef DEBUG
#define DEBUG_PRINT(x)     Serial.print (x)
#define DEBUG_PRINTDEC(x)     Serial.print (x, DEC)
#define DEBUG_PRINTHEX(x)     Serial.print (x, HEX)
#define DEBUG_PRINTLN(x)  Serial.println (x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTDEC(x)
#define DEBUG_PRINTHEX(x)
#define DEBUG_PRINTLN(x)
#endif



BLEDevice  ble;

static app_timer_id_t                    m_uart_rx_id;
static app_timer_id_t                    m_status_check_id;
static uint8_t rx_buf[TXRX_BUF_LEN];
static int rx_buf_num, rx_state = 0;
static uint8_t rx_temp_buf[20];

// TXRX Service & UDIDs
static const uint8_t uart_base_uuid[] = {0x03, 0xB8, 0x0E, 0x5A, 0xED, 0xE8, 0x4B, 0x33, 0xA7, 0x51, 0x6C, 0xE3, 0x4E, 0xC4, 0xC7, 0};
static const uint8_t uart_tx_uuid[]   = {0x77, 0x72, 0xE5, 0xDB, 0x38, 0x68, 0x41, 0x12, 0xA1, 0xA9, 0xF2, 0x66, 0x9D, 0x10, 0x6B, 0xF3};
static const uint8_t uart_rx_uuid[]   = {0x77, 0x72, 0xE5, 0xDB, 0x38, 0x68, 0x41, 0x12, 0xA1, 0xA9, 0xF2, 0x66, 0x9D, 0x10, 0x6B, 0xF3};

uint8_t txPayload[TXRX_BUF_LEN] = {0,};
uint8_t rxPayload[TXRX_BUF_LEN] = {0,};

GattCharacteristic  txCharacteristic
  (uart_tx_uuid, txPayload, 1, 20,
  GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
  GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE |
  GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
  GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);

GattCharacteristic *uartChars[] = {&txCharacteristic};
GattService         uartService(uart_base_uuid, uartChars,
                                sizeof(uartChars) / sizeof(GattCharacteristic *));

/* Parser States: START (uninitialized), MIDI, SYSEX, RUNNING MSG*/
typedef enum { STRT, MIDI, SYSEX, RUN} OutParserState;

/*******************************************************************************
 * INITIALIZE USB MIDI Variables
