From f5004a66a1a83e784c1ec186142d4e6e6733d610 Mon Sep 17 00:00:00 2001 From: Balazs Kelemen <10376327+prampec@users.noreply.github.com> Date: Tue, 11 Jan 2022 16:02:55 +0100 Subject: [PATCH] Introduce InputBroker --- src/plugins/CannedMessagePlugin.cpp | 14 ++++++++++---- src/plugins/CannedMessagePlugin.h | 3 +-- src/plugins/Plugins.cpp | 4 +++- src/plugins/input/HardwareInput.h | 2 ++ src/plugins/input/InputBroker.cpp | 17 +++++++++++++++++ src/plugins/input/InputBroker.h | 19 +++++++++++++++++++ .../input/RotaryEncoderInterruptBase.cpp | 17 +++++++++++------ .../input/RotaryEncoderInterruptBase.h | 6 +++--- .../input/RotaryEncoderInterruptImpl1.cpp | 2 ++ 9 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 src/plugins/input/InputBroker.cpp create mode 100644 src/plugins/input/InputBroker.h diff --git a/src/plugins/CannedMessagePlugin.cpp b/src/plugins/CannedMessagePlugin.cpp index 43a411985..4591761e5 100644 --- a/src/plugins/CannedMessagePlugin.cpp +++ b/src/plugins/CannedMessagePlugin.cpp @@ -1,22 +1,28 @@ #include "configuration.h" #include "CannedMessagePlugin.h" #include "MeshService.h" -#include "main.h" +#include "input/InputBroker.h" #include CannedMessagePlugin *cannedMessagePlugin; -CannedMessagePlugin::CannedMessagePlugin( - Observable *input) +CannedMessagePlugin::CannedMessagePlugin() : SinglePortPlugin("canned", PortNum_TEXT_MESSAGE_APP), concurrency::OSThread("CannedMessagePlugin") { - this->inputObserver.observe(input); + if (true) // if module enabled + { + this->inputObserver.observe(inputBroker); + } } int CannedMessagePlugin::handleInputEvent(const InputEvent *event) { + if (false) // test event->origin + { + return 0; + } bool validEvent = false; if (event->inputEvent == INPUT_EVENT_UP) { diff --git a/src/plugins/CannedMessagePlugin.h b/src/plugins/CannedMessagePlugin.h index 588716bf8..93bdc2cce 100644 --- a/src/plugins/CannedMessagePlugin.h +++ b/src/plugins/CannedMessagePlugin.h @@ -40,8 +40,7 @@ class CannedMessagePlugin : CallbackObserver( this, &CannedMessagePlugin::handleInputEvent); public: - CannedMessagePlugin( - Observable *input); + CannedMessagePlugin(); String getCurrentMessage(); String getPrevMessage(); String getNextMessage(); diff --git a/src/plugins/Plugins.cpp b/src/plugins/Plugins.cpp index cd6df21e9..09416a724 100644 --- a/src/plugins/Plugins.cpp +++ b/src/plugins/Plugins.cpp @@ -9,6 +9,7 @@ #include "plugins/RoutingPlugin.h" #include "plugins/AdminPlugin.h" #include "plugins/CannedMessagePlugin.h" +#include "plugins/input/InputBroker.h" #include "plugins/input/RotaryEncoderInterruptImpl1.h" #ifndef NO_ESP32 #include "plugins/esp32/SerialPlugin.h" @@ -22,6 +23,7 @@ */ void setupPlugins() { + inputBroker = new InputBroker(); adminPlugin = new AdminPlugin(); nodeInfoPlugin = new NodeInfoPlugin(); positionPlugin = new PositionPlugin(); @@ -37,7 +39,7 @@ void setupPlugins() rotaryEncoderInterruptImpl1->init( 22, 23, 21, INPUT_EVENT_UP, INPUT_EVENT_DOWN, INPUT_EVENT_SELECT); - cannedMessagePlugin = new CannedMessagePlugin(rotaryEncoderInterruptImpl1); + cannedMessagePlugin = new CannedMessagePlugin(); #ifndef NO_ESP32 // Only run on an esp32 based device. diff --git a/src/plugins/input/HardwareInput.h b/src/plugins/input/HardwareInput.h index ebc04447c..22ab84615 100644 --- a/src/plugins/input/HardwareInput.h +++ b/src/plugins/input/HardwareInput.h @@ -1,5 +1,6 @@ #pragma once +#define INPUT_EVENT_NULL 0 #define INPUT_EVENT_UP 17 #define INPUT_EVENT_DOWN 18 #define INPUT_EVENT_LEFT 19 @@ -9,5 +10,6 @@ #define INPUT_EVENT_CANCEL 24 typedef struct _InputEvent { + const char* origin; char inputEvent; } InputEvent; \ No newline at end of file diff --git a/src/plugins/input/InputBroker.cpp b/src/plugins/input/InputBroker.cpp new file mode 100644 index 000000000..165f16f75 --- /dev/null +++ b/src/plugins/input/InputBroker.cpp @@ -0,0 +1,17 @@ +#include "InputBroker.h" + +InputBroker *inputBroker; + +InputBroker::InputBroker() +{ +}; + +void InputBroker::registerOrigin(Observable *origin) +{ + this->inputEventObserver.observe(origin); +} + +int InputBroker::handleInputEvent(const InputEvent *event) +{ + this->notifyObservers(event); +} \ No newline at end of file diff --git a/src/plugins/input/InputBroker.h b/src/plugins/input/InputBroker.h new file mode 100644 index 000000000..2493cfedf --- /dev/null +++ b/src/plugins/input/InputBroker.h @@ -0,0 +1,19 @@ +#pragma once +#include "Observer.h" +#include "HardwareInput.h" + +class InputBroker : + public Observable +{ + CallbackObserver inputEventObserver = + CallbackObserver(this, &InputBroker::handleInputEvent); + + public: + InputBroker(); + void registerOrigin(Observable *origin); + + protected: + int handleInputEvent(const InputEvent *event); +}; + +extern InputBroker *inputBroker; \ No newline at end of file diff --git a/src/plugins/input/RotaryEncoderInterruptBase.cpp b/src/plugins/input/RotaryEncoderInterruptBase.cpp index 6d775b761..31dfe0142 100644 --- a/src/plugins/input/RotaryEncoderInterruptBase.cpp +++ b/src/plugins/input/RotaryEncoderInterruptBase.cpp @@ -10,7 +10,7 @@ RotaryEncoderInterruptBase::RotaryEncoderInterruptBase( const char *name) : concurrency::OSThread(name) { - + this->_originName = name; } void RotaryEncoderInterruptBase::init( @@ -43,26 +43,31 @@ void RotaryEncoderInterruptBase::init( int32_t RotaryEncoderInterruptBase::runOnce() { + InputEvent e; + e.inputEvent = INPUT_EVENT_NULL; + e.origin = this->_originName; + if (this->action == ROTARY_ACTION_PRESSED) { - InputEvent e; + DEBUG_MSG("Rotary event Press\n"); e.inputEvent = this->_eventPressed; - this->notifyObservers(&e); } else if (this->action == ROTARY_ACTION_CW) { DEBUG_MSG("Rotary event CW\n"); - InputEvent e; e.inputEvent = this->_eventCw; - this->notifyObservers(&e); } else if (this->action == ROTARY_ACTION_CCW) { DEBUG_MSG("Rotary event CW\n"); - InputEvent e; e.inputEvent = this->_eventCcw; + } + + if (e.inputEvent != INPUT_EVENT_NULL) + { this->notifyObservers(&e); } + this->action = ROTARY_ACTION_NONE; return 30000; diff --git a/src/plugins/input/RotaryEncoderInterruptBase.h b/src/plugins/input/RotaryEncoderInterruptBase.h index 7a5cef984..1f07f58b4 100644 --- a/src/plugins/input/RotaryEncoderInterruptBase.h +++ b/src/plugins/input/RotaryEncoderInterruptBase.h @@ -1,7 +1,6 @@ #pragma once -//#include -//#include "Observer.h" -#include "SinglePortPlugin.h" + +#include "SinglePortPlugin.h" // TODO: what header file to include? #include "HardwareInput.h" enum RotaryEncoderInterruptBaseStateType @@ -48,4 +47,5 @@ class RotaryEncoderInterruptBase : char _eventCw; char _eventCcw; char _eventPressed; + const char *_originName; }; \ No newline at end of file diff --git a/src/plugins/input/RotaryEncoderInterruptImpl1.cpp b/src/plugins/input/RotaryEncoderInterruptImpl1.cpp index 5c66d2c11..638868c3a 100644 --- a/src/plugins/input/RotaryEncoderInterruptImpl1.cpp +++ b/src/plugins/input/RotaryEncoderInterruptImpl1.cpp @@ -1,4 +1,5 @@ #include "RotaryEncoderInterruptImpl1.h" +#include "InputBroker.h" RotaryEncoderInterruptImpl1 *rotaryEncoderInterruptImpl1; @@ -18,6 +19,7 @@ void RotaryEncoderInterruptImpl1::init( RotaryEncoderInterruptImpl1::handleIntA, RotaryEncoderInterruptImpl1::handleIntB, RotaryEncoderInterruptImpl1::handleIntPressed); + inputBroker->registerOrigin(this); } void RotaryEncoderInterruptImpl1::handleIntA()