Commit 59d9bd2b authored by Theodor-Adrian Stana's avatar Theodor-Adrian Stana

Add time setting at noon from GPS with a setting to enable/disable

parent 6579f5e8
......@@ -199,6 +199,7 @@ FreeRTOS/Source/portable/GCC/ARM_CM3/port.c \
src/apps/settings/set_date.c \
src/apps/settings/set_time.c \
src/apps/settings/settings.c \
src/apps/settings/set_gmt_ofs.c \
src/apps/widgets/spinbox.c \
src/apps/widgets/status_bar.c \
src/apps/application.c \
......
......@@ -45,6 +45,8 @@ menu_list settings_menu = {
{ APP, &date_icon, { .app = &set_date } },
{ SETTING, NULL, { .setting = &setting_gps_on } },
{ SETTING, NULL, { .setting = &setting_coord_style } },
{ SETTING, NULL, { .setting = &setting_gps_sets_time } },
{ APP, &gps_receiving, { .app = &set_gmt_ofs } },
{ END, NULL, { NULL } }
}
};
......
/*
* Copyright (C) 2014 Julian Lewis
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* 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, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* Settings application.
*/
#include "application.h"
#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 = 20;
static const int POS_Y = 30;
static const int SIZE_X = 16;
static const int SIZE_Y = 38;
static void set_time_redraw(struct ui_widget *w)
{
gfx_centered_text(&w->dc, &font_helv38b, POS_Y, ":", 1);
}
static struct ui_widget set_time_screen = {
&set_time_redraw,
NULL,
{ 0, 0, 127, 127 },
0,
WF_ACTIVE | WF_VISIBLE,
NULL
};
#define SPINBOX_NUMBER 4
enum SPINBOX { H1 = 0, H2, M1, M2 };
static const char const *hours1 = "012";
static const char const *minutes1 = "012345";
static const char **char_sets[SPINBOX_NUMBER] = {
&hours1, &char_digits, // character set for hours
&minutes1, &char_digits // character set for minutes
};
// Spinboxes used for setting the hour & minute.
static struct spinbox sb_time[SPINBOX_NUMBER];
// Index that indicates the active spinbox
static int sb_index;
static inline char sb_digit(int idx)
{
// convert ascii character to a digit by subtracting 0x30 ('0')
return spinbox_get_value(&sb_time[idx]) - '0';
}
// Checks if spinboxes contain correct values
static bool is_valid(void)
{
return (sb_digit(H1) < 1 && sb_digit(M1) < 6);
}
// Sets the hour from spinboxes to GMT offset variable
static void save(void)
{
struct tm time;
time.tm_hour = sb_digit(H1) * 10 + sb_digit(H2);
time.tm_min = sb_digit(M1) * 10 + sb_digit(M2);
setting_gmt_ofs = time;
}
// Sets the spinboxes to the current hour
static void load(void)
{
struct tm time = setting_gmt_ofs;
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');
}
void set_gmt_ofs_main(void* params) {
(void)(params); // suppress unused parameter warning
struct event evt;
int i;
// initialize user interface
ui_clear();
// initialize widgets before adding them
for(i = 0; i < SPINBOX_NUMBER; ++i) {
struct rect pos = {POS_X + i * DIST_X, POS_Y,
POS_X + i * DIST_X + SIZE_X, POS_Y + SIZE_Y};
spinbox_init_widget(&sb_time[i], pos, *char_sets[i]);
}
sb_index = H1;
spinbox_set_active(&sb_time[H1], true);
ui_init_widget(&set_time_screen);
for(i = 0; i < SPINBOX_NUMBER; ++i) {
ui_add_widget(&sb_time[i].widget);
}
// widget belongs to the main screen
for(i = 0; i < SPINBOX_NUMBER; ++i) {
ui_add_child(&set_time_screen, &sb_time[i].widget);
}
ui_add_widget(&set_time_screen);
ui_init_widget(&status_bar);
ui_add_widget(&status_bar);
load();
ui_update(NULL);
// event loop
while(1) {
if(xQueueReceive(appQueue, &evt, 0)) {
switch(evt.type) {
case BUTTON_PRESSED:
if(evt.data.button == BUT_TL) {
return; // go back to the main menu
} else if(evt.data.button == BUT_TR) {
if(sb_index < SPINBOX_NUMBER - 1) {
spinbox_set_active(&sb_time[sb_index], false);
spinbox_set_active(&sb_time[++sb_index], true);
} else if(is_valid()) {
save();
return;
} else {
// the set hour is invalid, start from the beginning
spinbox_set_active(&sb_time[sb_index], false);
sb_index = H1;
spinbox_set_active(&sb_time[H1], true);
}
}
ui_update(&evt);
break;
default: // suppress warnings
break;
}
}
}
}
application set_gmt_ofs = {
.name = "Set GMT offset", // this will be shown in menu
.main = set_gmt_ofs_main
};
......@@ -25,8 +25,10 @@
#include <stdio.h>
#include <string.h>
setting_t setting_gps_on = { "GPS on", 0, 2 };
setting_t setting_coord_style = {"Coord style", 1, 3};
setting_t setting_gps_on = { "GPS on", 1, 2 };
setting_t setting_coord_style = { "Coord style", 1, 3 };
struct tm setting_gmt_ofs;
setting_t setting_gps_sets_time = { "GPS sets time", 1, 2 };
void setting_change(setting_t *setting)
{
......
......@@ -30,6 +30,8 @@
#include "application.h"
#include <time.h>
typedef struct setting {
char name[16];
int val;
......@@ -38,11 +40,14 @@ typedef struct setting {
extern application set_time;
extern application set_date;
extern application set_gmt_ofs;
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_gps_sets_time;
#endif /* SETTINGS_H */
......
......@@ -30,20 +30,24 @@
#include <usbdbg.h>
#include <stdio.h>
#include <time.h>
#include "application.h"
#include "clock.h"
#define GPSBKGND_TIMER_PERIOD (1000 / portTICK_RATE_MS)
static xTimerHandle timerGps;
static int firstrun;
static int firstrun, firstfix;
static int cgpson, pgpson;
static void gpsbkgnd_task(void *params)
{
(void) params;
struct event e;
struct tm time;
struct gps_utc gpstime;
/* Previous and current state of GPS on setting at timer tick */
pgpson = cgpson;
......@@ -64,6 +68,33 @@ static void gpsbkgnd_task(void *params)
return;
}
char b[64];
/* Set time from GPS at first fix or midday */
if (setting_gps_sets_time.val && gps_fixed()) {
time = clock_get_time();
sprintf(b, "BEF: %d-%d-%d %d:%d:%d\r\n",
time.tm_year, time.tm_mon, time.tm_wday,
time.tm_hour, time.tm_min, time.tm_sec);
usbdbg_puts(b);
if (firstfix ||
((time.tm_hour == 12) && (time.tm_min == 00))) {
gps_get_utc(&gpstime);
time.tm_year = gpstime.yr;
time.tm_mon = gpstime.mon;
time.tm_wday = gpstime.day;
time.tm_hour = gpstime.hr + setting_gmt_ofs.tm_hour;
time.tm_min = gpstime.min + setting_gmt_ofs.tm_min;
clock_set_time(&time);
}
if (firstfix) firstfix = 0;
}
e.type = GPS_TICK;
xQueueSendToBack(appQueue, (void *)&e, 0);
}
......@@ -71,7 +102,7 @@ static void gpsbkgnd_task(void *params)
void gpsbkgnd_init()
{
firstrun = 1;
firstfix = 1;
timerGps = xTimerCreate((signed char *)"timerGps",
GPSBKGND_TIMER_PERIOD, pdTRUE,
(void *)0, gpsbkgnd_task);
......
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