LCOV - code coverage report
Current view: top level - src/MIDI_Outputs/Abstract - MIDIRotaryEncoder.hpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 17 18 94.4 %
Date: 2019-11-24 14:50:27 Functions: 4 6 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #if not defined(Encoder_h_) && not defined(IDE)
       4             : #error                                                                         \
       5             :     "The PJRC Encoder library should be included before the Control-Surface "  \
       6             :     "library. (#include <Encoder.h>)"
       7             : #endif
       8             : 
       9             : #include <Def/Def.hpp>
      10             : #include <Encoder.h>
      11             : #include <Helpers/Array.hpp>
      12             : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>
      13             : 
      14             : BEGIN_CS_NAMESPACE
      15             : 
      16             : /**
      17             :  * @brief   An abstract class for rotary encoders that send MIDI events.
      18             :  */
      19             : template <class Sender>
      20           8 : class MIDIRotaryEncoder : public MIDIOutputElement {
      21             :   protected:
      22             :     /**
      23             :      * @brief   Construct a new MIDIRotaryEncoder.
      24             :      *
      25             :      * @todo    Documentation
      26             :      */
      27           1 :     MIDIRotaryEncoder(const EncoderPinList &pins,
      28             :                       const MIDICNChannelAddress &address,
      29             :                       uint8_t speedMultiply, uint8_t pulsesPerStep,
      30             :                       const Sender &sender)
      31           1 :         : encoder{pins.A, pins.B}, address(address),
      32           1 :           speedMultiply(speedMultiply),
      33           2 :           pulsesPerStep(pulsesPerStep), sender{sender} {}
      34             : 
      35             : // For tests only
      36             : #ifndef ARDUINO
      37           7 :     MIDIRotaryEncoder(const Encoder &encoder,
      38             :                       const MIDICNChannelAddress &address,
      39             :                       uint8_t speedMultiply, uint8_t pulsesPerStep,
      40             :                       const Sender &sender)
      41           7 :         : encoder{encoder}, address(address), speedMultiply(speedMultiply),
      42          28 :           pulsesPerStep(pulsesPerStep), sender{sender} {}
      43             : #endif
      44             : 
      45             :   public:
      46           0 :     void begin() final override {}
      47          10 :     void update() final override {
      48          10 :         long currentPosition = encoder.read();
      49          10 :         long difference = (currentPosition - previousPosition) / pulsesPerStep;
      50          10 :         if (difference) {
      51           8 :             sender.send(difference * speedMultiply, address);
      52           8 :             previousPosition += difference * pulsesPerStep;
      53           8 :         }
      54          10 :     }
      55             : 
      56             :   private:
      57             :     Encoder encoder;
      58             :     const MIDICNChannelAddress address;
      59             :     const uint8_t speedMultiply;
      60             :     const uint8_t pulsesPerStep;
      61           8 :     long previousPosition = 0;
      62             : 
      63             :   public:
      64             :     Sender sender;
      65             : };
      66             : 
      67             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-5-g4ff2ed6