LCOV - code coverage report
Current view: top level - src/MIDI_Interfaces - DebugMIDI_Interface.hpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 6 6 100.0 %
Date: 2019-11-24 14:50:27 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include "SerialMIDI_Interface.hpp"
       4             : #include <ctype.h>
       5             : 
       6             : BEGIN_CS_NAMESPACE
       7             : 
       8             : /**
       9             :  * @brief   A class for MIDI interfaces sending and receiving 
      10             :  *          human-readable MIDI messages over a Stream.
      11             :  * 
      12             :  * @ingroup MIDIInterfaces
      13             :  */
      14           9 : class StreamDebugMIDI_Interface : public StreamMIDI_Interface {
      15             :   public:
      16             :     /**
      17             :      * @brief   Construct a debug MIDI interface on the given Stream.
      18             :      * 
      19             :      * @param   stream
      20             :      *          The Stream interface.
      21             :      */
      22           9 :     StreamDebugMIDI_Interface(Stream &stream) : StreamMIDI_Interface(stream) {}
      23             : 
      24             :     MIDI_read_t read() override;
      25             : 
      26             :   protected:
      27             :     void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2,
      28             :                   uint8_t cn) override;
      29             : 
      30             :     void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t cn) override;
      31             : 
      32             :     void sendImpl(const uint8_t *data, size_t length, uint8_t cn) override;
      33             : 
      34             :     void sendImpl(uint8_t rt, uint8_t cn) override;
      35             : 
      36             :   private:
      37           9 :     char firstChar = '\0';
      38           9 :     char secondChar = '\0';
      39             : 
      40             :     /**
      41             :      * @brief   Convert a hexadecimal character to a 4-bit nibble.
      42             :      */
      43          29 :     static uint8_t hexCharToNibble(char hex) {
      44          29 :         return hex < 'a' ? hex - '0' : hex - 'a' + 10;
      45             :     }
      46             : };
      47             : 
      48             : /**
      49             :  * @brief   A wrapper class for debug MIDI interfaces sending and receiving 
      50             :  *          human-readable MIDI messages over a Serial port of class T.
      51             :  * 
      52             :  * @note    This is a template class because the class of the Serial object
      53             :  *          is completely different on different architectures, and they
      54             :  *          do not share a common super-class that has a `begin` method.
      55             :  * 
      56             :  * @ingroup MIDIInterfaces
      57             :  */
      58             : template <typename T>
      59             : class SerialDebugMIDI_Interface : public StreamDebugMIDI_Interface {
      60             :   public:
      61             :     /**
      62             :      * @brief   Construct a new Debug MIDI Interface on the given Serial 
      63             :      *          interface with the given baud rate.
      64             :      * 
      65             :      * @param   serial
      66             :      *          The Serial interface.
      67             :      * @param   baud
      68             :      *          The baud rate for the Serial interface.
      69             :      */
      70             :     SerialDebugMIDI_Interface(T &serial, unsigned long baud = defaultBaudRate)
      71             :         : StreamDebugMIDI_Interface(serial), serial(serial), baud(baud) {}
      72             :     /**
      73             :      * @brief   Start the Serial interface at the predefined baud rate.
      74             :      */
      75             :     void begin() override { serial.begin(baud); }
      76             : 
      77             :   private:
      78             :     T &serial;
      79             :     const unsigned long baud;
      80             : };
      81             : 
      82             : /**
      83             :  * @brief   A class for debug MIDI interfaces sending and receiving 
      84             :  *          human-readable MIDI messages over a HardwareSerial port.
      85             :  * 
      86             :  * @ingroup MIDIInterfaces
      87             :  */
      88             : class HardwareSerialDebugMIDI_Interface
      89             :     : public SerialDebugMIDI_Interface<HardwareSerial> {
      90             :   public:
      91             :     /**
      92             :      * @brief   Construct a new Debug MIDI Interface on the given HardwareSerial 
      93             :      *          interface with the given baud rate.
      94             :      * 
      95             :      * @param   serial
      96             :      *          The HardwareSerial interface.
      97             :      * @param   baud
      98             :      *          The baud rate for the serial interface.
      99             :      */
     100             :     HardwareSerialDebugMIDI_Interface(HardwareSerial &serial,
     101             :                                       unsigned long baud = defaultBaudRate)
     102             :         : SerialDebugMIDI_Interface(serial, baud) {}
     103             : };
     104             : 
     105             : /**
     106             :  * @brief   A class for debug MIDI interfaces sending and receiving 
     107             :  *          human-readable MIDI messages over the USB CDC connection.
     108             :  * 
     109             :  *          Boards without a native USB connection (UNO, MEGA, Nano ...)
     110             :  *          use HardwareSerial0 for USB communication.
     111             :  * 
     112             :  * @ingroup MIDIInterfaces
     113             :  */
     114             : class USBDebugMIDI_Interface
     115             :     : public SerialDebugMIDI_Interface<decltype(Serial)> {
     116             :   public:
     117             :     /**
     118             :      * @brief   Construct a USBDebugMIDI_Interface with the given baud rate.
     119             :      * 
     120             :      * @param   baud
     121             :      *          The baud rate to start the USB Serial connection with.
     122             :      */
     123             :     USBDebugMIDI_Interface(unsigned long baud = defaultBaudRate)
     124             :         : SerialDebugMIDI_Interface(Serial, baud) {}
     125             : };
     126             : 
     127             : // TODO: Teensy 4.0 SoftwareSerial bug
     128             : #if defined(__AVR__) || (defined(TEENSYDUINO) && TEENSYDUINO != 147) ||        \
     129             :     (defined(TEENSYDUINO) && !defined(__IMXRT1052__) &&                        \
     130             :      !defined(__IMXRT1062__))
     131             : #include <SoftwareSerial.h>
     132             : /**
     133             :  * @brief   A class for debug MIDI interfaces sending and receiving 
     134             :  *          human-readable MIDI messages over a SoftwareSerial interface.
     135             :  * 
     136             :  * @ingroup MIDIInterfaces
     137             :  */
     138             : class SoftwareSerialDebugMIDI_Interface
     139             :     : public SerialDebugMIDI_Interface<SoftwareSerial> {
     140             :   public:
     141             :     /**
     142             :      * @brief   Construct a SoftwareSerialDebugMIDI_Interface on the given 
     143             :      *          SoftwareSerial interface with the given baud rate.
     144             :      * 
     145             :      * @param   serial
     146             :      *          The SoftwareSerial interface.
     147             :      * @param   baud
     148             :      *          The baud rate for the serial interface.
     149             :      */
     150             :     SoftwareSerialDebugMIDI_Interface(SoftwareSerial &serial,
     151             :                                       unsigned long baud)
     152             :         : SerialDebugMIDI_Interface(serial, baud) {}
     153             : };
     154             : #endif
     155             : 
     156             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-5-g4ff2ed6