Reorganised system initialisation sequence, improved reactivity of UI and device threads

This commit is contained in:
Silvano Seva 2022-06-29 17:58:16 +02:00
parent 3a288769ba
commit 810daf7599
4 changed files with 60 additions and 65 deletions

View File

@ -33,6 +33,11 @@ void create_threads();
*/ */
#define DEV_TASK_STKSIZE 2048 #define DEV_TASK_STKSIZE 2048
/**
* Stack size for UI management, in bytes.
*/
#define UI_TASK_STKSIZE 2048
/** /**
* Stack size for baseband control task, in bytes. * Stack size for baseband control task, in bytes.
*/ */

View File

@ -21,45 +21,46 @@
#include <interfaces/platform.h> #include <interfaces/platform.h>
#include <interfaces/graphics.h> #include <interfaces/graphics.h>
#include <interfaces/delays.h> #include <interfaces/delays.h>
#include <interfaces/gps.h>
#include <threads.h> #include <threads.h>
#include <openrtx.h> #include <openrtx.h>
#include <ui.h> #include <ui.h>
extern void *ui_task(void *arg); extern void *dev_task(void *arg);
void openrtx_init() void openrtx_init()
{ {
// Initialize platform drivers platform_init(); // Initialize low-level platform drivers
platform_init(); state_init(); // Initialize radio state
// Initialize radio state gfx_init(); // Initialize display and graphics driver
state_init(); kbd_init(); // Initialize keyboard driver
ui_init(); // Initialize user interface
// Initialize display and graphics driver #ifdef SCREEN_CONTRAST
gfx_init();
// Set default contrast
display_setContrast(state.settings.contrast); display_setContrast(state.settings.contrast);
#endif
// Initialize user interface // Display splash screen, turn on backlight after a suitable time to
ui_init(); // hide random pixels during render process
ui_drawSplashScreen(true);
gfx_render();
sleepFor(0u, 30u);
platform_setBacklightLevel(state.settings.brightness);
// Detect and initialise GPS
#if defined(GPS_PRESENT)
state.gpsDetected = gps_detect(1000);
if(state.gpsDetected) gps_init(9600);
#endif
} }
void *openrtx_run() void *openrtx_run()
{ {
// Display splash screen
ui_drawSplashScreen(true);
gfx_render();
// Wait 30ms before turning on backlight to hide random pixels on screen
sleepFor(0u, 30u);
platform_setBacklightLevel(state.settings.brightness);
// Start the OpenRTX threads // Start the OpenRTX threads
create_threads(); create_threads();
// Jump to the UI task // Jump to the device management task
ui_task(NULL); dev_task(NULL);
return NULL; return NULL;
} }

View File

@ -82,7 +82,7 @@ void state_init()
#endif #endif
state.v_bat = platform_getVbat(); state.v_bat = platform_getVbat();
state.charge = battery_getCharge(state.v_bat); state.charge = battery_getCharge(state.v_bat);
state.rssi = rtx_getRssi(); state.rssi = -127.0f;
state.channel_index = 1; // Set default channel index (it is 1-based) state.channel_index = 1; // Set default channel index (it is 1-based)
state.bank_enabled = false; state.bank_enabled = false;

View File

@ -42,31 +42,29 @@
pthread_mutex_t rtx_mutex; pthread_mutex_t rtx_mutex;
/** /**
* \internal Task function in charge of updating the UI. * \internal Task function managing user input and UI
*/ */
void *ui_task(void *arg) void *ui_task(void *arg)
{ {
(void) arg; (void) arg;
bool sync_rtx = true;
rtxStatus_t rtx_cfg;
kbd_msg_t kbd_msg; kbd_msg_t kbd_msg;
rtxStatus_t rtx_cfg;
// Initialize keyboard driver bool sync_rtx = true;
kbd_init(); long long time = 0;
// Get initial state local copy
ui_saveState();
// Keep the splash screen for 1 second // Keep the splash screen for 1 second
sleepFor(1u, 0u); sleepFor(1u, 0u);
// Initial GUI draw // Load initial state and perform a GUI draw
ui_saveState();
ui_updateGUI(); ui_updateGUI();
gfx_render(); gfx_render();
while(1) while(1)
{ {
time = getTick();
// Scan keyboard // Scan keyboard
bool kbd_event = input_scanKeyboard(&kbd_msg); bool kbd_event = input_scanKeyboard(&kbd_msg);
if(kbd_event) if(kbd_event)
@ -77,18 +75,10 @@ void *ui_task(void *arg)
ui_pushEvent(event); ui_pushEvent(event);
} }
// Lock mutex, read and write state pthread_mutex_lock(&state_mutex); // Lock r/w access to radio state
pthread_mutex_lock(&state_mutex); ui_updateFSM(&sync_rtx); // Update UI FSM
// React to keypresses and update FSM inside state ui_saveState(); // Save local state copy
ui_updateFSM(&sync_rtx); pthread_mutex_unlock(&state_mutex); // Unlock r/w access to radio state
// Update state local copy
ui_saveState();
// Unlock mutex
pthread_mutex_unlock(&state_mutex);
// Redraw GUI based on last state copy
ui_updateGUI();
gfx_render();
// If synchronization needed take mutex and update RTX configuration // If synchronization needed take mutex and update RTX configuration
if(sync_rtx) if(sync_rtx)
@ -117,32 +107,30 @@ void *ui_task(void *arg)
sync_rtx = false; sync_rtx = false;
} }
// Update UI and render on screen
ui_updateGUI();
gfx_render();
// 40Hz update rate for keyboard and UI // 40Hz update rate for keyboard and UI
sleepFor(0, 25); time += 25;
sleepUntil(time);
} }
} }
/** /**
* \internal Task function in charge of updating the radio state. * \internal Task function in charge of managing the device and update the
* global state variable.
*/ */
void *dev_task(void *arg) void *dev_task(void *arg)
{ {
(void) arg; (void) arg;
#if defined(GPS_PRESENT) && !defined(MD3x0_ENABLE_DBG) long long time = 0;
bool gpsPresent = gps_detect(5000); uint8_t tick_5ms = 0;
pthread_mutex_lock(&state_mutex);
state.gpsDetected = gpsPresent;
pthread_mutex_unlock(&state_mutex);
if(state.gpsDetected) gps_init(9600);
#endif
uint8_t tick_5ms = 0;
while(state.shutdown == false) while(state.shutdown == false)
{ {
time = getTick();
tick_5ms++; tick_5ms++;
#if defined(GPS_PRESENT) && !defined(MD3x0_ENABLE_DBG) #if defined(GPS_PRESENT) && !defined(MD3x0_ENABLE_DBG)
@ -162,7 +150,8 @@ void *dev_task(void *arg)
} }
// Run this loop once every 5ms // Run this loop once every 5ms
sleepFor(0u, 5u); time += 5;
sleepUntil(time);
} }
#if defined(GPS_PRESENT) #if defined(GPS_PRESENT)
@ -215,11 +204,11 @@ void create_threads()
pthread_create(&rtx_thread, &rtx_attr, rtx_task, NULL); pthread_create(&rtx_thread, &rtx_attr, rtx_task, NULL);
// Create state thread // Create UI thread
pthread_t state_thread; pthread_t ui_thread;
pthread_attr_t state_attr; pthread_attr_t ui_attr;
pthread_attr_init(&state_attr); pthread_attr_init(&ui_attr);
pthread_attr_setstacksize(&state_attr, DEV_TASK_STKSIZE); pthread_attr_setstacksize(&ui_attr, UI_TASK_STKSIZE);
pthread_create(&state_thread, &state_attr, dev_task, NULL); pthread_create(&ui_thread, &ui_attr, ui_task, NULL);
} }