Skip to content

Commit

Permalink
try out loop() on inbox
Browse files Browse the repository at this point in the history
  • Loading branch information
balvig committed Sep 3, 2020
1 parent cc048dc commit 61a46ce
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 28 deletions.
8 changes: 3 additions & 5 deletions examples/ble/src/main.cpp
Expand Up @@ -2,7 +2,6 @@
#include <Bricks.Brick.h>
#include <Bricks.Inbox.h>
#include <Bricks.Outbox.h>
#include <Bricks.OtaAction.h>
using namespace Bricks;

// Local
Expand Down Expand Up @@ -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();
}
4 changes: 1 addition & 3 deletions examples/button-sleep/src/main.cpp
Expand Up @@ -2,7 +2,6 @@
#include <Bricks.Brick.h>
#include <Bricks.Constants.h>
#include <Bricks.Inbox.h>
#include <Bricks.OtaAction.h>
using namespace Bricks;

void setup() {
Expand All @@ -15,9 +14,8 @@ void setup() {

// Configure inbox
gInbox.init("Button");
gInbox.actions[0] = new OtaAction();
}

void loop() {
ArduinoOTA.handle();
gInbox.loop();
}
6 changes: 2 additions & 4 deletions examples/buzzer/src/main.cpp
@@ -1,7 +1,6 @@
// Bricks
#include <Bricks.Brick.h>
#include <Bricks.Inbox.h>
#include <Bricks.OtaAction.h>
using namespace Bricks;

// Local
Expand All @@ -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();
}
6 changes: 2 additions & 4 deletions examples/ir/src/main.cpp
@@ -1,7 +1,6 @@
// Bricks
#include <Bricks.Brick.h>
#include <Bricks.Inbox.h>
#include <Bricks.OtaAction.h>
using namespace Bricks;

// Local
Expand Down Expand Up @@ -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();
Expand All @@ -57,5 +55,5 @@ void loop() {
Log.notice(resultToSourceCode(&results).c_str());
irrecv.resume();
}
ArduinoOTA.handle();
gInbox.loop();
}
6 changes: 2 additions & 4 deletions examples/led-matrix/src/main.cpp
@@ -1,7 +1,6 @@
// Bricks
#include <Bricks.Brick.h>
#include <Bricks.Inbox.h>
#include <Bricks.OtaAction.h>
using namespace Bricks;

// Local
Expand Down Expand Up @@ -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();
}
8 changes: 3 additions & 5 deletions examples/led/src/main.cpp
@@ -1,7 +1,6 @@
// Bricks
#include <Bricks.Brick.h>
#include <Bricks.Inbox.h>
#include <Bricks.OtaAction.h>
using namespace Bricks;

const int ledPin = LED_BUILTIN;
Expand All @@ -15,7 +14,7 @@ void setPattern(const uint8_t *macAddr, const Message message) {
}
}

//Main
// Main
void setup() {
// Logging
Serial.begin(115200);
Expand All @@ -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();
}
4 changes: 1 addition & 3 deletions examples/pir/src/main.cpp
@@ -1,7 +1,6 @@
// Bricks
#include <Bricks.Brick.h>
#include <Bricks.Inbox.h>
#include <Bricks.OtaAction.h>
using namespace Bricks;

// Local
Expand Down Expand Up @@ -35,7 +34,6 @@ void setup() {

// Configure inbox
gInbox.init("PIR");
gInbox.actions[0] = new OtaAction();

// Configure PIR
pinMode(PIR, INPUT);
Expand All @@ -47,5 +45,5 @@ void loop() {
if(timer.onRestart()) {
notifyChanges();
}
ArduinoOTA.handle();
gInbox.loop();
}
4 changes: 4 additions & 0 deletions src/Bricks.Action.cpp
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/Bricks.Action.h
Expand Up @@ -12,6 +12,7 @@ namespace Bricks {
public:
Action(const char *key = "", std::function<void(const uint8_t *macAddr, const Message message)> customCallback = nullptr);
bool respondsTo(const char* compareKey);
virtual void loop();
virtual void callback(const uint8_t *macAddr, const Message message);
const char *key;
private:
Expand Down
7 changes: 7 additions & 0 deletions src/Bricks.Inbox.cpp
Expand Up @@ -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();
Expand All @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Bricks.Inbox.h
Expand Up @@ -13,6 +13,7 @@
#include <Bricks.AckAction.h>
#include <Bricks.BatteryAction.h>
#include <Bricks.ListAction.h>
#include <Bricks.OtaAction.h>
#include <Bricks.PongAction.h>
#include <Bricks.SleepAction.h>
#include <Bricks.StoreGatewayAction.h>
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Bricks.OtaAction.cpp
Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions src/Bricks.OtaAction.h
Expand Up @@ -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();
Expand Down

0 comments on commit 61a46ce

Please sign in to comment.