FortySevenEffects / arduino_midi_library

MIDI for Arduino
MIT License
1.59k stars 255 forks source link

Teensy specific Serial data format #196

Open jacobanana opened 3 years ago

jacobanana commented 3 years ago

It is possible to specify inverted pins on UART ports when using Teensy. This is documented here: https://www.pjrc.com/teensy/td_uart.html under the Serial1.begin(baud, format) section.

For MIDI, the relevant formats are :

Format Name Data Bits Parity RX Polarity TX Polarity
SERIAL_8N1 8 None Normal Normal
SERIAL_8N1_RXINV 8 None Inverted Normal
SERIAL_8N1_TXINV 8 None Normal Inverted
SERIAL_8N1_RXINV_TXINV 8 None Inverted Inverted

This allows more flexibility with input and output circuits that may be inverting.

In order to configure the polarity, it is important to inherit the custom settings struct from DefaultSerialSettings instead of DefaultSettings

struct TXInvMidiSettings : public midi::DefaultSerialSettings
{
    static const uint16_t SerialFormat = SERIAL_8N1_TXINV; // Invert TX
};

MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial1, midi1, TXInvMidiSettings);
franky47 commented 3 years ago

There might be special cases to handle for SofttwareSerial, which does not have this extra configuration input (see failing tests).