From f96aa7639ccadda85dd84dd65a39dc0916b641a4 Mon Sep 17 00:00:00 2001 From: Matti Airas Date: Wed, 2 Oct 2024 22:43:53 +0300 Subject: [PATCH] Fix event loop calls --- README.md | 2 +- examples/analog_input.cpp | 2 +- examples/async_repeat_sensor.cpp | 4 ++-- examples/chain_counter.cpp | 2 +- examples/constant_sensor.cpp | 2 +- .../max6675_signalk.cpp | 2 +- .../max6675_signalk_n2k.cpp | 2 +- examples/freertos_tasks.cpp | 4 ++-- .../fuel_level_sensor_example.cpp | 2 +- examples/hysteresis.cpp | 2 +- examples/join_and_zip.cpp | 2 +- examples/lambda_transform.cpp | 2 +- examples/manual_networking.cpp | 2 +- .../milone_level_sensor.cpp | 2 +- examples/minimal_app.cpp | 6 +++--- examples/raw_json.cpp | 4 ++-- examples/relay_control.cpp | 2 +- examples/repeat_sensor_analog_input.cpp | 2 +- examples/repeat_transform.cpp | 2 +- examples/rpm_counter.cpp | 2 +- .../smart_switch/remote_switch_example.cpp | 2 +- examples/smart_switch/smart_switch_example.cpp | 2 +- examples/temperature_sender.cpp | 2 +- examples/time_counter.cpp | 4 ++-- .../controllers/smart_switch_controller.cpp | 2 +- src/sensesp/net/discovery.cpp | 2 +- src/sensesp/net/http_server.h | 2 +- src/sensesp/net/networking.cpp | 4 ++-- src/sensesp/net/networking.h | 4 ++-- src/sensesp/net/ota.h | 4 ++-- src/sensesp/net/web/base_command_handler.cpp | 4 ++-- src/sensesp/sensors/analog_input.cpp | 2 +- src/sensesp/sensors/constant_sensor.h | 7 ++++--- src/sensesp/sensors/digital_input.h | 10 +++++----- src/sensesp/sensors/system_info.h | 12 ++++++------ src/sensesp/signalk/signalk_delta_queue.cpp | 2 +- src/sensesp/signalk/signalk_put_request.cpp | 4 ++-- src/sensesp/signalk/signalk_ws_client.cpp | 4 ++-- src/sensesp/signalk/signalk_ws_client.h | 4 ++-- src/sensesp/system/async_response_handler.h | 6 +++--- src/sensesp/system/base_button.h | 2 +- src/sensesp/system/led_blinker.cpp | 16 ++++++++-------- src/sensesp/system/stream_producer.h | 4 ++-- src/sensesp/system/task_queue_producer.h | 4 ++-- src/sensesp/transforms/click_type.cpp | 6 +++--- src/sensesp/transforms/debounce.h | 6 +++--- src/sensesp/transforms/press_repeater.cpp | 2 +- src/sensesp/transforms/repeat.h | 18 +++++++++--------- src/sensesp_app_builder.h | 2 +- 49 files changed, 97 insertions(+), 96 deletions(-) diff --git a/README.md b/README.md index 852fb1dc3..77913217c 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ `sensesp_app->get_event_loop()`. For example: ```cpp - SensESPBaseApp::get_event_loop()->onRepeat( + event_loop()->onRepeat( 1000, []() { Serial.println("Hello, world!"); } ); diff --git a/examples/analog_input.cpp b/examples/analog_input.cpp index ee8720d4d..36a701c9f 100644 --- a/examples/analog_input.cpp +++ b/examples/analog_input.cpp @@ -90,6 +90,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/async_repeat_sensor.cpp b/examples/async_repeat_sensor.cpp index 070ad07f2..2a186d3d7 100644 --- a/examples/async_repeat_sensor.cpp +++ b/examples/async_repeat_sensor.cpp @@ -33,7 +33,7 @@ void setup() { auto digital_read_callback = [](RepeatSensor* sensor) { ESP_LOGI("Example", "Pretend to trigger an asynchronous measurement operation here."); - SensESPBaseApp::get_event_loop()->onDelay(1000, + event_loop()->onDelay(1000, [sensor]() { sensor->emit(digitalRead(kDigitalInputPin)); }); }; @@ -57,6 +57,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/chain_counter.cpp b/examples/chain_counter.cpp index f84b02839..9a437ed29 100644 --- a/examples/chain_counter.cpp +++ b/examples/chain_counter.cpp @@ -149,6 +149,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop() event_loop->tick(); } diff --git a/examples/constant_sensor.cpp b/examples/constant_sensor.cpp index 336c3d3f7..5539b18ac 100644 --- a/examples/constant_sensor.cpp +++ b/examples/constant_sensor.cpp @@ -38,6 +38,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/dual_thermocouple_sensors/max6675_signalk.cpp b/examples/dual_thermocouple_sensors/max6675_signalk.cpp index 4a8ab8f27..d7c7c712f 100644 --- a/examples/dual_thermocouple_sensors/max6675_signalk.cpp +++ b/examples/dual_thermocouple_sensors/max6675_signalk.cpp @@ -58,6 +58,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/dual_thermocouple_sensors/max6675_signalk_n2k.cpp b/examples/dual_thermocouple_sensors/max6675_signalk_n2k.cpp index 79959c0d9..20e13d2e5 100644 --- a/examples/dual_thermocouple_sensors/max6675_signalk_n2k.cpp +++ b/examples/dual_thermocouple_sensors/max6675_signalk_n2k.cpp @@ -146,6 +146,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/freertos_tasks.cpp b/examples/freertos_tasks.cpp index e94d84b54..9745fdfe7 100644 --- a/examples/freertos_tasks.cpp +++ b/examples/freertos_tasks.cpp @@ -88,13 +88,13 @@ void setup() { [](float input) { ESP_LOGD("Example", "Heading: %f", input); })); // print out free heap - SensESPBaseApp::get_event_loop()->onRepeat( + event_loop() 2000, []() { ESP_LOGD("Example", "Free heap: %d", ESP.getFreeHeap()); }); } void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/fuel_level_sensor/fuel_level_sensor_example.cpp b/examples/fuel_level_sensor/fuel_level_sensor_example.cpp index beabd17cf..5cc5a9edb 100644 --- a/examples/fuel_level_sensor/fuel_level_sensor_example.cpp +++ b/examples/fuel_level_sensor/fuel_level_sensor_example.cpp @@ -38,6 +38,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/hysteresis.cpp b/examples/hysteresis.cpp index 7f6040e30..32295445b 100644 --- a/examples/hysteresis.cpp +++ b/examples/hysteresis.cpp @@ -48,6 +48,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/join_and_zip.cpp b/examples/join_and_zip.cpp index 640cde834..8602b9e65 100644 --- a/examples/join_and_zip.cpp +++ b/examples/join_and_zip.cpp @@ -150,6 +150,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/lambda_transform.cpp b/examples/lambda_transform.cpp index 023c5de92..f91a91d8a 100644 --- a/examples/lambda_transform.cpp +++ b/examples/lambda_transform.cpp @@ -93,6 +93,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/manual_networking.cpp b/examples/manual_networking.cpp index 081217e68..e3b45ef78 100644 --- a/examples/manual_networking.cpp +++ b/examples/manual_networking.cpp @@ -62,6 +62,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/milone_level_sensor/milone_level_sensor.cpp b/examples/milone_level_sensor/milone_level_sensor.cpp index 0dcee91cd..29dd7c411 100644 --- a/examples/milone_level_sensor/milone_level_sensor.cpp +++ b/examples/milone_level_sensor/milone_level_sensor.cpp @@ -132,6 +132,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/minimal_app.cpp b/examples/minimal_app.cpp index 54893adef..a2d37435a 100644 --- a/examples/minimal_app.cpp +++ b/examples/minimal_app.cpp @@ -58,17 +58,17 @@ void setup() { })); pinMode(output_pin1, OUTPUT); - SensESPBaseApp::get_event_loop()->onRepeat(5, + event_loop()->onRepeat(5, []() { digitalWrite(output_pin1, !digitalRead(output_pin1)); }); pinMode(output_pin2, OUTPUT); - SensESPBaseApp::get_event_loop()->onRepeat(100, + event_loop()->onRepeat(100, []() { digitalWrite(output_pin2, !digitalRead(output_pin2)); }); } void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/raw_json.cpp b/examples/raw_json.cpp index cf23e36bf..8cba6fcd9 100644 --- a/examples/raw_json.cpp +++ b/examples/raw_json.cpp @@ -19,7 +19,7 @@ void setup() { ->set_wifi("Hat Labs Sensors", "kanneluuri2406") ->get_app(); - SensESPBaseApp::get_event_loop()->onRepeat(1000, + event_loop()->onRepeat(1000, []() { toggler.set(!toggler.get()); }); // take some boolean input and convert it into a simple serialized JSON @@ -48,6 +48,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/relay_control.cpp b/examples/relay_control.cpp index 45ec47a19..827168c27 100644 --- a/examples/relay_control.cpp +++ b/examples/relay_control.cpp @@ -57,6 +57,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/repeat_sensor_analog_input.cpp b/examples/repeat_sensor_analog_input.cpp index 52bff0b96..e355d16ab 100644 --- a/examples/repeat_sensor_analog_input.cpp +++ b/examples/repeat_sensor_analog_input.cpp @@ -66,6 +66,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/repeat_transform.cpp b/examples/repeat_transform.cpp index 859086336..3a05d678f 100644 --- a/examples/repeat_transform.cpp +++ b/examples/repeat_transform.cpp @@ -141,6 +141,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/rpm_counter.cpp b/examples/rpm_counter.cpp index a8ba03ace..63be9d627 100644 --- a/examples/rpm_counter.cpp +++ b/examples/rpm_counter.cpp @@ -78,6 +78,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/smart_switch/remote_switch_example.cpp b/examples/smart_switch/remote_switch_example.cpp index 6c25c9704..dae51ddce 100644 --- a/examples/smart_switch/remote_switch_example.cpp +++ b/examples/smart_switch/remote_switch_example.cpp @@ -104,6 +104,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/smart_switch/smart_switch_example.cpp b/examples/smart_switch/smart_switch_example.cpp index 23806b8fb..870334a95 100644 --- a/examples/smart_switch/smart_switch_example.cpp +++ b/examples/smart_switch/smart_switch_example.cpp @@ -114,6 +114,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/temperature_sender.cpp b/examples/temperature_sender.cpp index 23bf596ba..03f3f826d 100644 --- a/examples/temperature_sender.cpp +++ b/examples/temperature_sender.cpp @@ -165,6 +165,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/examples/time_counter.cpp b/examples/time_counter.cpp index b91116642..f34c6e108 100644 --- a/examples/time_counter.cpp +++ b/examples/time_counter.cpp @@ -37,7 +37,7 @@ void setup() { // set GPIO 18 to output mode pinMode(18, OUTPUT); - SensESPBaseApp::get_event_loop()->onRepeat(10, []() { + event_loop()->onRepeat(10, []() { if (freq == 0) { if (millis() - freq_start_time >= 10000) { freq = 10; @@ -110,6 +110,6 @@ void setup() { void loop() { // We're storing the event loop in a static variable so that it's only // acquired once. Saves a few function calls per loop iteration. - static auto event_loop = SensESPBaseApp::get_event_loop(); + static auto event_loop = event_loop(); event_loop->tick(); } diff --git a/src/sensesp/controllers/smart_switch_controller.cpp b/src/sensesp/controllers/smart_switch_controller.cpp index 4817a39c8..37081adb1 100644 --- a/src/sensesp/controllers/smart_switch_controller.cpp +++ b/src/sensesp/controllers/smart_switch_controller.cpp @@ -23,7 +23,7 @@ SmartSwitchController::SmartSwitchController(bool auto_initialize, // Emit the initial state once the event loop starts if (auto_initialize_) { - SensESPBaseApp::get_event_loop()->onDelay(0, + event_loop()->onDelay(0, [this]() { this->emit(is_on); }); } } diff --git a/src/sensesp/net/discovery.cpp b/src/sensesp/net/discovery.cpp index 404720742..aeee82426 100644 --- a/src/sensesp/net/discovery.cpp +++ b/src/sensesp/net/discovery.cpp @@ -5,7 +5,7 @@ namespace sensesp { MDNSDiscovery::MDNSDiscovery() { - SensESPBaseApp::get_event_loop()->onDelay(0, [this]() { + event_loop()->onDelay(0, [this]() { String hostname = SensESPBaseApp::get_hostname(); // MDNS.begin(hostname) will crash if hostname is blank diff --git a/src/sensesp/net/http_server.h b/src/sensesp/net/http_server.h index 8ce0d5cc8..5002be11c 100644 --- a/src/sensesp/net/http_server.h +++ b/src/sensesp/net/http_server.h @@ -79,7 +79,7 @@ class HTTPServer : public Configurable { ESP_LOGE(__FILENAME__, "Only one HTTPServer instance is allowed"); return; } - SensESPBaseApp::get_event_loop()->onDelay(0, [this]() { + event_loop()->onDelay(0, [this]() { esp_err_t error = httpd_start(&server_, &config_); if (error != ESP_OK) { ESP_LOGE(__FILENAME__, "Error starting HTTP server: %s", diff --git a/src/sensesp/net/networking.cpp b/src/sensesp/net/networking.cpp index 34ae4b08f..292cbb147 100644 --- a/src/sensesp/net/networking.cpp +++ b/src/sensesp/net/networking.cpp @@ -92,7 +92,7 @@ Networking::Networking(String config_path, String client_ssid, dns_server_->setErrorReplyCode(DNSReplyCode::NoError); dns_server_->start(53, "*", WiFi.softAPIP()); - SensESPBaseApp::get_event_loop()->onRepeat( + event_loop()->onRepeat( 1, [this]() { dns_server_->processNextRequest(); }); } } @@ -193,7 +193,7 @@ void Networking::start_client_autoconnect() { // Launch a separate onRepeat event to (re-)establish WiFi connection. // Connecting is attempted only every 20 s to allow the previous connection // attempt to complete even if the network is slow. - SensESPBaseApp::get_event_loop()->onRepeat(20000, reconnect_cb); + event_loop()->onRepeat(20000, reconnect_cb); } /** diff --git a/src/sensesp/net/networking.h b/src/sensesp/net/networking.h index 750b3e71b..a6acd0bf2 100644 --- a/src/sensesp/net/networking.h +++ b/src/sensesp/net/networking.h @@ -49,7 +49,7 @@ class WiFiStateProducer : public ValueProducer { setup_wifi_callbacks(); // Emit the current state as soon as the event loop starts - SensESPBaseApp::get_event_loop()->onDelay(0, [this]() { this->emit(this->output); }); + event_loop()->onDelay(0, [this]() { this->emit(this->output); }); } void setup_wifi_callbacks() { @@ -89,7 +89,7 @@ class WiFiStateProducer : public ValueProducer { // Setting the AP mode happens immediately, // so this callback is likely called already before all startables have been // initiated. Delay the WiFi state update until the start of the event loop. - SensESPBaseApp::get_event_loop()->onDelay( + event_loop()->onDelay( 0, [this]() { this->emit(WiFiState::kWifiAPModeActivated); }); } diff --git a/src/sensesp/net/ota.h b/src/sensesp/net/ota.h index 8e2161bfa..41e3a7742 100644 --- a/src/sensesp/net/ota.h +++ b/src/sensesp/net/ota.h @@ -18,7 +18,7 @@ class OTA { * @param password A password to be used for the OTA update. */ OTA(const char* password) : password_{password} { - SensESPBaseApp::get_event_loop()->onDelay(0, [this]() { + event_loop()->onDelay(0, [this]() { ArduinoOTA.setPassword(password_); ArduinoOTA.onStart([]() { ESP_LOGW(__FILENAME__, "Starting OTA"); }); ArduinoOTA.onEnd([]() { ESP_LOGW(__FILENAME__, "OTA End"); }); @@ -41,7 +41,7 @@ class OTA { } }); ArduinoOTA.begin(); - SensESPBaseApp::get_event_loop()->onRepeat(20, OTA::handle_ota); + event_loop()->onRepeat(20, OTA::handle_ota); }); } diff --git a/src/sensesp/net/web/base_command_handler.cpp b/src/sensesp/net/web/base_command_handler.cpp index 71c6a614d..42d08b06d 100644 --- a/src/sensesp/net/web/base_command_handler.cpp +++ b/src/sensesp/net/web/base_command_handler.cpp @@ -9,7 +9,7 @@ void add_http_reset_handler(HTTPServer* server) { "Resetting device back to factory defaults. " "You may have to reconfigure the WiFi settings.", 0); - SensESPBaseApp::get_event_loop()->onDelay( + event_loop()->onDelay( 500, []() { SensESPBaseApp::get()->reset(); }); return ESP_OK; }); @@ -20,7 +20,7 @@ void add_http_restart_handler(HTTPServer* server) { HTTPRequestHandler* restart_handler = new HTTPRequestHandler( 1 << HTTP_POST, "/api/device/restart", [](httpd_req_t* req) { httpd_resp_send(req, "Restarting device", 0); - SensESPBaseApp::get_event_loop()->onDelay(500, []() { ESP.restart(); }); + event_loop()->onDelay(500, []() { ESP.restart(); }); return ESP_OK; }); server->add_handler(restart_handler); diff --git a/src/sensesp/sensors/analog_input.cpp b/src/sensesp/sensors/analog_input.cpp index 780f8b976..2574a07f8 100644 --- a/src/sensesp/sensors/analog_input.cpp +++ b/src/sensesp/sensors/analog_input.cpp @@ -18,7 +18,7 @@ AnalogInput::AnalogInput(uint8_t pin, unsigned int read_delay, load_configuration(); if (this->analog_reader->configure()) { - SensESPBaseApp::get_event_loop()->onRepeat(read_delay, [this]() { this->update(); }); + event_loop()->onRepeat(read_delay, [this]() { this->update(); }); } } diff --git a/src/sensesp/sensors/constant_sensor.h b/src/sensesp/sensors/constant_sensor.h index 8940d9a96..78cf091c0 100644 --- a/src/sensesp/sensors/constant_sensor.h +++ b/src/sensesp/sensors/constant_sensor.h @@ -59,10 +59,11 @@ class ConstantSensor : public Sensor { this->load_configuration(); // Emit the initial value once to set the output - SensESPBaseApp::get_event_loop()->onDelay(0, [this]() { this->emit(value_); }); + event_loop()->onDelay(0, + [this]() { this->emit(value_); }); // Then, emit the value at the specified interval - SensESPBaseApp::get_event_loop()->onRepeat(send_interval_ * 1000, - [this]() { this->emit(value_); }); + event_loop()->onRepeat( + send_interval_ * 1000, [this]() { this->emit(value_); }); } void set(T value) { value_ = value; } diff --git a/src/sensesp/sensors/digital_input.h b/src/sensesp/sensors/digital_input.h index e940c8e6a..d5560a0de 100644 --- a/src/sensesp/sensors/digital_input.h +++ b/src/sensesp/sensors/digital_input.h @@ -57,7 +57,7 @@ class DigitalInputState : public DigitalInput, public Sensor { set_requires_restart(true); load_configuration(); - SensESPBaseApp::get_event_loop()->onRepeat( + event_loop()->onRepeat( read_delay_, [this]() { emit(digitalRead(pin_)); }); } @@ -93,10 +93,10 @@ class DigitalInputCounter : public DigitalInput, public Sensor { unsigned int read_delay, String config_path = "") : DigitalInputCounter(pin, pin_mode, interrupt_type, read_delay, config_path, [this]() { this->counter_++; }) { - SensESPBaseApp::get_event_loop()->onInterrupt(pin_, interrupt_type_, + event_loop()->onInterrupt(pin_, interrupt_type_, interrupt_handler_); - SensESPBaseApp::get_event_loop()->onRepeat(read_delay_, [this]() { + event_loop()->onRepeat(read_delay_, [this]() { noInterrupts(); output = counter_; counter_ = 0; @@ -201,13 +201,13 @@ class DigitalInputChange : public DigitalInput, public Sensor { output = (bool)digitalRead(pin_); last_output_ = !output; // ensure that we always send the first output - SensESPBaseApp::get_event_loop()->onInterrupt( + event_loop()->onInterrupt( pin_, interrupt_type_, [this]() { output = (bool)digitalRead(pin_); triggered_ = true; }); - SensESPBaseApp::get_event_loop()->onTick([this]() { + event_loop()->onTick([this]() { if (triggered_ && (output != last_output_)) { noInterrupts(); triggered_ = false; diff --git a/src/sensesp/sensors/system_info.h b/src/sensesp/sensors/system_info.h index a1262bca3..5569ba279 100644 --- a/src/sensesp/sensors/system_info.h +++ b/src/sensesp/sensors/system_info.h @@ -47,8 +47,8 @@ class SystemHz : public ValueProducer { SystemHz() { elapsed_millis_ = 0; - SensESPBaseApp::get_event_loop()->onTick([this]() { this->tick(); }); - SensESPBaseApp::get_event_loop()->onRepeat(1000, + event_loop()->onTick([this]() { this->tick(); }); + event_loop()->onRepeat(1000, [this]() { this->update(); }); } String get_value_name() { return "systemhz"; } @@ -70,7 +70,7 @@ class SystemHz : public ValueProducer { class FreeMem : public ValueProducer { public: FreeMem() { - SensESPBaseApp::get_event_loop()->onRepeat(1000, + event_loop()->onRepeat(1000, [this]() { this->update(); }); } String get_value_name() { return "freemem"; } @@ -90,7 +90,7 @@ class FreeMem : public ValueProducer { class Uptime : public ValueProducer { public: Uptime() { - SensESPBaseApp::get_event_loop()->onRepeat(1000, + event_loop()->onRepeat(1000, [this]() { this->update(); }); } String get_value_name() { return "uptime"; } @@ -110,7 +110,7 @@ class Uptime : public ValueProducer { class IPAddrDev : public ValueProducer { public: IPAddrDev() { - SensESPBaseApp::get_event_loop()->onRepeat(10000, + event_loop()->onRepeat(10000, [this]() { this->update(); }); } String get_value_name() { return "ipaddr"; } @@ -130,7 +130,7 @@ class IPAddrDev : public ValueProducer { class WiFiSignal : public ValueProducer { public: WiFiSignal() { - SensESPBaseApp::get_event_loop()->onRepeat(3000, + event_loop()->onRepeat(3000, [this]() { this->update(); }); } String get_value_name() { return "wifisignal"; } diff --git a/src/sensesp/signalk/signalk_delta_queue.cpp b/src/sensesp/signalk/signalk_delta_queue.cpp index b8a850265..7774f9390 100644 --- a/src/sensesp/signalk/signalk_delta_queue.cpp +++ b/src/sensesp/signalk/signalk_delta_queue.cpp @@ -13,7 +13,7 @@ SKDeltaQueue::SKDeltaQueue(unsigned int max_buffer_size) : max_buffer_size{max_buffer_size}, meta_sent_{false} { semaphore_ = xSemaphoreCreateRecursiveMutex(); - SensESPBaseApp::get_event_loop()->onDelay( + event_loop()->onDelay( 0, [this]() { this->connect_emitters(); }); } diff --git a/src/sensesp/signalk/signalk_put_request.cpp b/src/sensesp/signalk/signalk_put_request.cpp index cb227878e..16304fc65 100644 --- a/src/sensesp/signalk/signalk_put_request.cpp +++ b/src/sensesp/signalk/signalk_put_request.cpp @@ -22,7 +22,7 @@ String SKRequest::send_request(JsonDocument& request, // After 10 seconds, if we haven't already handled a response, // assume its not coming. pending_request->timeout_cleanup = - SensESPBaseApp::get_event_loop()->onDelay(timeout, [pending_request]() { + event_loop()->onDelay(timeout, [pending_request]() { // Mark the delay eventll as it will be cleaned up by the ReactESP // framework if this executes... ESP_LOGW(__FILENAME__, "No response from server for request Id %s", @@ -80,7 +80,7 @@ void SKRequest::remove_request(String request_id) { // The timeout code was not called, so just // remove it from the ReactESP execution queue... pending_request->timeout_cleanup->remove( - SensESPBaseApp::get_event_loop()); + event_loop()); } // Now, remove the request from the map... diff --git a/src/sensesp/signalk/signalk_ws_client.cpp b/src/sensesp/signalk/signalk_ws_client.cpp index bf1e4566c..70b802269 100644 --- a/src/sensesp/signalk/signalk_ws_client.cpp +++ b/src/sensesp/signalk/signalk_ws_client.cpp @@ -90,7 +90,7 @@ SKWSClient::SKWSClient(const String& config_path, SKDeltaQueue* sk_delta_queue, [this]() { this->emit(this->connection_state_.get()); }); // process any received updates in the main task - SensESPBaseApp::get_event_loop()->onRepeat( + event_loop()->onRepeat( 1, [this]() { this->process_received_updates(); }); // set the singleton object pointer @@ -101,7 +101,7 @@ SKWSClient::SKWSClient(const String& config_path, SKDeltaQueue* sk_delta_queue, // Connect the counters delta_tx_tick_producer_.connect_to(&delta_tx_count_producer_); - SensESPBaseApp::get_event_loop()->onDelay(0, [this]() { + event_loop()->onDelay(0, [this]() { ESP_LOGD(__FILENAME__, "Starting SKWSClient"); xTaskCreate(ExecuteWebSocketTask, "SKWSClient", kWsClientTaskStackSize, this, 1, NULL); diff --git a/src/sensesp/signalk/signalk_ws_client.h b/src/sensesp/signalk/signalk_ws_client.h index cdaaf22c2..ca12e0476 100644 --- a/src/sensesp/signalk/signalk_ws_client.h +++ b/src/sensesp/signalk/signalk_ws_client.h @@ -106,7 +106,7 @@ class SKWSClient : public Configurable, TaskQueueProducer connection_state_ = TaskQueueProducer( SKWSConnectionState::kSKWSDisconnected, - SensESPBaseApp::get_event_loop()); + event_loop()); /// task_connection_state is used to track the internal task state which might /// be out of sync with the published connection state. @@ -118,7 +118,7 @@ class SKWSClient : public Configurable, SKDeltaQueue* sk_delta_queue_; /// @brief Emits the number of deltas sent since last report TaskQueueProducer delta_tx_tick_producer_ = - TaskQueueProducer(0, SensESPBaseApp::get_event_loop(), 5, 990); + TaskQueueProducer(0, event_loop(), 5, 990); Integrator delta_tx_count_producer_{1, 0, ""}; Integrator delta_rx_count_producer_{1, 0, ""}; diff --git a/src/sensesp/system/async_response_handler.h b/src/sensesp/system/async_response_handler.h index bffb59976..637facccc 100644 --- a/src/sensesp/system/async_response_handler.h +++ b/src/sensesp/system/async_response_handler.h @@ -50,11 +50,11 @@ class AsyncResponseHandler : public ValueConsumer, this->emit(status_); if (timeout_event_ != nullptr) { - SensESPBaseApp::get_event_loop()->remove(timeout_event_); + event_loop()->remove(timeout_event_); timeout_event_ = nullptr; } timeout_event_ = - SensESPBaseApp::get_event_loop()->onDelay(timeout_, [this]() { + event_loop()->onDelay(timeout_, [this]() { if (status_ == AsyncResponseStatus::kPending) { ESP_LOGV("AsyncResponseHandler", "Timeout"); status_ = AsyncResponseStatus::kTimeout; @@ -74,7 +74,7 @@ class AsyncResponseHandler : public ValueConsumer, // Clear the timeout event if (timeout_event_ != nullptr) { - SensESPBaseApp::get_event_loop()->remove(timeout_event_); + event_loop()->remove(timeout_event_); timeout_event_ = nullptr; } diff --git a/src/sensesp/system/base_button.h b/src/sensesp/system/base_button.h index 14e72c9e4..c313292e3 100644 --- a/src/sensesp/system/base_button.h +++ b/src/sensesp/system/base_button.h @@ -32,7 +32,7 @@ class BaseButtonHandler : public Configurable, public IEventHandler { ESP_LOGD(__FILENAME__, "Button handler started"); - SensESPBaseApp::get_event_loop()->onRepeat( + event_loop()->onRepeat( 4, [this]() { this->button_->check(); }); } diff --git a/src/sensesp/system/led_blinker.cpp b/src/sensesp/system/led_blinker.cpp index 1267467a2..173f64a56 100644 --- a/src/sensesp/system/led_blinker.cpp +++ b/src/sensesp/system/led_blinker.cpp @@ -11,7 +11,7 @@ namespace sensesp { BaseBlinker::BaseBlinker(int pin) : pin_{pin} { pinMode(pin, OUTPUT); - SensESPBaseApp::get_event_loop()->onDelay(1, [this]() { this->tick(); }); + event_loop()->onDelay(1, [this]() { this->tick(); }); } /** @@ -44,13 +44,13 @@ void BaseBlinker::blip(int duration) { bool const orig_state = this->state_; this->set_state(false); int const current_counter = this->update_counter_; - SensESPBaseApp::get_event_loop()->onDelay( + event_loop()->onDelay( duration, [this, duration, orig_state, current_counter]() { // only update if no-one has touched the LED in the meanwhile if (this->update_counter_ == current_counter) { this->set_state(true); int const new_counter = this->update_counter_; - SensESPBaseApp::get_event_loop()->onDelay( + event_loop()->onDelay( duration, [this, orig_state, new_counter]() { // again, only update if no-one has touched the LED if (this->update_counter_ == new_counter) { @@ -75,7 +75,7 @@ void BaseBlinker::set_enabled(bool state) { } else { this->set_state(false); if (was_enabled) { - event_->remove(SensESPBaseApp::get_event_loop()); + event_->remove(event_loop()); } } } @@ -91,7 +91,7 @@ void EvenBlinker::tick() { return; } this->flip_state(); - event_ = SensESPBaseApp::get_event_loop()->onDelay( + event_ = event_loop()->onDelay( period_, [this]() { this->tick(); }); } @@ -107,7 +107,7 @@ void RatioBlinker::tick() { int const off_duration = max(0, period_ - on_duration); unsigned int const ref_duration = state_ == false ? off_duration : on_duration; - event_ = SensESPBaseApp::get_event_loop()->onDelay( + event_ = event_loop()->onDelay( ref_duration, [this]() { this->tick(); }); } @@ -137,7 +137,7 @@ void PatternBlinker::tick() { // odd indices indicate times when LED should be OFF, even when ON bool const new_state = (pattern_ptr_ % 2) == 0; this->set_state(new_state); - event_ = SensESPBaseApp::get_event_loop()->onDelay( + event_ = event_loop()->onDelay( pattern_[pattern_ptr_++], [this]() { this->tick(); }); } @@ -145,7 +145,7 @@ void PatternBlinker::restart() { state_ = false; pattern_ptr_ = 0; if (event_ != NULL) { - event_->remove(SensESPBaseApp::get_event_loop()); + event_->remove(event_loop()); event_ = NULL; this->tick(); } diff --git a/src/sensesp/system/stream_producer.h b/src/sensesp/system/stream_producer.h index f600a9a01..10ae1960d 100644 --- a/src/sensesp/system/stream_producer.h +++ b/src/sensesp/system/stream_producer.h @@ -16,7 +16,7 @@ class StreamCharProducer : public ValueProducer { public: StreamCharProducer(Stream* stream) : stream_{stream} { read_event_ = - SensESPBaseApp::get_event_loop()->onAvailable(*stream_, [this]() { + event_loop()->onAvailable(*stream_, [this]() { while (stream_->available()) { char c = stream_->read(); this->emit(c); @@ -39,7 +39,7 @@ class StreamLineProducer : public ValueProducer { static int buf_pos = 0; buf_ = new char[max_line_length_ + 1]; read_event_ = - SensESPBaseApp::get_event_loop()->onAvailable(*stream_, [this]() { + event_loop()->onAvailable(*stream_, [this]() { while (stream_->available()) { char c = stream_->read(); if (c == '\n') { diff --git a/src/sensesp/system/task_queue_producer.h b/src/sensesp/system/task_queue_producer.h index 169bd4257..038d1c207 100644 --- a/src/sensesp/system/task_queue_producer.h +++ b/src/sensesp/system/task_queue_producer.h @@ -24,7 +24,7 @@ template class TaskQueueProducer : public ObservableValue { public: TaskQueueProducer(const T& value, - reactesp::EventLoop* consumer_app = SensESPBaseApp::get_event_loop(), + reactesp::EventLoop* consumer_app = event_loop(), int queue_size = 1, unsigned int poll_rate = 990) : ObservableValue(value), queue_size_{queue_size} { queue_ = xQueueCreate(queue_size, sizeof(T)); @@ -43,7 +43,7 @@ class TaskQueueProducer : public ObservableValue { TaskQueueProducer(const T& value, int queue_size = 1, unsigned int poll_rate = 990) - : TaskQueueProducer(value, SensESPBaseApp::get_event_loop(), queue_size, + : TaskQueueProducer(value, event_loop(), queue_size, poll_rate) {} virtual void set(const T& value) override { diff --git a/src/sensesp/transforms/click_type.cpp b/src/sensesp/transforms/click_type.cpp index 21a12ad5b..21e34f438 100644 --- a/src/sensesp/transforms/click_type.cpp +++ b/src/sensesp/transforms/click_type.cpp @@ -54,7 +54,7 @@ void ClickType::on_button_press() { // of the double_click_interval. Remove any "SingleClick" report that may // have been queued up.... if (delayed_click_report_ != NULL) { - delayed_click_report_->remove(SensESPBaseApp::get_event_loop()); + delayed_click_report_->remove(event_loop()); delayed_click_report_ = NULL; ESP_LOGD(__FILENAME__, "ClickType press is double click. Removed queued SingleClick " @@ -101,7 +101,7 @@ void ClickType::on_button_release() { // DoubleClick uint64_t const time_of_event = millis(); int64_t const pd = (long)press_duration_; - delayed_click_report_ = SensESPBaseApp::get_event_loop()->onDelay( + delayed_click_report_ = event_loop()->onDelay( double_click_interval_ + 20, [this, pd, time_of_event]() { ESP_LOGD( __FILENAME__, @@ -129,7 +129,7 @@ void ClickType::on_button_release() { } void ClickType::emitDelayed(ClickTypes value) { - SensESPBaseApp::get_event_loop()->onDelay( + event_loop()->onDelay( 5, [this, value]() { this->emit(value); }); } diff --git a/src/sensesp/transforms/debounce.h b/src/sensesp/transforms/debounce.h index 4da87a67f..ff5f2d450 100644 --- a/src/sensesp/transforms/debounce.h +++ b/src/sensesp/transforms/debounce.h @@ -50,11 +50,11 @@ class Debounce : public SymmetricTransform { debounced_value_ = input; if (event_) { - event_->remove(SensESPBaseApp::get_event_loop()); + event_->remove(event_loop()); event_ = nullptr; } - event_ = - SensESPBaseApp::get_event_loop()->onDelay(ms_min_delay_, [this, input]() { + event_ = event_loop()->onDelay( + ms_min_delay_, [this, input]() { this->event_ = nullptr; this->debounced_value_ = input; this->emit(input); diff --git a/src/sensesp/transforms/press_repeater.cpp b/src/sensesp/transforms/press_repeater.cpp index 5763a984e..d63bb3c48 100644 --- a/src/sensesp/transforms/press_repeater.cpp +++ b/src/sensesp/transforms/press_repeater.cpp @@ -12,7 +12,7 @@ PressRepeater::PressRepeater(const String& config_path, int integer_false, repeating_{false} { load_configuration(); - SensESPBaseApp::get_event_loop()->onRepeat(10, [this]() { + event_loop()->onRepeat(10, [this]() { if (pushed_) { // A press is currently in progress if (repeating_) { diff --git a/src/sensesp/transforms/repeat.h b/src/sensesp/transforms/repeat.h index 2da2c0e18..6a25ed4d1 100644 --- a/src/sensesp/transforms/repeat.h +++ b/src/sensesp/transforms/repeat.h @@ -33,9 +33,9 @@ class Repeat : public Transform { this->emit(input); if (repeat_event_ != nullptr) { // Delete the old repeat event - repeat_event_->remove(SensESPBaseApp::get_event_loop()); + repeat_event_->remove(event_loop()); } - repeat_event_ = SensESPBaseApp::get_event_loop()->onRepeat( + repeat_event_ = event_loop()->onRepeat( interval_, [this]() { this->notify(); }); } @@ -65,7 +65,7 @@ class RepeatStopping : public Repeat { // Delete the old repeat event this->repeat_event_->remove(); } - this->repeat_event_ = SensESPBaseApp::get_event_loop()->onRepeat( + this->repeat_event_ = event_loop()->onRepeat( this->interval_, [this]() { this->repeat_function(); }); } @@ -76,7 +76,7 @@ class RepeatStopping : public Repeat { // Delete the old repeat event this->repeat_event_->remove(); } - this->repeat_event_ = SensESPBaseApp::get_event_loop()->onRepeat( + this->repeat_event_ = event_loop()->onRepeat( this->interval_, [this]() { this->repeat_function(); }); } @@ -113,9 +113,9 @@ class RepeatExpiring : public Repeat> { if (this->repeat_event_ != nullptr) { // Delete the old repeat event - this->repeat_event_->remove(SensESPBaseApp::get_event_loop()); + this->repeat_event_->remove(event_loop()); } - this->repeat_event_ = SensESPBaseApp::get_event_loop()->onRepeat( + this->repeat_event_ = event_loop()->onRepeat( this->interval_, [this]() { this->repeat_function(); }); } @@ -124,9 +124,9 @@ class RepeatExpiring : public Repeat> { age_ = 0; if (this->repeat_event_ != nullptr) { // Delete the old repeat event - this->repeat_event_->remove(SensESPBaseApp::get_event_loop()); + this->repeat_event_->remove(event_loop()); } - this->repeat_event_ = SensESPBaseApp::get_event_loop()->onRepeat( + this->repeat_event_ = event_loop()->onRepeat( this->interval_, [this]() { this->repeat_function(); }); } @@ -167,7 +167,7 @@ class RepeatConstantRate : public RepeatExpiring { this->repeat_event_->remove(); } - this->repeat_event_ = SensESPBaseApp::get_event_loop()->onRepeat( + this->repeat_event_ = event_loop()->onRepeat( interval, [this]() { this->repeat_function(); }); } diff --git a/src/sensesp_app_builder.h b/src/sensesp_app_builder.h index bb8549614..f05753538 100644 --- a/src/sensesp_app_builder.h +++ b/src/sensesp_app_builder.h @@ -220,7 +220,7 @@ class SensESPAppBuilder : public SensESPBaseAppBuilder { input == SystemStatus::kWifiNoAP) { ESP_LOGW(__FILENAME__, "Unable to connect to wifi for too long; restarting."); - SensESPBaseApp::get_event_loop()->onDelay(1000, []() { ESP.restart(); }); + event_loop()->onDelay(1000, []() { ESP.restart(); }); } }));