Skip to content

Commit

Permalink
improved touch responsiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Mar 21, 2020
1 parent 5e0fb91 commit af78d5d
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/eez/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ osMessageQId g_guiMessageQueueId;
void startThread() {
decompressAssets();
mcu::display::onThemeChanged();
touch::init();
g_guiMessageQueueId = osMessageCreate(osMessageQ(g_guiMessageQueue), NULL);
g_guiTaskHandle = osThreadCreate(osThread(g_guiTask), nullptr);
}
Expand Down
161 changes: 141 additions & 20 deletions src/eez/gui/touch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,72 @@ namespace eez {
namespace gui {
namespace touch {

EventType g_eventType = EVENT_TYPE_TOUCH_NONE;
static EventType g_lastEventType = EVENT_TYPE_TOUCH_NONE;

int g_x;
int g_y;
bool g_pressed;
static int g_x = -1;
static int g_y = -1;
static bool g_pressed = false;

int g_calibratedX = -1;
int g_calibratedY = -1;
bool g_calibratedPressed;
static int g_calibratedX = -1;
static int g_calibratedY = -1;
static bool g_calibratedPressed = false;

int g_filteredX = -1;
int g_filteredY = -1;
bool g_filteredPressed;
static int g_filteredX = -1;
static int g_filteredY = -1;
static bool g_filteredPressed = false;

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

void tick() {
EventType g_eventType = EVENT_TYPE_TOUCH_NONE;
int g_eventX = -1;
int g_eventY = -1;

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

#if defined(EEZ_PLATFORM_STM32)

static const int EVENT_QUEUE_MAX_SIZE = 50;
struct Event {
EventType type;
int x;
int y;
};
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() {
bool pressed;
int x;
int y;
Expand All @@ -69,28 +118,100 @@ void tick() {
#endif

if (g_pressed) {
if (g_eventType == EVENT_TYPE_TOUCH_NONE || g_eventType == EVENT_TYPE_TOUCH_UP) {
g_eventType = EVENT_TYPE_TOUCH_DOWN;
if (g_lastEventType == EVENT_TYPE_TOUCH_NONE || g_lastEventType == EVENT_TYPE_TOUCH_UP) {
g_lastEventType = EVENT_TYPE_TOUCH_DOWN;
} else {
if (g_eventType == EVENT_TYPE_TOUCH_DOWN) {
g_eventType = EVENT_TYPE_TOUCH_MOVE;
if (g_lastEventType == EVENT_TYPE_TOUCH_DOWN) {
g_lastEventType = EVENT_TYPE_TOUCH_MOVE;
}
}
} else {
if (g_eventType == EVENT_TYPE_TOUCH_DOWN || g_eventType == EVENT_TYPE_TOUCH_MOVE) {
g_eventType = EVENT_TYPE_TOUCH_UP;
} else if (g_eventType == EVENT_TYPE_TOUCH_UP) {
if (g_lastEventType == EVENT_TYPE_TOUCH_DOWN || g_lastEventType == EVENT_TYPE_TOUCH_MOVE) {
g_lastEventType = EVENT_TYPE_TOUCH_UP;
} else if (g_lastEventType == EVENT_TYPE_TOUCH_UP) {
g_lastEventType = EVENT_TYPE_TOUCH_NONE;
}
}

#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;
}
}

Event &event = g_eventQueue[g_eventQueueHead];

event.type = g_lastEventType;
event.x = g_x;
event.y = g_y;

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

g_eventQueueHead = (g_eventQueueHead + 1) % EVENT_QUEUE_MAX_SIZE;

if (g_eventQueueHead == g_eventQueueTail) {
g_eventQueueFull = true;
}

osMutexRelease(g_eventQueueMutexId);
}
}
#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;
}

osMutexRelease(g_eventQueueMutexId);
}
#endif

#if defined(EEZ_PLATFORM_SIMULATOR)
oneIter();

g_eventType = g_lastEventType;
g_eventX = g_x;
g_eventY = g_y;
#endif
}

int getX() {
return g_x;
return g_eventX;
}

int getY() {
return g_y;
return g_eventY;
}

} // namespace touch
Expand Down

0 comments on commit af78d5d

Please sign in to comment.