This commit is contained in:
2021-03-28 14:50:31 -07:00
parent cadfd40b61
commit 92d5e73bd8
9 changed files with 141 additions and 96 deletions

View File

@ -29,19 +29,19 @@ InputSource::loop()
InputEvent
BufferedInputSource::read()
{
InputEvent ret = m_lastEvent;
m_lastEvent = InputEvent{};
InputEvent ret;
m_eventQueue.take(ret);
return ret;
}
void
BufferedInputSource::setEvent(InputEvent &&evt)
{
m_lastEvent = std::move(evt);
m_eventQueue.insert(std::move(evt));
}
void
BufferedInputSource::setEvent(InputEvent::Intent intent, Variant &&v)
{
m_lastEvent = InputEvent{intent, std::move(v)};
m_eventQueue.insert(InputEvent{intent, std::move(v)});
}

View File

@ -2,6 +2,7 @@
#include "application.h"
#include "./Geometry.h"
#include "./Figment.h"
#include "./Ringbuf.h"
#include "FastLED/FastLED.h"
typedef Vector3d<int> MotionVec;
@ -136,7 +137,7 @@ protected:
void setEvent(InputEvent::Intent intent, Variant &&v);
private:
InputEvent m_lastEvent;
Ringbuf<InputEvent, 5> m_eventQueue;
};
class InputMapper: public BufferedInputSource {

View File

@ -3,6 +3,7 @@
#include <vector>
#include <algorithm>
#include "./Input.h"
#include "./Ringbuf.h"
class Task;
class InputSource;
@ -50,42 +51,6 @@ struct Scheduler {
iterator end() { return iterator(*this, tasks.size()); }
};
template<typename T, int Size>
struct Ringbuf {
Ringbuf() : m_head(0), m_tail(0) {}
void clear() {
m_head = 0;
m_tail = 0;
}
bool take(T& dest) {
if (m_head == m_tail) {
return false;
}
const int cur = m_head;
const int nextHead = (m_head + 1) % Size;
m_head = nextHead;
dest = m_items[cur];
return true;
}
void insert(const T& src) {
const int cur = m_tail;
const int nextTail = (m_tail + 1) % Size;
if (nextTail == m_head) {
return;
} else {
m_tail = nextTail;
}
m_items[cur] = src;
}
private:
int m_head = 0;
int m_tail = 0;
std::array<T, Size> m_items;
};
struct MainLoop {
Scheduler scheduler;

View File

@ -0,0 +1,39 @@
#pragma once
#include <array>
template<typename T, int Size>
struct Ringbuf {
Ringbuf() : m_head(0), m_tail(0) {}
void clear() {
m_head = 0;
m_tail = 0;
}
bool take(T& dest) {
if (m_head == m_tail) {
return false;
}
const int cur = m_head;
const int nextHead = (m_head + 1) % Size;
m_head = nextHead;
dest = m_items[cur];
return true;
}
void insert(const T& src) {
const int cur = m_tail;
const int nextTail = (m_tail + 1) % Size;
if (nextTail == m_head) {
return;
} else {
m_tail = nextTail;
}
m_items[cur] = src;
}
private:
int m_head = 0;
int m_tail = 0;
std::array<T, Size> m_items;
};