From 61a46cece6f5d351e0c9074cf73edad458b37da8 Mon Sep 17 00:00:00 2001 From: Jens Balvig Date: Thu, 3 Sep 2020 16:54:31 +0900 Subject: [PATCH] try out loop() on inbox --- examples/ble/src/main.cpp | 8 +++----- examples/button-sleep/src/main.cpp | 4 +--- examples/buzzer/src/main.cpp | 6 ++---- examples/ir/src/main.cpp | 6 ++---- examples/led-matrix/src/main.cpp | 6 ++---- examples/led/src/main.cpp | 8 +++----- examples/pir/src/main.cpp | 4 +--- src/Bricks.Action.cpp | 4 ++++ src/Bricks.Action.h | 1 + src/Bricks.Inbox.cpp | 7 +++++++ src/Bricks.Inbox.h | 2 ++ src/Bricks.OtaAction.cpp | 4 ++++ src/Bricks.OtaAction.h | 1 + 13 files changed, 33 insertions(+), 28 deletions(-) diff --git a/examples/ble/src/main.cpp b/examples/ble/src/main.cpp index ba62d67..7cdee22 100644 --- a/examples/ble/src/main.cpp +++ b/examples/ble/src/main.cpp @@ -2,7 +2,6 @@ #include #include #include -#include using namespace Bricks; // Local @@ -56,11 +55,10 @@ void setup() { // Enable receiving messages gInbox.init("BLE Scanner"); - gInbox.actions[0] = new OtaAction(); - gInbox.actions[1] = new Action("scan", &passiveScan); - gInbox.actions[2] = new Action("activeScan", &activeScan); + gInbox.actions[0] = new Action("scan", &passiveScan); + gInbox.actions[1] = new Action("activeScan", &activeScan); } void loop() { - ArduinoOTA.handle(); + gInbox.loop(); } diff --git a/examples/button-sleep/src/main.cpp b/examples/button-sleep/src/main.cpp index 43af254..b21f720 100644 --- a/examples/button-sleep/src/main.cpp +++ b/examples/button-sleep/src/main.cpp @@ -2,7 +2,6 @@ #include #include #include -#include using namespace Bricks; void setup() { @@ -15,9 +14,8 @@ void setup() { // Configure inbox gInbox.init("Button"); - gInbox.actions[0] = new OtaAction(); } void loop() { - ArduinoOTA.handle(); + gInbox.loop(); } diff --git a/examples/buzzer/src/main.cpp b/examples/buzzer/src/main.cpp index 45cf12b..c8dc693 100644 --- a/examples/buzzer/src/main.cpp +++ b/examples/buzzer/src/main.cpp @@ -1,7 +1,6 @@ // Bricks #include #include -#include using namespace Bricks; // Local @@ -26,13 +25,12 @@ void setup() { // Configure inbox gInbox.init("Buzzer"); - gInbox.actions[0] = new OtaAction(); - gInbox.actions[1] = new Action("tone", &playTone); + gInbox.actions[0] = new Action("tone", &playTone); // Configure buzzer pinMode(BUZZER, OUTPUT); } void loop() { - ArduinoOTA.handle(); + gInbox.loop(); } diff --git a/examples/ir/src/main.cpp b/examples/ir/src/main.cpp index f885539..a8baa3a 100644 --- a/examples/ir/src/main.cpp +++ b/examples/ir/src/main.cpp @@ -1,7 +1,6 @@ // Bricks #include #include -#include using namespace Bricks; // Local @@ -38,8 +37,7 @@ void setup() { gBrick.init(); gInbox.init("IR Controller"); - gInbox.actions[0] = new OtaAction(); - gInbox.actions[1] = new Action("sendCode", &sendCode); + gInbox.actions[0] = new Action("sendCode", &sendCode); // Start the IR receiver / sender irrecv.enableIRIn(); @@ -57,5 +55,5 @@ void loop() { Log.notice(resultToSourceCode(&results).c_str()); irrecv.resume(); } - ArduinoOTA.handle(); + gInbox.loop(); } diff --git a/examples/led-matrix/src/main.cpp b/examples/led-matrix/src/main.cpp index 93deb5f..c5248c0 100644 --- a/examples/led-matrix/src/main.cpp +++ b/examples/led-matrix/src/main.cpp @@ -1,7 +1,6 @@ // Bricks #include #include -#include using namespace Bricks; // Local @@ -30,10 +29,9 @@ void setup() { // Configure inbox gInbox.init("LED 8x8 Matrix"); - gInbox.actions[0] = new OtaAction(); - gInbox.actions[1] = new Action("setValue", &setValue); + gInbox.actions[0] = new Action("setValue", &setValue); } void loop() { - ArduinoOTA.handle(); + gInbox.loop(); } diff --git a/examples/led/src/main.cpp b/examples/led/src/main.cpp index 586447a..fe2f003 100644 --- a/examples/led/src/main.cpp +++ b/examples/led/src/main.cpp @@ -1,7 +1,6 @@ // Bricks #include #include -#include using namespace Bricks; const int ledPin = LED_BUILTIN; @@ -15,7 +14,7 @@ void setPattern(const uint8_t *macAddr, const Message message) { } } -//Main +// Main void setup() { // Logging Serial.begin(115200); @@ -30,10 +29,9 @@ void setup() { // Enable receiving messages gInbox.init("LED"); - gInbox.actions[0] = new OtaAction(); - gInbox.actions[1] = new Action("setPattern", &setPattern); + gInbox.actions[0] = new Action("setPattern", &setPattern); } void loop() { - ArduinoOTA.handle(); + gInbox.loop(); } diff --git a/examples/pir/src/main.cpp b/examples/pir/src/main.cpp index 8e5ef3b..02052a9 100644 --- a/examples/pir/src/main.cpp +++ b/examples/pir/src/main.cpp @@ -1,7 +1,6 @@ // Bricks #include #include -#include using namespace Bricks; // Local @@ -35,7 +34,6 @@ void setup() { // Configure inbox gInbox.init("PIR"); - gInbox.actions[0] = new OtaAction(); // Configure PIR pinMode(PIR, INPUT); @@ -47,5 +45,5 @@ void loop() { if(timer.onRestart()) { notifyChanges(); } - ArduinoOTA.handle(); + gInbox.loop(); } diff --git a/src/Bricks.Action.cpp b/src/Bricks.Action.cpp index ca18ecc..10ce83e 100644 --- a/src/Bricks.Action.cpp +++ b/src/Bricks.Action.cpp @@ -6,6 +6,10 @@ namespace Bricks { this->customCallback = customCallback; } + void Action::loop() { + // Do nothing + } + void Action::callback(const uint8_t *macAddr, const Message message) { customCallback(macAddr, message); } diff --git a/src/Bricks.Action.h b/src/Bricks.Action.h index 58d0ee6..0f27c6f 100644 --- a/src/Bricks.Action.h +++ b/src/Bricks.Action.h @@ -12,6 +12,7 @@ namespace Bricks { public: Action(const char *key = "", std::function customCallback = nullptr); bool respondsTo(const char* compareKey); + virtual void loop(); virtual void callback(const uint8_t *macAddr, const Message message); const char *key; private: diff --git a/src/Bricks.Inbox.cpp b/src/Bricks.Inbox.cpp index 3eb849c..64f465c 100644 --- a/src/Bricks.Inbox.cpp +++ b/src/Bricks.Inbox.cpp @@ -2,6 +2,7 @@ namespace Bricks { void Inbox::init(const char *name) { + actions[3] = new OtaAction(); actions[4] = new ListAction(); actions[5] = new PongAction(name); actions[6] = new StoreGatewayAction(); @@ -26,6 +27,12 @@ namespace Bricks { gInbox.process(macAddr, message); } + void Inbox::loop() { + for(int i = 0; i < MAX_ACTIONS; i++) { + actions[i]->loop(); + } + } + void Inbox::process(const uint8_t *macAddr, const Message message) { for(int i = 0; i < MAX_ACTIONS; i++) { if(actions[i]->respondsTo(message.key)) { diff --git a/src/Bricks.Inbox.h b/src/Bricks.Inbox.h index 396d7d9..2319ad2 100644 --- a/src/Bricks.Inbox.h +++ b/src/Bricks.Inbox.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -25,6 +26,7 @@ namespace Bricks { Inbox() {} void init(const char *name); void initBase(); + void loop(); void process(const uint8_t *macAddr, const Message message); String listActions(); // The (supposedly) EVIL STRING? Action *actions[MAX_ACTIONS] = { new Action(), new Action(), new Action(), new Action(), new Action(), new Action(), new Action(), new Action(), new Action(), new Action() }; // what the diff --git a/src/Bricks.OtaAction.cpp b/src/Bricks.OtaAction.cpp index 80faa6c..2d8f8a2 100644 --- a/src/Bricks.OtaAction.cpp +++ b/src/Bricks.OtaAction.cpp @@ -4,6 +4,10 @@ namespace Bricks { OtaAction::OtaAction() : Action("setOta") { } + void OtaAction::loop() { + ArduinoOTA.handle(); + } + void OtaAction::callback(const uint8_t *macAddr, const Message message) { initOta(); gOutbox.send(macAddr, "ota", WiFi.softAPIP().toString().c_str()); diff --git a/src/Bricks.OtaAction.h b/src/Bricks.OtaAction.h index 58de410..e8d1bf6 100644 --- a/src/Bricks.OtaAction.h +++ b/src/Bricks.OtaAction.h @@ -10,6 +10,7 @@ namespace Bricks { class OtaAction : public Action { public: OtaAction(); + void loop(); void callback(const uint8_t *macAddr, const Message message); private: void initOta();