This commit is contained in:
2021-03-27 18:19:55 -07:00
parent 3b32d72d5e
commit cadfd40b61
47 changed files with 1258 additions and 205 deletions

View File

@ -5,6 +5,7 @@ void
Buttons::onStart()
{
for(int i = 0; i < 3; i++) {
Log.info("Bound pin %d to button %d", 2 + i, i);
m_buttons[i].attach(2 + i, INPUT_PULLDOWN);
m_buttons[i].interval(15);
}
@ -15,8 +16,22 @@ Buttons::read()
{
for(int i = 0; i < 3; i++) {
m_buttons[i].update();
if (m_buttons[i].fell()) {
return InputEvent{m_buttonMap[i]};
if (m_buttons[i].rose()) {
//Log.info("Read press on %d", i);
int buttonID = m_buttonMap[i];
for(int j = 0; j < 3; j++ ) {
if (j != i && m_buttons[j].held()) {
buttonID |= m_buttonMap[j];
Log.info("Chord with %d", j);
m_wasChord[j] = true;
}
}
if (m_wasChord[i]) {
//Log.info("Not emitting release from previous chord");
m_wasChord[i] = false;
} else {
return InputEvent{InputEvent::UserInput, buttonID};
}
}
}
return InputEvent{};

View File

@ -15,24 +15,27 @@ public:
if (readResult == HIGH) {
m_state = Started;
m_downStart = millis();
Log.info("Button %d is started!", m_pin);
//Log.info("Button %d is started!", m_pin);
}
} else if (m_state == Started && millis() - m_downStart >= m_interval) {
if (readResult == HIGH) {
m_state = Confirmed;
Log.info("Button %d is CONFIRMED!", m_pin);
//Log.info("Button %d is CONFIRMED!", m_pin);
} else {
m_state = Ready;
Log.info("Button %d bounced back to ready!", m_pin);
//Log.info("Button %d bounced back to ready!", m_pin);
}
} else if (m_state == Confirmed || m_state == Held) {
if (readResult == LOW) {
m_state = Ready;
Log.info("Button %d is released and back to ready!", m_pin);
//Log.info("Button %d is released", m_pin);
m_state = Released;
} else if (m_state == Confirmed) {
m_state = Held;
Log.info("Button %d is being held down!", m_pin);
//Log.info("Button %d is being held down!", m_pin);
}
} else if (m_state == Released) {
//Log.info("Button %d is ready!", m_pin);
m_state = Ready;
}
}
@ -44,6 +47,10 @@ public:
return m_state == Confirmed;
}
bool rose() const {
return m_state == Released;
}
bool held() const {
return m_state == Held;
}
@ -53,7 +60,8 @@ private:
Ready,
Started,
Confirmed,
Held
Held,
Released
};
State m_state = Ready;
@ -64,10 +72,23 @@ private:
class Buttons: public InputSource {
public:
Buttons() : InputSource("Buttons") {}
void onStart() override;
InputEvent read() override;
enum Chord {
None = 0,
Circle = 1,
Triangle = 2,
Cross = 4,
CircleTriangle = Circle | Triangle,
CircleCross = Circle | Cross,
TriangleCross = Triangle | Cross,
CircleTriangleCross = Circle | Triangle | Cross
};
private:
Bounce m_buttons[3];
InputEvent::Intent m_buttonMap[3] = {InputEvent::PowerToggle, InputEvent::NextPattern, InputEvent::UserInput};
Chord m_buttonMap[3] = {Circle, Triangle, Cross};
bool m_wasChord[3] = {false, false, false};
};

View File

@ -1,25 +0,0 @@
#include "CloudStatus.h"
#include "../Static.h"
void
CloudStatus::onStart()
{
SINGLE_THREADED_BLOCK() {
if (Particle.connected()) {
initNetwork(0, cloud_status_connected);
} else {
System.on(cloud_status, &CloudStatus::initNetwork);
}
}
}
void
CloudStatus::initNetwork(system_event_t event, int param) {
if (param == cloud_status_connected) {
Log.info("Connected to T H E C L O U D");
MainLoop::instance()->dispatch(InputEvent{InputEvent::NetworkStatus, true});
}
}
STATIC_ALLOC(CloudStatus);

View File

@ -1,11 +0,0 @@
#include "../Figments/Figments.h"
class CloudStatus: public Task {
public:
CloudStatus() : Task("Cloud") {}
void loop() override {}
void onStart() override;
private:
static void initNetwork(system_event_t event, int param);
};

View File

@ -7,14 +7,33 @@ public:
: InputSource(name, initialState), m_colors(colors) {}
InputEvent read() override {
EVERY_N_SECONDS(Period) {
m_idx %= m_colors.size();
return InputEvent{InputEvent::SetColor, m_colors[m_idx++]};
if (m_reset) {
m_reset = false;
m_override = false;
return InputEvent{InputEvent::SetColor, m_colors[m_idx]};
}
if (!m_override) {
EVERY_N_SECONDS(Period) {
m_idx %= m_colors.size();
return InputEvent{InputEvent::SetColor, m_colors[m_idx++]};
}
}
return InputEvent{};
}
void handleEvent(const InputEvent& event) {
if (event.intent == InputEvent::SetColor) {
m_override = true;
}
}
void onStart() override {
m_reset = true;
}
private:
std::vector<NSFastLED::CRGB> m_colors;
int m_idx = 0;
bool m_reset = true;
bool m_override = false;
};

View File

@ -13,6 +13,7 @@ class MPU5060: public InputSource {
const int ACCEL_CONFIG_REG = 0x1C;
public:
MPU5060() : InputSource("MPU5060", HardwareConfig::HAS_MPU_6050 ? Task::Running : Task::Stopped) {}
void onStart() override {
Wire.begin();

View File

@ -1,138 +0,0 @@
#include "Particle.h"
#include "../Figments/Figments.h"
#include "../colors.h"
#include "../Static.h"
#include "./Photon.h"
void
PhotonInput::onConnected()
{
Log.info("Connecting photon input...");
Particle.function("power", &PhotonInput::setPower, this);
Particle.function("next", &PhotonInput::nextPattern, this);
Particle.function("input", &PhotonInput::input, this);
Particle.function("previous", &PhotonInput::previousPattern, this);
Particle.function("pattern", &PhotonInput::setPattern, this);
Particle.function("setHue", &PhotonInput::setHue, this);
Particle.function("brightness", &PhotonInput::setBrightness, this);
Particle.function("reboot", &PhotonInput::reboot, this);
Particle.function("start", &PhotonInput::startThing, this);
Particle.function("stop", &PhotonInput::stopThing, this);
}
int
PhotonInput::startThing(String command)
{
command.toCharArray(m_commandBuf, sizeof(m_commandBuf));
setEvent(InputEvent(InputEvent::StartThing, m_commandBuf));
return 0;
}
int
PhotonInput::stopThing(String command)
{
command.toCharArray(m_commandBuf, sizeof(m_commandBuf));
setEvent(InputEvent(InputEvent::StopThing, m_commandBuf));
return 0;
}
void
PhotonInput::onStart()
{
System.on(firmware_update, &PhotonInput::onFirmwareUpdate);
System.on(button_click, &PhotonInput::onButtonClick);
System.on(reset, &PhotonInput::onReset);
}
void
PhotonInput::handleEvent(const InputEvent &evt)
{
if (evt.intent == InputEvent::NetworkStatus) {
onConnected();
}
}
int
PhotonInput::reboot(String command)
{
System.reset();
return 0;
}
void
PhotonInput::onReset(system_event_t event, int param)
{
NSFastLED::FastLED.clear();
}
void
PhotonInput::onButtonClick(system_event_t event, int param)
{
Static<PhotonInput>::instance()->setEvent(InputEvent{InputEvent::NextPattern, param});
}
void
PhotonInput::onFirmwareUpdate(system_event_t event, int param)
{
Static<PhotonInput>::instance()->setEvent(InputEvent{InputEvent::FirmwareUpdate, param});
}
int
PhotonInput::input(String command)
{
command.toCharArray(m_commandBuf, sizeof(m_commandBuf));
setEvent(InputEvent(InputEvent::UserInput, m_commandBuf));
return 0;
}
int
PhotonInput::setPattern(String command)
{
command.toCharArray(m_commandBuf, sizeof(m_commandBuf));
setEvent(InputEvent(InputEvent::SetPattern, m_commandBuf));
return 0;
}
int
PhotonInput::setHue(String colorName)
{
ColorInfo nextColor = colorForName(colorName);
setEvent(InputEvent(InputEvent::SetColor, nextColor.rgb));
return 0;
}
int
PhotonInput::nextPattern(String command)
{
setEvent(InputEvent(InputEvent::NextPattern));
return 0;
}
int
PhotonInput::previousPattern(String command)
{
setEvent(InputEvent(InputEvent::PreviousPattern));
return 0;
}
int
PhotonInput::setPower(String command)
{
if (command == "off") {
setEvent(InputEvent(InputEvent::SetPower, 0));
} else if (command == "on") {
setEvent(InputEvent(InputEvent::SetPower, 1));
} else {
setEvent(InputEvent(InputEvent::PowerToggle));
}
return 0;
}
int
PhotonInput::setBrightness(String command)
{
setEvent(InputEvent(InputEvent::SetBrightness, command.toInt()));
return 0;
}
STATIC_ALLOC(PhotonInput);

View File

@ -1,29 +0,0 @@
#include "Particle.h"
#include "../Figments/Figments.h"
class PhotonInput: public BufferedInputSource {
public:
PhotonInput() : BufferedInputSource("PhotonInput") {}
void onStart() override;
void handleEvent(const InputEvent &evt) override;
private:
char m_commandBuf[16];
void onConnected();
int reboot(String command);
int input(String command);
int setPattern(String command);
int setHue(String colorName);
int nextPattern(String command);
int previousPattern(String command);
int setPower(String command);
int setBrightness(String command);
int startThing(String command);
int stopThing(String command);
static void onReset(system_event_t event, int param);
static void onButtonClick(system_event_t event, int param);
static void onFirmwareUpdate(system_event_t event, int param);
};