Incrementing date workaround for dead RTC battery (#1479)

* Enable f_utime function
* Added file_update_date() function
* Added rtc_battery_workaround() function
* Update ui_navigation.cpp
This commit is contained in:
Mark Thompson 2023-10-09 05:04:46 -05:00 committed by GitHub
parent e5546159c5
commit 65e71508ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 5 deletions

View File

@ -325,6 +325,14 @@ FATTimestamp file_created_date(const std::filesystem::path& file_path) {
return {filinfo.fdate, filinfo.ftime};
}
std::filesystem::filesystem_error file_update_date(const std::filesystem::path& file_path, FATTimestamp timestamp) {
FILINFO filinfo{};
filinfo.fdate = timestamp.FAT_date;
filinfo.ftime = timestamp.FAT_time;
return f_utime(reinterpret_cast<const TCHAR*>(file_path.c_str()), &filinfo);
}
std::filesystem::filesystem_error make_new_file(
const std::filesystem::path& file_path) {
File f;

View File

@ -270,6 +270,7 @@ std::filesystem::filesystem_error rename_file(const std::filesystem::path& file_
std::filesystem::filesystem_error copy_file(const std::filesystem::path& file_path, const std::filesystem::path& dest_path);
FATTimestamp file_created_date(const std::filesystem::path& file_path);
std::filesystem::filesystem_error file_update_date(const std::filesystem::path& file_path, FATTimestamp timestamp);
std::filesystem::filesystem_error make_new_file(const std::filesystem::path& file_path);
std::filesystem::filesystem_error make_new_directory(const std::filesystem::path& dir_path);
std::filesystem::filesystem_error ensure_directory(const std::filesystem::path& dir_path);

View File

@ -159,6 +159,8 @@ SystemStatusView::SystemStatusView(
&status_icons,
});
rtc_battery_workaround();
if (pmem::should_use_sdcard_for_pmem()) {
pmem::load_persistent_settings_from_file();
}
@ -360,6 +362,55 @@ void SystemStatusView::on_title() {
nav_.pop();
}
void SystemStatusView::rtc_battery_workaround() {
if (sd_card::status() != sd_card::Status::Mounted)
return;
uint16_t year;
uint8_t month;
uint8_t day;
FATTimestamp timestamp;
rtc::RTC datetime;
rtcGetTime(&RTCD1, &datetime);
// if year is 0000, assume RTC battery is dead
if (datetime.year() == 0) {
// if timestamp file is present, use it's date and add 1 day
if (std::filesystem::file_exists(DATE_FILEFLAG)) {
timestamp = file_created_date(DATE_FILEFLAG);
year = (timestamp.FAT_date >> 9) + 1980;
month = (timestamp.FAT_date >> 5) & 0xF;
day = timestamp.FAT_date & 0x1F;
// bump to next month at 28 days for simplicity
if (++day > 28) {
day = 1;
if (++month > 12) {
month = 1;
++year;
}
}
} else {
make_new_file(DATE_FILEFLAG);
year = 1980;
month = 1;
day = 1;
}
// update RTC (keeps ticking while powered on regardless of RTC battery condition)
rtc::RTC new_datetime{year, month, day, datetime.hour(), datetime.minute(), datetime.second()};
rtcSetTime(&RTCD1, &new_datetime);
// update file date
timestamp.FAT_date = ((year - 1980) << 9) | ((uint16_t)month << 5) | day;
timestamp.FAT_time = 0;
file_update_date(DATE_FILEFLAG, timestamp);
}
}
/* Information View *****************************************************/
InformationView::InformationView(
@ -376,11 +427,8 @@ InformationView::InformationView(
&ltime});
version.set_style(&style_infobar);
ltime.set_hide_clock(pmem::hide_clock());
ltime.set_style(&style_infobar);
ltime.set_seconds_enabled(true);
ltime.set_date_enabled(pmem::clock_with_date());
refresh();
set_dirty();
}

View File

@ -44,6 +44,9 @@
#include <vector>
#include <utility>
// for incrementing fake date when RTC battery is dead
#define DATE_FILEFLAG u"/SETTINGS/DATE_FILEFLAG"
using namespace sd_card;
namespace ui {
@ -235,6 +238,7 @@ class SystemStatusView : public View {
void on_title();
void refresh();
void on_clk();
void rtc_battery_workaround();
MessageHandlerRegistration message_handler_refresh{
Message::ID::StatusRefresh,

View File

@ -47,7 +47,7 @@
#define _USE_EXPAND 0
/* This option switches f_expand function. (0:Disable or 1:Enable) */
#define _USE_CHMOD 0
#define _USE_CHMOD 1
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
/ (0:Disable or 1:Enable) Also _FS_READONLY needs to be 0 to enable this option. */