renderbug-cpp/lib/Figments/Input.cpp

101 lines
1.6 KiB
C++

#include <Arduino.h>
#include "./Input.h"
#include "./MainLoop.h"
CRGB
Variant::asRGB() const
{
return CRGB(m_value.asRGB[0], m_value.asRGB[1], m_value.asRGB[2]);
}
const char*
Variant::asString() const
{
return m_value.asString;
}
int
Variant::asInt() const
{
return m_value.asInt;
}
bool
Variant::asBool() const
{
return (bool)m_value.asInt;
}
void
InputSource::init()
{
#ifdef CONFIG_THREADED_INPUTS
m_queue = xQueueCreate(32, sizeof(InputEvent));
#endif
}
#ifdef CONFIG_THREADED_INPUTS
void
InputSource::readThread(void* data)
{
InputSource* self = static_cast<InputSource*>(data);
while(true) {
InputEvent evt = self->read();
if (evt.intent != InputEvent::None) {
xQueueSend(m_queue, &evt, 0)
}
taskYIELD();
}
}
#endif
void
InputSource::onStart()
{
#ifdef CONFIG_THREADED_INPUTS
m_threadLoop = MainLoop::instance();
xTaskCreate(
&InputSource::readThread,
name,
1000,
this,
1,
NULL
);
#endif
}
void
InputSource::loop()
{
#ifndef CONFIG_THREADED_INPUTS
MainLoop::instance()->dispatch(read());
#else
InputEvent evt;
xQueueReceive(m_queue, &evt, 0);
if (evt.intent != InputEvent::None) {
MainLoop::instance()->dispatch(evt);
}
#endif
}
InputEvent
BufferedInputSource::read()
{
InputEvent ret;
m_eventQueue.take(ret);
return ret;
}
void
BufferedInputSource::setEvent(InputEvent &&evt)
{
m_eventQueue.insert(std::move(evt));
}
void
BufferedInputSource::setEvent(InputEvent::Intent intent, Variant &&v)
{
m_eventQueue.insert(InputEvent{intent, std::move(v)});
}