From 62ee19752930df55fe9dacf7ebbc622418c36f07 Mon Sep 17 00:00:00 2001 From: Matti Airas Date: Mon, 3 Jun 2024 18:37:25 +0300 Subject: [PATCH 1/2] Add a convenience function to return the board serial number --- src/sensesp/system/serial_number.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/sensesp/system/serial_number.h diff --git a/src/sensesp/system/serial_number.h b/src/sensesp/system/serial_number.h new file mode 100644 index 000000000..5ce3d1322 --- /dev/null +++ b/src/sensesp/system/serial_number.h @@ -0,0 +1,16 @@ +#ifndef SENSESP_SRC_SENSESP_SYSTEM_SERIAL_NUMBER_H_ +#define SENSESP_SRC_SENSESP_SYSTEM_SERIAL_NUMBER_H_ + +#include + +#include + +uint64_t GetBoardSerialNumber() { + uint8_t chipid[6]; + esp_efuse_mac_get_default(chipid); + return ((uint64_t)chipid[0] << 0) + ((uint64_t)chipid[1] << 8) + + ((uint64_t)chipid[2] << 16) + ((uint64_t)chipid[3] << 24) + + ((uint64_t)chipid[4] << 32) + ((uint64_t)chipid[5] << 40); +} + +#endif // SENSESP_SRC_SENSESP_SYSTEM_SERIAL_NUMBER_H_ From c7e1969dac91f3ab663104e81085a14d81dbdfdd Mon Sep 17 00:00:00 2001 From: Matti Airas Date: Mon, 3 Jun 2024 18:38:18 +0300 Subject: [PATCH 2/2] Add support for expiring values --- src/sensesp/system/expiring_value.h | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/sensesp/system/expiring_value.h diff --git a/src/sensesp/system/expiring_value.h b/src/sensesp/system/expiring_value.h new file mode 100644 index 000000000..40360758f --- /dev/null +++ b/src/sensesp/system/expiring_value.h @@ -0,0 +1,56 @@ +#ifndef SENSESP_SRC_SENSESP_SYSTEM_EXPIRING_VALUE_H_ +#define SENSESP_SRC_SENSESP_SYSTEM_EXPIRING_VALUE_H_ + +namespace sensesp { + +/** + * @brief Value container that keeps track of its expiration time. + * + * The value is considered expired if the time since the last update is greater + * than the expiration duration. When expired, the value is replaced with an + * expiration placeholder value. + * + * @tparam T + */ +template +class ExpiringValue { + public: + ExpiringValue() + : value_{}, + expiration_duration_{1000}, + last_update_{0}, + expired_value_{T{}} {} + + ExpiringValue(T value, unsigned long expiration_duration, T expired_value) + : value_{value}, + expiration_duration_{expiration_duration}, + expired_value_{expired_value}, + last_update_{millis()} {} + + void update(T value) { + value_ = value; + last_update_ = millis(); + } + + T get() const { + if (!is_expired()) { + return value_; + } else { + return expired_value_; + } + } + + bool is_expired() const { + return millis() - last_update_ > expiration_duration_; + } + + private: + T value_; + T expired_value_; + unsigned long expiration_duration_; + unsigned long last_update_; +}; + +} // namespace sensesp + +#endif // SENSESP_SRC_SENSESP_SYSTEM_EXPIRING_VALUE_H_