LCOV - code coverage report
Current view: top level - src/MIDI_Interfaces - SerialMIDI_Interface.hpp (source / functions) Hit Total Coverage
Test: 90a1b9beff85a60dc6ebcea034a947a845e56960 Lines: 23 26 88.5 %
Date: 2019-11-30 15:53:32 Functions: 6 8 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include "MIDI_Interface.hpp"
       4             : #include <AH/Teensy/TeensyUSBTypes.hpp>
       5             : #include <Arduino.h> // Stream
       6             : #include <MIDI_Parsers/SerialMIDI_Parser.hpp>
       7             : #include <Settings/SettingsWrapper.hpp>
       8             : 
       9             : BEGIN_CS_NAMESPACE
      10             : 
      11             : /**
      12             :  * @brief   A class for MIDI interfaces sending and receiving MIDI messages
      13             :  *          over a Stream.
      14             :  * 
      15             :  * @ingroup MIDIInterfaces
      16             :  */
      17          18 : class StreamMIDI_Interface : public Parsing_MIDI_Interface {
      18             :   public:
      19             :     /**
      20             :      * @brief   Construct a StreamMIDI_Interface on the given Stream.
      21             :      *
      22             :      * @param   stream
      23             :      *          The Stream interface.
      24             :      */
      25          18 :     StreamMIDI_Interface(Stream &stream)
      26          36 :         : Parsing_MIDI_Interface(parser), stream(stream) {}
      27             : 
      28           5 :     MIDI_read_t read() override {
      29          22 :         while (stream.available() > 0) {
      30          21 :             uint8_t midiByte = stream.read();
      31          21 :             MIDI_read_t parseResult = parser.parse(midiByte);
      32          21 :             if (parseResult != NO_MESSAGE)
      33           4 :                 return parseResult;
      34             :         }
      35           1 :         return NO_MESSAGE;
      36           5 :     }
      37             : 
      38             :   protected:
      39             :     SerialMIDI_Parser parser;
      40             : 
      41           7 :     void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2,
      42             :                   uint8_t cn) override {
      43             :         (void)cn;
      44           7 :         stream.write(m | c); // Send the MIDI message over the stream
      45           7 :         stream.write(d1);
      46           7 :         stream.write(d2);
      47             :         // stream.flush(); // TODO
      48           7 :     }
      49             : 
      50           5 :     void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t cn) override {
      51             :         (void)cn;
      52           5 :         stream.write(m | c); // Send the MIDI message over the stream
      53           5 :         stream.write(d1);
      54             :         // stream.flush(); // TODO
      55           5 :     }
      56             : 
      57           1 :     void sendImpl(const uint8_t *data, size_t length, uint8_t cn) override {
      58             :         (void)cn;
      59           1 :         stream.write(data, length);
      60             :         // stream.flush(); // TODO
      61           1 :     }
      62             : 
      63           0 :     void sendImpl(uint8_t rt, uint8_t cn) override {
      64             :         (void)cn;
      65           0 :         stream.write(rt); // Send the MIDI message over the stream
      66             :         // stream.flush(); // TODO
      67           0 :     }
      68             : 
      69             :   protected:
      70             :     Stream &stream;
      71             : };
      72             : 
      73             : /**
      74             :  * @brief   A wrapper class for MIDI interfaces sending and receiving
      75             :  *          MIDI messages over a Serial port of class T.
      76             :  *
      77             :  * @note    This is a template class because the class of the Serial object
      78             :  *          is completely different on different architectures, and they
      79             :  *          do not share a common super-class that has a `begin` method.
      80             :  * 
      81             :  * @ingroup MIDIInterfaces
      82             :  */
      83             : template <class T>
      84             : class SerialMIDI_Interface : public StreamMIDI_Interface {
      85             :   public:
      86             :     /**
      87             :      * @brief   Create a new MIDI Interface on the given Serial interface
      88             :      *          with the given baud rate.
      89             :      *
      90             :      * @param   serial
      91             :      *          The Serial interface.
      92             :      * @param   baud
      93             :      *          The baud rate for the Serial interface.
      94             :      */
      95             :     SerialMIDI_Interface(T &serial, unsigned long baud)
      96             :         : StreamMIDI_Interface(serial), serial(serial), baud(baud) {}
      97             :     /**
      98             :      * @brief   Start the Serial interface at the predefined baud rate.
      99             :      */
     100             :     void begin() override { serial.begin(baud); }
     101             : 
     102             :   private:
     103             :     T &serial;
     104             :     const unsigned long baud;
     105             : };
     106             : 
     107             : /**
     108             :  * @brief   A class for MIDI interfaces sending and receiving
     109             :  *          MIDI messages over a Serial port of class T.
     110             :  * 
     111             :  * @ingroup MIDIInterfaces
     112             :  */
     113             : class HardwareSerialMIDI_Interface
     114             :     : public SerialMIDI_Interface<HardwareSerial> {
     115             :   public:
     116             :     /**
     117             :      * @brief   Construct a new MIDI Interface on the given HardwareSerial
     118             :      *          interface with the given baud rate.
     119             :      *
     120             :      * @param   serial
     121             :      *          The HardwareSerial interface.
     122             :      * @param   baud
     123             :      *          The baud rate for the serial interface.
     124             :      */
     125             :     HardwareSerialMIDI_Interface(HardwareSerial &serial, unsigned long baud)
     126             :         : SerialMIDI_Interface(serial, baud) {}
     127             : };
     128             : 
     129             : /**
     130             :  * @brief   A class for MIDI interfaces sending and receiving
     131             :  *          MIDI messages over the Serial port of the USB connection.
     132             :  *
     133             :  * @ingroup MIDIInterfaces
     134             :  */
     135             : class USBSerialMIDI_Interface : public SerialMIDI_Interface<decltype(Serial)> {
     136             :   public:
     137             :     /**
     138             :      * @brief   Construct a USBSerialMIDI_Interface with the given baud rate.
     139             :      *
     140             :      * @param   baud
     141             :      *          The baud rate to start the USB Serial connection with.
     142             :      */
     143             :     USBSerialMIDI_Interface(unsigned long baud)
     144             :         : SerialMIDI_Interface(Serial, baud) {}
     145             : };
     146             : 
     147             : #if !defined(TEENSYDUINO) ||                                                   \
     148             :     (defined(TEENSYDUINO) && defined(TEENSY_SERIALUSB_ENABLED))
     149             : /**
     150             :  * @brief   A class for MIDI Interfaces sending and receiving
     151             :  *          data over the USB Serial CDC connection for the use
     152             :  *          with the [Hairless MIDI<->Serial Bridge]
     153             :  *          (http://projectgus.github.io/hairless-midiserial/).
     154             :  * 
     155             :  * @ingroup MIDIInterfaces
     156             :  */
     157             : class HairlessMIDI_Interface : public USBSerialMIDI_Interface {
     158             :   public:
     159             :     /**
     160             :      * @brief   Construct a HairlessMIDI_Interface.
     161             :      *
     162             :      * The default Hairless baud rate of 115200 baud is used.
     163             :      * This can be changed in the Settings.hpp file.
     164             :      */
     165             :     HairlessMIDI_Interface() : USBSerialMIDI_Interface(HAIRLESS_BAUD){};
     166             : };
     167             : #endif
     168             : 
     169             : END_CS_NAMESPACE
     170             : 
     171             : // TODO: Teensy 4.0 SoftwareSerial bug
     172             : #if defined(__AVR__) || (defined(TEENSYDUINO) && TEENSYDUINO != 147) ||        \
     173             :     (defined(TEENSYDUINO) && !defined(__IMXRT1052__) &&                        \
     174             :      !defined(__IMXRT1062__))
     175             : 
     176             : #include <SoftwareSerial.h>
     177             : 
     178             : BEGIN_CS_NAMESPACE
     179             : 
     180             : /**
     181             :  * @brief   A class for MIDI interfaces sending and receiving
     182             :  *          MIDI messages over a SoftwareSerial interface.
     183             :  * 
     184             :  * @ingroup MIDIInterfaces
     185             :  */
     186             : class SoftwareSerialMIDI_Interface
     187             :     : public SerialMIDI_Interface<SoftwareSerial> {
     188             :   public:
     189             :     /**
     190             :      * @brief   Create a SoftwareSerialMIDI_Interface on the given
     191             :      *          SoftwareSerial interface with the given baud rate.
     192             :      *
     193             :      * @param   serial
     194             :      *          The SoftwareSerial interface.
     195             :      * @param   baud
     196             :      *          The baud rate for the serial interface.
     197             :      */
     198             :     SoftwareSerialMIDI_Interface(SoftwareSerial &serial, unsigned long baud)
     199             :         : SerialMIDI_Interface(serial, baud) {}
     200             : };
     201             : 
     202             : END_CS_NAMESPACE
     203             : 
     204             : #endif

Generated by: LCOV version 1.14-5-g4ff2ed6