[simulation] Fix some potential crashes & 26 memory leaks, improve LCD redraws. (#4634)

* [simulation] Hardening: Fix some potential crashes & 26 memory leaks, improve LCD performance.
  * [simpgmspace] Init trims; Make sure `REa` is really defined (to match board files).
  * [simpgmspace][opentxsimulator] Verify current running state before start/stop; Move rotary enc. init.
  * [simpgmspace][LcdWidget] Improve performance by moving LCD content change check to lcdRefresh() & limiting LcdWidget refresh time to 60 fps max.
  * [simueeprom] Ensure thread could be started, set default running state to false.
  * [simufatfs] Fix paths report trace.
  * [eepromimportexport] Fix memory leaks resulting from import debugging scheme being used.
  * [customdebug] Introduce new scheme for custom debug output in compliance with Qt recommendations (see docs for QLoggingCategory).
  * [opentxeeprom] Fix extra conversion table cache elements being created and also not properly deleted (and hence leaking).
  * [opentxinterface] Unregister EEpromInterfaces in unregisterOpenTxFirmwares();
  * [storage] Unregister storage factories on exit (fixes leak); create virtual StorageFormat/StorageFactory destructors (prevents warnings).
  * [helpers] GVarGroup now emits own signal, no need to pass ModelPanel pointer (removes dependency on modeledit.h)
  * [DebugOutput] Clear simulator trace hook before exiting (prevent possible issues); Fix leak and possible bad QString allocations when reading from buffer; Fix leak with combo box event filter.
  * [TelemetrySimulator] Fix leak by deleting LogPlaybackController object on exit; Only set up data fields once; convert timers to static.
  * [build] Consolidate all Companion/Simulator shared items in `common` library to reduce build time/etc (node & edge still remain awkward).

* [simulatorwidget] Delete removed spacer object (previously-forgotten "26th" leak).

* [DebugOutput] Fix stray trailing characters issue with new text buffer allocation (from previous commit); Increase maximum buffer sizes to better accommodate slower systems.

* Cosmetics.
This commit is contained in:
Max Paperno
2017-03-21 07:43:40 +01:00
committed by Bertrand Songis
parent 52e65179a8
commit 4aa0c1bbe4
28 changed files with 290 additions and 210 deletions
+5 -18
View File
@@ -40,19 +40,6 @@ uint8_t getStickMode()
return limit<uint8_t>(0, g_eeGeneral.stickMode, 3);
}
#if defined(PCBTARANIS)
void resetTrims()
{
TRIMS_GPIO_REG_RVD |= TRIMS_GPIO_PIN_RVD;
TRIMS_GPIO_REG_RVU |= TRIMS_GPIO_PIN_RVU;
TRIMS_GPIO_REG_RHL |= TRIMS_GPIO_PIN_RHL;
TRIMS_GPIO_REG_RHR |= TRIMS_GPIO_PIN_RHR;
TRIMS_GPIO_REG_LVD |= TRIMS_GPIO_PIN_LVD;
TRIMS_GPIO_REG_LVU |= TRIMS_GPIO_PIN_LVU;
TRIMS_GPIO_REG_LHL |= TRIMS_GPIO_PIN_LHL;
TRIMS_GPIO_REG_LHR |= TRIMS_GPIO_PIN_LHR;
}
#endif
OpenTxSimulator::OpenTxSimulator()
{
@@ -96,11 +83,8 @@ void OpenTxSimulator::start(QByteArray & ee, bool tests)
void OpenTxSimulator::start(const char * filename, bool tests)
{
#if defined(ROTARY_ENCODER_NAVIGATION)
for (uint8_t i=0; i<DIM(rotencValue); i++) {
rotencValue[i] = 0;
}
#endif
if (main_thread_running)
return;
StartEepromThread(filename);
StartAudioThread(volumeGain);
@@ -109,6 +93,9 @@ void OpenTxSimulator::start(const char * filename, bool tests)
void OpenTxSimulator::stop()
{
if (!main_thread_running)
return;
StopSimu();
#if defined(CPUARM)
StopAudioThread();
+24 -5
View File
@@ -23,7 +23,7 @@
#include <stdarg.h>
#include <string>
#if !defined _MSC_VER || defined __GNUC__
#if !defined (_MSC_VER) || defined (__GNUC__)
#include <chrono>
#include <sys/time.h>
#endif
@@ -135,6 +135,13 @@ void simuInit()
simuSetSwitch(i, 0);
simuSetKey(i, false); // a little dirty, but setting keys that don't exist is perfectly OK here
}
for (int i=0; i<(NUM_STICKS+NUM_AUX_TRIMS)*2; i++)
simuSetTrim(i, false);
#if defined(ROTARY_ENCODER_NAVIGATION)
for (uint8_t i=0; i<DIM(rotencValue); i++)
rotencValue[i] = 0;
#endif
}
#define NEG_CASE(sw_or_key, pin, mask) \
@@ -216,9 +223,9 @@ void simuSetKey(uint8_t key, bool state)
KEY_CASE(KEY_UP, KEYS_GPIO_REG_UP, KEYS_GPIO_PIN_UP)
KEY_CASE(KEY_DOWN, KEYS_GPIO_REG_DOWN, KEYS_GPIO_PIN_DOWN)
#endif
#if defined(PCBSKY9X) && !defined(REVX) && !defined(AR9X)
#if defined(PCBSKY9X) && !defined(REVX) && !defined(AR9X) && defined(ROTARY_ENCODERS)
KEY_CASE(BTN_REa, PIOB->PIO_PDSR, 0x40)
#elif defined(PCBGRUVIN9X) || defined(PCBMEGA2560)
#elif (defined(PCBGRUVIN9X) || defined(PCBMEGA2560)) && (defined(ROTARY_ENCODERS) || defined(ROTARY_ENCODER_NAVIGATION))
KEY_CASE(BTN_REa, pind, 0x20)
#elif defined(PCB9X) && defined(ROTARY_ENCODER_NAVIGATION)
KEY_CASE(BTN_REa, RotEncoder, 0x20)
@@ -337,6 +344,9 @@ void simuSetSwitch(uint8_t swtch, int8_t state)
void StartSimu(bool tests, const char * sdPath, const char * settingsPath)
{
if (main_thread_running)
return;
s_current_protocol[0] = 255;
menuLevel = 0;
@@ -380,6 +390,9 @@ void StartSimu(bool tests, const char * sdPath, const char * settingsPath)
void StopSimu()
{
if (!main_thread_running)
return;
main_thread_running = 0;
#if defined(CPUARM)
@@ -525,6 +538,7 @@ void StartAudioThread(int volumeGain)
simuAudio.leftoverLen = 0;
simuAudio.threadRunning = true;
simuAudio.volumeGain = volumeGain;
TRACE_SIMPGMSPACE("StartAudioThread(%d)", volumeGain);
setScaledVolume(VOLUME_LEVEL_DEF);
pthread_attr_t attr;
@@ -569,12 +583,17 @@ void lcdOff()
void lcdRefresh()
{
static bool lightEnabled = (bool)isBacklightEnabled();
#if defined(PCBFLAMENCO)
TW8823_SendScreen();
#endif
memcpy(simuLcdBuf, displayBuf, sizeof(simuLcdBuf));
simuLcdRefresh = true;
if ((bool(isBacklightEnabled()) != lightEnabled) || memcmp(simuLcdBuf, displayBuf, DISPLAY_BUFFER_SIZE)) {
memcpy(simuLcdBuf, displayBuf, DISPLAY_BUFFER_SIZE);
lightEnabled = (bool)isBacklightEnabled();
simuLcdRefresh = true;
}
}
void telemetryPortInit(uint8_t baudrate)
+9 -5
View File
@@ -28,7 +28,7 @@ uint8_t * eeprom_buffer_data;
volatile int32_t eeprom_buffer_size;
bool eeprom_read_operation;
bool eeprom_thread_running = true;
bool eeprom_thread_running = false;
#if defined(EEPROM_SIZE)
uint8_t eeprom[EEPROM_SIZE];
@@ -41,7 +41,7 @@ sem_t * eeprom_write_sem;
void eepromReadBlock (uint8_t * buffer, size_t address, size_t size)
{
assert(size);
if (fp) {
// TRACE("EEPROM read (pos=%d, size=%d)", pointer_eeprom, size);
if (fseek(fp, address, SEEK_SET) < 0)
@@ -154,6 +154,7 @@ void StartEepromThread(const char * filename)
if (!fp)
perror("error in fopen");
}
#ifdef __APPLE__
eeprom_write_sem = sem_open("eepromsem", O_CREAT, S_IRUSR | S_IWUSR, 0);
#else
@@ -161,8 +162,10 @@ void StartEepromThread(const char * filename)
sem_init(eeprom_write_sem, 0, 0);
#endif
eeprom_thread_running = true;
pthread_create(&eeprom_thread_pid, NULL, &eeprom_thread_function, NULL);
if (!pthread_create(&eeprom_thread_pid, NULL, &eeprom_thread_function, NULL))
eeprom_thread_running = true;
else
perror("Could not create eeprom thread.");
}
void StopEepromThread()
@@ -178,5 +181,6 @@ void StopEepromThread()
free(eeprom_write_sem);
#endif
if (fp) fclose(fp);
if (fp)
fclose(fp);
}
+2 -2
View File
@@ -97,8 +97,8 @@ void simuFatfsSetPaths(const char * sdPath, const char * settingsPath)
if (settingsPath) {
simuSettingsDirectory = removeTrailingPathDelimiter(fixPathDelimiters(settingsPath));
}
TRACE_SIMPGMSPACE("simuFatfsSetPaths(): simuSdDirectory: \"\"", simuSdDirectory.c_str());
TRACE_SIMPGMSPACE("simuFatfsSetPaths(): simuSettingsDirectory: \"\"", simuSettingsDirectory.c_str());
TRACE_SIMPGMSPACE("simuFatfsSetPaths(): simuSdDirectory: \"%s\"", simuSdDirectory.c_str());
TRACE_SIMPGMSPACE("simuFatfsSetPaths(): simuSettingsDirectory: \"%s\"", simuSettingsDirectory.c_str());
}
bool startsWith(const char *path, const char * start)