InputPollable: System for polling after interrupts

This commit is contained in:
WillyJL 2025-09-19 22:06:09 +02:00
parent 3c6b65c3b3
commit e59a53c5ec
No known key found for this signature in database
3 changed files with 69 additions and 2 deletions

View File

@ -3,16 +3,58 @@
InputBroker *inputBroker = nullptr;
InputBroker::InputBroker(){};
InputBroker::InputBroker()
{
#ifdef HAS_FREE_RTOS
inputEventQueue = xQueueCreate(5, sizeof(InputEvent));
pollSoonQueue = xQueueCreate(5, sizeof(InputPollable *));
xTaskCreate(pollSoonWorker, "input-pollSoon", 2 * 1024, this, 10, &pollSoonTask);
#endif
}
void InputBroker::registerSource(Observable<const InputEvent *> *source)
{
this->inputEventObserver.observe(source);
}
#ifdef HAS_FREE_RTOS
void InputBroker::pollSoonRequestFromIsr(InputPollable *pollable)
{
xQueueSendFromISR(pollSoonQueue, &pollable, NULL);
}
void InputBroker::queueInputEvent(const InputEvent *event)
{
xQueueSend(inputEventQueue, event, portMAX_DELAY);
}
void InputBroker::processInputEventQueue()
{
InputEvent event;
while (xQueueReceive(inputEventQueue, &event, 0)) {
handleInputEvent(&event);
}
}
#endif
int InputBroker::handleInputEvent(const InputEvent *event)
{
powerFSM.trigger(EVENT_INPUT); // todo: not every input should wake, like long hold release
this->notifyObservers(event);
return 0;
}
}
#ifdef HAS_FREE_RTOS
void InputBroker::pollSoonWorker(void *p)
{
InputBroker *instance = (InputBroker *)p;
while (true) {
InputPollable *pollable = NULL;
xQueueReceive(instance->pollSoonQueue, &pollable, portMAX_DELAY);
if (pollable) {
pollable->pollOnce();
}
}
vTaskDelete(NULL);
}
#endif

View File

@ -1,5 +1,7 @@
#pragma once
#include "Observer.h"
#include "freertosinc.h"
enum input_broker_event {
INPUT_BROKER_NONE = 0,
@ -41,6 +43,13 @@ typedef struct _InputEvent {
uint16_t touchX;
uint16_t touchY;
} InputEvent;
class InputPollable
{
public:
virtual void pollOnce() = 0;
};
class InputBroker : public Observable<const InputEvent *>
{
CallbackObserver<InputBroker, const InputEvent *> inputEventObserver =
@ -50,9 +59,22 @@ class InputBroker : public Observable<const InputEvent *>
InputBroker();
void registerSource(Observable<const InputEvent *> *source);
void injectInputEvent(const InputEvent *event) { handleInputEvent(event); }
#ifdef HAS_FREE_RTOS
void pollSoonRequestFromIsr(InputPollable *pollable);
void queueInputEvent(const InputEvent *event);
void processInputEventQueue();
#endif
protected:
int handleInputEvent(const InputEvent *event);
private:
#ifdef HAS_FREE_RTOS
QueueHandle_t inputEventQueue;
QueueHandle_t pollSoonQueue;
TaskHandle_t pollSoonTask;
static void pollSoonWorker(void *p);
#endif
};
extern InputBroker *inputBroker;

View File

@ -1595,6 +1595,9 @@ void loop()
#endif
service->loop();
#if !MESHTASTIC_EXCLUDE_INPUTBROKER
inputBroker->processInputEventQueue();
#endif
#if defined(LGFX_SDL)
if (screen) {
auto dispdev = screen->getDisplayDevice();