renderbug-cpp/lib/Figments/Input.cpp

153 lines
3.0 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)});
}
const char*
InputEvent::name() const
{
switch (intent) {
case InputEvent::BeatDetect:
return "beat-detection";
case InputEvent::Beat:
return "beat";
case InputEvent::ReadyToRoll:
return "ready-to-roll";
case InputEvent::PowerToggle:
return "power-toggle";
case InputEvent::SetPower:
return "set-power";
case InputEvent::SetPattern:
return "set-pattern";
case InputEvent::SetScene:
return "set-scene";
case InputEvent::SetColor:
return "set-color";
case InputEvent::Acceleration:
return "acceleration";
case InputEvent::SetBrightness:
return "set-brightness";
case InputEvent::FirmwareUpdate:
return "firmware-update";
case InputEvent::NetworkStatus:
return "network-status";
case InputEvent::NetworkActivity:
return "network-activity";
case InputEvent::StartThing:
return "start-thing";
case InputEvent::StopThing:
return "stop-thing";
case InputEvent::SaveConfigurationRequest:
return "save-configuration";
case InputEvent::ConfigurationChanged:
return "configuration-changed";
case InputEvent::IdleStart:
return "idle-start";
case InputEvent::IdleStop:
return "idle-stop";
case InputEvent::ButtonPress:
return "button";
case InputEvent::LoadConfigurationByName:
return "load-config";
case None:
return "none";
}
return "unknown";
}