Added field to state data structure containing the current operating state of the device. Implemented clean shutdown of all the threads.

This commit is contained in:
Silvano Seva 2022-07-01 14:27:05 +02:00
parent 810daf7599
commit 5f40ceceb3
6 changed files with 72 additions and 51 deletions

View File

@ -46,6 +46,7 @@ m17_t;
*/
typedef struct
{
uint8_t devStatus;
datetime_t time;
uint16_t v_bat;
uint8_t charge;
@ -61,12 +62,13 @@ typedef struct
uint16_t bank;
uint8_t rtxStatus;
bool shutdown;
bool emergency;
settings_t settings;
gps_t gps_data;
bool gps_set_time;
bool gpsDetected;
bool backup_eflash;
bool restore_eflash;
m17_t m17_data;
}
state_t;
@ -86,6 +88,15 @@ enum RtxStatus
RTX_TX
};
enum DeviceStatus
{
PWROFF = 0,
STARTUP,
RUNNING,
DATATRANSFER,
SHUTDOWN
};
extern state_t state;
extern pthread_mutex_t state_mutex;

View File

@ -30,6 +30,8 @@ extern void *dev_task(void *arg);
void openrtx_init()
{
state.devStatus = STARTUP;
platform_init(); // Initialize low-level platform drivers
state_init(); // Initialize radio state
@ -47,20 +49,29 @@ void openrtx_init()
sleepFor(0u, 30u);
platform_setBacklightLevel(state.settings.brightness);
// Detect and initialise GPS
#if defined(GPS_PRESENT)
// Detect and initialise GPS
state.gpsDetected = gps_detect(1000);
if(state.gpsDetected) gps_init(9600);
#else
// Keep the splash screen for 1 second
sleepFor(1u, 0u);
#endif
}
void *openrtx_run()
{
state.devStatus = RUNNING;
// Start the OpenRTX threads
create_threads();
// Jump to the device management task
dev_task(NULL);
// Device task terminated, complete shutdown sequence
state_terminate();
platform_terminate();
return NULL;
}

View File

@ -87,7 +87,6 @@ void state_init()
state.channel_index = 1; // Set default channel index (it is 1-based)
state.bank_enabled = false;
state.rtxStatus = RTX_OFF;
state.shutdown = false;
state.emergency = false;
// Force brightness field to be in range 0 - 100

View File

@ -33,6 +33,7 @@
#include <string.h>
#include <utils.h>
#include <input.h>
#include <backup.h>
#ifdef GPS_PRESENT
#include <interfaces/gps.h>
#include <gps.h>
@ -48,31 +49,25 @@ void *ui_task(void *arg)
{
(void) arg;
event_t kbd_event = {{EVENT_KBD, 0}};
kbd_msg_t kbd_msg;
rtxStatus_t rtx_cfg;
bool sync_rtx = true;
long long time = 0;
// Keep the splash screen for 1 second
sleepFor(1u, 0u);
// Load initial state and perform a GUI draw
ui_saveState();
ui_updateGUI();
gfx_render();
while(1)
while(state.devStatus != SHUTDOWN)
{
time = getTick();
// Scan keyboard
bool kbd_event = input_scanKeyboard(&kbd_msg);
if(kbd_event)
if(input_scanKeyboard(&kbd_msg))
{
event_t event;
event.type = EVENT_KBD;
event.payload = kbd_msg.value;
ui_pushEvent(event);
kbd_event.payload = kbd_msg.value;
ui_pushEvent(kbd_event);
}
pthread_mutex_lock(&state_mutex); // Lock r/w access to radio state
@ -115,6 +110,11 @@ void *ui_task(void *arg)
time += 25;
sleepUntil(time);
}
ui_terminate();
gfx_terminate();
return NULL;
}
/**
@ -128,17 +128,47 @@ void *dev_task(void *arg)
long long time = 0;
uint8_t tick_5ms = 0;
while(state.shutdown == false)
while(state.devStatus != SHUTDOWN)
{
time = getTick();
tick_5ms++;
// Check if power off is requested
pthread_mutex_lock(&state_mutex);
if(platform_pwrButtonStatus() == false)
state.devStatus = SHUTDOWN;
pthread_mutex_unlock(&state_mutex);
// Handle external flash backup/restore
#if !defined(PLATFORM_LINUX) && !defined(PLATFORM_MOD17)
if(state.backup_eflash)
{
eflash_dump();
pthread_mutex_lock(&state_mutex);
state.backup_eflash = false;
state.devStatus = SHUTDOWN;
pthread_mutex_unlock(&state_mutex);
}
if(state.restore_eflash)
{
eflash_restore();
pthread_mutex_lock(&state_mutex);
state.restore_eflash = false;
state.devStatus = SHUTDOWN;
pthread_mutex_unlock(&state_mutex);
}
#endif
// Run GPS task
#if defined(GPS_PRESENT) && !defined(MD3x0_ENABLE_DBG)
if(state.gpsDetected)
gps_taskFunc();
#endif
// Update radio state and push an event to the UI every 100ms
// Update radio state every 100ms
if((tick_5ms % 20) == 0)
{
state_update();
@ -170,7 +200,7 @@ void *rtx_task(void *arg)
rtx_init(&rtx_mutex);
while(state.shutdown == false)
while(state.devStatus == RUNNING)
{
rtx_taskFunc();
}

View File

@ -936,15 +936,6 @@ void ui_saveState()
void ui_updateFSM(bool *sync_rtx)
{
// User wants to power off the radio, so shutdown.
if(!platform_pwrButtonStatus())
{
state.shutdown = true;
state_terminate();
platform_terminate();
return;
}
// Check for events
if(evQueue_wrPos == evQueue_rdPos) return;

View File

@ -21,7 +21,6 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <backup.h>
#include <utils.h>
#include <ui.h>
#include <interfaces/nvmem.h>
@ -464,18 +463,8 @@ void _ui_drawMenuBackup(ui_state_t* ui_state)
gfx_print(line, FONT_SIZE_8PT, TEXT_ALIGN_CENTER,
color_white, "press PTT to start.");
// Shutdown all the other parts
state.shutdown = true;
if(platform_getPttStatus() == 1)
{
platform_ledOn(GREEN);
#if !defined(PLATFORM_LINUX) && !defined(PLATFORM_MOD17)
eflash_dump();
platform_terminate();
NVIC_SystemReset();
#endif
}
state.devStatus = DATATRANSFER;
state.backup_eflash = true;
}
void _ui_drawMenuRestore(ui_state_t* ui_state)
@ -497,18 +486,8 @@ void _ui_drawMenuRestore(ui_state_t* ui_state)
gfx_print(line, FONT_SIZE_8PT, TEXT_ALIGN_CENTER,
color_white, "press PTT to start.");
// Shutdown all the other parts
state.shutdown = true;
if(platform_getPttStatus() == 1)
{
platform_ledOn(GREEN);
#if !defined(PLATFORM_LINUX) && !defined(PLATFORM_MOD17)
eflash_restore();
platform_terminate();
NVIC_SystemReset();
#endif
}
state.devStatus = DATATRANSFER;
state.restore_eflash = true;
}
void _ui_drawMenuInfo(ui_state_t* ui_state)