From b832b82ec6dcb777c24bf117aafbbad0ab57c424 Mon Sep 17 00:00:00 2001 From: Balazs Kelemen <10376327+prampec@users.noreply.github.com> Date: Tue, 11 Jan 2022 13:12:04 +0100 Subject: [PATCH] Use init() instead of constructor. --- src/plugins/Plugins.cpp | 3 ++- .../input/RotaryEncoderInterruptBase.cpp | 17 ++++++++++++----- src/plugins/input/RotaryEncoderInterruptBase.h | 7 ++++--- .../input/RotaryEncoderInterruptImpl1.cpp | 17 +++++++++++------ src/plugins/input/RotaryEncoderInterruptImpl1.h | 10 +++++++++- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/plugins/Plugins.cpp b/src/plugins/Plugins.cpp index 32b67998d..cd6df21e9 100644 --- a/src/plugins/Plugins.cpp +++ b/src/plugins/Plugins.cpp @@ -33,7 +33,8 @@ void setupPlugins() new RemoteHardwarePlugin(); new ReplyPlugin(); rotaryEncoderInterruptImpl1 = - new RotaryEncoderInterruptImpl1( + new RotaryEncoderInterruptImpl1(); + rotaryEncoderInterruptImpl1->init( 22, 23, 21, INPUT_EVENT_UP, INPUT_EVENT_DOWN, INPUT_EVENT_SELECT); cannedMessagePlugin = new CannedMessagePlugin(rotaryEncoderInterruptImpl1); diff --git a/src/plugins/input/RotaryEncoderInterruptBase.cpp b/src/plugins/input/RotaryEncoderInterruptBase.cpp index 49143dedf..6d775b761 100644 --- a/src/plugins/input/RotaryEncoderInterruptBase.cpp +++ b/src/plugins/input/RotaryEncoderInterruptBase.cpp @@ -7,12 +7,17 @@ */ RotaryEncoderInterruptBase::RotaryEncoderInterruptBase( - const char *name, + const char *name) : + concurrency::OSThread(name) +{ + +} + +void RotaryEncoderInterruptBase::init( uint8_t pinA, uint8_t pinB, uint8_t pinPress, char eventCw, char eventCcw, char eventPressed, -// std::function onIntA, std::function onIntB, std::function onIntPress) : - void (*onIntA)(), void (*onIntB)(), void (*onIntPress)()) : - concurrency::OSThread(name) +// std::function onIntA, std::function onIntB, std::function onIntPress) : + void (*onIntA)(), void (*onIntB)(), void (*onIntPress)()) { this->_pinA = pinA; this->_pinB = pinB; @@ -20,20 +25,22 @@ RotaryEncoderInterruptBase::RotaryEncoderInterruptBase( this->_eventCcw = eventCcw; this->_eventPressed = eventPressed; - // TODO: make pins configurable pinMode(pinPress, INPUT_PULLUP); pinMode(this->_pinA, INPUT_PULLUP); pinMode(this->_pinB, INPUT_PULLUP); + // attachInterrupt(pinPress, onIntPress, RISING); attachInterrupt(pinPress, onIntPress, RISING); attachInterrupt(this->_pinA, onIntA, CHANGE); attachInterrupt(this->_pinB, onIntB, CHANGE); + this->rotaryLevelA = digitalRead(this->_pinA); this->rotaryLevelB = digitalRead(this->_pinB); DEBUG_MSG("Rotary initialized (%d, %d, %d)\n", this->_pinA, this->_pinB, pinPress); } + int32_t RotaryEncoderInterruptBase::runOnce() { if (this->action == ROTARY_ACTION_PRESSED) diff --git a/src/plugins/input/RotaryEncoderInterruptBase.h b/src/plugins/input/RotaryEncoderInterruptBase.h index 110596cce..7a5cef984 100644 --- a/src/plugins/input/RotaryEncoderInterruptBase.h +++ b/src/plugins/input/RotaryEncoderInterruptBase.h @@ -24,9 +24,10 @@ class RotaryEncoderInterruptBase : { public: RotaryEncoderInterruptBase( - const char *name, - uint8_t pinA, uint8_t pinB, uint8_t pinPress, - char eventCw, char eventCcw, char eventPressed, + const char *name); + void init( + uint8_t pinA, uint8_t pinB, uint8_t pinPress, + char eventCw, char eventCcw, char eventPressed, // std::function onIntA, std::function onIntB, std::function onIntPress); void (*onIntA)(), void (*onIntB)(), void (*onIntPress)()); void intPressHandler(); diff --git a/src/plugins/input/RotaryEncoderInterruptImpl1.cpp b/src/plugins/input/RotaryEncoderInterruptImpl1.cpp index ebd8e8309..5c66d2c11 100644 --- a/src/plugins/input/RotaryEncoderInterruptImpl1.cpp +++ b/src/plugins/input/RotaryEncoderInterruptImpl1.cpp @@ -2,17 +2,22 @@ RotaryEncoderInterruptImpl1 *rotaryEncoderInterruptImpl1; -RotaryEncoderInterruptImpl1::RotaryEncoderInterruptImpl1( - uint8_t pinA, uint8_t pinB, uint8_t pinPress, - char eventCw, char eventCcw, char eventPressed) : +RotaryEncoderInterruptImpl1::RotaryEncoderInterruptImpl1() : RotaryEncoderInterruptBase( - "rotEnc1", + "rotEnc1") +{ +} + +void RotaryEncoderInterruptImpl1::init( + uint8_t pinA, uint8_t pinB, uint8_t pinPress, + char eventCw, char eventCcw, char eventPressed) +{ + RotaryEncoderInterruptBase::init( pinA, pinB, pinPress, eventCw, eventCcw, eventPressed, RotaryEncoderInterruptImpl1::handleIntA, RotaryEncoderInterruptImpl1::handleIntB, - RotaryEncoderInterruptImpl1::handleIntPressed) -{ + RotaryEncoderInterruptImpl1::handleIntPressed); } void RotaryEncoderInterruptImpl1::handleIntA() diff --git a/src/plugins/input/RotaryEncoderInterruptImpl1.h b/src/plugins/input/RotaryEncoderInterruptImpl1.h index 17613e8e3..61d9195d8 100644 --- a/src/plugins/input/RotaryEncoderInterruptImpl1.h +++ b/src/plugins/input/RotaryEncoderInterruptImpl1.h @@ -1,11 +1,19 @@ #pragma once #include "RotaryEncoderInterruptBase.h" +/** + * @brief The idea behind this class to have static methods for the event handlers. + * Check attachInterrupt() at RotaryEncoderInteruptBase.cpp + * Technically you can have as many rotary encoders hardver attached + * to your device as you wish, but you always need to have separate event + * handlers, thus you need to have a RotaryEncoderInterrupt implementation. + */ class RotaryEncoderInterruptImpl1 : public RotaryEncoderInterruptBase { public: - RotaryEncoderInterruptImpl1( + RotaryEncoderInterruptImpl1(); + void init( uint8_t pinA, uint8_t pinB, uint8_t pinPress, char eventCw, char eventCcw, char eventPressed); static void handleIntA();