Commit d6ba21ca authored by Federico Vaga's avatar Federico Vaga

freewatch: use event manager

Signed-off-by: 's avatarFederico Vaga <federico.vaga@gmail.com>
parent 0ade13d9
......@@ -31,6 +31,9 @@
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <event.h>
struct app_evt_callback;
///> Shared application task handle
extern xTaskHandle appTask;
......@@ -56,10 +59,25 @@ typedef struct application {
void (*main)(void* params);
void (*quit)(struct application *app);
///> array of event's handler
struct app_evt_callback *event_handlers;
///> number of event's handler in the array
unsigned int event_handlers_n;
///> different status flags
uint32_t flags;
} application;
struct app_evt_callback {
///> Event type to handle
enum event_type type;
///> event callback
int (*callback)(struct application *app, struct event *evt);
};
/**
* @brief Initializes the application task and event queue.
* After that runs one as the main application.
......
......@@ -111,23 +111,30 @@ void clock_main(void* params) {
continue;
// Handle clock's events
switch(evt.type) {
case BUTTON_PRESSED:
if (evt.data.button == BUT_TR ||
evt.data.button == BUT_TL)
fwatch_application_stop(&clock_app);
// no break; fall through
default: // suppress warnings
ui_update(&evt); // forward event to widgets
break;
}
fwatch_event_input_handler(&clock_app, &evt);
ui_update(&evt);
}
}
static int clock_event_button(struct application *app, struct event *evt)
{
if (evt->data.button == BUT_TR || evt->data.button == BUT_TL)
fwatch_application_stop(app);
return 0;
}
static struct app_evt_callback evt_call[] = {
{
.type = BUTTON_PRESSED,
.callback = clock_event_button,
},
};
application clock_app = {
.name = "Clock",
.main = clock_main
.main = clock_main,
.event_handlers = evt_call,
.event_handlers_n = ARRAY_SIZE(evt_call),
};
// Settings
......
......@@ -145,29 +145,7 @@ void compass_main(void *params)
while(fwatch_is_application_running(&compass)) {
ret = xQueueReceive(appQueue, &evt, 50 / portTICK_RATE_MS);
if(ret == pdTRUE) {
switch(evt.type) {
case BUTTON_PRESSED:
if (evt.data.button == BUT_TR ||
evt.data.button == BUT_TL)
fwatch_application_stop(&compass);
/*if(evt.data.button == BUT_BR) {
c_min.x = 0x7fff;
c_min.y = 0x7fff;
c_min.z = 0x7fff;
c_max.x = -0x7fff;
c_max.y = -0x7fff;
c_max.z = -0x7fff;
calib = 1;
}
if(evt.data.button == BUT_BL && calib) {
iron = lsm303_calib_iron(&c_max, &c_min);
calib = 2;
}
if(evt.data.button == BUT_TL && calib==2) {
calib = 0;
}*/
}
fwatch_event_input_handler(&compass, &evt);
} else {
lsm303_get_sample(DEV_ACC, &acc);
lsm303_get_sample(DEV_MAG, &mag);
......@@ -228,7 +206,23 @@ void compass_main(void *params)
}
}
static int compass_event_button(struct application *app, struct event *evt)
{
if (evt->data.button == BUT_TR || evt->data.button == BUT_TL)
fwatch_application_stop(app);
return 0;
}
static struct app_evt_callback evt_call[] = {
{
.type = BUTTON_PRESSED,
.callback = compass_event_button,
},
};
application compass = {
.name = "Compass",
.main = compass_main
.main = compass_main,
.event_handlers = evt_call,
.event_handlers_n = ARRAY_SIZE(evt_call),
};
......@@ -131,18 +131,8 @@ void game_main(void *params)
/*main loop*/
while(fwatch_is_application_running(&game)) {
ret = xQueueReceive(appQueue, &evt, portMAX_DELAY);
if(ret == pdTRUE) {
switch(evt.type) {
case BUTTON_PRESSED:
if (evt.data.button == BUT_TR ||
evt.data.button == BUT_TL)
fwatch_application_stop(&game);
if(evt.data.button == BUT_BR)
mode = MODE_HIGH;
if(evt.data.button == BUT_BL)
mode = MODE_NORM;
}
}
if(ret == pdTRUE)
fwatch_event_input_handler(&game, &evt);
lsm303_get_sample(DEV_ACC, &acc);
ball_x -= acc.x/100;
......@@ -210,8 +200,35 @@ static void game_quit(struct application *app)
buzzer_disable();
}
static int game_event_button(struct application *app, struct event *evt)
{
switch (evt->data.button) {
case BUT_TR:
case BUT_TL:
fwatch_application_stop(app);
break;
case BUT_BR:
mode = MODE_HIGH;
break;
case BUT_BL:
mode = MODE_NORM;
break;
}
return 0;
}
static struct app_evt_callback evt_call[] = {
{
.type = BUTTON_PRESSED,
.callback = game_event_button,
},
};
application game = {
.name = "Ball game",
.main = game_main,
.quit = game_quit,
.event_handlers = evt_call,
.event_handlers_n = ARRAY_SIZE(evt_call),
};
......@@ -161,12 +161,14 @@ void gpscoord_main(void *params)
ret = xQueueReceive(appQueue, &evt, portMAX_DELAY);
if(ret != pdTRUE)
continue;
fwatch_event_input_handler(&gpscoord, &evt);
}
}
switch (evt.type) {
case BUTTON_PRESSED:
if (evt.data.button == BUT_TR ||
evt.data.button == BUT_TL)
fwatch_application_stop(&gpscoord);
static int gpscoord_event_button(struct application *app, struct event *evt)
{
if (evt->data.button == BUT_TR || evt->data.button == BUT_TL)
fwatch_application_stop(app);
// else if (evt.data.button == BUT_BR) {
// /*
// * Toggle between coordinate and
......@@ -175,18 +177,29 @@ void gpscoord_main(void *params)
// gpsscreen += 1;
// gpsscreen %= 2;
// }
/* fall through */
return 0;
}
case GPS_TICK:
/* Update GPS UI with event content */
ui_update(&evt);
default:
break;
}
}
static int gpscoord_event_gps_tick(struct application *app, struct event *evt)
{
ui_update(evt);
return 0;
}
static struct app_evt_callback evt_call[] = {
{
.type = BUTTON_PRESSED,
.callback = gpscoord_event_button,
},
{
.type = GPS_TICK,
.callback = gpscoord_event_gps_tick,
},
};
application gpscoord = {
.name = "uerdefucami",
.main = gpscoord_main
.main = gpscoord_main,
.event_handlers = evt_call,
.event_handlers_n = ARRAY_SIZE(evt_call),
};
......@@ -70,31 +70,44 @@ void reset_main(void *params)
ret = xQueueReceive(appQueue, &evt, portMAX_DELAY);
if (ret != pdTRUE)
continue;
fwatch_event_input_handler(&reset, &evt);
}
}
static int reset_event_button(struct application *app, struct event *evt)
{
switch (evt->data.button) {
case BUT_TR:
case BUT_TL:
fwatch_application_stop(app);
break;
case BUT_BR:
SCB->AIRCR = 0x05FA0004;
break;
}
switch (evt.type) {
case BUTTON_PRESSED:
if (evt.data.button == BUT_TR ||
evt.data.button == BUT_TL)
fwatch_application_stop(&reset);
/* Reset on bottom right and bottom left buttons
* pressed */
if (evt.data.button == BUT_BR)
SCB->AIRCR = 0x05FA0004;
// Turn GPS off if setting is ON, to prepare it for
// the turn-on pulse on reset
if (setting_get(&setting_gps_on)) {
int i;
gps_on_off_pulse();
for (i = 0; i < 1000000; i++)
;
}
break;
}
// Turn GPS off if setting is ON, to prepare it for
// the turn-on pulse on reset
if (setting_get(&setting_gps_on)) {
int i;
gps_on_off_pulse();
for (i = 0; i < 1000000; i++)
;
}
return 0;
}
static struct app_evt_callback evt_call[] = {
{
.type = BUTTON_PRESSED,
.callback = reset_event_button,
},
};
application reset = {
.name = "Reset",
.main = reset_main
.main = reset_main,
.event_handlers = evt_call,
.event_handlers_n = ARRAY_SIZE(evt_call),
};
......@@ -184,38 +184,45 @@ void set_date_main(void* params) {
ret = xQueueReceive(appQueue, &evt, portMAX_DELAY);
if(ret != pdTRUE)
continue;
fwatch_event_input_handler(&set_date, &evt);
ui_update(&evt);
}
}
switch(evt.type) {
case BUTTON_PRESSED:
if(evt.data.button == BUT_TL) {
fwatch_application_stop(&set_date);
} else if(evt.data.button == BUT_TR) {
if(sb_index < SPINBOX_NUMBER - 1) {
spinbox_set_active(&sb_date[sb_index], false);
spinbox_set_active(&sb_date[++sb_index], true);
} else if(is_valid()) {
save();
return;
} else {
// the set hour is invalid, start from the beginning
spinbox_set_active(&sb_date[sb_index], false);
sb_index = D1;
spinbox_set_active(&sb_date[D1], true);
}
}
ui_update(&evt);
break;
/* Update UI on any event, so the status bar is updated */
default:
ui_update(&evt);
break;
static int set_date_event_button(struct application *app, struct event *evt)
{
switch (evt->data.button) {
case BUT_TL:
fwatch_application_stop(app);
break;
case BUT_TR:
if(sb_index < SPINBOX_NUMBER - 1) {
spinbox_set_active(&sb_date[sb_index], false);
spinbox_set_active(&sb_date[++sb_index], true);
} else if(is_valid()) {
save();
fwatch_application_stop(app);
} else {
// the set hour is invalid, start from the beginning
spinbox_set_active(&sb_date[sb_index], false);
sb_index = D1;
spinbox_set_active(&sb_date[D1], true);
}
break;
}
return 0;
}
static struct app_evt_callback evt_call[] = {
{
.type = BUTTON_PRESSED,
.callback = set_date_event_button,
},
};
application set_date = {
.name = "Set date", // this will be shown in menu
.main = set_date_main
.main = set_date_main,
.event_handlers = evt_call,
.event_handlers_n = ARRAY_SIZE(evt_call),
};
......@@ -171,37 +171,45 @@ void set_gmt_ofs_main(void* params) {
if(ret != pdTRUE)
continue;
switch(evt.type) {
case BUTTON_PRESSED:
if(evt.data.button == BUT_TL) {
fwatch_application_stop(&set_gmt_ofs);
} 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 = S;
spinbox_set_active(&sb_time[S], true);
}
}
ui_update(&evt);
break;
/* Update UI on any event, so the status bar is updated */
default:
ui_update(&evt);
break;
fwatch_event_input_handler(&set_gmt_ofs, &evt);
ui_update(&evt);
}
}
static int set_gmt_ofs_event_button(struct application *app, struct event *evt)
{
switch (evt->data.button) {
case BUT_TL:
fwatch_application_stop(app);
break;
case 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();
fwatch_application_stop(app);
} else {
// the set hour is invalid, start from the beginning
spinbox_set_active(&sb_time[sb_index], false);
sb_index = S;
spinbox_set_active(&sb_time[S], true);
}
break;
}
return 0;
}
static struct app_evt_callback evt_call[] = {
{
.type = BUTTON_PRESSED,
.callback = set_gmt_ofs_event_button,
},
};
application set_gmt_ofs = {
.name = "Set GMT offset", // this will be shown in menu
.main = set_gmt_ofs_main
.main = set_gmt_ofs_main,
.event_handlers = evt_call,
.event_handlers_n = ARRAY_SIZE(evt_call),
};
......@@ -148,37 +148,45 @@ void set_time_main(void* params) {
if(ret != pdTRUE)
continue;
switch(evt.type) {
case BUTTON_PRESSED:
if(evt.data.button == BUT_TL) {
fwatch_application_stop(&set_time);
} 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;
/* Update UI on any event, so the status bar is updated */
default:
ui_update(&evt);
break;
fwatch_event_input_handler(&set_time, &evt);
ui_update(&evt);
}
}
static int set_time_event_button(struct application *app, struct event *evt)
{
switch (evt->data.button) {
case BUT_TL:
fwatch_application_stop(app);
break;
case 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();
fwatch_application_stop(app);
} 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);
}
break;
}
return 0;
}
static struct app_evt_callback evt_call[] = {
{
.type = BUTTON_PRESSED,
.callback = set_time_event_button,
},
};
application set_time = {
.name = "Set time", // this will be shown in menu
.main = set_time_main
.main = set_time_main,
.event_handlers = evt_call,
.event_handlers_n = ARRAY_SIZE(evt_call),
};
......@@ -99,19 +99,8 @@ void usb_ms_main(void* params) {
MSDD_Handler();
continue;
}
switch(evt.type) {
case BUTTON_PRESSED:
if (evt.data.button == BUT_TR ||
evt.data.button == BUT_TL)
fwatch_application_stop(&usb_ms);
break;
default: // suppress warnings
// ignore events that were not mentioned above
ui_update(&evt);
break;
}
fwatch_event_input_handler(&usb_ms, &evt);
ui_update(&evt);
}
}
......@@ -128,8 +117,30 @@ static void usb_ms_quit(struct application *app)
xSemaphoreGive(mutexSdCardAccess);
}
static int usb_ms_event_button(struct application *app, struct event *evt)
{
switch (evt->data.button) {
case BUT_TR:
case BUT_TL:
fwatch_application_stop(app);
break;
}
return 0;
}
static struct app_evt_callback evt_call[] = {
{
.type = BUTTON_PRESSED,
.callback = usb_ms_event_button,
},
};
application usb_ms = {
.name = "USB drive",
.main = usb_ms_main,
.quit = usb_ms_quit,
.event_handlers = evt_call,
.event_handlers_n = ARRAY_SIZE(evt_call),
};
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