From b4ebd347eb8b3df8483d1ecfb1f5033a987567d5 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Fri, 13 Jul 2018 16:16:44 +0200 Subject: [PATCH] introduce dispatch method to simplify things --- README.md | 8 +++++++- library.json | 2 +- src/Sprocket.cpp | 1 + src/Sprocket.h | 4 ++++ src/examples/mesh/MeshApp.h | 4 ++-- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 63ef55f..f2ca9a0 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,10 @@ Library to build a mesh network of single purpose nodes. ## WTF is a sprocket? -A sprocket is a device that has a single purpose, for example a PIR sensor node that notifies other nodes if there is motion or an LED node that lights up when a message is received. \ No newline at end of file +A sprocket is a device that has a single purpose, for example a PIR sensor node that notifies other nodes if there is motion or an LED node that lights up when a message is received. + +# Useful commands +```sh +# erase flash + esptool --port /dev/ttyUSB0 erase_flash +``` \ No newline at end of file diff --git a/library.json b/library.json index 1ec316d..706a6f7 100644 --- a/library.json +++ b/library.json @@ -1,4 +1,4 @@ -{ +k{ "name": "sprocket-core", "keywords": "esp8266, sprocket, stack", "description": "Core stack for Sprockets", diff --git a/src/Sprocket.cpp b/src/Sprocket.cpp index 2a70fb6..3ec142f 100644 --- a/src/Sprocket.cpp +++ b/src/Sprocket.cpp @@ -22,6 +22,7 @@ Sprocket* Sprocket::activate() { Sprocket* Sprocket::join(Network& net){ Serial.println("join network"); net.init(&scheduler); + net.onReceive(bind(&Sprocket::dispatch,this, _1, _2)); net.connect(); return activate(&scheduler, &net); } diff --git a/src/Sprocket.h b/src/Sprocket.h index 6d485b8..fbd3c34 100644 --- a/src/Sprocket.h +++ b/src/Sprocket.h @@ -6,6 +6,9 @@ #include #include "Network.h" +using namespace std; +using namespace std::placeholders; + struct SprocketConfig { int startupDelay; int serialBaudRate; @@ -25,6 +28,7 @@ class Sprocket { virtual Sprocket* activate(); virtual Sprocket* activate(Scheduler*) { return this; } virtual Sprocket* activate(Scheduler*, Network*) { return this; } + virtual void dispatch(uint32_t from, String &msg) {}; }; #endif \ No newline at end of file diff --git a/src/examples/mesh/MeshApp.h b/src/examples/mesh/MeshApp.h index 15df943..5c7b0cc 100644 --- a/src/examples/mesh/MeshApp.h +++ b/src/examples/mesh/MeshApp.h @@ -15,7 +15,7 @@ class MeshApp : public Sprocket { MeshApp(SprocketConfig cfg) : Sprocket(cfg) {} Sprocket* activate(Scheduler* scheduler, Network* network) { net = static_cast(network); - net->onReceive(bind(&MeshApp::onReceive,this, _1, _2)); + //net->onReceive(bind(&MeshApp::onReceive,this, _1, _2)); // add a task that sends stuff to the mesh someTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this, net)); @@ -28,7 +28,7 @@ class MeshApp : public Sprocket { network->broadcast(msg); } - void onReceive( uint32_t from, String &msg ) { + void dispatch( uint32_t from, String &msg ) { Serial.printf("MeshApp: received from %u msg=%s\n", from, msg.c_str()); // respond in receive callback can cause an endless loop when all nodes run the same firmware //String foo = String("cheerz back to ") + String(from);