Add RTC time persistence to flash

Save last known unix timestamp to /rtc_save on shutdown and restore it
on startup, so the clock shows correct time immediately after restart
without waiting for GPS fix.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jakub
2026-05-10 18:23:25 +02:00
co-authored by Claude Sonnet 4.6
parent 6cf8544979
commit 0fc12b60fe
5 changed files with 25 additions and 0 deletions
+20
View File
@@ -288,6 +288,26 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_
} }
} }
void DataStore::saveRTCTime() {
uint32_t t = _clock->getCurrentTime();
if (t < 1000000000UL) return; // don't save if time not yet synced
File file = openWrite(_fs, "/rtc_save");
if (file) {
file.write((uint8_t *)&t, sizeof(t));
file.close();
}
}
void DataStore::restoreRTCTime() {
File file = openRead(_fs, "/rtc_save");
if (file) {
uint32_t t = 0;
file.read((uint8_t *)&t, sizeof(t));
file.close();
if (t > 1000000000UL) _clock->setCurrentTime(t);
}
}
void DataStore::loadContacts(DataStoreHost* host) { void DataStore::loadContacts(DataStoreHost* host) {
File file = openRead(_getContactsChannelsFS(), "/contacts3"); File file = openRead(_getContactsChannelsFS(), "/contacts3");
if (file) { if (file) {
+2
View File
@@ -49,6 +49,8 @@ public:
bool removeFile(FILESYSTEM* fs, const char* filename); bool removeFile(FILESYSTEM* fs, const char* filename);
uint32_t getStorageUsedKb() const; uint32_t getStorageUsedKb() const;
uint32_t getStorageTotalKb() const; uint32_t getStorageTotalKb() const;
void saveRTCTime();
void restoreRTCTime();
private: private:
FILESYSTEM* _getContactsChannelsFS() const { if (_fsExtra) return _fsExtra; return _fs;}; FILESYSTEM* _getContactsChannelsFS() const { if (_fsExtra) return _fsExtra; return _fs;};
+1
View File
@@ -165,6 +165,7 @@ protected:
public: public:
void savePrefs() { _store->savePrefs(_prefs, sensors.node_lat, sensors.node_lon); } void savePrefs() { _store->savePrefs(_prefs, sensors.node_lat, sensors.node_lon); }
void saveRTCTime() { _store->saveRTCTime(); }
#if ENV_INCLUDE_GPS == 1 #if ENV_INCLUDE_GPS == 1
void applyGpsPrefs() { void applyGpsPrefs() {
+1
View File
@@ -211,6 +211,7 @@ void setup() {
#error "need to define filesystem" #error "need to define filesystem"
#endif #endif
store.restoreRTCTime();
sensors.begin(); sensors.begin();
#if ENV_INCLUDE_GPS == 1 #if ENV_INCLUDE_GPS == 1
@@ -986,6 +986,7 @@ void UITask::setCurrScreen(UIScreen* c) {
hardware-agnostic pre-shutdown activity should be done here hardware-agnostic pre-shutdown activity should be done here
*/ */
void UITask::shutdown(bool restart){ void UITask::shutdown(bool restart){
the_mesh.saveRTCTime();
#ifdef PIN_BUZZER #ifdef PIN_BUZZER
/* note: we have a choice here - /* note: we have a choice here -