Reorganised data structures and functions to manage date and time

This commit is contained in:
Silvano Seva 2022-06-16 09:13:36 +02:00
parent 45d919f50f
commit d16eb04696
13 changed files with 174 additions and 91 deletions

View File

@ -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',

View File

@ -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 <http://www.gnu.org/licenses/> *
***************************************************************************/
#ifndef DATETIME_H
#define DATETIME_H
#include <stdint.h>
#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 */

View File

@ -40,7 +40,7 @@ sat_t;
*/
typedef struct
{
curTime_t timestamp; // Timestamp of the latest GPS update
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

View File

@ -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 */

View File

@ -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

View File

@ -20,6 +20,7 @@
#ifndef RTC_H
#define RTC_H
#include <datetime.h>
#include <stdint.h>
/**
@ -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.

View File

@ -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 <http://www.gnu.org/licenses/> *
***************************************************************************/
#include <datetime.h>
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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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);

View File

@ -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");

View File

@ -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

View File

@ -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;