LCOV - code coverage report
Current view: top level - src/MIDI_Outputs/Abstract - MIDIRotaryEncoder.hpp (source / functions) Hit Total Coverage
Test: 169c36a3797bc662d84b5726f34a3f37d3c58247 Lines: 8 9 88.9 %
Date: 2024-11-09 15:32:27 Functions: 2 3 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <AH/STL/utility> // std::forward
       4             : #include <Def/Def.hpp>
       5             : #include <Def/TypeTraits.hpp>
       6             : #include <MIDI_Outputs/Abstract/EncoderState.hpp>
       7             : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>
       8             : 
       9             : #ifdef ARDUINO
      10             : #include <Submodules/Encoder/AHEncoder.hpp>
      11             : #else
      12             : #include <Encoder.h> // Mock
      13             : #endif
      14             : 
      15             : BEGIN_CS_NAMESPACE
      16             : 
      17             : /**
      18             :  * @brief   An abstract class for rotary encoders that send MIDI events.
      19             :  */
      20             : template <class Enc, class Sender>
      21             : class GenericMIDIRotaryEncoder : public MIDIOutputElement {
      22             :   public:
      23             :     /**
      24             :      * @brief   Construct a new MIDIRotaryEncoder.
      25             :      *
      26             :      * @todo    Documentation
      27             :      */
      28          13 :     GenericMIDIRotaryEncoder(Enc &&encoder, MIDIAddress address,
      29             :                              int16_t speedMultiply, uint8_t pulsesPerStep,
      30             :                              const Sender &sender)
      31          13 :         : encoder(std::forward<Enc>(encoder)), address(address),
      32          26 :           encstate(speedMultiply, pulsesPerStep), sender(sender) {}
      33             : 
      34           0 :     void begin() override { begin_if_possible(encoder); }
      35             : 
      36          32 :     void update() override {
      37          32 :         auto encval = encoder.read();
      38          32 :         if (int16_t delta = encstate.update(encval)) {
      39          29 :             sender.send(delta, address);
      40             :         }
      41          32 :     }
      42             : 
      43             :     void setSpeedMultiply(int16_t speedMultiply) {
      44             :         encstate.setSpeedMultiply(speedMultiply);
      45             :     }
      46             :     int16_t getSpeedMultiply() const { return encstate.getSpeedMultiply(); }
      47             : 
      48             :     /// Get the MIDI address.
      49             :     MIDIAddress getAddress() const { return this->address; }
      50             :     /// Set the MIDI address.
      51             :     void setAddress(MIDIAddress address) { this->address = address; }
      52             : 
      53             :     int16_t resetPositionOffset() {
      54             :         auto encval = encoder.read();
      55             :         return encstate.update(encval);
      56             :     }
      57             : 
      58             :   private:
      59             :     Enc encoder;
      60             :     MIDIAddress address;
      61             :     EncoderState<decltype(encoder.read())> encstate;
      62             : 
      63             :   public:
      64             :     Sender sender;
      65             : };
      66             : 
      67             : template <class Sender>
      68             : using MIDIRotaryEncoder = GenericMIDIRotaryEncoder<AHEncoder, Sender>;
      69             : 
      70             : template <class Sender>
      71             : using BorrowedMIDIRotaryEncoder = GenericMIDIRotaryEncoder<AHEncoder &, Sender>;
      72             : 
      73             : END_CS_NAMESPACE

Generated by: LCOV version 1.15