diff --git a/openrtx/include/event.h b/openrtx/include/event.h new file mode 100644 index 00000000..e3acfcbb --- /dev/null +++ b/openrtx/include/event.h @@ -0,0 +1,49 @@ +/*************************************************************************** + * Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN, * + * Silvano Seva IU2KWO * + * * + * 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 * + ***************************************************************************/ + +#ifndef EVENT_H +#define EVENT_H + +/** + * This enum describes the event message type: + * - EVENT_KBD is used to send a keypress + * - EVENT_STATUS is used to send a status change notification + */ +enum eventType_t +{ + EVENT_KBD = 0, + EVENT_STATUS = 1 +}; + +/** + * The event message is constrained to 32 bits size + * This is necessary to send event messages in uC OS/III Queues + * That accept a void * type message, which is 32-bits wide on + * ARM cortex-M MCUs + * uC OS/III Queues are meant to send pointer to allocated data + * But if we keep the size under 32 bits, we can sent the + * entire message, casting it to a void * pointer. + */ +typedef struct +{ + uint32_t type : 3, + uint32_t payload : 29 +}event_t; + +#endif /* EVENT_H */ diff --git a/openrtx/src/threads.c b/openrtx/src/threads.c index 00fc8293..347011e9 100644 --- a/openrtx/src/threads.c +++ b/openrtx/src/threads.c @@ -26,6 +26,7 @@ #include #include #include +#include #include @@ -33,7 +34,7 @@ static OS_MUTEX state_mutex; /* Queue for sending and receiving keyboard status */ -static OS_Q kbd_queue; +static OS_Q ui_queue; /**************************** IMPORTANT NOTE *********************************** * * @@ -103,20 +104,19 @@ static void ui_task(void *arg) while(1) { // Read from the keyboard queue (returns 0 if no message is present) - void * msg = OSQPend(&kbd_queue, 0u, OS_OPT_PEND_NON_BLOCKING, - &msg_size, 0u, &os_err); // Copy keyboard_t keys from received void * pointer msg - keyboard_t keys = msg; - // Lock mutex, read and update state + event_t event = OSQPend(&ui_queue, 0u, OS_OPT_PEND_NON_BLOCKING, + &msg_size, 0u, &os_err); + // Lock mutex, read and write state OSMutexPend(&state_mutex, 0u, OS_OPT_PEND_BLOCKING, 0u, &os_err); // React to keypresses and update FSM inside state - ui_updateFSM(last_state, keys); + ui_updateFSM(last_state, event); // Update state local copy last_state = state; // Unlock mutex OSMutexPost(&state_mutex, OS_OPT_POST_NONE, &os_err); - // Redraw GUI + // Redraw GUI based on last state copy bool renderNeeded = ui_updateGUI(last_state); if(renderNeeded) @@ -147,10 +147,12 @@ static void kbd_task(void *arg) // Check if some key is pressed if(keys != 0) { - // Copy keyboard_t keys in void * message to use with OSQPost - void * msg = keys; + // Send event_t as void * message to use with OSQPost + event_t kbd_msg; + kbd_msg.type = EVENT_KBD; + kbd_msg.payload = keys; // Send keyboard status in queue - OSQPost(&kbd_queue, msg, sizeof(keyboard_t), + OSQPost(&ui_queue, (void *)kbd_msg, sizeof(event_t), OS_OPT_POST_FIFO + OS_OPT_POST_NO_SCHED, &os_err); } // Read keyboard state at 5Hz @@ -226,9 +228,9 @@ void create_threads() (CPU_CHAR *) "State Mutex", (OS_ERR *) &os_err); - // Create keyboard queue - OSQCreate((OS_Q *) &kbd_queue, - (CPU_CHAR *) "Keyboard Queue", + // Create UI event queue + OSQCreate((OS_Q *) &ui_queue, + (CPU_CHAR *) "UI event queue", (OS_MSG_QTY ) 10, (OS_ERR *) &os_err);