[simu] Refactor simu startup and shutdown handling (for 2.3 branch):

* Use pwrCheck() as primary control point for shutdown, centralizes thread control in simpgmspace module;
 * Remove main_thread_running in favor of separate variables for startup mode and shutdown flag;
 * Don't start a thread for ARM startup, just run simuMain() directly (the thread just exits anyway);
 * Refactor SIMU_SLEEP() macro, and remove all unnecessary uses of it, also remove SIMU_SLEEP_NORET;
 * Add startType param to opentxStart() to control splash and startup checks;
 * Fixes backlight never turning off in simulator with "SWITCH" power button type (pwrPressed() was always true);
 * Fixes simu shutdown when model checklist is displayed at startup;
 * Adds stubs to possibly simulate a "soft" power button in the future.
This commit is contained in:
Max Paperno
2018-09-08 22:04:37 -04:00
parent 942ae1847d
commit a2006940db
12 changed files with 97 additions and 70 deletions
+50 -13
View File
@@ -40,10 +40,12 @@ uint8_t portb, portc, porth=0, dummyport;
uint16_t dummyport16;
int g_snapshot_idx = 0;
pthread_t main_thread_pid;
uint8_t main_thread_running = 0;
uint8_t simu_start_mode = 0;
char * main_thread_error = NULL;
bool simu_shutdown = false;
bool simu_running = false;
#if defined(STM32)
uint32_t Peri1_frequency, Peri2_frequency;
GPIO_TypeDef gpioa, gpiob, gpioc, gpiod, gpioe, gpiof, gpiog, gpioh, gpioi, gpioj;
@@ -132,6 +134,13 @@ void simuInit()
RCC->CSR = 0;
#endif
// set power button to "not pressed"
#if defined(PWR_SWITCH_GPIO) // STM32
GPIO_SetBits(PWR_SWITCH_GPIO, PWR_SWITCH_GPIO_PIN);
#elif defined(PIO_PC17) // AT91SAM3
PIOC->PIO_PDSR &= ~PIO_PC17;
#endif
for (int i = 0; i <= 17; i++) {
simuSetSwitch(i, 0);
simuSetKey(i, false); // a little dirty, but setting keys that don't exist is perfectly OK here
@@ -315,13 +324,14 @@ void simuSetSwitch(uint8_t swtch, int8_t state)
void StartSimu(bool tests, const char * sdPath, const char * settingsPath)
{
if (main_thread_running)
if (simu_running)
return;
s_current_protocol[0] = 255;
menuLevel = 0;
main_thread_running = (tests ? 1 : 2); // TODO rename to simu_run_mode with #define
simu_start_mode = (tests ? 0 : 0x02 /* OPENTX_START_NO_CHECKS */);
simu_shutdown = false;
simuFatfsSetPaths(sdPath, settingsPath);
@@ -350,7 +360,9 @@ void StartSimu(bool tests, const char * sdPath, const char * settingsPath)
try {
#endif
pthread_create(&main_thread_pid, NULL, &simuMain, NULL);
simuMain();
simu_running = true;
#if defined(SIMU_EXCEPTIONS)
}
@@ -361,14 +373,15 @@ void StartSimu(bool tests, const char * sdPath, const char * settingsPath)
void StopSimu()
{
if (!main_thread_running)
if (!simu_running)
return;
main_thread_running = 0;
simu_shutdown = true;
pthread_join(mixerTaskId, NULL);
pthread_join(menusTaskId, NULL);
pthread_join(main_thread_pid, NULL);
simu_running = false;
}
struct SimulatorAudio {
@@ -380,6 +393,21 @@ struct SimulatorAudio {
pthread_t threadPid;
} simuAudio;
bool simuIsRunning()
{
return simu_running;
}
bool simuSleep(uint32_t ms)
{
for (uint32_t i = 0; i < ms; ++i){
if (simu_shutdown || !simu_running)
return false;
sleep(1);
}
return true;
}
void audioConsumeCurrentBuffer()
{
}
@@ -572,16 +600,25 @@ int lcdRestoreBackupBuffer()
return 1;
}
uint32_t pwrCheck()
{
// TODO: ability to simulate shutdown warning for a "soft" simulator restart
return simu_shutdown ? e_power_off : e_power_on;
}
void pwrOff()
{
}
uint32_t pwrPressed()
{
#if defined(PWR_BUTTON_PRESS)
return false;
// TODO: simulate power button
#if defined(PWR_SWITCH_GPIO) // STM32
return GPIO_ReadInputDataBit(PWR_SWITCH_GPIO, PWR_SWITCH_GPIO_PIN) == Bit_RESET;
#elif defined(PIO_PC17) // AT91SAM3
return PIOC->PIO_PDSR & PIO_PC17;
#else
return true;
return false;
#endif
}
@@ -648,9 +685,9 @@ FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG) { return SET; }
ErrorStatus RTC_WaitForSynchro(void) { return SUCCESS; }
void unlockFlash() { }
void lockFlash() { }
void flashWrite(uint32_t *address, uint32_t *buffer) { SIMU_SLEEP(100); }
void flashWrite(uint32_t *address, uint32_t *buffer) { simuSleep(100); }
uint32_t isBootloaderStart(const uint8_t * block) { return 1; }
#endif // defined(PCBTARANIS)
#endif // defined(STM32)
#if defined(PCBHORUS)
void LCD_ControlLight(uint16_t dutyCycle) { }