forked from bareboat-necessities/bbn_esp32_sensors_hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_limit_switch.h
44 lines (37 loc) · 1.04 KB
/
gpio_limit_switch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef gpio_limit_switch_h
#define gpio_limit_switch_h
#include "NmeaXDR.h"
#define LIMIT_SWITCH_PIN G2
struct LimitSwitch {
bool reached = false;
unsigned long last_limit_reported = 0UL;
};
LimitSwitch limit_switch;
void gpio_limit_switch_report() {
bool reached = !digitalRead(LIMIT_SWITCH_PIN);
if (reached != limit_switch.reached) {
limit_switch.reached = reached;
if (reached) {
gen_nmea0183_xdr("$BBXDR,S,1,,LIMIT_NEW", 1);
} else {
gen_nmea0183_xdr("$BBXDR,S,0,,LIMIT_NEW", 0);
}
limit_switch.last_limit_reported = millis();
}
if (millis() - limit_switch.last_limit_reported > 5000) {
if (limit_switch.reached) {
gen_nmea0183_xdr("$BBXDR,S,1,,LIMIT_CUR", 1);
} else {
gen_nmea0183_xdr("$BBXDR,S,0,,LIMIT_CUR", 0);
}
limit_switch.last_limit_reported = millis();
}
}
void gpio_limit_switch_try_init() {
pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP);
limit_switch.reached = !digitalRead(LIMIT_SWITCH_PIN);
app.onRepeat(50, []() {
gpio_limit_switch_report();
});
}
#endif