diff --git a/meson.build b/meson.build index 31165a75..dff22416 100644 --- a/meson.build +++ b/meson.build @@ -31,6 +31,7 @@ openrtx_src = ['openrtx/src/core/state.c', 'openrtx/src/core/dsp.cpp', 'openrtx/src/core/cps.c', 'openrtx/src/core/crc.c', + 'openrtx/src/core/datetime.c', 'openrtx/src/core/openrtx.c', 'openrtx/src/core/audio_codec.c', 'openrtx/src/core/data_conversion.c', diff --git a/openrtx/include/core/datetime.h b/openrtx/include/core/datetime.h new file mode 100644 index 00000000..a2996225 --- /dev/null +++ b/openrtx/include/core/datetime.h @@ -0,0 +1,71 @@ +/*************************************************************************** + * Copyright (C) 2022 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN, * + * Silvano Seva IU2KWO, * + * Frederik Saraci IU2NRO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#ifndef DATETIME_H +#define DATETIME_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief Date and time type + * + * Data type representing current date and time, optimized for minimum space + * occupancy. + */ +typedef struct +{ + uint8_t hour : 5; // Hours (0-23) + uint8_t minute : 6; // Minutes (0-59) + uint8_t second : 6; // Seconds (0-59) + uint8_t day : 3; // Day of the week (1-7) + uint8_t date : 5; // Day of the month (1-31) + uint8_t month : 4; // Month (1-12) + uint8_t year : 7; // Year (0-99) + uint8_t : 4; // Padding to 40 bits +} +datetime_t; + +/** + * Convert from UTC time to local time, given the destination timezone. + * + * @param utc_time: UTC time. + * @param timezone: destination time zone. + * @return converted local time. + */ +datetime_t utcToLocalTime(const datetime_t utc_time, const int8_t timezone); + +/** + * Convert local time to UTC, given the source timezone. + * + * @param local_time: local time. + * @param timezone: source time zone. + * @return converted UTC time. + */ +datetime_t localTimeToUtc(const datetime_t local_time, const int8_t timezone); + +#ifdef __cplusplus +} +#endif + +#endif /* DATATYPES_H */ diff --git a/openrtx/include/core/gps.h b/openrtx/include/core/gps.h index 232a4c46..d8ab8c26 100644 --- a/openrtx/include/core/gps.h +++ b/openrtx/include/core/gps.h @@ -40,19 +40,19 @@ sat_t; */ typedef struct { - curTime_t timestamp; // Timestamp of the latest GPS update - uint8_t fix_quality; // 0: no fix, 1: GPS, 2: GPS SPS, 3: GPS PPS - uint8_t fix_type; // 0: no fix, 1: 2D, 2: 3D - uint8_t satellites_tracked; // Number of tracked satellites - uint8_t satellites_in_view; // Satellites in view - sat_t satellites[12]; // Details about satellites in view - uint32_t active_sats; // Bitmap representing which sats are part of the fix - float latitude; // Latitude coordinates - float longitude; // Longitude coordinates - float altitude; // Antenna altitude above mean sea level (geoid) in m - float speed; // Ground speed in km/h - float tmg_mag; // Course over ground, degrees, magnetic - float tmg_true; // Course over ground, degrees, true + datetime_t timestamp; // Timestamp of the latest GPS update + uint8_t fix_quality; // 0: no fix, 1: GPS, 2: GPS SPS, 3: GPS PPS + uint8_t fix_type; // 0: no fix, 1: 2D, 2: 3D + uint8_t satellites_tracked; // Number of tracked satellites + uint8_t satellites_in_view; // Satellites in view + sat_t satellites[12]; // Details about satellites in view + uint32_t active_sats; // Bitmap representing which sats are part of the fix + float latitude; // Latitude coordinates + float longitude; // Longitude coordinates + float altitude; // Antenna altitude above mean sea level (geoid) in m + float speed; // Ground speed in km/h + float tmg_mag; // Course over ground, degrees, magnetic + float tmg_true; // Course over ground, degrees, true } gps_t; diff --git a/openrtx/include/core/state.h b/openrtx/include/core/state.h index 06850b64..782dd652 100644 --- a/openrtx/include/core/state.h +++ b/openrtx/include/core/state.h @@ -45,7 +45,7 @@ m17_t; */ typedef struct { - curTime_t time; + datetime_t time; uint16_t v_bat; uint8_t charge; float rssi; @@ -107,17 +107,4 @@ void state_update(); */ void state_resetSettingsAndVfo(); -/** - * The RTC and state.time are set to UTC time - * Use this function to get local time from UTC time based on timezone setting - */ -curTime_t state_getLocalTime(curTime_t utc_time); - -/** - * The RTC and state.time are set to UTC time - * Use this function to get UTC time from local time based on timezone setting - */ -curTime_t state_getUTCTime(curTime_t local_time); - - #endif /* STATE_H */ diff --git a/openrtx/include/core/ui.h b/openrtx/include/core/ui.h index 6136b976..1d6fcb8c 100644 --- a/openrtx/include/core/ui.h +++ b/openrtx/include/core/ui.h @@ -178,7 +178,7 @@ typedef struct ui_state_t char new_tx_freq_buf[14]; #ifdef HAS_RTC // Variables used for Time & Date input - curTime_t new_timedate; + datetime_t new_timedate; char new_date_buf[9]; char new_time_buf[9]; #endif diff --git a/openrtx/include/interfaces/rtc.h b/openrtx/include/interfaces/rtc.h index fd94c546..ae4baca1 100644 --- a/openrtx/include/interfaces/rtc.h +++ b/openrtx/include/interfaces/rtc.h @@ -20,6 +20,7 @@ #ifndef RTC_H #define RTC_H +#include #include /** @@ -30,18 +31,6 @@ * to the internal lithium backup battery. */ -typedef struct -{ - uint8_t hour : 5; /* Hours (0-23) */ - uint8_t minute : 6; /* Minutes (0-59) */ - uint8_t second : 6; /* Seconds (0-59) */ - uint8_t day : 3; /* Day of the week (1-7) */ - uint8_t date : 5; /* Day of the month (1-31) */ - uint8_t month : 4; /* Month (1-12) */ - uint8_t year : 7; /* Year (0-99) */ - uint8_t : 4; /* Padding to 40 bits */ -}curTime_t; - /** * Initialise and start RTC. */ @@ -57,7 +46,7 @@ void rtc_terminate(); * @param t: struct of type curTime_t, whose content is used to initialise both * clock and calendar registers. */ -void rtc_setTime(curTime_t t); +void rtc_setTime(datetime_t t); /** * Set RTC clock keeping untouched the calendar part. @@ -79,7 +68,7 @@ void rtc_setDate(uint8_t date, uint8_t month, uint8_t year); * Get current date and time. * @return structure of type curTime_t with current clock and calendar values. */ -curTime_t rtc_getTime(); +datetime_t rtc_getTime(); /** * Activate daylight saving time (DST), adding one hour to the current time. diff --git a/openrtx/src/core/datetime.c b/openrtx/src/core/datetime.c new file mode 100644 index 00000000..f6a2ac59 --- /dev/null +++ b/openrtx/src/core/datetime.c @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2022 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN, * + * Silvano Seva IU2KWO, * + * Frederik Saraci IU2NRO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#include + + +datetime_t utcToLocalTime(const datetime_t utc_time, const int8_t timezone) +{ + datetime_t local_time = utc_time; + + if(local_time.hour + timezone >= 24) + { + local_time.hour = local_time.hour - 24 + timezone; + local_time.date += 1; + } + else if(local_time.hour + timezone < 0) + { + local_time.hour = local_time.hour + 24 + timezone; + local_time.date -= 1; + } + else + { + local_time.hour += timezone; + } + + return local_time; +} + +datetime_t localTimeToUtc(const datetime_t local_time, const int8_t timezone) +{ + datetime_t utc_time = local_time; + + if(utc_time.hour - timezone >= 24) + { + utc_time.hour = utc_time.hour - 24 - timezone; + utc_time.date += 1; + } + else if(utc_time.hour - timezone < 0) + { + utc_time.hour = utc_time.hour + 24 - timezone; + utc_time.date -= 1; + } + else + { + utc_time.hour -= timezone; + } + + return utc_time; +} diff --git a/openrtx/src/core/state.c b/openrtx/src/core/state.c index fe463b43..493fdc18 100644 --- a/openrtx/src/core/state.c +++ b/openrtx/src/core/state.c @@ -128,39 +128,3 @@ void state_resetSettingsAndVfo() state.settings = default_settings; state.channel = cps_getDefaultChannel(); } - -curTime_t state_getLocalTime(curTime_t utc_time) -{ - curTime_t local_time = utc_time; - if(local_time.hour + state.settings.utc_timezone >= 24) - { - local_time.hour = local_time.hour - 24 + state.settings.utc_timezone; - local_time.date += 1; - } - else if(local_time.hour + state.settings.utc_timezone < 0) - { - local_time.hour = local_time.hour + 24 + state.settings.utc_timezone; - local_time.date -= 1; - } - else - local_time.hour += state.settings.utc_timezone; - return local_time; -} - -curTime_t state_getUTCTime(curTime_t local_time) -{ - curTime_t utc_time = local_time; - if(utc_time.hour - state.settings.utc_timezone >= 24) - { - utc_time.hour = utc_time.hour - 24 - state.settings.utc_timezone; - utc_time.date += 1; - } - else if(utc_time.hour - state.settings.utc_timezone < 0) - { - utc_time.hour = utc_time.hour + 24 - state.settings.utc_timezone; - local_time.date -= 1; - } - else - utc_time.hour -= state.settings.utc_timezone; - return utc_time; -} diff --git a/openrtx/src/ui/ui.c b/openrtx/src/ui/ui.c index 51f4ebe6..f50b3ffa 100644 --- a/openrtx/src/ui/ui.c +++ b/openrtx/src/ui/ui.c @@ -461,7 +461,7 @@ freq_t _ui_freq_add_digit(freq_t freq, uint8_t pos, uint8_t number) } #ifdef HAS_RTC -void _ui_timedate_add_digit(curTime_t *timedate, uint8_t pos, uint8_t number) +void _ui_timedate_add_digit(datetime_t *timedate, uint8_t pos, uint8_t number) { switch(pos) { @@ -1461,7 +1461,7 @@ void ui_updateFSM(event_t event, bool *sync_rtx) state.ui_screen = SETTINGS_TIMEDATE_SET; // Reset input position and selection ui_state.input_position = 0; - memset(&ui_state.new_timedate, 0, sizeof(curTime_t)); + memset(&ui_state.new_timedate, 0, sizeof(datetime_t)); } else if(msg.keys & KEY_ESC) _ui_menuBack(MENU_SETTINGS); @@ -1475,7 +1475,8 @@ void ui_updateFSM(event_t event, bool *sync_rtx) break; // Return to Time&Date menu, saving values // NOTE: The user inserted a local time, we must save an UTC time - curTime_t utc_time = state_getUTCTime(ui_state.new_timedate); + datetime_t utc_time = localTimeToUtc(ui_state.new_timedate, + state.settings.utc_timezone); rtc_setTime(utc_time); state.time = utc_time; state.ui_screen = SETTINGS_TIMEDATE; diff --git a/openrtx/src/ui/ui_main.c b/openrtx/src/ui/ui_main.c index 6230bba8..62c53f7d 100644 --- a/openrtx/src/ui/ui_main.c +++ b/openrtx/src/ui/ui_main.c @@ -37,7 +37,8 @@ void _ui_drawMainTop() { #ifdef HAS_RTC // Print clock on top bar - curTime_t local_time = state_getLocalTime(last_state.time); + datetime_t local_time = utcToLocalTime(last_state.time, + last_state.settings.utc_timezone); gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER, color_white, "%02d:%02d:%02d", local_time.hour, local_time.minute, local_time.second); diff --git a/openrtx/src/ui/ui_menu.c b/openrtx/src/ui/ui_menu.c index 30f395d2..b22e8de9 100644 --- a/openrtx/src/ui/ui_menu.c +++ b/openrtx/src/ui/ui_menu.c @@ -569,7 +569,8 @@ void _ui_drawSettingsGPS(ui_state_t* ui_state) void _ui_drawSettingsTimeDate() { gfx_clearScreen(); - curTime_t local_time = state_getLocalTime(last_state.time); + datetime_t local_time = utcToLocalTime(last_state.time, + last_state.settings.utc_timezone); // Print "Time&Date" on top bar gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER, color_white, "Time&Date"); diff --git a/platform/mcu/STM32F4xx/drivers/rtc.c b/platform/mcu/STM32F4xx/drivers/rtc.c index 493e902c..f8b2656a 100644 --- a/platform/mcu/STM32F4xx/drivers/rtc.c +++ b/platform/mcu/STM32F4xx/drivers/rtc.c @@ -43,7 +43,7 @@ void rtc_terminate() RCC->BDCR &= ~ RCC_BDCR_RTCEN | RCC_BDCR_LSEON; } -void rtc_setTime(curTime_t t) +void rtc_setTime(datetime_t t) { /* * Convert values to BCD representation placing data in the correct @@ -77,7 +77,7 @@ void rtc_setTime(curTime_t t) void rtc_setHour(uint8_t hours, uint8_t minutes, uint8_t seconds) { - curTime_t t = rtc_getTime(); + datetime_t t = rtc_getTime(); t.hour = hours; t.minute = minutes; t.second = seconds; @@ -86,16 +86,16 @@ void rtc_setHour(uint8_t hours, uint8_t minutes, uint8_t seconds) void rtc_setDate(uint8_t date, uint8_t month, uint8_t year) { - curTime_t t = rtc_getTime(); + datetime_t t = rtc_getTime(); t.date = date; t.month = month; t.year = year; rtc_setTime(t); } -curTime_t rtc_getTime() +datetime_t rtc_getTime() { - curTime_t t; + datetime_t t; /* * Obtain time and date values in BCD format from RTC registers, and fill diff --git a/platform/mcu/x86_64/drivers/rtc.c b/platform/mcu/x86_64/drivers/rtc.c index cfb8db73..5f121588 100644 --- a/platform/mcu/x86_64/drivers/rtc.c +++ b/platform/mcu/x86_64/drivers/rtc.c @@ -31,8 +31,10 @@ void rtc_terminate() printf("rtc_shutdown()\n"); } -void rtc_setTime(__attribute__((unused)) curTime_t t) +void rtc_setTime(datetime_t t) { + (void) t; + printf("rtc_setTime(t)\n"); } @@ -46,9 +48,9 @@ void rtc_setDate(uint8_t date, uint8_t month, uint8_t year) printf("rtc_setDate(%d, %d, %d)\n", date, month, year); } -curTime_t rtc_getTime() +datetime_t rtc_getTime() { - curTime_t t; + datetime_t t; time_t rawtime; struct tm * timeinfo;