Skip to content

Commit

Permalink
touch, gui and psu thread optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jan 1, 2021
1 parent 07a374f commit 51a1bff
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 96 deletions.
1 change: 0 additions & 1 deletion src/eez/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ osMessageQId g_guiMessageQueueId;
void startThread() {
decompressAssets();
mcu::display::onThemeChanged();
touch::init();
mouse::init();
g_guiMessageQueueId = osMessageCreate(osMessageQ(g_guiMessageQueue), 0);
g_guiTaskHandle = osThreadCreate(osThread(g_guiTask), nullptr);
Expand Down
109 changes: 34 additions & 75 deletions src/eez/gui/touch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ static int g_eventY = -1;
////////////////////////////////////////////////////////////////////////////////

#if defined(EEZ_PLATFORM_STM32)

static const int EVENT_QUEUE_MAX_SIZE = 50;
struct Event {
EventType type;
Expand All @@ -61,38 +60,9 @@ static Event g_eventQueue[EVENT_QUEUE_MAX_SIZE];
static uint8_t g_eventQueueHead = 0;
static uint8_t g_eventQueueTail = 0;
static bool g_eventQueueFull;
osMutexId(g_eventQueueMutexId);
osMutexDef(g_eventQueueMutex);

void mainLoop(const void *);

#if defined(EEZ_PLATFORM_STM32)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#endif

osThreadDef(g_touchTask, mainLoop, osPriorityNormal, 0, 1024);

#if defined(EEZ_PLATFORM_STM32)
#pragma GCC diagnostic pop
#endif

void oneIter();

void mainLoop(const void *) {
#ifdef __EMSCRIPTEN__
oneIter();
#else
while (1) {
oneIter();
osDelay(10);
}
#endif
}

#endif

void oneIter() {
void tickHighPriority() {
bool pressed;
int x;
int y;
Expand Down Expand Up @@ -135,70 +105,59 @@ void oneIter() {

#if defined(EEZ_PLATFORM_STM32)
if (g_lastEventType != EVENT_TYPE_TOUCH_NONE) {
if (osMutexWait(g_eventQueueMutexId, 5) == osOK) {
if (g_eventQueueFull || g_eventQueueTail != g_eventQueueHead) {
uint32_t previousEventQueueHead = g_eventQueueHead == 0 ? EVENT_QUEUE_MAX_SIZE - 1 : g_eventQueueHead - 1;
Event &event = g_eventQueue[previousEventQueueHead];
if (event.type == g_lastEventType) {
g_eventQueueHead = previousEventQueueHead;
}
if (g_eventQueueFull || g_eventQueueTail != g_eventQueueHead) {
uint32_t previousEventQueueHead = g_eventQueueHead == 0 ? EVENT_QUEUE_MAX_SIZE - 1 : g_eventQueueHead - 1;
Event &event = g_eventQueue[previousEventQueueHead];
if (event.type == g_lastEventType) {
g_eventQueueHead = previousEventQueueHead;
}
}

Event &event = g_eventQueue[g_eventQueueHead];

event.type = g_lastEventType;
event.x = g_x;
event.y = g_y;
Event &event = g_eventQueue[g_eventQueueHead];

if (g_eventQueueFull) {
g_eventQueueTail = (g_eventQueueTail + 1) % EVENT_QUEUE_MAX_SIZE;
}
event.type = g_lastEventType;
event.x = g_x;
event.y = g_y;

g_eventQueueHead = (g_eventQueueHead + 1) % EVENT_QUEUE_MAX_SIZE;
if (g_eventQueueFull) {
g_eventQueueTail = (g_eventQueueTail + 1) % EVENT_QUEUE_MAX_SIZE;
}

if (g_eventQueueHead == g_eventQueueTail) {
g_eventQueueFull = true;
}
g_eventQueueHead = (g_eventQueueHead + 1) % EVENT_QUEUE_MAX_SIZE;

osMutexRelease(g_eventQueueMutexId);
if (g_eventQueueHead == g_eventQueueTail) {
g_eventQueueFull = true;
}
}
#endif
}

////////////////////////////////////////////////////////////////////////////////

void init() {
#if defined(EEZ_PLATFORM_STM32)
g_eventQueueMutexId = osMutexCreate(osMutex(g_eventQueueMutex));
osThreadCreate(osThread(g_touchTask), nullptr);
#endif
}

void tick() {
#if defined(EEZ_PLATFORM_STM32)
if (osMutexWait(g_eventQueueMutexId, 5) == osOK) {
if (g_eventQueueFull || g_eventQueueTail != g_eventQueueHead) {
Event &event = g_eventQueue[g_eventQueueTail];

g_eventType = event.type;
g_eventX = event.x;
g_eventY = event.y;

g_eventQueueTail = (g_eventQueueTail + 1) % EVENT_QUEUE_MAX_SIZE;
g_eventQueueFull = false;
} else {
g_eventType = EVENT_TYPE_TOUCH_NONE;
g_eventX = -1;
g_eventY = -1;
}
taskENTER_CRITICAL();

if (g_eventQueueFull || g_eventQueueTail != g_eventQueueHead) {
Event &event = g_eventQueue[g_eventQueueTail];

osMutexRelease(g_eventQueueMutexId);
g_eventType = event.type;
g_eventX = event.x;
g_eventY = event.y;

g_eventQueueTail = (g_eventQueueTail + 1) % EVENT_QUEUE_MAX_SIZE;
g_eventQueueFull = false;
} else {
g_eventType = EVENT_TYPE_TOUCH_NONE;
g_eventX = -1;
g_eventY = -1;
}

taskEXIT_CRITICAL();
#endif

#if defined(EEZ_PLATFORM_SIMULATOR)
oneIter();
tickHighPriority();

g_eventType = g_lastEventType;
g_eventX = g_x;
Expand Down
2 changes: 1 addition & 1 deletion src/eez/gui/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace eez {
namespace gui {
namespace touch {

void init();
void tickHighPriority();
void tick();

EventType getEventType();
Expand Down
10 changes: 7 additions & 3 deletions src/eez/modules/mcu/stm32/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ void animate() {
DMA2D_WAIT;

// wait for VSYNC
while (!(LTDC->CDSR & LTDC_CDSR_VSYNCS));
while (!(LTDC->CDSR & LTDC_CDSR_VSYNCS)) {
osDelay(1);
}

HAL_LTDC_SetAddress(&hltdc, (uint32_t)g_animationBuffer, 0);
} else {
Expand All @@ -443,7 +445,9 @@ void animate() {

void swapBuffers() {
// wait for VSYNC
while (!(LTDC->CDSR & LTDC_CDSR_VSYNCS)) {}
while (!(LTDC->CDSR & LTDC_CDSR_VSYNCS)) {
osDelay(1);
}

HAL_LTDC_SetAddress(&hltdc, (uint32_t)g_buffer, 0);

Expand Down Expand Up @@ -701,4 +705,4 @@ void drawStr(const char *text, int textLength, int x, int y, int clip_x1, int cl
} // namespace mcu
} // namespace eez

#endif // EEZ_PLATFORM_STM32
#endif // EEZ_PLATFORM_STM32
2 changes: 1 addition & 1 deletion src/eez/modules/mcu/stm32/touch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void touchMeasure() {

uint8_t result[5];
memset(result, 0, 5);
HAL_I2C_Master_Receive(&hi2c1, TOUCH_DEVICE_ADDRESS, result, 5, 100);
HAL_I2C_Master_Receive(&hi2c1, TOUCH_DEVICE_ADDRESS, result, 5, 5);

taskEXIT_CRITICAL();

Expand Down
20 changes: 5 additions & 15 deletions src/eez/modules/psu/psu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ void onThreadMessage(uint8_t type, uint32_t param) {
tick();
}
#endif
} if (type == PSU_MESSAGE_CHANGE_POWER_STATE) {
} else if (type == PSU_MESSAGE_CHANGE_POWER_STATE) {
changePowerState(param ? true : false);
} else if (type == PSU_MESSAGE_RESET) {
reset();
Expand Down Expand Up @@ -1211,17 +1211,6 @@ void onProtectionTripped() {

////////////////////////////////////////////////////////////////////////////////

typedef void (*TickFunc)();
static TickFunc g_tickFuncs[] = {
temperature::tick,
#if OPTION_FAN
aux_ps::fan::tick,
#endif
datetime::tick
};
static const int NUM_TICK_FUNCS = sizeof(g_tickFuncs) / sizeof(TickFunc);
static int g_tickFuncIndex = 0;

void tick() {
for (int i = 0; i < NUM_SLOTS; i++) {
g_slots[i]->tick();
Expand All @@ -1238,9 +1227,10 @@ void tick() {
dlog_record::tick();

io_pins::tick();

g_tickFuncs[g_tickFuncIndex]();
g_tickFuncIndex = (g_tickFuncIndex + 1) % NUM_TICK_FUNCS;
temperature::tick();
aux_ps::fan::tick();
datetime::tick();
touch::tickHighPriority();

if (g_diagCallback) {
g_diagCallback();
Expand Down

0 comments on commit 51a1bff

Please sign in to comment.