Skip to content

Commit

Permalink
enabledChanged/isEnabled and connectedChanged/isConnected + setOn/Off
Browse files Browse the repository at this point in the history
pio.ini: enable artnet and ddp

Module.h
- rename enabled to isEnabled
- refactor event functions enabledChanged and connectedChanged

SysModModules
- rename Modules class to SysModModules
- add isConnected
- mdlEnabled: check of value.size same as nrOfModules otherwise recreate
- rename connected() to connectedChanged()

SysModWeb
- connectedChanged: only addhandlers if isConnected (tbd: disable???)

UserModArtNet and DDP
- disabled by default (in constructor)
- connectedChanged commented! (as main checks if connected)

UserModE131
- disabled by default (in constructor)
- connectedChanged and enabledChanged call setOn and setOff (tbd setoff!)

 UserModHA (not included in built yet)
- disabled by default (in constructor)
- connectedChanged; tbd if disconnected, and move to setOn and setOff (see E131)
  • Loading branch information
ewowi committed Aug 9, 2023
1 parent 20b2f23 commit 31e3f5b
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 101 deletions.
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ lib_deps =
[appmod_leds]
build_flags =
-D APPMOD_LEDS
; -D USERMOD_ARTNET
; -D USERMOD_DDP
-D USERMOD_ARTNET
-D USERMOD_DDP
lib_deps =
https://github.com/FastLED/FastLED.git

Expand Down
10 changes: 5 additions & 5 deletions src/App/AppModLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ class AppModLeds:public Module {
web->addResponse(var["id"], "comment", "Depends on how much leds fastled has configured");
});

ui->initNumber(parentVar, "dataPin", DATA_PIN, false, [](JsonObject var) { //uiFun
web->addResponseV(var["id"], "comment", "Not implemented yet (fixed to %d)", DATA_PIN);
}, [](JsonObject var) { //chFun
print->print("Set data pin to %d\n", var["value"].as<int>());
});
// ui->initNumber(parentVar, "dataPin", DATA_PIN, false, [](JsonObject var) { //uiFun
// web->addResponseV(var["id"], "comment", "Not implemented yet (fixed to %d)", DATA_PIN);
// }, [](JsonObject var) { //chFun
// print->print("Set data pin to %d\n", var["value"].as<int>());
// });

effects.push_back(new RainbowEffect);
effects.push_back(new RainbowWithGlitterEffect);
Expand Down
8 changes: 4 additions & 4 deletions src/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ class Module {
public:
const char * name;
bool success;
bool enabled;
bool isEnabled;
unsigned long secondMillis = 0; // Feels like it should be private, bit doesn't compile if set as such

JsonObject parentVar;

Module(const char * name) {
this->name = name;
success = true;
enabled = true;
isEnabled = true;
// Serial.printf("Constructor %s %s\n", __PRETTY_FUNCTION__, name);
}

virtual void setup() {}

virtual void loop() {}

virtual void connected() {}
virtual void enabledChanged(bool tf) {}
virtual void connectedChanged() {}
virtual void enabledChanged() {}

virtual void testManager() {}
virtual void performanceManager() {}
Expand Down
47 changes: 27 additions & 20 deletions src/Sys/SysModModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
#include "SysModUI.h"
#include "SysModWeb.h"

bool Modules::newConnection = false;
std::vector<Module *> Modules::modules;
bool SysModModules::newConnection = false;
bool SysModModules::isConnected = false;

Modules::Modules() :Module("Modules") {
std::vector<Module *> SysModModules::modules;

SysModModules::SysModModules() :Module("Modules") {
print->print("%s %s\n", __PRETTY_FUNCTION__, name);

print->print("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
};

void Modules::setup() {
void SysModModules::setup() {
for (Module *module:modules) {
module->setup();
}
Expand All @@ -34,11 +36,11 @@ void Modules::setup() {
// web->addResponse(var["id"], "label", "Files");
web->addResponse(var["id"], "comment", "List of modules");
JsonArray rows = web->addResponseA(var["id"], "table");
for (Module *module:Modules::modules) {
for (Module *module:SysModModules::modules) {
JsonArray row = rows.createNestedArray();
row.add(module->name); //create a copy!
row.add(module->success);
row.add(module->enabled);
row.add(module->isEnabled);
}
});
ui->initText(tableVar, "mdlName", nullptr, true, [](JsonObject var) { //uiFun
Expand All @@ -52,30 +54,34 @@ void Modules::setup() {
web->addResponse(var["id"], "label", "Enabled");
}, [](JsonObject var) { //chFun
print->printJson("mdlEnabled.chFun", var);
//if value not array, create and initialize
uint8_t rowNr = 0;
if (!var["value"].is<JsonArray>()) {

//if value not array, create array
if (!var["value"].is<JsonArray>())
var.createNestedArray("value");

//if value array not same size as nr of modules
if (var["value"].size() != modules.size()) {
for (Module *module: modules) {
var["value"][rowNr] = module->enabled;
var["value"][rowNr] = module->isEnabled;
rowNr++;
}
} else { //read array and set module enabled
for (bool enabled:var["value"].as<JsonArray>()) {
if (modules[rowNr]->enabled != enabled) {
print->print(" mdlEnabled.chFun %d %s: %d->%d\n", rowNr, modules[rowNr]->name, modules[rowNr]->enabled, enabled);
modules[rowNr]->enabled = enabled;
modules[rowNr]->enabledChanged(enabled);
for (bool isEnabled:var["value"].as<JsonArray>()) {
if (modules[rowNr]->isEnabled != isEnabled) {
print->print(" mdlEnabled.chFun %d %s: %d->%d\n", rowNr, modules[rowNr]->name, modules[rowNr]->isEnabled, isEnabled);
modules[rowNr]->isEnabled = isEnabled;
modules[rowNr]->enabledChanged();
}
rowNr++;
}
}
});
}

void Modules::loop() {
void SysModModules::loop() {
for (Module *module:modules) {
if (module->enabled && module->success) {
if (module->isEnabled && module->success) {
module->loop();
// module->testManager();
// module->performanceManager();
Expand All @@ -84,17 +90,18 @@ void Modules::loop() {
}
}
if (newConnection) {
connected();
newConnection = false;
isConnected = true;
connectedChanged();
}
}

void Modules::add(Module* module) {
void SysModModules::add(Module* module) {
modules.push_back(module);
}

void Modules::connected() {
void SysModModules::connectedChanged() {
for (Module *module:modules) {
module->connected();
module->connectedChanged();
}
}
9 changes: 5 additions & 4 deletions src/Sys/SysModModules.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@

#include <vector>

class Modules:public Module {
class SysModModules:public Module {
public:
static bool newConnection; //need to be static otherwise crash
static bool isConnected;

Modules();
SysModModules();

void setup();

void loop();

void add(Module* module);

void connected();
void connectedChanged();

private:
static std::vector<Module *> modules;
};

static Modules *mdls;
static SysModModules *mdls;
4 changes: 2 additions & 2 deletions src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void SysModNetwork::handleConnection() {

interfacesInited = true;

Modules::newConnection = true; // send all modules connect notification
SysModModules::newConnection = true; // send all modules connect notification

// shut down AP
if (apActive) { //apBehavior != AP_BEHAVIOR_ALWAYS
Expand Down Expand Up @@ -131,7 +131,7 @@ void SysModNetwork::initAP() {
{
mdl->setValueP("nwstatus", "AP %s / %s @ %s", apSSID, apPass, WiFi.softAPIP().toString().c_str());

Modules::newConnection = true; // send all modules connect notification
SysModModules::newConnection = true; // send all modules connect notification

dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", WiFi.softAPIP());
Expand Down
18 changes: 11 additions & 7 deletions src/Sys/SysModWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "SysModUI.h"
#include "SysModPrint.h"
#include "SysModFiles.h"
#include "SysModModules.h"

#include "AsyncJson.h"

Expand Down Expand Up @@ -112,15 +113,18 @@ void SysModWeb::loop() {
}
}

void SysModWeb::connected() {
ws->onEvent(wsEvent);
// ws->onEvent(wsEvent2);
server->addHandler(ws);
void SysModWeb::connectedChanged() {
if (SysModModules::isConnected) {
ws->onEvent(wsEvent);
// ws->onEvent(wsEvent2);
server->addHandler(ws);

server->begin();
server->begin();

// print->print("%s server (re)started\n", name); //causes crash for some reason...
print->print("server (re)started\n"); //and this not causes crash ??? whats with name?
// print->print("%s server (re)started\n", name); //causes crash for some reason...
print->print("server (re)started\n"); //and this not causes crash ??? whats with name?
}
//else remove handlers...
}

//WebSocket connection to 'ws://192.168.8.152/ws' failed: The operation couldn’t be completed. Protocol error
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModWeb.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SysModWeb:public Module {

void loop();

void connected();
void connectedChanged();

static void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
static void wsEvent2(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
Expand Down
14 changes: 8 additions & 6 deletions src/User/UserModArtNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class UserModArtNet:public Module {
UserModArtNet() :Module("ArtNet") {
print->print("%s %s\n", __PRETTY_FUNCTION__, name);

isEnabled = false;

print->print("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
};

Expand All @@ -33,15 +35,16 @@ class UserModArtNet:public Module {
print->print("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
}

void connected() {
print->print("%s %s - Connected\n", __PRETTY_FUNCTION__, name);
isConnected = true;
}
// void connectedChanged() {
// if (SysModModules::isConnected) {
// print->print("%s %s - Connected\n", __PRETTY_FUNCTION__, name);
// }
// }

void loop(){
// Module::loop();

if(!isConnected) return;
if(!SysModModules::isConnected) return;

if(!lds->newFrame) return;

Expand Down Expand Up @@ -107,7 +110,6 @@ class UserModArtNet:public Module {
}

private:
bool isConnected = false;
size_t sequenceNumber = 0;

};
Expand Down
11 changes: 6 additions & 5 deletions src/User/UserModDDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class UserModDDP:public Module {
UserModDDP() :Module("DDP") {
print->print("%s %s\n", __PRETTY_FUNCTION__, name);

isEnabled = false;

print->print("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
};

Expand All @@ -49,15 +51,15 @@ class UserModDDP:public Module {
print->print("%s %s %s\n", __PRETTY_FUNCTION__, name, success?"success":"failed");
}

void connected() {
print->print("%s %s - Connected\n", __PRETTY_FUNCTION__, name);
isConnected = true;
void connectedChanged() {
if (SysModModules::isConnected)
print->print("%s %s - Connected\n", __PRETTY_FUNCTION__, name);
}

void loop(){
// Module::loop();

if(!isConnected) return;
if(!SysModModules::isConnected) return;

if(!lds->newFrame) return;

Expand Down Expand Up @@ -129,7 +131,6 @@ class UserModDDP:public Module {
}

private:
bool isConnected = false;
size_t sequenceNumber = 0;

};
Expand Down
Loading

0 comments on commit 31e3f5b

Please sign in to comment.