From 5e0a972b1856be8d90c07fa2baffbdb44675c21e Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Sun, 14 Sep 2025 08:08:15 +0200 Subject: [PATCH] fix time --- CMakeLists.txt | 6 +++++- lwipopts.h | 4 +--- net_utils.cpp | 1 - smart_alarm.cpp | 17 +++++++++++------ timezones.h | 29 +++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 timezones.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 92ecb50..985fd41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(PICO_SDK_PATH "/usr/share/pico-sdk") set(PICO_BOARD pico_w CACHE STRING "Board type") +set(PICO_STACK_SIZE 0x2000) # Pull in Raspberry Pi Pico SDK (must be before project) include(pico_sdk_import.cmake) @@ -29,7 +30,7 @@ pico_sdk_init() # Add executable. Default name is the project name, version 0.1 add_executable(smart_alarm smart_alarm.cpp - net_utils.cpp) + net_utils.cpp timezones.cpp) pico_set_program_name(smart_alarm "smart_alarm") pico_set_program_version(smart_alarm "0.2") @@ -62,6 +63,9 @@ target_compile_definitions(smart_alarm PRIVATE SNTP_SET_SYSTEM_TIME_NTP=sync_system_time SNTP_UPDATE_DELAY=1800000 # 30 minutes ) +target_compile_options(smart_alarm PRIVATE + -Wno-write-strings +) pico_add_extra_outputs(smart_alarm) diff --git a/lwipopts.h b/lwipopts.h index db49f95..32fa885 100644 --- a/lwipopts.h +++ b/lwipopts.h @@ -246,7 +246,7 @@ hardware: #ifdef __cplusplus extern "C" { #endif - u32_t lwip_rand(void); +u32_t lwip_rand(void); #ifdef __cplusplus } #endif @@ -263,8 +263,6 @@ void sync_system_time(unsigned int sec, unsigned int usec); } #endif - - #endif /* __LWIPOPTS_H__ */ /*****END OF FILE****/ diff --git a/net_utils.cpp b/net_utils.cpp index 52c87c6..c1bc28a 100644 --- a/net_utils.cpp +++ b/net_utils.cpp @@ -1,6 +1,5 @@ #include "hardware/clocks.h" #include "lwip/ip_addr.h" -#include "nlohmann/json.hpp" #include "pico/cyw43_arch.h" #include "pico/util/datetime.h" #include diff --git a/smart_alarm.cpp b/smart_alarm.cpp index 3d4ac4b..626ef3a 100644 --- a/smart_alarm.cpp +++ b/smart_alarm.cpp @@ -17,6 +17,7 @@ #include "lwip/init.h" #include "lwip/tcp.h" #include "net_utils.h" +#include "timezones.h" // SPI Defines // We are going to use SPI 0, and allocate it to the following GPIO pins @@ -27,6 +28,12 @@ #define PIN_CS 17 #define PIN_SCK 18 #define PIN_MOSI 19 +#define PIN_DC 16 +#define PIN_RST 20 + +#define NTP_DELTA 2208988800UL + +volatile u16_t timezone_index = 353; extern "C" void sync_system_time(unsigned int sec, unsigned int usec) { if (sec == 0) { @@ -37,12 +44,8 @@ extern "C" void sync_system_time(unsigned int sec, unsigned int usec) { printf("SNTP callback received: %lu\n", (unsigned long)sec); // Convert NTP timestamp to Unix timestamp - #define NTP_DELTA 2208988800UL time_t unix_time = (time_t)(sec - NTP_DELTA); - u16_t timezone_offset = 7200; - - unix_time += timezone_offset; struct tm *time_info = gmtime(&unix_time); if (time_info == NULL) { @@ -64,11 +67,13 @@ extern "C" void sync_system_time(unsigned int sec, unsigned int usec) { .sec = static_cast(time_info->tm_sec) }; + timezone_offset(timezone_index, dt); + // Set RTC if (rtc_set_datetime(&dt)) { printf("SNTP: Time synchronized to %04d-%02d-%02d %02d:%02d:%02d %s\n", dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec, - (timezone_offset == 0) ? "UTC" : "Local"); + "Local"); } else { printf("SNTP: Failed to set RTC\n"); } @@ -85,7 +90,7 @@ extern "C" void sync_system_time(unsigned int sec, unsigned int usec) { if (rtc_set_datetime(&dt)) { printf("SNTP: Time synchronized to %04d-%02d-%02d %02d:%02d:%02d %s\n", dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec, - (timezone_offset == 0) ? "UTC" : "Local"); + "Local"); } else { printf("SNTP: Failed to set RTC\n"); } diff --git a/timezones.h b/timezones.h new file mode 100644 index 0000000..4fedb8f --- /dev/null +++ b/timezones.h @@ -0,0 +1,29 @@ +#ifndef SMART_ALARM_TIMEZONES_H +#define SMART_ALARM_TIMEZONES_H +#include "lwip/arch.h" + +typedef struct { + char zone_name[31]; // Timezone name (e.g., "Europe/Berlin") + char std_abbr[6]; // Standard time abbreviation + char dst_abbr[6]; // DST abbreviation + s8_t std_offset_hours; // Standard time UTC offset hours (-12 to +14) + u8_t std_offset_mins; // Standard time UTC offset minutes (0-59) + s8_t dst_offset_hours; // DST UTC offset hours (-12 to +14) + u8_t dst_offset_mins; // DST UTC offset minutes (0-59) + u8_t dst_start_month; // DST start month (1-12) + u8_t dst_start_week; // DST start week (1-5, where 5=last week) + u8_t dst_start_day; // DST start day of week (0=Sunday..6=Saturday) + s8_t dst_start_hour; // DST start hour (0-23) + u8_t dst_start_min; // DST start minute (0-59) + u8_t dst_end_month; // DST end month (1-12) + u8_t dst_end_week; // DST end week (1-5, where 5=last week) + u8_t dst_end_day; // DST end day of week (0=Sunday..6=Saturday) + u8_t dst_end_hour; // DST end hour (0-23) + u8_t dst_end_min; // DST end minute (0-59) + bool has_dst; // 1 if timezone uses DST, 0 otherwise + u8_t reserved; // Padding for alignment +} timezone_t; +extern const timezone_t timezones[]; + +void timezone_offset(u16_t timezone_index, datetime_t &dt); +#endif //SMART_ALARM_TIMEZONES_H \ No newline at end of file