Skip to content

Commit

Permalink
Fix event loop calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mairas committed Oct 5, 2024
1 parent 91d4409 commit f96aa76
Show file tree
Hide file tree
Showing 49 changed files with 97 additions and 96 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!"); }
);
Expand Down
2 changes: 1 addition & 1 deletion examples/analog_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
4 changes: 2 additions & 2 deletions examples/async_repeat_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void setup() {
auto digital_read_callback = [](RepeatSensor<bool>* 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)); });
};

Expand All @@ -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();
}
2 changes: 1 addition & 1 deletion examples/chain_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/constant_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/dual_thermocouple_sensors/max6675_signalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/dual_thermocouple_sensors/max6675_signalk_n2k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
4 changes: 2 additions & 2 deletions examples/freertos_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/fuel_level_sensor/fuel_level_sensor_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/hysteresis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/join_and_zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/lambda_transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/manual_networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/milone_level_sensor/milone_level_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
6 changes: 3 additions & 3 deletions examples/minimal_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
4 changes: 2 additions & 2 deletions examples/raw_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/relay_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/repeat_sensor_analog_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/repeat_transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/rpm_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/smart_switch/remote_switch_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/smart_switch/smart_switch_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/temperature_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
4 changes: 2 additions & 2 deletions examples/time_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion src/sensesp/controllers/smart_switch_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/net/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/net/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/sensesp/net/networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); });
}
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/sensesp/net/networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class WiFiStateProducer : public ValueProducer<WiFiState> {
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() {
Expand Down Expand Up @@ -89,7 +89,7 @@ class WiFiStateProducer : public ValueProducer<WiFiState> {
// 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); });
}

Expand Down
4 changes: 2 additions & 2 deletions src/sensesp/net/ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"); });
Expand All @@ -41,7 +41,7 @@ class OTA {
}
});
ArduinoOTA.begin();
SensESPBaseApp::get_event_loop()->onRepeat(20, OTA::handle_ota);
event_loop()->onRepeat(20, OTA::handle_ota);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/sensesp/net/web/base_command_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/sensors/analog_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); });
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/sensesp/sensors/constant_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ class ConstantSensor : public Sensor<T> {
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; }
Expand Down
Loading

0 comments on commit f96aa76

Please sign in to comment.