Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bankable::CCAbsoluteEncoder ability Fixed #227

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Control_Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include <MIDI_Outputs/ManyAddresses/PCButton.hpp>

#ifdef Encoder_h_
#include <MIDI_Outputs/Bankable/CCAbsoluteEncoder.hpp>
#include <MIDI_Outputs/Bankable/CCRotaryEncoder.hpp>
#include <MIDI_Outputs/CCAbsoluteEncoder.hpp>
#include <MIDI_Outputs/CCRotaryEncoder.hpp>
Expand Down
4 changes: 4 additions & 0 deletions src/MIDI_Outputs/Bankable/Abstract/MIDIAbsoluteEncoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifdef TEST_COMPILE_ALL_HEADERS_SEPARATELY
#include <Encoder.h>
#include "MIDIAbsoluteEncoder.hpp"
#endif
88 changes: 88 additions & 0 deletions src/MIDI_Outputs/Bankable/Abstract/MIDIAbsoluteEncoder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#pragma once

#if not defined(Encoder_h_) && not defined(IDE)
#error \
"The PJRC Encoder library should be included before the Control-Surface " \
"library. (#include <Encoder.h>)"
#endif

#include <Banks/BankableAddresses.hpp>
#include <Def/Def.hpp>
#include <Encoder.h>
#include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>

BEGIN_CS_NAMESPACE

namespace Bankable {

/**
* @brief An abstract class for rotary encoders that send MIDI events and that
* can be added to a Bank.
*/
template <uint8_t NumBanks,class BankAddress, class Sender>
class MIDIAbsoluteEncoder : public MIDIOutputElement {
protected:
/**
* @brief Construct a new MIDIAbsoluteEncoder.
*
* @todo Documentation
*/
MIDIAbsoluteEncoder(BankAddress bankAddress,
const EncoderPinList &pins,
int16_t multiplier,
uint8_t pulsesPerStep, const Sender &sender)
: address(bankAddress), encoder{pins.A, pins.B},
multiplier(multiplier), pulsesPerStep(pulsesPerStep), sender(sender) {
}

public:
void begin() final override {}

void update() override {
Enc_t encval = encoder.read();
// If Enc_t is an unsigned type, integer overflow is well-defined, which
// is what we want when Enc_t is small and expected to overflow.
// However, we need it to be signed because we're interested in the
// delta.
int16_t delta = SignedEnc_t(Enc_t(encval - deltaOffset));
delta = delta * multiplier / pulsesPerStep;
if (delta) {
address.lock();
int16_t oldValue = values[address.getSelection()];
int16_t newValue = oldValue + delta;
newValue = constrain(newValue, 0, maxValue);
if (oldValue != newValue) {
sender.send(newValue, address.getActiveAddress());
values[address.getSelection()] = newValue;
}
address.unlock();
deltaOffset += Enc_t(delta * pulsesPerStep / multiplier);
}
}

uint16_t getValue(setting_t bank) const { return values[bank]; }
uint16_t getValue() const { return getValue(address.getSelection()); }

void setValue(uint16_t value, setting_t bank) { values[bank] = value; }
void setValue(uint16_t value) { setValue(value, address.getSelection()); }

private:
BankAddress address;
Encoder encoder;
const int16_t multiplier;
const uint8_t pulsesPerStep;
// long previousPosition = 0;
Array<uint16_t, NumBanks> values = {{}};
using Enc_t = decltype(encoder.read());
using SignedEnc_t = typename std::make_signed<Enc_t>::type;
Enc_t deltaOffset = 0;

constexpr static int16_t maxValue = uint16_t(1u << Sender::precision()) - 1;

public:
Sender sender;
};

} // namespace Bankable

END_CS_NAMESPACE
4 changes: 4 additions & 0 deletions src/MIDI_Outputs/Bankable/CCAbsoluteEncoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifdef TEST_COMPILE_ALL_HEADERS_SEPARATELY
#include <Encoder.h>
#include "CCAbsoluteEncoder.hpp"
#endif
66 changes: 66 additions & 0 deletions src/MIDI_Outputs/Bankable/CCAbsoluteEncoder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include <Banks/BankAddresses.hpp>
#include <MIDI_Outputs/Bankable/Abstract/MIDIAbsoluteEncoder.hpp>
#include <MIDI_Senders/ContinuousCCSender.hpp>

BEGIN_CS_NAMESPACE

namespace Bankable {

/**
* @brief A class of MIDIOutputElement%s that read the input of a **quadrature
* (rotary) encoder** and send out relative MIDI **Control Change**
* events.
*
* This version can be banked.
*
* @note To use this class, include the [PJRC Encoder library]
* (https://github.com/PaulStoffregen/Encoder) before the
* Control-Surface library.
*
* @ingroup BankableMIDIOutputElements
*/
template <size_t NumBanks>
class CCAbsoluteEncoder
: public MIDIAbsoluteEncoder<NumBanks, SingleAddress, ContinuousCCSender> {
public:
/**
* @brief Construct a new Bankable CCAbsoluteEncoder object with the given
* pins, controller, channel, speed factor, and number of pulses
* per step.
*
* @param config
* The bank configuration to use: the bank to add this element to,
* and whether to change the address, channel or cable number.
* @param pins
* A list of the two pins connected to the A and B outputs of the
* encoder.
* The internal pull-up resistors will be enabled by the Encoder
* library.
* @param address
* The MIDI address containing the controller number [0, 119],
* channel [CHANNEL_1, CHANNEL_16], and optional cable number
* [0, 15].
* @param multiplier
* A constant factor to increase the speed of the rotary encoder.
* The position will just be multiplied by this factor.
* @param pulsesPerStep
* The number of pulses per physical click of the encoder.
* For a normal encoder, this is 4. If you want to increase the
* resolution, for the use of Jog wheels, for example, you can go
* as 1.
* Whereas a greater speedMultiplier factor will increase the
* speed, increasing the number of pulsesPerStep will result in a
* lower speed.
*/
CCAbsoluteEncoder(OutputBankConfig<NumBanks> config, const EncoderPinList &pins,
const MIDIAddress &address, int16_t multiplier = 1,
uint8_t pulsesPerStep = 4)
: MIDIAbsoluteEncoder({config, address}, pins, multiplier,
pulsesPerStep, {}) {}
};

} // namespace Bankable

END_CS_NAMESPACE