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 4e18f5d commit fd29b2b
Show file tree
Hide file tree
Showing 49 changed files with 120 additions and 224 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
5 changes: 1 addition & 4 deletions examples/analog_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,5 @@ 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();
event_loop->tick();
event_loop()->tick();
}
7 changes: 2 additions & 5 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 @@ -55,8 +55,5 @@ 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();
event_loop->tick();
event_loop()->tick();
}
5 changes: 1 addition & 4 deletions examples/chain_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,5 @@ void setup() {
// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events needed.
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();
event_loop->tick();
event_loop()->tick();
}
5 changes: 1 addition & 4 deletions examples/constant_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@ 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();
event_loop->tick();
event_loop()->tick();
}
5 changes: 1 addition & 4 deletions examples/dual_thermocouple_sensors/max6675_signalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,5 @@ 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();
event_loop->tick();
event_loop()->tick();
}
5 changes: 1 addition & 4 deletions examples/dual_thermocouple_sensors/max6675_signalk_n2k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,5 @@ void setup() {

// main program loop
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();
event_loop->tick();
event_loop()->tick();
}
9 changes: 2 additions & 7 deletions examples/freertos_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,8 @@ void setup() {
[](float input) { ESP_LOGD("Example", "Heading: %f", input); }));

// print out free heap
SensESPBaseApp::get_event_loop()->onRepeat(
event_loop()->onRepeat(
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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
7 changes: 1 addition & 6 deletions examples/fuel_level_sensor/fuel_level_sensor_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,4 @@ void setup() {
new SKOutputFloat("tanks.fuel.0.currentLevel"));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
7 changes: 1 addition & 6 deletions examples/hysteresis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,4 @@ void setup() {
->connect_to(new SKOutputBool(sk_path));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
11 changes: 2 additions & 9 deletions examples/join_and_zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,7 @@ void setup() {
// merged values to the console.

merged_string->connect_to(new LambdaConsumer<String>(
[](String value) {
ESP_LOGD("App", "Merged: %s", value.c_str());
}));
[](String value) { ESP_LOGD("App", "Merged: %s", value.c_str()); }));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
7 changes: 1 addition & 6 deletions examples/lambda_transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,4 @@ void setup() {
->connect_to(new SKOutputFloat(sk_path));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
15 changes: 6 additions & 9 deletions examples/manual_networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "sensesp/net/http_server.h"
#include "sensesp/net/networking.h"
#include "sensesp/sensors/digital_input.h"
#include "sensesp/signalk/signalk_output.h"
#include "sensesp/system/lambda_consumer.h"
#include "sensesp/transforms/linear.h"
#include "sensesp/transforms/typecast.h"
#include "sensesp_minimal_app_builder.h"
#include "sensesp/signalk/signalk_output.h"

using namespace sensesp;

Expand Down Expand Up @@ -43,7 +43,8 @@ void setup() {

Serial.println("");

ESP_LOGD("Example", "Connected to WiFi. IP address: %s", WiFi.localIP().toString().c_str());
ESP_LOGD("Example", "Connected to WiFi. IP address: %s",
WiFi.localIP().toString().c_str());

WiFi.setHostname(SensESPBaseApp::get_hostname().c_str());

Expand All @@ -56,12 +57,8 @@ void setup() {
Serial.printf("Digin: %d\n", input);
}));

digin->connect_to(new SKOutputBool("electrical.switches.0.state", "/digin/state"));
digin->connect_to(
new SKOutputBool("electrical.switches.0.state", "/digin/state"));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
9 changes: 2 additions & 7 deletions examples/milone_level_sensor/milone_level_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ETapeInterpreter : public CurveInterpolator {
};

void setup() {
// Some initialization boilerplate when in debug mode...
// Some initialization boilerplate when in debug mode...
SetupLogging();

// Create a builder object
Expand Down Expand Up @@ -129,9 +129,4 @@ void setup() {
new SKOutputFloat("tanks.freshwater.starboard.currentLevel"));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
15 changes: 5 additions & 10 deletions examples/minimal_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,12 @@ void setup() {
}));

pinMode(output_pin1, OUTPUT);
SensESPBaseApp::get_event_loop()->onRepeat(5,
[]() { digitalWrite(output_pin1, !digitalRead(output_pin1)); });
event_loop()->onRepeat(
5, []() { digitalWrite(output_pin1, !digitalRead(output_pin1)); });

pinMode(output_pin2, OUTPUT);
SensESPBaseApp::get_event_loop()->onRepeat(100,
[]() { digitalWrite(output_pin2, !digitalRead(output_pin2)); });
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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
10 changes: 2 additions & 8 deletions examples/raw_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ void setup() {
->set_wifi("Hat Labs Sensors", "kanneluuri2406")
->get_app();

SensESPBaseApp::get_event_loop()->onRepeat(1000,
[]() { toggler.set(!toggler.get()); });
event_loop()->onRepeat(1000, []() { toggler.set(!toggler.get()); });

// take some boolean input and convert it into a simple serialized JSON
// document
Expand All @@ -45,9 +44,4 @@ void setup() {
jsonify->connect_to(new SKOutputRawJson(sk_path, ""));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
9 changes: 2 additions & 7 deletions examples/relay_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace sensesp;
// light when it gets dark outside.

void setup() {
// Some initialization boilerplate when in debug mode...
// Some initialization boilerplate when in debug mode...
SetupLogging();

// Create a builder object
Expand Down Expand Up @@ -54,9 +54,4 @@ void setup() {
->connect_to(new DigitalOutput(5));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
7 changes: 1 addition & 6 deletions examples/repeat_sensor_analog_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,4 @@ void setup() {
new SKOutputFloat(sk_path, "", new SKMetadata("ratio", "Indoor light")));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
13 changes: 4 additions & 9 deletions examples/repeat_transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ void setup() {

// Repeat the values every 2 seconds

auto repeat_A = new Repeat<char>(2000);
auto repeat_a = new Repeat<char>(2000);
auto repeat_int = new Repeat<int>(2000);
auto repeat_A = new Repeat<char, char>(2000);
auto repeat_a = new Repeat<char, char>(2000);
auto repeat_int = new Repeat<int, int>(2000);

// Pay attention to the individual columns of the program console output.
// Capital letters are produced every second. Repeat gets always triggered as
Expand Down Expand Up @@ -138,9 +138,4 @@ void setup() {
[](int value) { ESP_LOGD("App", "Repeat: %d", value); }));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
9 changes: 2 additions & 7 deletions examples/rpm_counter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <Arduino.h>

//#define SERIAL_DEBUG_DISABLED
// #define SERIAL_DEBUG_DISABLED

#include "sensesp/sensors/digital_input.h"
#include "sensesp/signalk/signalk_output.h"
Expand Down Expand Up @@ -75,9 +75,4 @@ void setup() {
// to a Signal K Output as a number
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
7 changes: 1 addition & 6 deletions examples/smart_switch/remote_switch_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,4 @@ void setup() {
sk_listener->connect_to(controller);
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
9 changes: 2 additions & 7 deletions examples/smart_switch/smart_switch_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,8 @@ void setup() {
// to be reported to the server every 10 seconds, regardless of whether
// or not it has changed. That keeps the value on the server fresh and
// lets the server know the switch is still alive.
load_switch->connect_to(new Repeat<bool>(10000))
load_switch->connect_to(new Repeat<bool, bool>(10000))
->connect_to(new SKOutputBool(sk_path, config_path_sk_output));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
7 changes: 1 addition & 6 deletions examples/temperature_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,4 @@ void setup() {
new SKOutputFloat(sk_path, "/12V_alternator/temp/sk", metadata));
}

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();
event_loop->tick();
}
void loop() { event_loop()->tick(); }
Loading

0 comments on commit fd29b2b

Please sign in to comment.