Commit 1e7f7e98 authored by Theodor-Adrian Stana's avatar Theodor-Adrian Stana

settings: Added functions, changed the GMT offset setting type

The setting_apply() and setting_get() functions were added, to make it easier to
work with the settings structs. Also, to make the GMT offset setting work with
these two functions, the setting was split into two settings, now of type
setting_t.
parent ec86e02d
......@@ -29,7 +29,6 @@
#include "widgets/status_bar.h"
#include "widgets/spinbox.h"
#include "settings/settings.h"
#include <time.h>
static const int DIST_X = 24;
static const int POS_X = 10;
......@@ -98,30 +97,34 @@ static bool is_valid(void)
// Sets the hour from spinboxes to GMT offset variable
static void save(void)
{
struct tm time = setting_gmt_ofs;
int hr = setting_get(&setting_gmt_ofs_hr);
int min = setting_get(&setting_gmt_ofs_min);
time.tm_hour = sb_digit(H1) * 10 + sb_digit(H2);
time.tm_min = sb_digit(M1) * 10 + sb_digit(M2);
hr = sb_digit(H1) * 10 + sb_digit(H2);
min = sb_digit(M1) * 10 + sb_digit(M2);
if (sb_sign() == '-')
time.tm_hour *= -1;
hr *= -1;
setting_gmt_ofs = time;
/* setting_apply makes sure no interrupts occur while applying */
setting_apply(&setting_gmt_ofs_hr, hr);
setting_apply(&setting_gmt_ofs_min, min);
}
// Sets the spinboxes to the current hour
static void load(void)
{
struct tm time = setting_gmt_ofs;
int hr = setting_get(&setting_gmt_ofs_hr);
int min = setting_get(&setting_gmt_ofs_min);
if (time.tm_hour < 0) {
time.tm_hour *= -1;
if (hr < 0) {
hr *= -1;
spinbox_set_value(&sb_time[S], '-');
}
spinbox_set_value(&sb_time[H1], (time.tm_hour / 10) + '0');
spinbox_set_value(&sb_time[H2], (time.tm_hour % 10) + '0');
spinbox_set_value(&sb_time[M1], (time.tm_min / 10) + '0');
spinbox_set_value(&sb_time[M2], (time.tm_min % 10) + '0');
spinbox_set_value(&sb_time[H1], (hr / 10) + '0');
spinbox_set_value(&sb_time[H2], (hr % 10) + '0');
spinbox_set_value(&sb_time[M1], (min / 10) + '0');
spinbox_set_value(&sb_time[M2], (min % 10) + '0');
}
void set_gmt_ofs_main(void* params) {
......
......@@ -42,8 +42,8 @@ void set_time_fr_gps_main(void *params)
time.tm_year = gpstime.yr;
time.tm_mon = gpstime.mon;
time.tm_mday = gpstime.day;
time.tm_hour = gpstime.hr + setting_gmt_ofs.tm_hour;
time.tm_min = gpstime.min + setting_gmt_ofs.tm_min;
time.tm_hour = gpstime.hr + setting_get(&setting_gmt_ofs_hr);
time.tm_min = gpstime.min + setting_get(&setting_gmt_ofs_min);
time.tm_sec = gpstime.sec;
time.tm_isdst = 0;
......
......@@ -25,11 +25,16 @@
#include <stdio.h>
#include <string.h>
#include <task.h>
setting_t setting_gps_on = { "GPS on", 1, 2 };
setting_t setting_coord_style = { "Coord style", 1, 3 };
struct tm setting_gmt_ofs = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
setting_t setting_gps_sets_time = { "GPS sets time", 1, 2 };
/* Settings with nrvals == 0 do not wrap around, they are set externally */
setting_t setting_gmt_ofs_hr = { "GMT offset hours" , 0, 0 };
setting_t setting_gmt_ofs_min = { "GMT offset minutes", 0, 0 };
void setting_change(setting_t *setting)
{
int v = setting->val;
......@@ -40,5 +45,17 @@ void setting_change(setting_t *setting)
sprintf(s, ": %d", v);
setting->val = v;
setting_apply(setting, v);
}
void setting_apply(setting_t *setting, int val)
{
taskENTER_CRITICAL();
setting->val = val;
taskEXIT_CRITICAL();
}
int setting_get(setting_t *setting)
{
return setting->val;
}
......@@ -30,8 +30,6 @@
#include "application.h"
#include <time.h>
typedef struct setting {
char name[16];
int val;
......@@ -43,13 +41,16 @@ extern application set_date;
extern application set_gmt_ofs;
extern application set_time_fr_gps;
void setting_change(setting_t *setting);
extern setting_t setting_gps_on;
extern setting_t setting_coord_style;
extern struct tm setting_gmt_ofs;
extern setting_t setting_gmt_ofs_hr;
extern setting_t setting_gmt_ofs_min;
extern setting_t setting_gps_sets_time;
void setting_change(setting_t *setting);
void setting_apply(setting_t *setting, int val);
int setting_get(setting_t *setting);
#endif /* SETTINGS_H */
......@@ -54,7 +54,7 @@ static void gpsbkgnd_task(void *params)
/* Previous and current state of GPS on setting at timer tick */
pgpson = cgpson;
cgpson = setting_gps_on.val;
cgpson = setting_get(&setting_gps_on);
/* Pulse GPS ON_OFF pin if setting changed */
if ((pgpson != cgpson) && !firstrun)
......@@ -72,7 +72,7 @@ static void gpsbkgnd_task(void *params)
}
/* Set time from GPS at first fix or midday */
if (setting_gps_sets_time.val && gps_fixed()) {
if (setting_get(&setting_gps_sets_time) && gps_fixed()) {
time = clock_get_time();
if (firstfix ||
......@@ -82,9 +82,10 @@ static void gpsbkgnd_task(void *params)
time.tm_year = gpstime.yr;
time.tm_mon = gpstime.mon;
time.tm_mday = gpstime.day;
time.tm_hour = gpstime.hr + setting_gmt_ofs.tm_hour;
time.tm_min = gpstime.min + setting_gmt_ofs.tm_min;
time.tm_hour = gpstime.hr + setting_get(&setting_gmt_ofs_hr);
time.tm_min = gpstime.min + setting_get(&setting_gmt_ofs_min);
time.tm_sec = gpstime.sec;
time.tm_isdst = 0;
clock_set_time(&time);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment