diff --git a/main/Module.h b/main/Module.h index b5e9f0bd..d60366f3 100644 --- a/main/Module.h +++ b/main/Module.h @@ -1,5 +1,6 @@ #pragma once #include +#include class Module { @@ -8,7 +9,7 @@ class Module virtual ~Module(); virtual void setup() = 0; virtual void loop() = 0; - virtual void act() = 0; - virtual void set() = 0; - virtual void get() = 0; + virtual void act(JsonObject job) = 0; + virtual void set(JsonObject job) = 0; + virtual void get(JsonObject job) = 0; }; \ No newline at end of file diff --git a/main/ModuleController.cpp b/main/ModuleController.cpp index 9439c498..f6decbcd 100644 --- a/main/ModuleController.cpp +++ b/main/ModuleController.cpp @@ -10,8 +10,7 @@ namespace RestApi } void setModules() { - deserialize(); - moduleController.set(); + moduleController.set(deserialize()); serialize(); } }; @@ -139,42 +138,41 @@ void ModuleController::get() (*jdoc)[key_modules][key_joy] = moduleConfig->analogJoystick; } // {"task":"/modules_set", "modules" : {"led" : 1, "motor": 1, "slm" : 0, "analogin" : 0, "pid" : 0, "laser" : 0, "dac" : 0, "analogout" : 0, "digitalout" : 0, "digitalin" : 0, "scanner" : 0}} -void ModuleController::set() +void ModuleController::set(JsonObject j) { - DynamicJsonDocument *jdoc = WifiController::getJDoc(); - if (jdoc->containsKey(key_modules)) - { - if ((*jdoc)[key_modules].containsKey(keyLed)) - moduleConfig->led = (*jdoc)[key_modules][keyLed]; - if ((*jdoc)[key_modules].containsKey(key_motor)) - moduleConfig->motor = (*jdoc)[key_modules][key_motor]; - if ((*jdoc)[key_modules].containsKey(key_home)) - moduleConfig->home = (*jdoc)[key_modules][key_home]; - if ((*jdoc)[key_modules].containsKey(key_slm)) - moduleConfig->slm = (*jdoc)[key_modules][key_slm]; - if ((*jdoc)[key_modules].containsKey(key_analogin)) - moduleConfig->analogin = (*jdoc)[key_modules][key_analogin]; - if ((*jdoc)[key_modules].containsKey(key_pid)) - moduleConfig->pid = (*jdoc)[key_modules][key_pid]; - if ((*jdoc)[key_modules].containsKey(key_laser)) - moduleConfig->laser = (*jdoc)[key_modules][key_laser]; - if ((*jdoc)[key_modules].containsKey(key_dac)) - moduleConfig->dac = (*jdoc)[key_modules][key_dac]; - if ((*jdoc)[key_modules].containsKey(key_analogout)) - moduleConfig->analogout = (*jdoc)[key_modules][key_analogout]; - if ((*jdoc)[key_modules].containsKey(key_digitalout)) - moduleConfig->digitalout = (*jdoc)[key_modules][key_digitalout]; - if ((*jdoc)[key_modules].containsKey(key_digitalin)) - moduleConfig->digitalin = (*jdoc)[key_modules][key_digitalin]; - if ((*jdoc)[key_modules].containsKey(key_scanner)) - moduleConfig->scanner = (*jdoc)[key_modules][key_scanner]; - if ((*jdoc)[key_modules].containsKey(key_joy)) - moduleConfig->analogJoystick = (*jdoc)[key_modules][key_joy]; + if (j.containsKey(key_modules)) + { + if (j[key_modules].containsKey(keyLed)) + moduleConfig->led = j[key_modules][keyLed]; + if (j[key_modules].containsKey(key_motor)) + moduleConfig->motor = j[key_modules][key_motor]; + if (j[key_modules].containsKey(key_home)) + moduleConfig->home = j[key_modules][key_home]; + if (j[key_modules].containsKey(key_slm)) + moduleConfig->slm = j[key_modules][key_slm]; + if (j[key_modules].containsKey(key_analogin)) + moduleConfig->analogin = j[key_modules][key_analogin]; + if (j[key_modules].containsKey(key_pid)) + moduleConfig->pid = j[key_modules][key_pid]; + if (j[key_modules].containsKey(key_laser)) + moduleConfig->laser = j[key_modules][key_laser]; + if (j[key_modules].containsKey(key_dac)) + moduleConfig->dac = j[key_modules][key_dac]; + if (j[key_modules].containsKey(key_analogout)) + moduleConfig->analogout = j[key_modules][key_analogout]; + if (j[key_modules].containsKey(key_digitalout)) + moduleConfig->digitalout = j[key_modules][key_digitalout]; + if (j[key_modules].containsKey(key_digitalin)) + moduleConfig->digitalin = j[key_modules][key_digitalin]; + if (j[key_modules].containsKey(key_scanner)) + moduleConfig->scanner = j[key_modules][key_scanner]; + if (j[key_modules].containsKey(key_joy)) + moduleConfig->analogJoystick = j[key_modules][key_joy]; Config::setModuleConfig(moduleConfig); setup(); WifiController::restartWebServer(); } - jdoc->clear(); + WifiController::getJDoc()->clear(); } ModuleController moduleController; \ No newline at end of file diff --git a/main/ModuleController.h b/main/ModuleController.h index 62ab7023..34356d99 100644 --- a/main/ModuleController.h +++ b/main/ModuleController.h @@ -51,7 +51,7 @@ class ModuleController void loop(); Module *get(AvailableModules mod); void get(); - void set(); + void set(JsonObject j); }; extern ModuleController moduleController; \ No newline at end of file diff --git a/main/docs.txt b/main/docs.txt index 2272aac3..e00d4f0a 100644 --- a/main/docs.txt +++ b/main/docs.txt @@ -17,6 +17,12 @@ {"task": "/state_set", "isdebug":0} {"task": "/state_get", "active":1} {"task": "/wifi/connect", "AP": 1,"PW": "","ssid": "Uc2"} +//cant run inside tasks +{"task": "/wifi/connect", "AP": 1,"PW": "","ssid": "Uc2"} +//cant run inside tasks +{"task":"/modules_set", "modules" : {"motor": 1, joy:1}} +//apply all pins at once +{"tasks": [{"task":"/motor_set","motor":{"steppers": [{ "stepperid": 1, "step": 26, "dir": 16, "enable": 12, "step_inverted": 0, "dir_inverted": 0, "enable_inverted": 1 },{ "stepperid": 2, "step": 25, "dir": 27, "enable": 12, "step_inverted": 0, "dir_inverted": 0, "enable_inverted": 1 }]}},{"task":"/analog_joystick_set", "joy" : {"joyX": 35, joyY:34}}]} retrieve sensor value diff --git a/main/src/analogin/AnalogInController.cpp b/main/src/analogin/AnalogInController.cpp index acdf6739..78ea57fc 100644 --- a/main/src/analogin/AnalogInController.cpp +++ b/main/src/analogin/AnalogInController.cpp @@ -8,14 +8,14 @@ AnalogInController::~AnalogInController(){}; void AnalogInController::loop() {} // Custom function accessible by the API -void AnalogInController::act() { +void AnalogInController::act(JsonObject ob) { // here you can do something if (DEBUG) Serial.println("readanalogin_act_fct"); - int readanaloginID = (int)(*WifiController::getJDoc())["readanaloginID"]; + int readanaloginID = (int)(ob)["readanaloginID"]; int mN_analogin_avg = N_analogin_avg; - if (WifiController::getJDoc()->containsKey("N_analogin_avg")) - mN_analogin_avg = (int)(*WifiController::getJDoc())["N_analogin_avg"]; + if (ob.containsKey("N_analogin_avg")) + mN_analogin_avg = (int)(ob)["N_analogin_avg"]; int analoginpin = 0 ; if (DEBUG) Serial.print("readanaloginID "); Serial.println(readanaloginID); @@ -46,12 +46,12 @@ void AnalogInController::act() { -void AnalogInController::get() { +void AnalogInController::get(JsonObject ob) { if (DEBUG) Serial.println("readanalogin_set_fct"); - int readanaloginID = (int)(*WifiController::getJDoc())["readanaloginID"]; - int readanaloginPIN = (int)(*WifiController::getJDoc())["readanaloginPIN"]; - if (WifiController::getJDoc()->containsKey("N_analogin_avg")) - N_analogin_avg = (int)(*WifiController::getJDoc())["N_analogin_avg"]; + int readanaloginID = (int)(ob)["readanaloginID"]; + int readanaloginPIN = (int)(ob)["readanaloginPIN"]; + if (ob.containsKey("N_analogin_avg")) + N_analogin_avg = (int)(ob)["N_analogin_avg"]; switch (readanaloginID) { case 0: @@ -74,9 +74,9 @@ void AnalogInController::get() { // Custom function accessible by the API -void AnalogInController::set() { +void AnalogInController::set(JsonObject jsonDocument) { if (DEBUG) Serial.println("readanalogin_get_fct"); - int readanaloginID = (int)(*WifiController::getJDoc())["readanaloginID"]; + int readanaloginID = (int)(jsonDocument)["readanaloginID"]; int readanaloginPIN = 0; switch (readanaloginID) { case 0: @@ -89,11 +89,6 @@ if (DEBUG) Serial.println("readanalogin_get_fct"); readanaloginPIN = pins.analogin_PIN_2; break; } - - WifiController::getJDoc()->clear(); - (*WifiController::getJDoc())["N_analogin_avg"] = N_analogin_avg; - (*WifiController::getJDoc())["readanaloginPIN"] = readanaloginPIN; - (*WifiController::getJDoc())["readanaloginID"] = readanaloginID; } diff --git a/main/src/analogin/AnalogInController.h b/main/src/analogin/AnalogInController.h index 3ecc5151..14266442 100644 --- a/main/src/analogin/AnalogInController.h +++ b/main/src/analogin/AnalogInController.h @@ -18,9 +18,9 @@ class AnalogInController : public Module int N_analogin_avg; //no idea if it should be equal to that that one inside PidController.h void setup() override; - void act() override; - void set() override; - void get() override; + void act(JsonObject jsonDocument) override; + void set(JsonObject jsonDocument) override; + void get(JsonObject jsonDocument) override; void loop() override; }; diff --git a/main/src/analogin/AnalogJoystick.cpp b/main/src/analogin/AnalogJoystick.cpp index 1b3b526f..9224bd82 100644 --- a/main/src/analogin/AnalogJoystick.cpp +++ b/main/src/analogin/AnalogJoystick.cpp @@ -4,14 +4,12 @@ namespace RestApi { void AnalogJoystick_set() { - deserialize(); - moduleController.get(AvailableModules::analogJoystick)->set(); + moduleController.get(AvailableModules::analogJoystick)->set(deserialize()); serialize(); } void AnalogJoystick_get() { - deserialize(); - moduleController.get(AvailableModules::analogJoystick)->get(); + moduleController.get(AvailableModules::analogJoystick)->get(deserialize()); serialize(); } }; @@ -25,21 +23,20 @@ void AnalogJoystick::setup() pinMode(pins->x_pin, INPUT); pinMode(pins->y_pin, INPUT); } -void AnalogJoystick::act() {} -void AnalogJoystick::set() { - DynamicJsonDocument *doc = WifiController::getJDoc(); - if (doc->containsKey(key_joy)) +void AnalogJoystick::act(JsonObject jsonDocument) {} +void AnalogJoystick::set(JsonObject doc) { + if (doc.containsKey(key_joy)) { - if((*doc)[key_joy].containsKey(key_joiypinX)) - pins->x_pin = (*doc)[key_joy][key_joiypinX]; - if((*doc)[key_joy].containsKey(key_joiypinY)) - pins->y_pin = (*doc)[key_joy][key_joiypinY]; + if((doc)[key_joy].containsKey(key_joiypinX)) + pins->x_pin = (doc)[key_joy][key_joiypinX]; + if((doc)[key_joy].containsKey(key_joiypinY)) + pins->y_pin = (doc)[key_joy][key_joiypinY]; } - doc->clear(); + doc.clear(); Config::setAnalogJoyStickPins(pins); setup(); } -void AnalogJoystick::get() { +void AnalogJoystick::get(JsonObject jsonDocument) { DynamicJsonDocument *doc = WifiController::getJDoc(); doc->clear(); (*doc)[key_joy][key_joiypinX] = pins->x_pin; diff --git a/main/src/analogin/AnalogJoystick.h b/main/src/analogin/AnalogJoystick.h index e0470a00..e561f877 100644 --- a/main/src/analogin/AnalogJoystick.h +++ b/main/src/analogin/AnalogJoystick.h @@ -25,9 +25,9 @@ class AnalogJoystick : public Module AnalogJoystick(); virtual ~AnalogJoystick(); void setup() override; - void act() override; - void set() override; - void get() override; + void act(JsonObject jsonDocument) override; + void set(JsonObject jsonDocument) override; + void get(JsonObject jsonDocument) override; void loop() override; }; \ No newline at end of file diff --git a/main/src/analogout/AnalogOutController.cpp b/main/src/analogout/AnalogOutController.cpp index 4aaedd13..8ab366ac 100644 --- a/main/src/analogout/AnalogOutController.cpp +++ b/main/src/analogout/AnalogOutController.cpp @@ -5,22 +5,19 @@ namespace RestApi { void AnalogOut_act() { - deserialize(); - moduleController.get(AvailableModules::analogout)->act(); + moduleController.get(AvailableModules::analogout)->act(deserialize()); serialize(); } void AnalogOut_get() { - deserialize(); - moduleController.get(AvailableModules::analogout)->get(); + moduleController.get(AvailableModules::analogout)->get(deserialize()); serialize(); } void AnalogOut_set() { - deserialize(); - moduleController.get(AvailableModules::analogout)->set(); + moduleController.get(AvailableModules::analogout)->set(deserialize()); serialize(); } } @@ -45,13 +42,13 @@ void AnalogOutController::setup() } // Custom function accessible by the API -void AnalogOutController::act() +void AnalogOutController::act(JsonObject ob) { // here you can do something Serial.println("analogout_act_fct"); - int analogoutid = (*WifiController::getJDoc())["analogoutid"]; - int analogoutval = (*WifiController::getJDoc())["analogoutval"]; + int analogoutid = (ob)["analogoutid"]; + int analogoutval = (ob)["analogoutval"]; if (DEBUG) { @@ -80,20 +77,20 @@ void AnalogOutController::act() (*WifiController::getJDoc())["return"] = 1; } -void AnalogOutController::set() +void AnalogOutController::set(JsonObject ob) { // here you can set parameters int analogoutid = 0; - if (WifiController::getJDoc()->containsKey("analogoutid")) + if (ob.containsKey("analogoutid")) { - analogoutid = (*WifiController::getJDoc())["analogoutid"]; + analogoutid = (ob)["analogoutid"]; } int analogoutpin = 0; - if (WifiController::getJDoc()->containsKey("analogoutpin")) + if (ob.containsKey("analogoutpin")) { - int analogoutpin = (*WifiController::getJDoc())["analogoutpin"]; + int analogoutpin = (ob)["analogoutpin"]; } if (DEBUG) @@ -137,12 +134,11 @@ void AnalogOutController::set() ledcWrite(PWM_CHANNEL_analogout_3, 0); } Config::setAnalogOutPins(pins); - WifiController::getJDoc()->clear(); - (*WifiController::getJDoc())["return"] = 1; + } // Custom function accessible by the API -void AnalogOutController::get() +void AnalogOutController::get(JsonObject jsonDocument) { // GET SOME PARAMETERS HERE int analogoutid = (*WifiController::getJDoc())["analogoutid"]; diff --git a/main/src/analogout/AnalogOutController.h b/main/src/analogout/AnalogOutController.h index 2e1f5af4..a400291e 100644 --- a/main/src/analogout/AnalogOutController.h +++ b/main/src/analogout/AnalogOutController.h @@ -52,9 +52,9 @@ class AnalogOutController : public Module int PWM_CHANNEL_analogout_2 = 5; int PWM_CHANNEL_analogout_3 = 6; - void act() override; - void set() override; - void get() override; + void act(JsonObject jsonDocument) override; + void set(JsonObject jsonDocument) override; + void get(JsonObject jsonDocument) override; void setup() override; void loop() override; diff --git a/main/src/dac/DacController.cpp b/main/src/dac/DacController.cpp index 60b37103..79f3e508 100644 --- a/main/src/dac/DacController.cpp +++ b/main/src/dac/DacController.cpp @@ -12,22 +12,19 @@ namespace RestApi { void Dac_act() { - deserialize(); - moduleController.get(AvailableModules::dac)->act(); + moduleController.get(AvailableModules::dac)->act(deserialize()); serialize(); } void Dac_get() { - deserialize(); - moduleController.get(AvailableModules::dac)->get(); + moduleController.get(AvailableModules::dac)->get(deserialize()); serialize(); } void Dac_set() { - deserialize(); - moduleController.get(AvailableModules::dac)->set(); + moduleController.get(AvailableModules::dac)->set(deserialize()); serialize(); } } @@ -54,7 +51,7 @@ void DacController::setup() void DacController::loop() {} // Custom function accessible by the API -void DacController::act() +void DacController::act(JsonObject ob) { // here you can do something @@ -63,42 +60,42 @@ void DacController::act() // apply default parameters // DAC Channel dac_channel = DAC_CHANNEL_1; - if (WifiController::getJDoc()->containsKey("dac_channel")) + if (ob.containsKey("dac_channel")) { - dac_channel = (*WifiController::getJDoc())["dac_channel"]; + dac_channel = (ob)["dac_channel"]; } // DAC Frequency frequency = 1000; - if (WifiController::getJDoc()->containsKey("frequency")) + if (ob.containsKey("frequency")) { - frequency = (*WifiController::getJDoc())["frequency"]; + frequency = (ob)["frequency"]; } // DAC offset int offset = 0; - if (WifiController::getJDoc()->containsKey("offset")) + if (ob.containsKey("offset")) { - int offset = (*WifiController::getJDoc())["offset"]; + int offset = (ob)["offset"]; } // DAC amplitude int amplitude = 0; - if (WifiController::getJDoc()->containsKey("amplitude")) + if (ob.containsKey("amplitude")) { - int amplitude = (*WifiController::getJDoc())["amplitude"]; + int amplitude = (ob)["amplitude"]; } // DAC clk_div int clk_div = 0; - if (WifiController::getJDoc()->containsKey("clk_div")) + if (ob.containsKey("clk_div")) { - int clk_div = (*WifiController::getJDoc())["clk_div"]; + int clk_div = (ob)["clk_div"]; } - if ((*WifiController::getJDoc())["dac_channel"] == 1) + if ((ob)["dac_channel"] == 1) dac_channel = DAC_CHANNEL_1; - else if ((*WifiController::getJDoc())["dac_channel"] == 2) + else if ((ob)["dac_channel"] == 2) dac_channel = DAC_CHANNEL_2; // Scale output of a DAC channel using two bit pattern: @@ -144,25 +141,24 @@ void DacController::act() (*WifiController::getJDoc())["return"] = 1; } -void DacController::set() +void DacController::set(JsonObject ob) { - if ((*WifiController::getJDoc()).containsKey(keyDACfake1Pin)) + if ((ob).containsKey(keyDACfake1Pin)) { - pins.dac_fake_1 = (*WifiController::getJDoc())[keyDACfake1Pin]; + pins.dac_fake_1 = (ob)[keyDACfake1Pin]; Config::setDacPins(pins); setup(); } - if ((*WifiController::getJDoc()).containsKey(keyDACfake2Pin)) + if ((ob).containsKey(keyDACfake2Pin)) { - pins.dac_fake_2 = (*WifiController::getJDoc())[keyDACfake2Pin]; + pins.dac_fake_2 = (ob)[keyDACfake2Pin]; Config::setDacPins(pins); setup(); } - WifiController::getJDoc()->clear(); } // Custom function accessible by the API -void DacController::get() +void DacController::get(JsonObject jsonDocument) { // GET SOME PARAMETERS HERE int dac_variable = 12343; diff --git a/main/src/dac/DacController.h b/main/src/dac/DacController.h index f5ddfb8c..254fb745 100644 --- a/main/src/dac/DacController.h +++ b/main/src/dac/DacController.h @@ -39,9 +39,9 @@ class DacController : public Module void setup() override; - void act() override; - void set() override; - void get() override; + void act(JsonObject jsonDocument) override; + void set(JsonObject jsonDocument) override; + void get(JsonObject jsonDocument) override; void loop() override; static void drive_galvo(void *parameter); }; diff --git a/main/src/digitalin/DigitalInController.cpp b/main/src/digitalin/DigitalInController.cpp index e08e4e03..b44a9be0 100644 --- a/main/src/digitalin/DigitalInController.cpp +++ b/main/src/digitalin/DigitalInController.cpp @@ -8,22 +8,19 @@ namespace RestApi { void DigitalIn_act() { - deserialize(); - moduleController.get(AvailableModules::digitalin)->act(); + moduleController.get(AvailableModules::digitalin)->act(deserialize()); serialize(); } void DigitalIn_get() { - deserialize(); - moduleController.get(AvailableModules::digitalin)->get(); + moduleController.get(AvailableModules::digitalin)->get(deserialize()); serialize(); } void DigitalIn_set() { - deserialize(); - moduleController.get(AvailableModules::digitalin)->set(); + moduleController.get(AvailableModules::digitalin)->set(deserialize()); serialize(); } } @@ -32,21 +29,19 @@ DigitalInController::DigitalInController(/* args */){}; DigitalInController::~DigitalInController(){}; // Custom function accessible by the API -void DigitalInController::act() +void DigitalInController::act(JsonObject jsonDocument) { - DynamicJsonDocument * jsonDocument = WifiController::getJDoc(); // here you can do something Serial.println("digitalin_act_fct"); - jsonDocument->clear(); - (*jsonDocument)["return"] = 1; + jsonDocument.clear(); + (jsonDocument)["return"] = 1; } -void DigitalInController::set() +void DigitalInController::set(JsonObject jsonDocument) { // here you can set parameters - DynamicJsonDocument * jsonDocument = WifiController::getJDoc(); - int digitalinid = (*jsonDocument)["digitalinid"]; - int digitalinpin = (*jsonDocument)["digitalinpin"]; + int digitalinid = (jsonDocument)["digitalinid"]; + int digitalinpin = (jsonDocument)["digitalinpin"]; if (DEBUG) Serial.print("digitalinid "); Serial.println(digitalinid); @@ -74,12 +69,10 @@ void DigitalInController::set() } Config::setDigitalInPins(pins); isBusy = false; - jsonDocument->clear(); - (*jsonDocument)["return"] = 1; } // Custom function accessible by the API -void DigitalInController::get() +void DigitalInController::get(JsonObject j) { DynamicJsonDocument * jsonDocument = WifiController::getJDoc(); // GET SOME PARAMETERS HERE diff --git a/main/src/digitalin/DigitalInController.h b/main/src/digitalin/DigitalInController.h index 0d7322b2..ead3fa38 100644 --- a/main/src/digitalin/DigitalInController.h +++ b/main/src/digitalin/DigitalInController.h @@ -28,9 +28,9 @@ class DigitalInController : public Module int digitalin_val_2 = 0; int digitalin_val_3 = 0; - void act() override; - void set() override; - void get() override; + void act(JsonObject jsonDocument) override; + void set(JsonObject jsonDocument) override; + void get(JsonObject jsonDocument) override; void setup() override; void loop() override; }; \ No newline at end of file diff --git a/main/src/digitalout/DigitalOutController.cpp b/main/src/digitalout/DigitalOutController.cpp index 671cc23c..4bc85a5e 100644 --- a/main/src/digitalout/DigitalOutController.cpp +++ b/main/src/digitalout/DigitalOutController.cpp @@ -8,22 +8,19 @@ namespace RestApi { void DigitalOut_act() { - deserialize(); - moduleController.get(AvailableModules::digitalout)->act(); + moduleController.get(AvailableModules::digitalout)->act(deserialize()); serialize(); } void DigitalOut_get() { - deserialize(); - moduleController.get(AvailableModules::digitalout)->get(); + moduleController.get(AvailableModules::digitalout)->get(deserialize()); serialize(); } void DigitalOut_set() { - deserialize(); - moduleController.get(AvailableModules::digitalout)->set(); + moduleController.get(AvailableModules::digitalout)->set(deserialize()); serialize(); } } @@ -32,16 +29,15 @@ DigitalOutController::DigitalOutController(/* args */){}; DigitalOutController::~DigitalOutController(){}; // Custom function accessible by the API -void DigitalOutController::act() +void DigitalOutController::act(JsonObject jsonDocument) { - DynamicJsonDocument * jsonDocument = WifiController::getJDoc(); // here you can do something Serial.println("digitalout_act_fct"); isBusy = true; int triggerdelay = 10; - int digitaloutid = (*jsonDocument)["digitaloutid"]; - int digitaloutval = (*jsonDocument)["digitaloutval"]; + int digitaloutid = (jsonDocument)["digitaloutid"]; + int digitaloutval = (jsonDocument)["digitaloutval"]; if (DEBUG) { @@ -102,16 +98,15 @@ void DigitalOutController::act() Serial.println(pins.digitalout_PIN_3); } } - jsonDocument->clear(); - (*jsonDocument)["return"] = 1; + jsonDocument.clear(); + (jsonDocument)["return"] = 1; } -void DigitalOutController::set() +void DigitalOutController::set(JsonObject jsonDocument) { // here you can set parameters - DynamicJsonDocument * jsonDocument = WifiController::getJDoc(); - int digitaloutid = (*jsonDocument)["digitaloutid"]; - int digitaloutpin = (*jsonDocument)["digitaloutpin"]; + int digitaloutid = (jsonDocument)["digitaloutid"]; + int digitaloutpin = (jsonDocument)["digitaloutpin"]; if (DEBUG) Serial.print("digitaloutid "); Serial.println(digitaloutid); @@ -142,12 +137,10 @@ void DigitalOutController::set() } Config::setDigitalOutPins(pins); isBusy = false; - jsonDocument->clear(); - (*jsonDocument)["return"] = 1; } // Custom function accessible by the API -void DigitalOutController::get() +void DigitalOutController::get(JsonObject j) { DynamicJsonDocument * jsonDocument = WifiController::getJDoc(); // GET SOME PARAMETERS HERE diff --git a/main/src/digitalout/DigitalOutController.h b/main/src/digitalout/DigitalOutController.h index 5eb237a0..8c74c8db 100644 --- a/main/src/digitalout/DigitalOutController.h +++ b/main/src/digitalout/DigitalOutController.h @@ -28,9 +28,9 @@ class DigitalOutController : public Module int digitalout_val_2 = 0; int digitalout_val_3 = 0; - void act() override; - void set() override; - void get() override; + void act(JsonObject ob) override; + void set(JsonObject ob) override; + void get(JsonObject ob) override; void setup() override; void loop() override; }; \ No newline at end of file diff --git a/main/src/home/HomeMotor.cpp b/main/src/home/HomeMotor.cpp index ac5c94c3..4ae46af0 100644 --- a/main/src/home/HomeMotor.cpp +++ b/main/src/home/HomeMotor.cpp @@ -12,22 +12,19 @@ namespace RestApi { void HomeMotor_act() { - deserialize(); - moduleController.get(AvailableModules::home)->act(); + moduleController.get(AvailableModules::home)->act(deserialize()); serialize(); } void HomeMotor_get() { - deserialize(); - moduleController.get(AvailableModules::home)->get(); + moduleController.get(AvailableModules::home)->get(deserialize()); serialize(); } void HomeMotor_set() { - deserialize(); - moduleController.get(AvailableModules::home)->set(); + moduleController.get(AvailableModules::home)->set(deserialize()); serialize(); } } @@ -35,31 +32,30 @@ namespace RestApi Handle REST calls to the HomeMotor module */ //{"task":"/home_act", "home":{"steppers":[{"endpospin":1, "timeout":1000, "speed":1000, "direction":1]}} -void HomeMotor::act() +void HomeMotor::act(JsonObject j) { if (DEBUG) Serial.println("home_act_fct"); - DynamicJsonDocument *j = WifiController::getJDoc(); - if ((*j).containsKey(key_home)){ - if ((*j)[key_home].containsKey(key_steppers)) + if ((j).containsKey(key_home)){ + if ((j)[key_home].containsKey(key_steppers)) { Serial.println("contains key"); - for (int i = 0; i < (*j)[key_home][key_steppers].size(); i++) + for (int i = 0; i < (j)[key_home][key_steppers].size(); i++) { - Stepper s = static_cast((*j)[key_home][key_steppers][i][key_stepperid]); - - if ((*j)[key_home][key_steppers][i].containsKey(key_home_timeout)) - hdata[s]->homeTimeout = (*j)[key_home][key_steppers][i][key_home_timeout]; - if ((*j)[key_home][key_steppers][i].containsKey(key_home_speed)) - hdata[s]->homeSpeed = (*j)[key_home][key_steppers][i][key_home_speed]; - if ((*j)[key_home][key_steppers][i].containsKey(key_home_maxspeed)) - hdata[s]->homeMaxspeed = (*j)[key_home][key_steppers][i][key_home_maxspeed]; - if ((*j)[key_home][key_steppers][i].containsKey(key_home_direction)) - hdata[s]->homeDirection = (*j)[key_home][key_steppers][i][key_home_direction]; - if ((*j)[key_home][key_steppers][i].containsKey(key_home_endposrelease)) - hdata[s]->homeEndposRelease = (*j)[key_home][key_steppers][i][key_home_endposrelease]; + Stepper s = static_cast((j)[key_home][key_steppers][i][key_stepperid]); + + if ((j)[key_home][key_steppers][i].containsKey(key_home_timeout)) + hdata[s]->homeTimeout = (j)[key_home][key_steppers][i][key_home_timeout]; + if (j[key_home][key_steppers][i].containsKey(key_home_speed)) + hdata[s]->homeSpeed = (j)[key_home][key_steppers][i][key_home_speed]; + if (j[key_home][key_steppers][i].containsKey(key_home_maxspeed)) + hdata[s]->homeMaxspeed = (j)[key_home][key_steppers][i][key_home_maxspeed]; + if (j[key_home][key_steppers][i].containsKey(key_home_direction)) + hdata[s]->homeDirection = j[key_home][key_steppers][i][key_home_direction]; + if (j[key_home][key_steppers][i].containsKey(key_home_endposrelease)) + hdata[s]->homeEndposRelease = j[key_home][key_steppers][i][key_home_endposrelease]; // grab current time hdata[s]->homeTimeStarted = millis(); @@ -81,7 +77,7 @@ void HomeMotor::act() WifiController::getJDoc()->clear(); } -void HomeMotor::get() +void HomeMotor::get(JsonObject ob) { if (DEBUG) Serial.println("home_get_fct"); @@ -118,7 +114,7 @@ void HomeMotor::get() } -void HomeMotor::set() +void HomeMotor::set(JsonObject ob) { } diff --git a/main/src/home/HomeMotor.h b/main/src/home/HomeMotor.h index 90e2977f..d6d62c80 100644 --- a/main/src/home/HomeMotor.h +++ b/main/src/home/HomeMotor.h @@ -44,9 +44,9 @@ class HomeMotor : public Module bool isHoming = false; std::array hdata; - void act() override; - void set() override; - void get() override; + void act(JsonObject ob) override; + void set(JsonObject ob) override; + void get(JsonObject ob) override; void setup() override; void loop() override; diff --git a/main/src/laser/LaserController.cpp b/main/src/laser/LaserController.cpp index 02477990..4f519674 100644 --- a/main/src/laser/LaserController.cpp +++ b/main/src/laser/LaserController.cpp @@ -5,9 +5,8 @@ namespace RestApi { void Laser_act() { - deserialize(); if (moduleController.get(AvailableModules::laser) != nullptr) - moduleController.get(AvailableModules::laser)->act(); + moduleController.get(AvailableModules::laser)->act(deserialize()); else log_i("laser controller is null!"); serialize(); @@ -15,9 +14,8 @@ namespace RestApi void Laser_get() { - deserialize(); if (moduleController.get(AvailableModules::laser) != nullptr) - moduleController.get(AvailableModules::laser)->get(); + moduleController.get(AvailableModules::laser)->get(deserialize()); else log_i("laser controller is null!"); serialize(); @@ -26,9 +24,8 @@ namespace RestApi void Laser_set() { log_i("laser set!"); - deserialize(); if (moduleController.get(AvailableModules::laser) != nullptr) - moduleController.get(AvailableModules::laser)->set(); + moduleController.get(AvailableModules::laser)->set(deserialize()); else log_i("laser controller is null!"); serialize(); @@ -86,20 +83,20 @@ void LaserController::LASER_despeckle(int LASERdespeckle, int LASERid, int LASER } // Custom function accessible by the API -void LaserController::act() +void LaserController::act(JsonObject ob) { // here you can do something Serial.println("LASER_act_fct"); isBusy = true; - int LASERid = (*WifiController::getJDoc())["LASERid"]; - int LASERval = (*WifiController::getJDoc())["LASERval"]; - int LASERdespeckle = (*WifiController::getJDoc())["LASERdespeckle"]; + int LASERid = (ob)["LASERid"]; + int LASERval = (ob)["LASERval"]; + int LASERdespeckle = (ob)["LASERdespeckle"]; int LASERdespecklePeriod = 20; - if (WifiController::getJDoc()->containsKey("LASERdespecklePeriod")) + if (ob.containsKey("LASERdespecklePeriod")) { - LASERdespecklePeriod = (*WifiController::getJDoc())["LASERdespecklePeriod"]; + LASERdespecklePeriod = (ob)["LASERdespecklePeriod"]; } if (DEBUG) @@ -157,13 +154,13 @@ void LaserController::act() isBusy = false; } -void LaserController::set() +void LaserController::set(JsonObject ob) { // here you can set parameters - if (WifiController::getJDoc()->containsKey("LASERid") && WifiController::getJDoc()->containsKey("LASERpin")) + if (ob.containsKey("LASERid") && ob.containsKey("LASERpin")) { - int LASERid = (*WifiController::getJDoc())["LASERid"]; - int LASERpin = (*WifiController::getJDoc())["LASERpin"]; + int LASERid = (ob)["LASERid"]; + int LASERpin = (ob)["LASERpin"]; log_i("LaserId: %i Pin:%i", LASERid, LASERpin); if (LASERpin != 0) { @@ -200,11 +197,10 @@ void LaserController::set() } Config::setLaserPins(pins); } - WifiController::getJDoc()->clear(); } // Custom function accessible by the API -void LaserController::get() +void LaserController::get(JsonObject ob) { WifiController::getJDoc()->clear(); (*WifiController::getJDoc())["LASER1pin"] = pins.LASER_PIN_1; diff --git a/main/src/laser/LaserController.h b/main/src/laser/LaserController.h index 1dd19221..8887ec05 100644 --- a/main/src/laser/LaserController.h +++ b/main/src/laser/LaserController.h @@ -58,9 +58,9 @@ class LaserController : public Module bool DEBUG = false; void LASER_despeckle(int LASERdespeckle, int LASERid, int LASERperiod); - void act() override; - void set() override; - void get() override; + void act(JsonObject ob) override; + void set(JsonObject ob) override; + void get(JsonObject ob) override; void setup() override; void loop() override; diff --git a/main/src/led/LedController.cpp b/main/src/led/LedController.cpp index f4a6d232..63d2854d 100644 --- a/main/src/led/LedController.cpp +++ b/main/src/led/LedController.cpp @@ -5,22 +5,19 @@ namespace RestApi { void Led_act() { - deserialize(); - moduleController.get(AvailableModules::led)->act(); + moduleController.get(AvailableModules::led)->act(deserialize()); serialize(); } void Led_get() { - deserialize(); - moduleController.get(AvailableModules::led)->get(); + moduleController.get(AvailableModules::led)->get(deserialize()); serialize(); } void Led_set() { - deserialize(); - moduleController.get(AvailableModules::led)->set(); + moduleController.get(AvailableModules::led)->set(deserialize()); serialize(); } } @@ -50,22 +47,22 @@ void LedController::loop() } // Custom function accessible by the API -void LedController::act() +void LedController::act(JsonObject ob) { - if (WifiController::getJDoc()->containsKey(keyLed)) + if (ob.containsKey(keyLed)) { - LedModes LEDArrMode = static_cast((*WifiController::getJDoc())[keyLed][keyLEDArrMode]); // "array", "full", "single", "off", "left", "right", "top", "bottom", + LedModes LEDArrMode = static_cast(ob[keyLed][keyLEDArrMode]); // "array", "full", "single", "off", "left", "right", "top", "bottom", // individual pattern gets adressed // PYTHON: send_LEDMatrix_array(self, led_pattern, timeout=1) if (LEDArrMode == LedModes::array || LEDArrMode == LedModes::multi) { - for (int i = 0; i < (*WifiController::getJDoc())[keyLed][key_led_array].size(); i++) + for (int i = 0; i < ob[keyLed][key_led_array].size(); i++) { set_led_RGB( - (*WifiController::getJDoc())[keyLed][key_led_array][i][keyid], - (*WifiController::getJDoc())[keyLed][key_led_array][i][keyRed], - (*WifiController::getJDoc())[keyLed][key_led_array][i][keyGreen], - (*WifiController::getJDoc())[keyLed][key_led_array][i][keyBlue]); + ob[keyLed][key_led_array][i][keyid], + ob[keyLed][key_led_array][i][keyRed], + ob[keyLed][key_led_array][i][keyGreen], + ob[keyLed][key_led_array][i][keyBlue]); } } // only if a single led will be updated, all others stay the same @@ -73,18 +70,18 @@ void LedController::act() else if (LEDArrMode == LedModes::single) { set_led_RGB( - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyid], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyRed], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyGreen], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyBlue]); + ob[keyLed][key_led_array][0][keyid], + ob[keyLed][key_led_array][0][keyRed], + ob[keyLed][key_led_array][0][keyGreen], + ob[keyLed][key_led_array][0][keyBlue]); } // turn on all LEDs // PYTHON: send_LEDMatrix_full(self, intensity = (255,255,255),timeout=1) else if (LEDArrMode == LedModes::full) { - u_int8_t r = (*WifiController::getJDoc())[keyLed][key_led_array][0][keyRed]; - u_int8_t g = (*WifiController::getJDoc())[keyLed][key_led_array][0][keyGreen]; - u_int8_t b = (*WifiController::getJDoc())[keyLed][key_led_array][0][keyBlue]; + u_int8_t r = ob[keyLed][key_led_array][0][keyRed]; + u_int8_t g = ob[keyLed][key_led_array][0][keyGreen]; + u_int8_t b = ob[keyLed][key_led_array][0][keyBlue]; isOn = r == 0 && g == 0 && b == 0 ? false : true; set_all(r, g, b); } @@ -93,36 +90,36 @@ void LedController::act() { set_left( ledconfig->ledCount, - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyRed], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyGreen], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyBlue]); + ob[keyLed][key_led_array][0][keyRed], + ob[keyLed][key_led_array][0][keyGreen], + ob[keyLed][key_led_array][0][keyBlue]); } // turn off all LEDs else if (LEDArrMode == LedModes::right) { set_right( ledconfig->ledCount, - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyRed], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyGreen], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyBlue]); + ob[keyLed][key_led_array][0][keyRed], + ob[keyLed][key_led_array][0][keyGreen], + ob[keyLed][key_led_array][0][keyBlue]); } // turn off all LEDs else if (LEDArrMode == LedModes::top) { set_top( ledconfig->ledCount, - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyRed], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyGreen], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyBlue]); + ob[keyLed][key_led_array][0][keyRed], + ob[keyLed][key_led_array][0][keyGreen], + ob[keyLed][key_led_array][0][keyBlue]); } // turn off all LEDs else if (LEDArrMode == LedModes::bottom) { set_bottom( ledconfig->ledCount, - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyRed], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyGreen], - (*WifiController::getJDoc())[keyLed][key_led_array][0][keyBlue]); + ob[keyLed][key_led_array][0][keyRed], + ob[keyLed][key_led_array][0][keyGreen], + ob[keyLed][key_led_array][0][keyBlue]); } else if (LEDArrMode == LedModes::off) { @@ -140,23 +137,22 @@ void LedController::act() //{"led":{"LEDArrMode":1,"led_array":[{"id":0,"blue":"128","red":"128","green":"128"}]}} //{"task" : "/ledarr_act", "led":{"LEDArrMode":1,"led_array":[{"id":0,"blue":"0","red":"0","green":"0"}]}} -void LedController::set() +void LedController::set(JsonObject ob) { - if (WifiController::getJDoc()->containsKey(keyLed)) + if (ob.containsKey(keyLed)) { - if ((*WifiController::getJDoc())[keyLed].containsKey(keyLEDPin)) - ledconfig->ledPin = (*WifiController::getJDoc())[keyLed][keyLEDPin]; - if ((*WifiController::getJDoc())[keyLed].containsKey(keyLEDCount)) - ledconfig->ledCount = (*WifiController::getJDoc())[keyLed][keyLEDCount]; + if (ob[keyLed].containsKey(keyLEDPin)) + ledconfig->ledPin = ob[keyLed][keyLEDPin]; + if (ob[keyLed].containsKey(keyLEDCount)) + ledconfig->ledCount = ob[keyLed][keyLEDCount]; log_i("led pin:%i count:%i", ledconfig->ledPin, ledconfig->ledCount); Config::setLedPins(ledconfig); setup(); } - WifiController::getJDoc()->clear(); } // Custom function accessible by the API -void LedController::get() +void LedController::get(JsonObject ob) { WifiController::getJDoc()->clear(); (*WifiController::getJDoc())[keyLEDCount] = ledconfig->ledCount; diff --git a/main/src/led/LedController.h b/main/src/led/LedController.h index 281cdf4f..9c0dc814 100644 --- a/main/src/led/LedController.h +++ b/main/src/led/LedController.h @@ -119,7 +119,7 @@ class LedController : public Module } } */ - void act() override; + void act(JsonObject ob) override; /*{ "led": { "ledArrNum": 64, @@ -127,8 +127,8 @@ class LedController : public Module } } */ - void set() override; - void get() override; + void set(JsonObject ob) override; + void get(JsonObject ob) override; void set_all(u_int8_t R, u_int8_t G, u_int8_t B); void set_center(u_int8_t R, u_int8_t G, u_int8_t B); }; diff --git a/main/src/motor/FocusMotor.cpp b/main/src/motor/FocusMotor.cpp index a6dcbb24..8f65eb1c 100644 --- a/main/src/motor/FocusMotor.cpp +++ b/main/src/motor/FocusMotor.cpp @@ -5,30 +5,26 @@ namespace RestApi { void FocusMotor_act() { - deserialize(); - moduleController.get(AvailableModules::motor)->act(); + moduleController.get(AvailableModules::motor)->act(deserialize()); serialize(); } void FocusMotor_get() { - deserialize(); - moduleController.get(AvailableModules::motor)->get(); + moduleController.get(AvailableModules::motor)->get(deserialize()); serialize(); } void FocusMotor_set() { - deserialize(); - moduleController.get(AvailableModules::motor)->set(); + moduleController.get(AvailableModules::motor)->set(deserialize()); serialize(); } void FocusMotor_setCalibration() { - deserialize(); FocusMotor *motor = (FocusMotor *)moduleController.get(AvailableModules::motor); - motor->setMinMaxRange(); + motor->setMinMaxRange(deserialize()); serialize(); } } @@ -36,35 +32,34 @@ namespace RestApi FocusMotor::FocusMotor() : Module() { log_i("ctor"); } FocusMotor::~FocusMotor() { log_i("~ctor"); } -void FocusMotor::act() +void FocusMotor::act(JsonObject doc) { if (DEBUG) Serial.println("motor_act_fct"); - DynamicJsonDocument *j = WifiController::getJDoc(); - if (j->containsKey(key_motor)) + if (doc.containsKey(key_motor)) { - if ((*j)[key_motor].containsKey(key_steppers)) + if (doc[key_motor].containsKey(key_steppers)) { - for (int i = 0; i < (*j)[key_motor][key_steppers].size(); i++) + for (int i = 0; i < doc[key_motor][key_steppers].size(); i++) { - Stepper s = static_cast((*j)[key_motor][key_steppers][i][key_stepperid]); + Stepper s = static_cast(doc[key_motor][key_steppers][i][key_stepperid]); - if ((*j)[key_motor][key_steppers][i].containsKey(key_speed)) - data[s]->speed = (*j)[key_motor][key_steppers][i][key_speed]; + if (doc[key_motor][key_steppers][i].containsKey(key_speed)) + data[s]->speed = doc[key_motor][key_steppers][i][key_speed]; - if ((*j)[key_motor][key_steppers][i].containsKey(key_position)) - data[s]->targetPosition = (*j)[key_motor][key_steppers][i][key_position]; + if (doc[key_motor][key_steppers][i].containsKey(key_position)) + data[s]->targetPosition = doc[key_motor][key_steppers][i][key_position]; - if ((*j)[key_motor][key_steppers][i].containsKey(key_isforever)) - data[s]->isforever = (*j)[key_motor][key_steppers][i][key_isforever]; + if (doc[key_motor][key_steppers][i].containsKey(key_isforever)) + data[s]->isforever = doc[key_motor][key_steppers][i][key_isforever]; - if ((*j)[key_motor][key_steppers][i].containsKey(key_isabs)) - data[s]->absolutePosition = (*j)[key_motor][key_steppers][i][key_isabs]; + if (doc[key_motor][key_steppers][i].containsKey(key_isabs)) + data[s]->absolutePosition = doc[key_motor][key_steppers][i][key_isabs]; - if ((*j)[key_motor][key_steppers][i].containsKey(key_isaccel)) - data[s]->isaccelerated = (*j)[key_motor][key_steppers][i][key_isaccel]; + if (doc[key_motor][key_steppers][i].containsKey(key_isaccel)) + data[s]->isaccelerated = doc[key_motor][key_steppers][i][key_isaccel]; - if ((*j)[key_motor][key_steppers][i].containsKey(key_isstop)) + if (doc[key_motor][key_steppers][i].containsKey(key_isstop)) stopStepper(s); else { @@ -120,45 +115,42 @@ void FocusMotor::startStepper(int i) pins[i]->current_position = steppers[i]->currentPosition(); } -void FocusMotor::set() +void FocusMotor::set(JsonObject doc) { - DynamicJsonDocument *doc = WifiController::getJDoc(); - if (doc->containsKey(key_motor)) + if (doc.containsKey(key_motor)) { - if ((*doc)[key_motor].containsKey(key_steppers)) + if (doc[key_motor].containsKey(key_steppers)) { - for (int i = 0; i < (*doc)[key_motor][key_steppers].size(); i++) + for (int i = 0; i < doc[key_motor][key_steppers].size(); i++) { - Stepper s = static_cast((*doc)[key_motor][key_steppers][i][key_stepperid]); - pins[s]->DIR = (*doc)[key_motor][key_steppers][i][key_dir]; - pins[s]->STEP = (*doc)[key_motor][key_steppers][i][key_step]; - pins[s]->ENABLE = (*doc)[key_motor][key_steppers][i][key_enable]; - pins[s]->direction_inverted = (*doc)[key_motor][key_steppers][i][key_dir_inverted]; - pins[s]->step_inverted = (*doc)[key_motor][key_steppers][i][key_step_inverted]; - pins[s]->enable_inverted = (*doc)[key_motor][key_steppers][i][key_enable_inverted]; + Stepper s = static_cast(doc[key_motor][key_steppers][i][key_stepperid]); + pins[s]->DIR = doc[key_motor][key_steppers][i][key_dir]; + pins[s]->STEP = doc[key_motor][key_steppers][i][key_step]; + pins[s]->ENABLE = doc[key_motor][key_steppers][i][key_enable]; + pins[s]->direction_inverted = doc[key_motor][key_steppers][i][key_dir_inverted]; + pins[s]->step_inverted = doc[key_motor][key_steppers][i][key_step_inverted]; + pins[s]->enable_inverted = doc[key_motor][key_steppers][i][key_enable_inverted]; } Config::setMotorPinConfig(pins); setup(); } } - doc->clear(); } -void FocusMotor::setMinMaxRange() +void FocusMotor::setMinMaxRange(JsonObject doc) { - DynamicJsonDocument *doc = WifiController::getJDoc(); - if (doc->containsKey(key_motor)) + if (doc.containsKey(key_motor)) { - if ((*doc)[key_motor].containsKey(key_steppers)) + if (doc[key_motor].containsKey(key_steppers)) { - for (int i = 0; i < (*doc)[key_motor][key_steppers].size(); i++) + for (int i = 0; i < doc[key_motor][key_steppers].size(); i++) { - Stepper s = static_cast((*doc)[key_motor][key_steppers][i][key_stepperid]); - if ((*doc)[key_motor][key_steppers][i].containsKey(key_min_position) && (*doc)[key_motor][key_steppers][i].containsKey(key_max_position)) + Stepper s = static_cast(doc[key_motor][key_steppers][i][key_stepperid]); + if (doc[key_motor][key_steppers][i].containsKey(key_min_position) && doc[key_motor][key_steppers][i].containsKey(key_max_position)) resetMotorPos(s); - else if ((*doc)[key_motor][key_steppers][i].containsKey(key_min_position)) + else if (doc[key_motor][key_steppers][i].containsKey(key_min_position)) applyMinPos(s); - else if ((*doc)[key_motor][key_steppers][i].containsKey(key_max_position)) + else if (doc[key_motor][key_steppers][i].containsKey(key_max_position)) applyMaxPos(s); } } @@ -188,7 +180,7 @@ void FocusMotor::applyMaxPos(int i) Config::setMotorPinConfig(pins); } -void FocusMotor::get() +void FocusMotor::get(JsonObject ob) { DynamicJsonDocument *doc = WifiController::getJDoc(); doc->clear(); diff --git a/main/src/motor/FocusMotor.h b/main/src/motor/FocusMotor.h index 6ee276d4..a1c2b150 100644 --- a/main/src/motor/FocusMotor.h +++ b/main/src/motor/FocusMotor.h @@ -79,9 +79,9 @@ class FocusMotor : public Module // std::array pins; MotorPins *pins[4]; - void setMinMaxRange(); - void act() override; - void set() override; + void setMinMaxRange(JsonObject ob); + void act(JsonObject ob) override; + void set(JsonObject ob) override; /* returns { @@ -137,7 +137,7 @@ class FocusMotor : public Module ] } */ - void get() override; + void get(JsonObject ob) override; void setup() override; void loop() override; void stopAllDrives(); diff --git a/main/src/motor/MotorPins.h b/main/src/motor/MotorPins.h index c7676f34..7f7e2734 100644 --- a/main/src/motor/MotorPins.h +++ b/main/src/motor/MotorPins.h @@ -7,7 +7,7 @@ struct MotorPins bool step_inverted = false; bool direction_inverted = false; bool enable_inverted = false; - int min_position = -100000; // some very low number by default - int max_position = 100000; // some very large number by default + int min_position = 0; + int max_position = 0; int current_position =0; }; \ No newline at end of file diff --git a/main/src/pid/PidController.cpp b/main/src/pid/PidController.cpp index 8c9b0296..98a84bf7 100644 --- a/main/src/pid/PidController.cpp +++ b/main/src/pid/PidController.cpp @@ -5,22 +5,19 @@ namespace RestApi { void Pid_act() { - deserialize(); - moduleController.get(AvailableModules::pid)->act(); + moduleController.get(AvailableModules::pid)->act(deserialize()); serialize(); } void Pid_get() { - deserialize(); - moduleController.get(AvailableModules::pid)->get(); + moduleController.get(AvailableModules::pid)->get(deserialize()); serialize(); } void Pid_set() { - deserialize(); - moduleController.get(AvailableModules::pid)->set(); + moduleController.get(AvailableModules::pid)->set(deserialize()); serialize(); } } @@ -29,25 +26,25 @@ PidController::PidController(/* args */){}; PidController::~PidController(){}; // Custom function accessible by the API -void PidController::act() +void PidController::act(JsonObject ob) { // here you can do something if (DEBUG) Serial.println("PID_act_fct"); - if (WifiController::getJDoc()->containsKey("PIDactive")) - PID_active = (int)(*WifiController::getJDoc())["PIDactive"]; - if (WifiController::getJDoc()->containsKey("Kp")) - PID_Kp = (*WifiController::getJDoc())["Kp"]; - if ((*WifiController::getJDoc()).containsKey("Ki")) - PID_Ki = (*WifiController::getJDoc())["Ki"]; - if (WifiController::getJDoc()->containsKey("Kd")) - PID_Kd = (*WifiController::getJDoc())["Kd"]; - if (WifiController::getJDoc()->containsKey("target")) - PID_target = (*WifiController::getJDoc())["target"]; - if (WifiController::getJDoc()->containsKey("PID_updaterate")) - PID_updaterate = (int)(*WifiController::getJDoc())["PID_updaterate"]; + if (ob.containsKey("PIDactive")) + PID_active = (int)(ob)["PIDactive"]; + if (ob.containsKey("Kp")) + PID_Kp = (ob)["Kp"]; + if ((ob).containsKey("Ki")) + PID_Ki = (ob)["Ki"]; + if (ob.containsKey("Kd")) + PID_Kd = (ob)["Kd"]; + if (ob.containsKey("target")) + PID_target = (ob)["target"]; + if (ob.containsKey("PID_updaterate")) + PID_updaterate = (int)(ob)["PID_updaterate"]; if (!PID_active) { @@ -129,14 +126,14 @@ long PidController::returnControlValue(float controlTarget, float analoginValue, return stepperOut; } -void PidController::set() +void PidController::set(JsonObject ob) { if (DEBUG) Serial.println("PID_set_fct"); - int PIDID = (int)(*WifiController::getJDoc())["PIDID"]; - int PIDPIN = (int)(*WifiController::getJDoc())["PIDPIN"]; - if (WifiController::getJDoc()->containsKey("N_analogin_avg")) - N_analogin_avg = (int)(*WifiController::getJDoc())["N_analogin_avg"]; + int PIDID = (int)(ob)["PIDID"]; + int PIDPIN = (int)(ob)["PIDPIN"]; + if (ob.containsKey("N_analogin_avg")) + N_analogin_avg = (int)(ob)["N_analogin_avg"]; switch (PIDID) { @@ -150,14 +147,10 @@ void PidController::set() pins.analogin_PIN_2 = PIDPIN; break; } - - WifiController::getJDoc()->clear(); - (*WifiController::getJDoc())["PIDPIN"] = PIDPIN; - (*WifiController::getJDoc())["PIDID"] = PIDID; } // Custom function accessible by the API -void PidController::get() +void PidController::get(JsonObject ob) { if (DEBUG) Serial.println("PID_get_fct"); diff --git a/main/src/pid/PidController.h b/main/src/pid/PidController.h index a90a8d78..03117102 100644 --- a/main/src/pid/PidController.h +++ b/main/src/pid/PidController.h @@ -38,7 +38,7 @@ class PidController : public Module void setup() override; void loop() override; - void act() override; - void get() override; - void set() override; + void act(JsonObject ob) override; + void get(JsonObject ob) override; + void set(JsonObject ob) override; }; diff --git a/main/src/scanner/ScannerController.cpp b/main/src/scanner/ScannerController.cpp index b9f72675..2091491a 100644 --- a/main/src/scanner/ScannerController.cpp +++ b/main/src/scanner/ScannerController.cpp @@ -85,16 +85,15 @@ void ScannerController::loop() } // Custom function accessible by the API -void ScannerController::act() +void ScannerController::act(JsonObject ob) { - DynamicJsonDocument *jsonDocument = WifiController::getJDoc(); // here you can do something if (DEBUG) Serial.println("scanner_act_fct"); // select scanning mode - const char *scannerMode = (*jsonDocument)["scannerMode"]; + const char *scannerMode = (ob)["scannerMode"]; if (strcmp(scannerMode, "pattern") == 0) { @@ -107,32 +106,32 @@ void ScannerController::act() scannernFrames = 1; scannerDelay = 0; - if (jsonDocument->containsKey("scannerExposure")) + if (ob.containsKey("scannerExposure")) { - scannerExposure = (*jsonDocument)["scannerExposure"]; + scannerExposure = ob["scannerExposure"]; } - if (jsonDocument->containsKey("scannerLaserVal")) + if (ob.containsKey("scannerLaserVal")) { - scannerLaserVal = (*jsonDocument)["scannerLaserVal"]; + scannerLaserVal = ob["scannerLaserVal"]; } - if (jsonDocument->containsKey("scannerDelay")) + if (ob.containsKey("scannerDelay")) { - scannerDelay = (*jsonDocument)["scannerDelay"]; + scannerDelay = ob["scannerDelay"]; } - if (jsonDocument->containsKey("scannernFrames")) + if (ob.containsKey("scannernFrames")) { - scannernFrames = (*jsonDocument)["scannernFrames"]; + scannernFrames = ob["scannernFrames"]; } - if (jsonDocument->containsKey("arraySize")) + if (ob.containsKey("arraySize")) { - arraySize = (*jsonDocument)["arraySize"]; + arraySize = ob["arraySize"]; } for (int iFrame = 0; iFrame < scannernFrames; iFrame++) { for (int i = 0; i < arraySize; i++) { // Iterate through results - int scannerIndex = (*jsonDocument)["i"][i]; // Implicit cast + int scannerIndex = ob["i"][i]; // Implicit cast int scannerPosY = scannerIndex % 255; int scannerPosX = scannerIndex / 255; dacWrite(scannerPinY, scannerPosY); @@ -183,65 +182,65 @@ void ScannerController::act() scannerDelay = 0; - if (jsonDocument->containsKey("scannernFrames")) + if (ob.containsKey("scannernFrames")) { - scannernFrames = (*jsonDocument)["scannernFrames"]; + scannernFrames = ob["scannernFrames"]; } - if (jsonDocument->containsKey("scannerXFrameMax")) + if (ob.containsKey("scannerXFrameMax")) { - scannerXFrameMax = (*jsonDocument)["scannerXFrameMax"]; + scannerXFrameMax = ob["scannerXFrameMax"]; } - if (jsonDocument->containsKey("scannerXFrameMin")) + if (ob.containsKey("scannerXFrameMin")) { - scannerXFrameMin = (*jsonDocument)["scannerXFrameMin"]; + scannerXFrameMin = ob["scannerXFrameMin"]; } - if (jsonDocument->containsKey("scannerYFrameMax")) + if (ob.containsKey("scannerYFrameMax")) { - scannerYFrameMax = (*jsonDocument)["scannerYFrameMax"]; + scannerYFrameMax = ob["scannerYFrameMax"]; } - if (jsonDocument->containsKey("scannerYFrameMin")) + if (ob.containsKey("scannerYFrameMin")) { - scannerYFrameMin = (*jsonDocument)["scannerYFrameMin"]; + scannerYFrameMin = ob["scannerYFrameMin"]; } - if (jsonDocument->containsKey("scannerXStep")) + if (ob.containsKey("scannerXStep")) { - scannerXStep = (*jsonDocument)["scannerXStep"]; + scannerXStep = ob["scannerXStep"]; } - if (jsonDocument->containsKey("scannerYStep")) + if (ob.containsKey("scannerYStep")) { - scannerYStep = (*jsonDocument)["scannerYStep"]; + scannerYStep = ob["scannerYStep"]; } - if (jsonDocument->containsKey("scannerxMin")) + if (ob.containsKey("scannerxMin")) { - scannerxMin = (*jsonDocument)["scannerxMin"]; + scannerxMin = ob["scannerxMin"]; } - if (jsonDocument->containsKey("scannerLaserVal")) + if (ob.containsKey("scannerLaserVal")) { - scannerLaserVal = (*jsonDocument)["scannerLaserVal"]; + scannerLaserVal = ob["scannerLaserVal"]; } - if (jsonDocument->containsKey("scanneryMin")) + if (ob.containsKey("scanneryMin")) { - scanneryMin = (*jsonDocument)["scanneryMin"]; + scanneryMin = ob["scanneryMin"]; } - if (jsonDocument->containsKey("scannerxMax")) + if (ob.containsKey("scannerxMax")) { - scannerxMax = (*jsonDocument)["scannerxMax"]; + scannerxMax = ob["scannerxMax"]; } - if (jsonDocument->containsKey("scanneryMax")) + if (ob.containsKey("scanneryMax")) { - scanneryMax = (*jsonDocument)["scanneryMax"]; + scanneryMax = ob["scanneryMax"]; } - if (jsonDocument->containsKey("scannerExposure")) + if (ob.containsKey("scannerExposure")) { - scannerExposure = (*jsonDocument)["scannerExposure"]; + scannerExposure = ob["scannerExposure"]; } - if (jsonDocument->containsKey("scannerEnable")) + if (ob.containsKey("scannerEnable")) { - scannerEnable = (*jsonDocument)["scannerEnable"]; + scannerEnable = ob["scannerEnable"]; } - if (jsonDocument->containsKey("scannerDelay")) + if (ob.containsKey("scannerDelay")) { - scannerDelay = (*jsonDocument)["scannerDelay"]; + scannerDelay = ob["scannerDelay"]; } if (DEBUG) @@ -290,22 +289,20 @@ void ScannerController::act() Serial.print("scannerDelay "); Serial.println(scannerDelay); - jsonDocument->clear(); if (DEBUG) Serial.println("Start controlGalvoTask"); isScanRunning = scannerEnable; // Trigger a frame acquisition if (DEBUG) Serial.println("Done with setting up Tasks"); - (*jsonDocument)["return"] = 1; } } -void ScannerController::set() +void ScannerController::set(JsonObject ob) { } // Custom function accessible by the API -void ScannerController::get() +void ScannerController::get(JsonObject ob) { } diff --git a/main/src/scanner/ScannerController.h b/main/src/scanner/ScannerController.h index 87c4694a..33cc99ad 100644 --- a/main/src/scanner/ScannerController.h +++ b/main/src/scanner/ScannerController.h @@ -42,9 +42,9 @@ class ScannerController : public Module int scannernFrames = 1; - void act() override; - void get() override; - void set() override; + void act(JsonObject ob) override; + void get(JsonObject ob) override; + void set(JsonObject ob) override; void setup() override; void loop() override; static void controlGalvoTask(void *parameters); diff --git a/main/src/serial/SerialProcess.cpp b/main/src/serial/SerialProcess.cpp index 35adaaa4..359700b6 100644 --- a/main/src/serial/SerialProcess.cpp +++ b/main/src/serial/SerialProcess.cpp @@ -25,34 +25,33 @@ void SerialProcess::loop() return; } Serial.flush(); - - String task_s = (*jsonDocument)["task"]; - char task[50]; - task_s.toCharArray(task, 256); - -// jsonDocument.garbageCollect(); // memory leak? -/*if (task == "null") return;*/ -#ifdef DEBUG_MAIN - Serial.print("TASK: "); - Serial.println(task); -#endif - - // do the processing based on the incoming stream - if (strcmp(task, "multitable") == 0) + if((*jsonDocument).containsKey("tasks")) { - // multiple tasks - tableProcessor(jsonDocument); + log_i("task to process:%i", (*jsonDocument)["tasks"].size()); + for(int i =0; i < (*jsonDocument)["tasks"].size(); i++) + { + String task_s = (*jsonDocument)["tasks"][i]["task"]; + JsonObject doc = (*jsonDocument)["tasks"][i].as(); + jsonProcessor(task_s, doc); + } } else { - // Process individual tasks - jsonProcessor(task, jsonDocument); + log_i("process singel task"); + String task_s = (*jsonDocument)["task"]; + JsonObject ob = (*jsonDocument).as(); + jsonProcessor(task_s, ob); } } } -void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument) +void SerialProcess::jsonProcessor(String task, JsonObject jsonDocument) { + + if(task == modules_set_endpoint) + moduleController.set(jsonDocument); + if(task == modules_get_endpoint) + moduleController.get(); /* Return state */ @@ -69,15 +68,15 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument { if (task == motor_act_endpoint) { - moduleController.get(AvailableModules::motor)->act(); + moduleController.get(AvailableModules::motor)->act(jsonDocument); } if (task == motor_set_endpoint) { - moduleController.get(AvailableModules::motor)->set(); + moduleController.get(AvailableModules::motor)->set(jsonDocument); } if (task == motor_get_endpoint) { - moduleController.get(AvailableModules::motor)->get(); + moduleController.get(AvailableModules::motor)->get(jsonDocument); } } /* @@ -87,15 +86,15 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument { if (task == home_act_endpoint) { - moduleController.get(AvailableModules::home)->act(); + moduleController.get(AvailableModules::home)->act(jsonDocument); } if (task == home_set_endpoint) { - moduleController.get(AvailableModules::home)->set(); + moduleController.get(AvailableModules::home)->set(jsonDocument); } if (task == home_get_endpoint) { - moduleController.get(AvailableModules::home)->get(); + moduleController.get(AvailableModules::home)->get(jsonDocument); } } @@ -107,15 +106,15 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument { if (task == slm_act_endpoint) { - moduleController.get(AvailableModules::slm)->act(); + moduleController.get(AvailableModules::slm)->act(jsonDocument); } if (task == slm_set_endpoint) { - moduleController.get(AvailableModules::slm)->set(); + moduleController.get(AvailableModules::slm)->set(jsonDocument); } if (task == slm_get_endpoint) { - moduleController.get(AvailableModules::slm)->get(); + moduleController.get(AvailableModules::slm)->get(jsonDocument); } } /* @@ -124,11 +123,11 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument if (moduleController.get(AvailableModules::dac) != nullptr) { if (task == dac_act_endpoint) - moduleController.get(AvailableModules::dac)->act(); + moduleController.get(AvailableModules::dac)->act(jsonDocument); if (task == dac_set_endpoint) - moduleController.get(AvailableModules::dac)->set(); + moduleController.get(AvailableModules::dac)->set(jsonDocument); if (task == dac_get_endpoint) - moduleController.get(AvailableModules::dac)->get(); + moduleController.get(AvailableModules::dac)->get(jsonDocument); } /* Drive Laser @@ -136,11 +135,11 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument if (moduleController.get(AvailableModules::laser) != nullptr) { if (task == laser_act_endpoint) - moduleController.get(AvailableModules::laser)->act(); + moduleController.get(AvailableModules::laser)->act(jsonDocument); if (task == laser_set_endpoint) - moduleController.get(AvailableModules::laser)->set(); + moduleController.get(AvailableModules::laser)->set(jsonDocument); if (task == laser_get_endpoint) - moduleController.get(AvailableModules::laser)->get(); + moduleController.get(AvailableModules::laser)->get(jsonDocument); } /* Drive analogout @@ -148,11 +147,11 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument if (moduleController.get(AvailableModules::analogout) != nullptr) { if (task == analogout_act_endpoint) - moduleController.get(AvailableModules::analogout)->act(); + moduleController.get(AvailableModules::analogout)->act(jsonDocument); if (task == analogout_set_endpoint) - moduleController.get(AvailableModules::analogout)->set(); + moduleController.get(AvailableModules::analogout)->set(jsonDocument); if (task == analogout_get_endpoint) - moduleController.get(AvailableModules::analogout)->get(); + moduleController.get(AvailableModules::analogout)->get(jsonDocument); } /* Drive digitalout @@ -160,11 +159,11 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument if (moduleController.get(AvailableModules::digitalout) != nullptr) { if (task == digitalout_act_endpoint) - moduleController.get(AvailableModules::digitalout)->act(); + moduleController.get(AvailableModules::digitalout)->act(jsonDocument); if (task == digitalout_set_endpoint) - moduleController.get(AvailableModules::digitalout)->set(); + moduleController.get(AvailableModules::digitalout)->set(jsonDocument); if (task == digitalout_get_endpoint) - moduleController.get(AvailableModules::digitalout)->get(); + moduleController.get(AvailableModules::digitalout)->get(jsonDocument); } /* Drive digitalin @@ -172,11 +171,11 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument if (moduleController.get(AvailableModules::digitalin) != nullptr) { if (task == digitalin_act_endpoint) - moduleController.get(AvailableModules::digitalin)->act(); + moduleController.get(AvailableModules::digitalin)->act(jsonDocument); if (task == digitalin_set_endpoint) - moduleController.get(AvailableModules::digitalin)->set(); + moduleController.get(AvailableModules::digitalin)->set(jsonDocument); if (task == digitalin_get_endpoint) - moduleController.get(AvailableModules::digitalin)->get(); + moduleController.get(AvailableModules::digitalin)->get(jsonDocument); } /* Drive LED Matrix @@ -184,11 +183,11 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument if (moduleController.get(AvailableModules::led) != nullptr) { if (task == ledarr_act_endpoint) - moduleController.get(AvailableModules::led)->act(); + moduleController.get(AvailableModules::led)->act(jsonDocument); if (task == ledarr_set_endpoint) - moduleController.get(AvailableModules::led)->set(); + moduleController.get(AvailableModules::led)->set(jsonDocument); if (task == ledarr_get_endpoint) - moduleController.get(AvailableModules::led)->get(); + moduleController.get(AvailableModules::led)->get(jsonDocument); } /* @@ -197,11 +196,11 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument if (moduleController.get(AvailableModules::analogin) != nullptr) { if (task == readanalogin_act_endpoint) - moduleController.get(AvailableModules::analogin)->act(); + moduleController.get(AvailableModules::analogin)->act(jsonDocument); if (task == readanalogin_set_endpoint) - moduleController.get(AvailableModules::analogin)->set(); + moduleController.get(AvailableModules::analogin)->set(jsonDocument); if (task == readanalogin_get_endpoint) - moduleController.get(AvailableModules::analogin)->get(); + moduleController.get(AvailableModules::analogin)->get(jsonDocument); } /* @@ -210,19 +209,19 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument if (moduleController.get(AvailableModules::pid) != nullptr) { if (task == PID_act_endpoint) - moduleController.get(AvailableModules::pid)->act(); + moduleController.get(AvailableModules::pid)->act(jsonDocument); if (task == PID_set_endpoint) - moduleController.get(AvailableModules::pid)->set(); + moduleController.get(AvailableModules::pid)->set(jsonDocument); if (task == PID_get_endpoint) - moduleController.get(AvailableModules::pid)->get(); + moduleController.get(AvailableModules::pid)->get(jsonDocument); } if(moduleController.get(AvailableModules::analogJoystick) != nullptr) { if(task ==analog_joystick_set_endpoint) - moduleController.get(AvailableModules::analogJoystick)->set(); + moduleController.get(AvailableModules::analogJoystick)->set(jsonDocument); if(task ==analog_joystick_get_endpoint) - moduleController.get(AvailableModules::analogJoystick)->get(); + moduleController.get(AvailableModules::analogJoystick)->get(jsonDocument); } if (task == scanwifi_endpoint) @@ -231,66 +230,19 @@ void SerialProcess::jsonProcessor(String task, DynamicJsonDocument *jsonDocument } if (task == connectwifi_endpoint) { - WifiController::connect(); + WifiController::connect(jsonDocument); } if (task == reset_nv_flash_endpoint) { RestApi::resetNvFLash(); } - if(task == modules_set_endpoint) - moduleController.set(); - if(task == modules_get_endpoint) - moduleController.get(); + Serial.println(jsonDocument); + // Send JSON information back Serial.println("++"); - serializeJson((*jsonDocument), Serial); - Serial.println(); + Serial.println(task); Serial.println("--"); - jsonDocument->clear(); - jsonDocument->garbageCollect(); -} - -void SerialProcess::tableProcessor(DynamicJsonDocument *jsonDocument) -{ - // 1. Copy the table - DynamicJsonDocument tmpJsonDoc = (*jsonDocument); - jsonDocument->clear(); - - // 2. now we need to extract the indidvidual tasks - int N_tasks = tmpJsonDoc["task_n"]; - int N_repeats = tmpJsonDoc["repeats_n"]; - - Serial.println("N_tasks"); - Serial.println(N_tasks); - Serial.println("N_repeats"); - Serial.println(N_repeats); - - for (int irepeats = 0; irepeats < N_repeats; irepeats++) - { - for (int itask = 0; itask < N_tasks; itask++) - { - char json_string[256]; - // Hacky, but should work - Serial.println(itask); - serializeJson(tmpJsonDoc[String(itask)], json_string); - Serial.println(json_string); - deserializeJson((*jsonDocument), json_string); - - String task_s = (*jsonDocument)["task"]; - char task[50]; - task_s.toCharArray(task, 256); - -// jsonDocument.garbageCollect(); // memory leak? -/*if (task == "null") return;*/ -#ifdef DEBUG_MAIN - Serial.print("TASK: "); - Serial.println(task); -#endif - jsonProcessor(task, jsonDocument); - } - } - tmpJsonDoc.clear(); } SerialProcess serial; \ No newline at end of file diff --git a/main/src/serial/SerialProcess.h b/main/src/serial/SerialProcess.h index 0a9ab576..95dec123 100644 --- a/main/src/serial/SerialProcess.h +++ b/main/src/serial/SerialProcess.h @@ -13,8 +13,7 @@ class SerialProcess { private: - void jsonProcessor(String task,DynamicJsonDocument * jsonDocument); - void tableProcessor(DynamicJsonDocument * jsonDocument); + void jsonProcessor(String task,JsonObject jsonDocument); /* data */ public: SerialProcess(/* args */); diff --git a/main/src/slm/SlmController.cpp b/main/src/slm/SlmController.cpp index 00faff5c..2428fe74 100644 --- a/main/src/slm/SlmController.cpp +++ b/main/src/slm/SlmController.cpp @@ -5,22 +5,19 @@ namespace RestApi { void Slm_act() { - deserialize(); - moduleController.get(AvailableModules::slm)->act(); + moduleController.get(AvailableModules::slm)->act(deserialize()); serialize(); } void Slm_get() { - deserialize(); - moduleController.get(AvailableModules::slm)->get(); + moduleController.get(AvailableModules::slm)->get(deserialize()); serialize(); } void Slm_set() { - deserialize(); - moduleController.get(AvailableModules::slm)->set(); + moduleController.get(AvailableModules::slm)->set(deserialize()); serialize(); } } @@ -31,7 +28,7 @@ SlmController::~SlmController(){}; void SlmController::loop(){} // Custom function accessible by the API -void SlmController::act() +void SlmController::act(JsonObject ob) { // here you can do something @@ -39,7 +36,7 @@ void SlmController::act() Serial.println("slm_act_fct"); isBusy = true; - const char *slmMode = (*WifiController::getJDoc())["slmMode"]; // "ring", "clear" + const char *slmMode = ob["slmMode"]; // "ring", "clear" // individual pattern gets adressed // PYTHON: send_LEDMatrix_array(self, led_pattern, timeout=1) @@ -52,14 +49,14 @@ void SlmController::act() int radius = 0; uint16_t color = 0; - if (WifiController::getJDoc()->containsKey("posX")) - posX = (*WifiController::getJDoc())["posX"]; - if (WifiController::getJDoc()->containsKey("posY")) - posY = (*WifiController::getJDoc())["posY"]; - if (WifiController::getJDoc()->containsKey("radius")) - radius = (*WifiController::getJDoc())["radius"]; - if (WifiController::getJDoc()->containsKey("color")) - color = (*WifiController::getJDoc())["color"]; + if (ob.containsKey("posX")) + posX = ob["posX"]; + if (ob.containsKey("posY")) + posY = ob["posY"]; + if (ob.containsKey("radius")) + radius = ob["radius"]; + if (ob.containsKey("color")) + color = ob["color"]; tft.fillCircle(posX, posY, radius, color); } // PYTHON: send_LEDMatrix_array(self, led_pattern, timeout=1) @@ -73,16 +70,16 @@ void SlmController::act() int nY = 0; uint16_t color = 0; - if (WifiController::getJDoc()->containsKey("posX")) - posX = (*WifiController::getJDoc())["posX"]; - if (WifiController::getJDoc()->containsKey("posY")) - posY = (*WifiController::getJDoc())["posY"]; - if (WifiController::getJDoc()->containsKey("nX")) - nX = (*WifiController::getJDoc())["nX"]; - if (WifiController::getJDoc()->containsKey("nY")) - nY = (*WifiController::getJDoc())["nY"]; - if (WifiController::getJDoc()->containsKey("color")) - color = (*WifiController::getJDoc())["color"]; + if (ob.containsKey("posX")) + posX = ob["posX"]; + if (ob.containsKey("posY")) + posY = ob["posY"]; + if (ob.containsKey("nX")) + nX = ob["nX"]; + if (ob.containsKey("nY")) + nY = ob["nY"]; + if (ob.containsKey("color")) + color = ob["color"]; tft.drawRect(posX, posY, nX, nY, color); } @@ -90,7 +87,7 @@ void SlmController::act() { if (DEBUG) Serial.println("full"); - uint16_t color = (*WifiController::getJDoc())["color"]; + uint16_t color = ob["color"]; tft.fillScreen(color); } if (strcmp(slmMode, "clear")) @@ -105,10 +102,10 @@ void SlmController::act() { if (DEBUG) Serial.println("image"); - int startX = (*WifiController::getJDoc())["startX"]; - int startY = (*WifiController::getJDoc())["startY"]; - int endX = (*WifiController::getJDoc())["endX"]; - int endY = (*WifiController::getJDoc())["endY"]; + int startX = ob["startX"]; + int startY = ob["startY"]; + int endX = ob["endX"]; + int endY = ob["endY"]; int nX = endX - startX; int nY = endY - startY; @@ -116,7 +113,7 @@ void SlmController::act() { for (int iy = 0; iy < nY; iy++) { - uint16_t color = (*WifiController::getJDoc())["color"][ix * (nX) + iy]; // Implicit cast + uint16_t color = ob["color"][ix * (nX) + iy]; // Implicit cast Serial.println(color); tft.drawPixel(iy + nY, ix + nX, color); } @@ -128,30 +125,28 @@ void SlmController::act() isBusy = false; } -void SlmController::set() +void SlmController::set(JsonObject ob) { /* - if (WifiController::getJDoc()["LED_ARRAY_PIN"] != 0) { - //if (DEBUG) Serial.print("LED_ARRAY_PIN "); Serial.println(WifiController::getJDoc()["LED_ARRAY_PIN"]); - LED_ARRAY_PIN = WifiController::getJDoc()["LED_ARRAY_PIN"]; + if (ob["LED_ARRAY_PIN"] != 0) { + //if (DEBUG) Serial.print("LED_ARRAY_PIN "); Serial.println(ob["LED_ARRAY_PIN"]); + LED_ARRAY_PIN = ob["LED_ARRAY_PIN"]; } - if (WifiController::getJDoc()["LED_N_X"] != 0) { - //if (DEBUG) Serial.print("LED_N_X "); Serial.println(WifiController::getJDoc()["LED_N_X"]); - LED_N_X = WifiController::getJDoc()["LED_N_X"]; + if (ob["LED_N_X"] != 0) { + //if (DEBUG) Serial.print("LED_N_X "); Serial.println(ob["LED_N_X"]); + LED_N_X = ob["LED_N_X"]; } - if (WifiController::getJDoc()["LED_N_Y"] != 0) { - //if (DEBUG) Serial.print("LED_N_Y "); Serial.println(WifiController::getJDoc()["LED_N_Y"]); - LED_N_Y = WifiController::getJDoc()["LED_N_Y"]; + if (ob["LED_N_Y"] != 0) { + //if (DEBUG) Serial.print("LED_N_Y "); Serial.println(ob["LED_N_Y"]); + LED_N_Y = ob["LED_N_Y"]; } */ - (*WifiController::getJDoc()).clear(); - (*WifiController::getJDoc())["return"] = 1; } // Custom function accessible by the API -void SlmController::get() +void SlmController::get(JsonObject ob) { /* WifiController::getJDoc().clear(); diff --git a/main/src/slm/SlmController.h b/main/src/slm/SlmController.h index 0128aa31..cade8c5a 100644 --- a/main/src/slm/SlmController.h +++ b/main/src/slm/SlmController.h @@ -59,9 +59,9 @@ class SlmController : public Module bool DEBUG = false; bool isBusy; - void act() override; - void set() override; - void get() override; + void act(JsonObject job) override; + void set(JsonObject job) override; + void get(JsonObject job) override; void setup() override; void loop() override; diff --git a/main/src/wifi/RestApiCallbacks.cpp b/main/src/wifi/RestApiCallbacks.cpp index 7df16467..25ccb1cf 100644 --- a/main/src/wifi/RestApiCallbacks.cpp +++ b/main/src/wifi/RestApiCallbacks.cpp @@ -35,9 +35,10 @@ namespace RestApi (*WifiController::getServer()).send(404, "text/plain", message); } - void deserialize() + JsonObject deserialize() { deserializeJson(*WifiController::getJDoc(), WifiController::getServer()->arg("plain")); + return WifiController::getJDoc()->as(); // serializeJsonPretty((*WifiController::getJDoc()), Serial); } diff --git a/main/src/wifi/RestApiCallbacks.h b/main/src/wifi/RestApiCallbacks.h index e245cd64..85a6deca 100644 --- a/main/src/wifi/RestApiCallbacks.h +++ b/main/src/wifi/RestApiCallbacks.h @@ -26,7 +26,7 @@ namespace RestApi /* load the body data from the client request into the jsondoc */ - void deserialize(); + JsonObject deserialize(); /* fill the input from the jsondoc and send a response to the client */ diff --git a/main/src/wifi/WifiController.cpp b/main/src/wifi/WifiController.cpp index e04c77ff..18d4f57a 100644 --- a/main/src/wifi/WifiController.cpp +++ b/main/src/wifi/WifiController.cpp @@ -22,8 +22,7 @@ namespace RestApi void connectToWifi() { - deserialize(); - WifiController::connect(); + WifiController::connect(deserialize()); serialize(); // ESP.restart(); } @@ -68,13 +67,13 @@ namespace WifiController server->handleClient(); } - void connect() + void connect(JsonObject j) { log_i("connectToWifi"); - bool ap = (*WifiController::getJDoc())[keyWifiAP]; - String ssid = (*WifiController::getJDoc())[keyWifiSSID]; - String pw = (*WifiController::getJDoc())[keyWifiPW]; + bool ap = j[keyWifiAP]; + String ssid = j[keyWifiSSID]; + String pw = j[keyWifiPW]; log_i("ssid: %s wifi:%s", ssid.c_str(), WifiController::getSsid().c_str()); log_i("pw: %s wifi:%s", pw.c_str(), WifiController::getPw().c_str()); log_i("ap: %s wifi:%s", boolToChar(ap), boolToChar(WifiController::getAp())); @@ -82,7 +81,6 @@ namespace WifiController log_i("ssid json: %s wifi:%s", ssid, WifiController::getSsid()); log_i("pw json: %s wifi:%s", pw, WifiController::getPw()); log_i("ap json: %s wifi:%s", boolToChar(ap), boolToChar(WifiController::getAp())); - WifiController::getJDoc()->clear(); WifiController::setup(); WifiController::begin(); } @@ -105,9 +103,9 @@ namespace WifiController log_i("[%u] get Text: %s\n", num, payload); deserializeJson(*WifiController::getJDoc(), payload); if (WifiController::getJDoc()->containsKey(keyLed) && moduleController.get(AvailableModules::led) != nullptr) - moduleController.get(AvailableModules::led)->act(); + moduleController.get(AvailableModules::led)->act(WifiController::getJDoc()->as()); if (WifiController::getJDoc()->containsKey(key_motor) && moduleController.get(AvailableModules::motor) != nullptr) - moduleController.get(AvailableModules::motor)->act(); + moduleController.get(AvailableModules::motor)->act(WifiController::getJDoc()->as()); break; default: diff --git a/main/src/wifi/WifiController.h b/main/src/wifi/WifiController.h index b16523aa..67e5db48 100644 --- a/main/src/wifi/WifiController.h +++ b/main/src/wifi/WifiController.h @@ -74,5 +74,5 @@ namespace WifiController void getCSS(); void getjquery(); void getjs(); - void connect(); + void connect(JsonObject j); }