Reorganised data structures and functions to manage date and time
This commit is contained in:
parent
45d919f50f
commit
d16eb04696
|
|
@ -31,6 +31,7 @@ openrtx_src = ['openrtx/src/core/state.c',
|
||||||
'openrtx/src/core/dsp.cpp',
|
'openrtx/src/core/dsp.cpp',
|
||||||
'openrtx/src/core/cps.c',
|
'openrtx/src/core/cps.c',
|
||||||
'openrtx/src/core/crc.c',
|
'openrtx/src/core/crc.c',
|
||||||
|
'openrtx/src/core/datetime.c',
|
||||||
'openrtx/src/core/openrtx.c',
|
'openrtx/src/core/openrtx.c',
|
||||||
'openrtx/src/core/audio_codec.c',
|
'openrtx/src/core/audio_codec.c',
|
||||||
'openrtx/src/core/data_conversion.c',
|
'openrtx/src/core/data_conversion.c',
|
||||||
|
|
|
||||||
|
|
@ -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 */
|
||||||
|
|
@ -40,19 +40,19 @@ sat_t;
|
||||||
*/
|
*/
|
||||||
typedef struct
|
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_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 fix_type; // 0: no fix, 1: 2D, 2: 3D
|
||||||
uint8_t satellites_tracked; // Number of tracked satellites
|
uint8_t satellites_tracked; // Number of tracked satellites
|
||||||
uint8_t satellites_in_view; // Satellites in view
|
uint8_t satellites_in_view; // Satellites in view
|
||||||
sat_t satellites[12]; // Details about 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
|
uint32_t active_sats; // Bitmap representing which sats are part of the fix
|
||||||
float latitude; // Latitude coordinates
|
float latitude; // Latitude coordinates
|
||||||
float longitude; // Longitude coordinates
|
float longitude; // Longitude coordinates
|
||||||
float altitude; // Antenna altitude above mean sea level (geoid) in m
|
float altitude; // Antenna altitude above mean sea level (geoid) in m
|
||||||
float speed; // Ground speed in km/h
|
float speed; // Ground speed in km/h
|
||||||
float tmg_mag; // Course over ground, degrees, magnetic
|
float tmg_mag; // Course over ground, degrees, magnetic
|
||||||
float tmg_true; // Course over ground, degrees, true
|
float tmg_true; // Course over ground, degrees, true
|
||||||
}
|
}
|
||||||
gps_t;
|
gps_t;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ m17_t;
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
curTime_t time;
|
datetime_t time;
|
||||||
uint16_t v_bat;
|
uint16_t v_bat;
|
||||||
uint8_t charge;
|
uint8_t charge;
|
||||||
float rssi;
|
float rssi;
|
||||||
|
|
@ -107,17 +107,4 @@ void state_update();
|
||||||
*/
|
*/
|
||||||
void state_resetSettingsAndVfo();
|
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 */
|
#endif /* STATE_H */
|
||||||
|
|
|
||||||
|
|
@ -178,7 +178,7 @@ typedef struct ui_state_t
|
||||||
char new_tx_freq_buf[14];
|
char new_tx_freq_buf[14];
|
||||||
#ifdef HAS_RTC
|
#ifdef HAS_RTC
|
||||||
// Variables used for Time & Date input
|
// Variables used for Time & Date input
|
||||||
curTime_t new_timedate;
|
datetime_t new_timedate;
|
||||||
char new_date_buf[9];
|
char new_date_buf[9];
|
||||||
char new_time_buf[9];
|
char new_time_buf[9];
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
#ifndef RTC_H
|
#ifndef RTC_H
|
||||||
#define RTC_H
|
#define RTC_H
|
||||||
|
|
||||||
|
#include <datetime.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -30,18 +31,6 @@
|
||||||
* to the internal lithium backup battery.
|
* 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.
|
* 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
|
* @param t: struct of type curTime_t, whose content is used to initialise both
|
||||||
* clock and calendar registers.
|
* clock and calendar registers.
|
||||||
*/
|
*/
|
||||||
void rtc_setTime(curTime_t t);
|
void rtc_setTime(datetime_t t);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set RTC clock keeping untouched the calendar part.
|
* 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.
|
* Get current date and time.
|
||||||
* @return structure of type curTime_t with current clock and calendar values.
|
* @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.
|
* Activate daylight saving time (DST), adding one hour to the current time.
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -128,39 +128,3 @@ void state_resetSettingsAndVfo()
|
||||||
state.settings = default_settings;
|
state.settings = default_settings;
|
||||||
state.channel = cps_getDefaultChannel();
|
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;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -461,7 +461,7 @@ freq_t _ui_freq_add_digit(freq_t freq, uint8_t pos, uint8_t number)
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAS_RTC
|
#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)
|
switch(pos)
|
||||||
{
|
{
|
||||||
|
|
@ -1461,7 +1461,7 @@ void ui_updateFSM(event_t event, bool *sync_rtx)
|
||||||
state.ui_screen = SETTINGS_TIMEDATE_SET;
|
state.ui_screen = SETTINGS_TIMEDATE_SET;
|
||||||
// Reset input position and selection
|
// Reset input position and selection
|
||||||
ui_state.input_position = 0;
|
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)
|
else if(msg.keys & KEY_ESC)
|
||||||
_ui_menuBack(MENU_SETTINGS);
|
_ui_menuBack(MENU_SETTINGS);
|
||||||
|
|
@ -1475,7 +1475,8 @@ void ui_updateFSM(event_t event, bool *sync_rtx)
|
||||||
break;
|
break;
|
||||||
// Return to Time&Date menu, saving values
|
// Return to Time&Date menu, saving values
|
||||||
// NOTE: The user inserted a local time, we must save an UTC time
|
// 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);
|
rtc_setTime(utc_time);
|
||||||
state.time = utc_time;
|
state.time = utc_time;
|
||||||
state.ui_screen = SETTINGS_TIMEDATE;
|
state.ui_screen = SETTINGS_TIMEDATE;
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,8 @@ void _ui_drawMainTop()
|
||||||
{
|
{
|
||||||
#ifdef HAS_RTC
|
#ifdef HAS_RTC
|
||||||
// Print clock on top bar
|
// 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,
|
gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER,
|
||||||
color_white, "%02d:%02d:%02d", local_time.hour,
|
color_white, "%02d:%02d:%02d", local_time.hour,
|
||||||
local_time.minute, local_time.second);
|
local_time.minute, local_time.second);
|
||||||
|
|
|
||||||
|
|
@ -569,7 +569,8 @@ void _ui_drawSettingsGPS(ui_state_t* ui_state)
|
||||||
void _ui_drawSettingsTimeDate()
|
void _ui_drawSettingsTimeDate()
|
||||||
{
|
{
|
||||||
gfx_clearScreen();
|
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
|
// Print "Time&Date" on top bar
|
||||||
gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER,
|
gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER,
|
||||||
color_white, "Time&Date");
|
color_white, "Time&Date");
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ void rtc_terminate()
|
||||||
RCC->BDCR &= ~ RCC_BDCR_RTCEN | RCC_BDCR_LSEON;
|
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
|
* 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)
|
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.hour = hours;
|
||||||
t.minute = minutes;
|
t.minute = minutes;
|
||||||
t.second = seconds;
|
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)
|
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.date = date;
|
||||||
t.month = month;
|
t.month = month;
|
||||||
t.year = year;
|
t.year = year;
|
||||||
rtc_setTime(t);
|
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
|
* Obtain time and date values in BCD format from RTC registers, and fill
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,10 @@ void rtc_terminate()
|
||||||
printf("rtc_shutdown()\n");
|
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");
|
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);
|
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;
|
time_t rawtime;
|
||||||
struct tm * timeinfo;
|
struct tm * timeinfo;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue