LCOV - code coverage report
Current view: top level - src/Control_Surface - Control_Surface_Class.cpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 8 100 8.0 %
Date: 2019-11-24 14:50:27 Functions: 3 11 27.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "Control_Surface_Class.hpp"
       2             : #include <Hardware/ExtendedInputOutput/ExtendedIOElement.hpp>
       3             : #include <Hardware/FilteredAnalog.hpp>
       4             : #include <MIDI_Constants/Control_Change.hpp>
       5             : #include <MIDI_Inputs/MIDIInputElementCC.hpp>
       6             : #include <MIDI_Inputs/MIDIInputElementChannelPressure.hpp>
       7             : #include <MIDI_Inputs/MIDIInputElementNote.hpp>
       8             : #include <MIDI_Inputs/MIDIInputElementPC.hpp>
       9             : #include <MIDI_Inputs/MIDIInputElementSysEx.hpp>
      10             : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>
      11             : #include <Selectors/Selector.hpp>
      12             : 
      13             : #include <Arduino.h>
      14             : 
      15             : BEGIN_CS_NAMESPACE
      16             : 
      17           2 : Control_Surface_ &Control_Surface_::getInstance() {
      18           2 :     static Control_Surface_ instance;
      19           2 :     return instance;
      20           0 : }
      21             : 
      22           0 : void Control_Surface_::begin() {
      23             : #if defined(ARDUINO) && defined(DEBUG_OUT)
      24             :     DEBUG_OUT.begin(defaultBaudRate);
      25             :     delay(250);
      26             : #endif
      27           0 :     FilteredAnalog<>::setupADC();
      28           0 :     ExtendedIOElement::beginAll();
      29           0 :     MIDI().begin(); // initialize the MIDI interface
      30           0 :     MIDI().setCallbacks(this);
      31           0 :     DisplayInterface::beginAll(); // initialize all displays
      32           0 :     MIDIInputElementCC::beginAll();
      33           0 :     MIDIInputElementPC::beginAll();
      34           0 :     MIDIInputElementChannelPressure::beginAll();
      35           0 :     MIDIInputElementNote::beginAll();
      36           0 :     MIDIInputElementSysEx::beginAll();
      37           0 :     Updatable<>::beginAll();
      38           0 :     Updatable<Potentiometer>::beginAll();
      39           0 :     Updatable<MotorFader>::beginAll();
      40           0 :     Updatable<Display>::beginAll();
      41           0 :     potentiometerTimer.begin();
      42           0 :     displayTimer.begin();
      43           0 : }
      44             : 
      45           0 : void Control_Surface_::loop() {
      46           0 :     Updatable<>::updateAll();
      47           0 :     if (potentiometerTimer)
      48           0 :         Updatable<Potentiometer>::updateAll();
      49           0 :     updateMidiInput();
      50           0 :     updateInputs();
      51           0 :     if (displayTimer)
      52           0 :         updateDisplays();
      53           0 : }
      54             : 
      55          94 : MIDI_Interface &Control_Surface_::MIDI() {
      56          94 :     MIDI_Interface *midi = MIDI_Interface::getDefault();
      57          94 :     if (midi == nullptr)
      58           0 :         FATAL_ERROR(F("No default MIDI interface is selected."), 0xDEAD);
      59             : 
      60          94 :     return *midi;
      61           0 : }
      62             : 
      63           0 : void Control_Surface_::updateMidiInput() {
      64           0 :     MIDI_Interface &midi = MIDI();
      65           0 :     midi.update();
      66           0 : }
      67             : 
      68           0 : void Control_Surface_::onChannelMessage(Parsing_MIDI_Interface &midi) {
      69           0 :     ChannelMessage midichmsg = midi.getChannelMessage();
      70           0 :     ChannelMessageMatcher midimsg = {midichmsg};
      71             : 
      72             : #ifdef DEBUG_MIDI_PACKETS
      73             :     // TODO: print CN
      74             :     if (midimsg.type != PROGRAM_CHANGE && midimsg.type != CHANNEL_PRESSURE)
      75             :         DEBUG(">>> " << hex << midichmsg.header << ' ' << midimsg.data1 << ' '
      76             :                      << midimsg.data2 << dec);
      77             :     else
      78             :         DEBUG(">>> " << hex << midichmsg.header << ' ' << midimsg.data1 << dec);
      79             : #endif
      80             : 
      81             :     // If the Channel Message callback exists, call it to see if we have to
      82             :     // continue handling it.
      83           0 :     if (channelMessageCallback && channelMessageCallback(midichmsg))
      84           0 :         return;
      85             : 
      86           0 :     if (midimsg.type == CC && midimsg.data1 == 0x79) {
      87             :         // Reset All Controllers
      88           0 :         DEBUG(F("Reset All Controllers"));
      89           0 :         MIDIInputElementCC::resetAll();
      90           0 :         MIDIInputElementChannelPressure::resetAll();
      91           0 :     } else if (midimsg.type == CC && midimsg.data1 == MIDI_CC::All_Notes_Off) {
      92           0 :         MIDIInputElementNote::resetAll();
      93           0 :     } else {
      94           0 :         if (midimsg.type == CC) {
      95             :             // Control Change
      96           0 :             DEBUGFN(F("Updating CC elements with new MIDI message."));
      97           0 :             MIDIInputElementCC::updateAllWith(midimsg);
      98             : 
      99           0 :         } else if (midimsg.type == NOTE_OFF || midimsg.type == NOTE_ON) {
     100             :             // Note
     101           0 :             DEBUGFN(F("Updating Note elements with new MIDI message."));
     102           0 :             MIDIInputElementNote::updateAllWith(midimsg);
     103             : 
     104           0 :         } else if (midimsg.type == CHANNEL_PRESSURE) {
     105             :             // Channel Pressure
     106           0 :             DEBUGFN(F("Updating Channel Pressure elements with new "
     107             :                       "MIDI message."));
     108           0 :             MIDIInputElementChannelPressure::updateAllWith(midimsg);
     109           0 :         } else if (midimsg.type == PROGRAM_CHANGE) {
     110             :             // Channel Pressure
     111           0 :             DEBUGFN(F("Updating Program Change elements with new "
     112             :                       "MIDI message."));
     113           0 :             MIDIInputElementPC::updateAllWith(midimsg);
     114           0 :         }
     115             :     }
     116           0 : }
     117             : 
     118           0 : void Control_Surface_::onSysExMessage(Parsing_MIDI_Interface &midi) {
     119             :     // System Exclusive
     120           0 :     SysExMessage msg = midi.getSysExMessage();
     121             : #ifdef DEBUG_MIDI_PACKETS
     122             :     const uint8_t *data = msg.data;
     123             :     size_t len = msg.length;
     124             :     // TODO: print CN
     125             :     DEBUG_OUT << hex;
     126             :     for (size_t i = 0; i < len; i++)
     127             :         DEBUG_OUT << data[i] << ' ';
     128             :     DEBUG_OUT << dec << endl;
     129             : #endif
     130             :     // If the SysEx Message callback exists, call it to see if we have to
     131             :     // continue handling it.
     132           0 :     if (sysExMessageCallback && sysExMessageCallback(msg))
     133           0 :         return;
     134           0 :     MIDIInputElementSysEx::updateAllWith(msg);
     135           0 : }
     136             : 
     137           0 : void Control_Surface_::onRealtimeMessage(Parsing_MIDI_Interface &midi,
     138             :                                          uint8_t message) {
     139           0 :     RealTimeMessage rtMessage = {message, midi.getCN()};
     140             :     // If the Real-Time Message callback exists, call it to see if we have to
     141             :     // continue handling it.
     142           0 :     if (realTimeMessageCallback && realTimeMessageCallback(rtMessage))
     143           0 :         return;
     144             :     // TODO: handle Real-Time input
     145           0 : }
     146             : 
     147           0 : void Control_Surface_::updateInputs() {
     148           0 :     MIDIInputElementCC::updateAll();
     149           0 :     MIDIInputElementNote::updateAll();
     150           0 :     MIDIInputElementChannelPressure::updateAll();
     151           0 :     MIDIInputElementPC::updateAll();
     152           0 :     MIDIInputElementSysEx::updateAll();
     153           0 : }
     154             : 
     155           0 : void Control_Surface_::updateDisplays() {
     156           0 :     DisplayInterface *previousDisplay = nullptr;
     157           0 :     for (DisplayElement &displayElement : DisplayElement::getAll()) {
     158           0 :         DisplayInterface *thisDisplay = &displayElement.getDisplay();
     159           0 :         if (thisDisplay != previousDisplay) {
     160           0 :             if (previousDisplay)
     161           0 :                 previousDisplay->display();
     162           0 :             thisDisplay->clearAndDrawBackground();
     163           0 :         }
     164           0 :         displayElement.draw();
     165           0 :         previousDisplay = thisDisplay;
     166             :     }
     167           0 :     if (previousDisplay)
     168           0 :         previousDisplay->display();
     169           0 : }
     170             : 
     171           2 : Control_Surface_ &Control_Surface = Control_Surface_::getInstance();
     172             : 
     173             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-5-g4ff2ed6