-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcave-sleeper.ino
99 lines (84 loc) · 2.14 KB
/
cave-sleeper.ino
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "bluetooth.hpp"
#include "config.hpp"
#include "i2c.hpp"
#include "led.hpp"
#include "log.hpp"
#include "print.hpp"
#include "rtc.hpp"
#include "sensors.hpp"
#include "sleep.hpp"
#include <Arduino.h>
namespace cvslpr {
// Set to false if initialization or a component fails.
// If false, the main loop executes nothing.
static bool status_good = true;
void
setup()
{
delay(STARTUP_DELAY);
if (!init_led()) {
status_good = false;
return;
}
// Init print first so that we can inform the user of any errors
if (!init_print()) {
status_good = false;
led_signal_error_perpetual();
}
// Init
if (!init_i2c() || !init_sleep() || !init_log() || !init_rtc() ||
!init_sensors() || !init_bluetooth()) {
status_good = false;
msg_println(F("===========================\nInitialization "
"failed.\n==========================="));
led_signal_error_perpetual();
}
msg_println(F("===========================\nInitialization "
"successful.\n==========================="));
led_signal_good();
// Should RTC be initialized?
if constexpr (INIT_RTC_TIME || INIT_RTC_TIME_AND_RUN) {
init_rtc_time();
}
// If we just wanted to init the RTC, go sleep
if constexpr (INIT_RTC_TIME && !INIT_RTC_TIME_AND_RUN) {
go_sleep(true);
}
rtc_interrupt_registered = true;
}
void
loop()
{
if constexpr (!INIT_RTC_TIME || INIT_RTC_TIME_AND_RUN) {
if (status_good) {
if (bluetooth_switch_interrupt_registered) {
bluetooth_handle_transfer();
bluetooth_switch_interrupt_registered = false;
}
if (rtc_interrupt_registered) {
const auto now = get_current_time();
const auto readout = measure();
log(readout, now);
if (!set_alarm_time(now)) {
status_good = false;
msg_println(F("Setting alarm failed."));
led_signal_error_perpetual();
}
rtc_interrupt_registered = false;
}
go_sleep(!bluetooth_switch_interrupt_registered &&
!rtc_interrupt_registered);
}
}
}
} // namespace cvslpr
void
setup()
{
cvslpr::setup();
}
void
loop()
{
cvslpr::loop();
}