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
"
19
#include "
sensesp/sensors/digital_input.h
"
20
#include "
sensesp/transforms/lambda_transform.h
"
21
#include "
sensesp/transforms/time_counter.h
"
22
#include "
sensesp/transforms/frequency.h
"
23
#include "
sensesp_app_builder.h
"
24
25
using namespace
sensesp
;
26
27
reactesp::ReactESP
app
;
28
29
unsigned
long
cycle_start_time
= 0;
30
unsigned
long
freq_start_time
= 0;
31
int
freq
= 0;
32
33
void
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
91
frequency
->
connect_to
(
propulsion_state
);
92
93
// create engine hours counter using PersistentDuration
94
auto
*
engine_hours
=
95
new
TimeCounter<float>
(
"/Transforms/Engine Hours"
);
96
97
frequency
->
connect_to
(
engine_hours
);
98
99
// create and connect the frequency output object
100
frequency
->
connect_to
(
101
new
SKOutput<float>
(
"propulsion.main.revolutions"
,
""
,
102
new
SKMetadata
(
"Hz"
,
"Main Engine Revolutions"
)));
103
104
// create and connect the propulsion state output object
105
propulsion_state
->
connect_to
(
106
new
SKOutput<String>
(
"propulsion.main.state"
,
""
,
107
new
SKMetadata
(
""
,
"Main Engine State"
)));
108
109
// create and connect the engine hours output object
110
engine_hours
->
connect_to
(
111
new
SKOutput<float>
(
"propulsion.main.runTime"
,
""
,
112
new
SKMetadata
(
"s"
,
"Main Engine running time"
)));
113
114
// Start the SensESP application running
115
sensesp_app
->
start
();
116
117
}
118
119
void
loop
() {
app
.tick(); }
sensesp::DigitalInputCounter
DigitalInputCounter counts interrupts and reports the count every read_delay ms.
Definition
digital_input.h:89
sensesp::Frequency
Transforms its input into frequency (Hz: cycles per second)
Definition
frequency.h:21
sensesp::LambdaTransform
Construct a new transform based on a single function.
Definition
lambda_transform.h:71
sensesp::SKMetadata
Holds Signal K meta data that is associated with the sk_path an SKEmitter class may optionally send t...
Definition
signalk_metadata.h:24
sensesp::SensESPAppBuilder
A class for quickly configuring a SensESP application object before wiring up your sensors.
Definition
sensesp_app_builder.h:16
sensesp::SensESPAppBuilder::get_app
SensESPApp * get_app() override final
Get the SensESPApp object.
Definition
sensesp_app_builder.h:206
sensesp::SensESPBaseApp::start
virtual void start()
Start the app (activate all the subcomponents)
Definition
sensesp_base_app.cpp:47
sensesp::ValueProducer::connect_to
void connect_to(ValueConsumer< T > *consumer, uint8_t input_channel=0)
Definition
valueproducer.h:42
digital_input.h
frequency.h
lambda_transform.h
cycle_start_time
unsigned long cycle_start_time
Definition
main.cpp:29
setup
void setup()
Definition
main.cpp:33
freq_start_time
unsigned long freq_start_time
Definition
main.cpp:30
freq
int freq
Definition
main.cpp:31
app
reactesp::ReactESP app
Definition
main.cpp:27
loop
void loop()
Definition
main.cpp:119
sensesp
Definition
sensesp.h:22
sensesp::sensesp_app
SensESPApp * sensesp_app
Definition
sensesp_app.cpp:89
sensesp::SetupSerialDebug
void SetupSerialDebug(uint32_t baudrate)
Definition
sensesp_base_app.cpp:5
sensesp_app.h
sensesp_app_builder.h
time_counter.h
src
main.cpp
Generated by
1.9.8