Add some code comments
This commit is contained in:
parent
65c7e82554
commit
cbb7c5d4e3
29
src/main.cpp
29
src/main.cpp
@ -7,7 +7,8 @@
|
|||||||
#include "storage.h"
|
#include "storage.h"
|
||||||
#include "temperature.h"
|
#include "temperature.h"
|
||||||
|
|
||||||
Temperature samples[temperatureSensorCount];
|
// The delay to perform when waiting idle (not sleeping) for next measurement
|
||||||
|
constexpr uint32_t delayWaitingForNextMeasurementMS = 50;
|
||||||
|
|
||||||
// Indicate when the next temperature measurement should be performed
|
// Indicate when the next temperature measurement should be performed
|
||||||
// Time is measured in seconds since power-on via RTC clock
|
// Time is measured in seconds since power-on via RTC clock
|
||||||
@ -48,6 +49,12 @@ uint32_t secondsUntilNextTemperatureMeasurement() {
|
|||||||
return nextTimeToMeasureTemperatureSeconds - currentTime;
|
return nextTimeToMeasureTemperatureSeconds - currentTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Start sleep if next measurement is sufficiently in the future
|
||||||
|
*
|
||||||
|
* If the measurement is less than one second in the future, don't do anything.
|
||||||
|
* If the device sleeps, then execution starts again in the `setup()` function.
|
||||||
|
*/
|
||||||
void deepSleepUntilNextTemperatureMeasurement() {
|
void deepSleepUntilNextTemperatureMeasurement() {
|
||||||
uint32_t seconds = secondsUntilNextTemperatureMeasurement();
|
uint32_t seconds = secondsUntilNextTemperatureMeasurement();
|
||||||
if (seconds == 0) {
|
if (seconds == 0) {
|
||||||
@ -74,10 +81,23 @@ bool wakeupButtonIsPressed() {
|
|||||||
return gpio_get_level(wakeupButtonPin) == 0;
|
return gpio_get_level(wakeupButtonPin) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Shift the timestamp into the future where the device is allowed to go to sleep.
|
||||||
|
*
|
||||||
|
* Updates are performed when the wake button was pressed or a device is connected via bluetooth.
|
||||||
|
*/
|
||||||
void updateStayAwakeTime() {
|
void updateStayAwakeTime() {
|
||||||
sleepStartAfterButtonPressOrConnection = time(NULL) + wakeupDurationAfterButtonPress;
|
sleepStartAfterButtonPressOrConnection = time(NULL) + wakeupDurationAfterButtonPress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check if the device should go to sleep
|
||||||
|
*
|
||||||
|
* Sleeping is allowed if no bluetooth connection exists, or if the last wake button press
|
||||||
|
* was far enough in the past.
|
||||||
|
* @return true Allow the device to go to deep sleep
|
||||||
|
* @return false Keep the device awake
|
||||||
|
*/
|
||||||
bool shouldGoToSleep() {
|
bool shouldGoToSleep() {
|
||||||
if (bluetoothIsConnected()) {
|
if (bluetoothIsConnected()) {
|
||||||
return false;
|
return false;
|
||||||
@ -102,7 +122,8 @@ void enableLED() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(serialBaudRate);
|
// No need for serial output in production
|
||||||
|
//Serial.begin(serialBaudRate);
|
||||||
|
|
||||||
// LED useless inside case
|
// LED useless inside case
|
||||||
//enableLED();
|
//enableLED();
|
||||||
@ -134,6 +155,8 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
if (shouldMeasureTemperature()) {
|
if (shouldMeasureTemperature()) {
|
||||||
Serial.println("Measuring");
|
Serial.println("Measuring");
|
||||||
|
|
||||||
|
Temperature samples[temperatureSensorCount];
|
||||||
temperaturePerformUpdate(samples);
|
temperaturePerformUpdate(samples);
|
||||||
saveTemperatures(samples);
|
saveTemperatures(samples);
|
||||||
setNextTemperatureMeasurementInterval();
|
setNextTemperatureMeasurementInterval();
|
||||||
@ -149,5 +172,5 @@ void loop() {
|
|||||||
// Otherwise control flow starts with setup() again
|
// Otherwise control flow starts with setup() again
|
||||||
deepSleepUntilNextTemperatureMeasurement();
|
deepSleepUntilNextTemperatureMeasurement();
|
||||||
}
|
}
|
||||||
delay(50);
|
delay(delayWaitingForNextMeasurementMS);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user