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:
paths:
- firmware
stages:
- build
before_script:
- "pip install -U platformio"

View File

@@ -8,54 +8,65 @@
using namespace std;
using namespace std::placeholders;
#define STATION_MODE 1
#define WIFI_CHANNEL 11
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
#define STATION_SSID "Th1ngs4p"
#define STATION_PWD "th3r31sn0sp00n"
#define HOSTNAME "MeshNode"
class MeshNet : public Network {
public:
painlessMesh mesh;
Network* init(){
Serial.println("init mesh");
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION);
mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, 11 );
//mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2));
mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1));
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
painlessMesh mesh;
return this;
}
Network* connect(){
Serial.println("connect station");
mesh.stationManual("tErAx1d", "ramalamadingdong");
mesh.setHostname("MeshNode");
return this;
}
void broadcast(String msg){
mesh.sendBroadcast(msg);
}
void update(){
mesh.update();
}
void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}
Network* init(){
void newConnectionCallback(uint32_t nodeId) {
id = nodeId;
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}
Serial.println("init mesh");
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION);
mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, WIFI_CHANNEL );
//mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2));
mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1));
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
if(STATION_MODE){
Serial.println("connect station");
mesh.stationManual(STATION_SSID, STATION_PWD);
mesh.setHostname(HOSTNAME);
}
void changedConnectionCallback() {
Serial.printf("Changed connections %s\n",mesh.subConnectionJson().c_str());
}
return this;
}
Network* connect(){
return this;
}
void broadcast(String msg){
mesh.sendBroadcast(msg);
}
void update(){
mesh.update();
}
void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}
void nodeTimeAdjustedCallback(int32_t offset) {
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}
void newConnectionCallback(uint32_t nodeId) {
id = nodeId;
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}
void changedConnectionCallback() {
Serial.printf("Changed connections %s\n",mesh.subConnectionJson().c_str());
}
void nodeTimeAdjustedCallback(int32_t offset) {
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}
};
#endif

View File

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

View File

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

View File

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

View File

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