Skip to content

Commit

Permalink
Move event loop into SensESPBaseApp
Browse files Browse the repository at this point in the history
Also provide explicit event loop in all *Event->remove() calls.
  • Loading branch information
mairas committed Sep 2, 2024
1 parent 525dac1 commit 57264e4
Show file tree
Hide file tree
Showing 49 changed files with 240 additions and 187 deletions.
6 changes: 1 addition & 5 deletions docs/pages/tutorials/arduino_style/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,10 @@ First, let's add the required header file include statements to the `src/main.cp

The `sensesp_app_builder.h` file contains the `SensESPAppBuilder` class, which we'll use to build the SensESP app. The `signalk_output.h` file contains the `SKOutput` class, which we'll use to create the Signal K output objects.

We'll also need to add a namespace definition and create the ReactESP app that SensESP uses to run the code. Add the following lines after the `#define` statements:
We also need to add a namespace definition. Add the following lines after the `#include` statements:

```cpp
using namespace sensesp;

reactesp::EventLoop app;
```

Next, we'll create the SensESP application using the builder class. Add the following lines to the `setup()` function, right before the `Wire.begin()` line:
Expand Down Expand Up @@ -304,8 +302,6 @@ Here's the complete code for the `src/main.cpp` file:

using namespace sensesp;

reactesp::EventLoop app;

Adafruit_BME280 bme;

unsigned long delayTime = 1000;
Expand Down
13 changes: 6 additions & 7 deletions examples/analog_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

using namespace sensesp;

// SensESP builds upon the ReactESP framework. Every ReactESP application
// must instantiate the "app" object.
reactesp::EventLoop app;

// The setup function performs one-time application initialization.
void setup() {
// Some initialization boilerplate when in debug mode...
Expand Down Expand Up @@ -91,6 +87,9 @@ void setup() {
new SKMetadata("ratio", "Indoor light")));
}

// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events as needed.
void loop() { app.tick(); }
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();
}
13 changes: 7 additions & 6 deletions examples/async_repeat_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

using namespace sensesp;

reactesp::EventLoop app;

// The setup function performs one-time application initialization.
void setup() {
SetupLogging();
Expand All @@ -35,7 +33,7 @@ void setup() {
auto digital_read_callback = [](RepeatSensor<bool>* sensor) {
ESP_LOGI("Example",
"Pretend to trigger an asynchronous measurement operation here.");
app.onDelay(1000,
SensESPBaseApp::get_event_loop()->onDelay(1000,
[sensor]() { sensor->emit(digitalRead(kDigitalInputPin)); });
};

Expand All @@ -56,6 +54,9 @@ void setup() {
digital->connect_to(new SKOutputFloat(sk_path, ""));
}

// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events as needed.
void loop() { app.tick(); }
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();
}
9 changes: 6 additions & 3 deletions examples/chain_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ using namespace sensesp;
* A bi-directional chain counter is possible, but this is not one.
*/

reactesp::EventLoop app;

void setup() {
SetupLogging();

Expand Down Expand Up @@ -148,4 +146,9 @@ 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() { app.tick(); }
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();
}
9 changes: 6 additions & 3 deletions examples/constant_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

using namespace sensesp;

reactesp::EventLoop app;

void setup() {
SetupLogging();

Expand All @@ -37,4 +35,9 @@ void setup() {
new SKMetadata("m3", "Fresh Water Tank Capacity")));
}

void loop() { app.tick(); }
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();
}
9 changes: 6 additions & 3 deletions examples/dual_thermocouple_sensors/max6675_signalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

using namespace sensesp;

reactesp::EventLoop app;

MAX6675 thermocouple0(thermoCLK, thermo1CS, thermoDO);
MAX6675 thermocouple1(thermoCLK, thermo2CS, thermoDO);

Expand Down Expand Up @@ -57,4 +55,9 @@ void setup() {

}

void loop() { app.tick(); }
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();
}
9 changes: 6 additions & 3 deletions examples/dual_thermocouple_sensors/max6675_signalk_n2k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ using namespace sensesp;

tNMEA2000* nmea2000;

reactesp::EventLoop app;

// MAX6675 setup
MAX6675 thermocouple0(thermoCLK, thermo1CS, thermoDO);
MAX6675 thermocouple1(thermoCLK, thermo2CS, thermoDO);
Expand Down Expand Up @@ -145,4 +143,9 @@ void setup() {
}

// main program loop
void loop() { app.tick(); }
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();
}
13 changes: 7 additions & 6 deletions examples/freertos_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const int kTestOutputInterval = 410;

const uint8_t kDigitalInputPin = 15;

reactesp::EventLoop app;

void ToggleTestOutputPin(void *parameter) {
while (true) {
digitalWrite(kTestOutputPin, !digitalRead(kTestOutputPin));
Expand Down Expand Up @@ -90,10 +88,13 @@ void setup() {
[](float input) { ESP_LOGD("Example", "Heading: %f", input); }));

// print out free heap
app.onRepeat(
SensESPBaseApp::get_event_loop()->onRepeat(
2000, []() { ESP_LOGD("Example", "Free heap: %d", ESP.getFreeHeap()); });
}

// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events as needed.
void loop() { app.tick(); }
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();
}
11 changes: 6 additions & 5 deletions examples/fuel_level_sensor/fuel_level_sensor_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

using namespace sensesp;

reactesp::EventLoop app;

void setup() {
SetupLogging();

Expand Down Expand Up @@ -37,6 +35,9 @@ void setup() {
new SKOutputFloat("tanks.fuel.0.currentLevel"));
}

// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events as needed.
void loop() { app.tick(); }
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();
}
11 changes: 6 additions & 5 deletions examples/hysteresis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

using namespace sensesp;

reactesp::EventLoop app;

void setup() {
SetupLogging();

Expand Down Expand Up @@ -47,6 +45,9 @@ void setup() {
->connect_to(new SKOutputBool(sk_path));
}

// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events as needed.
void loop() { app.tick(); }
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();
}
9 changes: 6 additions & 3 deletions examples/join_and_zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

using namespace sensesp;

reactesp::EventLoop app;

SensESPMinimalApp* sensesp_app;

void setup() {
Expand Down Expand Up @@ -149,4 +147,9 @@ void setup() {
}));
}

void loop() { app.tick(); }
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();
}
11 changes: 6 additions & 5 deletions examples/lambda_transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

using namespace sensesp;

reactesp::EventLoop app;

void setup() {
SetupLogging();

Expand Down Expand Up @@ -92,6 +90,9 @@ void setup() {
->connect_to(new SKOutputFloat(sk_path));
}

// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events as needed.
void loop() { app.tick(); }
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();
}
11 changes: 6 additions & 5 deletions examples/manual_networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ const uint8_t input_pin1 = 0;
// The program reacts to changes on GPIO pin 0 and prints the value to the
// serial console.

reactesp::EventLoop app;

void setup() {
SetupLogging();

Expand Down Expand Up @@ -61,6 +59,9 @@ void setup() {
digin->connect_to(new SKOutputBool("electrical.switches.0.state", "/digin/state"));
}

// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events as needed.
void loop() { app.tick(); }
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();
}
13 changes: 6 additions & 7 deletions examples/milone_level_sensor/milone_level_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ class ETapeInterpreter : public CurveInterpolator {
}
};

// SensESP builds upon the ReactESP framework. Every ReactESP application
// defines an "app" object.
reactesp::EventLoop app;

void setup() {
// Some initialization boilerplate when in debug mode...
SetupLogging();
Expand Down Expand Up @@ -133,6 +129,9 @@ void setup() {
new SKOutputFloat("tanks.freshwater.starboard.currentLevel"));
}

// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events as needed.
void loop() { app.tick(); }
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();
}
15 changes: 8 additions & 7 deletions examples/minimal_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const uint8_t output_pin2 = 21;
// with a jumper wire, you should see changes in the reported values on the
// serial console.

reactesp::EventLoop app;

void setup() {
SetupLogging();

Expand Down Expand Up @@ -60,14 +58,17 @@ void setup() {
}));

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

pinMode(output_pin2, OUTPUT);
app.onRepeat(100,
SensESPBaseApp::get_event_loop()->onRepeat(100,
[]() { digitalWrite(output_pin2, !digitalRead(output_pin2)); });
}

// The loop function is called in an endless loop during program execution.
// It simply calls `app.tick()` which will then execute all events as needed.
void loop() { app.tick(); }
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();
}
11 changes: 7 additions & 4 deletions examples/raw_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

using namespace sensesp;

reactesp::EventLoop app;

ObservableValue<bool> toggler;

void setup() {
Expand All @@ -21,7 +19,7 @@ void setup() {
->set_wifi("Hat Labs Sensors", "kanneluuri2406")
->get_app();

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

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

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

0 comments on commit 57264e4

Please sign in to comment.