SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
18#include "sensesp_app.h"
23#include "sensesp_app_builder.h"
24
25using namespace sensesp;
26
27reactesp::ReactESP app;
28
29unsigned long cycle_start_time = 0;
30unsigned long freq_start_time = 0;
31int freq = 0;
32
33void setup() {
34 // Some initialization boilerplate when in debug mode...
35#ifndef SERIAL_DEBUG_DISABLED
36 SetupSerialDebug(115200);
37#endif
38
39 // Create the builder object
40 SensESPAppBuilder builder;
41 sensesp_app = builder.get_app();
42
43 // set GPIO 18 to output mode
44 pinMode(18, OUTPUT);
45 app.onRepeat(10, []() {
46 if (freq == 0) {
47 if (millis() - freq_start_time >= 10000) {
48 freq = 10;
49 freq_start_time = millis();
50 } else {
51 return;
52 }
53 } else {
54 if (millis() - freq_start_time >= 1000) {
55 freq += 10;
56 freq_start_time = millis();
57 }
58 if (freq > 100) {
59 freq = 0;
60 return;
61 }
62
63 if ((millis() - cycle_start_time) >= 1000 / freq) {
64 cycle_start_time = millis();
65 } else {
66 return;
67 }
68 }
69 digitalWrite(18, !digitalRead(18));
70 });
71
72 // Create a digital input counter sensor
73 auto digin_counter =
74 new DigitalInputCounter(15, INPUT, FALLING, 500, "/Sensors/Counter");
75
76 // Create a frequency transform
77 auto* frequency = new Frequency(1, "/Transforms/Frequency");
78 digin_counter->connect_to(frequency);
79
80 // create a propulsion state lambda transform
81 auto* propulsion_state = new LambdaTransform<float, String>(
82 [](bool freq) {
83 if (freq > 0) {
84 return "started";
85 } else {
86 return "stopped";
87 }
88 },
89 "/Transforms/Propulsion State");
90
92
93 // create engine hours counter using PersistentDuration
94 auto* engine_hours =
95 new TimeCounter<float>("/Transforms/Engine Hours");
96
98
99 // create and connect the frequency output object
101 new SKOutput<float>("propulsion.main.revolutions", "",
102 new SKMetadata("Hz", "Main Engine Revolutions")));
103
104 // create and connect the propulsion state output object
106 new SKOutput<String>("propulsion.main.state", "",
107 new SKMetadata("", "Main Engine State")));
108
109 // create and connect the engine hours output object
111 new SKOutput<float>("propulsion.main.runTime", "",
112 new SKMetadata("s", "Main Engine running time")));
113
114 // Start the SensESP application running
116
117}
118
119void loop() { app.tick(); }
DigitalInputCounter counts interrupts and reports the count every read_delay ms.
Transforms its input into frequency (Hz: cycles per second)
Definition frequency.h:21
Construct a new transform based on a single function.
Holds Signal K meta data that is associated with the sk_path an SKEmitter class may optionally send t...
A class for quickly configuring a SensESP application object before wiring up your sensors.
SensESPApp * get_app() override final
Get the SensESPApp object.
virtual void start()
Start the app (activate all the subcomponents)
void connect_to(ValueConsumer< T > *consumer, uint8_t input_channel=0)
unsigned long cycle_start_time
Definition main.cpp:29
void setup()
Definition main.cpp:33
unsigned long freq_start_time
Definition main.cpp:30
int freq
Definition main.cpp:31
reactesp::ReactESP app
Definition main.cpp:27
void loop()
Definition main.cpp:119
SensESPApp * sensesp_app
void SetupSerialDebug(uint32_t baudrate)