refactorz

This commit is contained in:
2018-06-12 01:25:59 +02:00
parent 0b941fee60
commit 8b04d8642a
6 changed files with 72 additions and 60 deletions

View File

@@ -1,10 +1,10 @@
stages:
- build
cache: cache:
paths: paths:
- firmware - firmware
stages:
- build
before_script: before_script:
- "pip install -U platformio" - "pip install -U platformio"

View File

@@ -8,30 +8,41 @@
using namespace std; using namespace std;
using namespace std::placeholders; using namespace std::placeholders;
#define STATION_MODE 1
#define WIFI_CHANNEL 11
#define MESH_PREFIX "whateverYouLike" #define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky" #define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555 #define MESH_PORT 5555
#define STATION_SSID "Th1ngs4p"
#define STATION_PWD "th3r31sn0sp00n"
#define HOSTNAME "MeshNode"
class MeshNet : public Network { class MeshNet : public Network {
public: public:
painlessMesh mesh; painlessMesh mesh;
Network* init(){ Network* init(){
Serial.println("init mesh"); Serial.println("init mesh");
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on //mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION); mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION);
mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, 11 ); mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, WIFI_CHANNEL );
//mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2)); //mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2));
mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1)); mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1));
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this)); mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1)); mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
if(STATION_MODE){
Serial.println("connect station");
mesh.stationManual(STATION_SSID, STATION_PWD);
mesh.setHostname(HOSTNAME);
}
return this; return this;
} }
Network* connect(){ Network* connect(){
Serial.println("connect station");
mesh.stationManual("tErAx1d", "ramalamadingdong");
mesh.setHostname("MeshNode");
return this; return this;
} }
void broadcast(String msg){ void broadcast(String msg){

View File

@@ -15,11 +15,11 @@ Sprocket* Sprocket::join(Network& net, App& app){
} }
Sprocket* Sprocket::join(Network& net){ Sprocket* Sprocket::join(Network& net){
network = net;
Serial.println("join network"); Serial.println("join network");
net.setScheduler(&scheduler); net.setScheduler(&scheduler);
net.init(); net.init();
net.connect(); net.connect();
activate(&scheduler, &net);
return this; return this;
} }
Sprocket* Sprocket::use(AppStack* stk){ Sprocket* Sprocket::use(AppStack* stk){

View File

@@ -15,18 +15,22 @@ struct SprocketConfig {
class Sprocket { class Sprocket {
private: private:
AppStack* stack; AppStack* stack; // REMOVE
Scheduler scheduler; Scheduler scheduler;
Network network; Network network;
public: public:
Sprocket(); Sprocket();
Sprocket* init(SprocketConfig); Sprocket* init(SprocketConfig);
Sprocket* join(Network&); Sprocket* join(Network&);
Sprocket* join(Network&, App&); Sprocket* join(Network&, App&); // REMOVE
Sprocket* use(AppStack*); Sprocket* use(AppStack*); // REMOVE
Sprocket* addTask(Task&); Sprocket* addTask(Task&); // REMOVE
Sprocket* app(App&); Sprocket* app(App&); // REMOVE
void loop(); void loop();
virtual Sprocket* activate() {
activate(&scheduler, &network);
}
virtual Sprocket* activate(Scheduler* scheduler, Network* network) {}
}; };
#endif #endif

View File

@@ -20,7 +20,7 @@ using namespace std::placeholders;
WiFiClient wifiClient; WiFiClient wifiClient;
class MqttMeshBridge : public App { class MqttMeshBridge : public Sprocket {
public: public:
MeshNet* net; MeshNet* net;
PubSubClient* client; PubSubClient* client;
@@ -30,25 +30,23 @@ class MqttMeshBridge : public App {
MqttMeshBridge() { MqttMeshBridge() {
} }
void activate(Scheduler* scheduler, Network* network) { Sprocket* activate(Scheduler* scheduler, Network* network) {
net = static_cast<MeshNet*>(network); net = static_cast<MeshNet*>(network);
net->mesh.onReceive(bind(&MqttMeshBridge::receivedCallback,this, _1, _2)); net->mesh.onReceive(bind(&MqttMeshBridge::receivedCallback,this, _1, _2));
client = new PubSubClient(MQTT_BROKER_HOST, MQTT_BROKER_PORT, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient); client = new PubSubClient(MQTT_BROKER_HOST, MQTT_BROKER_PORT, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient);
// add a task that sends stuff to the mesh
enableConnectTask(scheduler); enableConnectTask(scheduler);
enableProcessTask(scheduler); enableProcessTask(scheduler);
return this;
} }
void enableConnectTask(Scheduler* scheduler) { void enableConnectTask(Scheduler* scheduler) {
connectTask.set(TASK_SECOND * 5, TASK_FOREVER, connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::connect, this));
bind(&MqttMeshBridge::connect, this));
scheduler->addTask(connectTask); scheduler->addTask(connectTask);
connectTask.enable(); connectTask.enable();
} }
void enableProcessTask(Scheduler* scheduler) { void enableProcessTask(Scheduler* scheduler) {
processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::process, this));
bind(&MqttMeshBridge::process, this));
scheduler->addTask(processTask); scheduler->addTask(processTask);
processTask.enable(); processTask.enable();
} }

View File

@@ -19,11 +19,10 @@ MqttMeshBridge app;
void setup() { void setup() {
delay(STARTUP_DELAY); delay(STARTUP_DELAY);
sprocket.init(config); sprocket.init(config);
sprocket.join(net, app); sprocket.join(net);
} }
void loop() { void loop() {
net.update();
sprocket.loop(); sprocket.loop();
yield(); yield();
} }