Add some code comments

This commit is contained in:
Christoph Hagen 2023-06-05 17:51:48 +02:00
parent 65c7e82554
commit cbb7c5d4e3

View File

@ -7,7 +7,8 @@
#include "storage.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
// Time is measured in seconds since power-on via RTC clock
@ -48,6 +49,12 @@ uint32_t secondsUntilNextTemperatureMeasurement() {
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() {
uint32_t seconds = secondsUntilNextTemperatureMeasurement();
if (seconds == 0) {
@ -74,10 +81,23 @@ bool wakeupButtonIsPressed() {
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() {
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() {
if (bluetoothIsConnected()) {
return false;
@ -102,7 +122,8 @@ void enableLED() {
}
void setup() {
Serial.begin(serialBaudRate);
// No need for serial output in production
//Serial.begin(serialBaudRate);
// LED useless inside case
//enableLED();
@ -134,6 +155,8 @@ void setup() {
void loop() {
if (shouldMeasureTemperature()) {
Serial.println("Measuring");
Temperature samples[temperatureSensorCount];
temperaturePerformUpdate(samples);
saveTemperatures(samples);
setNextTemperatureMeasurementInterval();
@ -149,5 +172,5 @@ void loop() {
// Otherwise control flow starts with setup() again
deepSleepUntilNextTemperatureMeasurement();
}
delay(50);
delay(delayWaitingForNextMeasurementMS);
}