mirror of
https://github.com/arwidcool/Solder-Plate.git
synced 2025-02-17 19:09:23 +01:00
refactor
This commit is contained in:
parent
a7e9adbcf9
commit
27593e1916
@ -2,7 +2,7 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
// Constructor
|
||||
Button::Button(ButtonKind kind, uint8_t pin) : kind(kind), pin(pin), state(WrappedState<ButtonKind, ButtonState>(kind, ButtonState::IDLE))
|
||||
Button::Button(ButtonKind kind, uint8_t pin) : kind(kind), pin(pin), state(WrappedState<ButtonState>(ButtonState::IDLE))
|
||||
{
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
}
|
||||
@ -17,14 +17,14 @@ uint8_t Button::getPin()
|
||||
return this->pin;
|
||||
}
|
||||
|
||||
StateChangeEvent<ButtonKind, ButtonState> *Button::lastChange()
|
||||
StateChangeEvent<ButtonState> *Button::lastChange()
|
||||
{
|
||||
return this->state.lastChangeEvent;
|
||||
}
|
||||
|
||||
bool Button::loop()
|
||||
{
|
||||
StateChangeEvent<ButtonKind, ButtonState> *evt = NULL;
|
||||
StateChangeEvent<ButtonState> *evt = NULL;
|
||||
|
||||
if (digitalRead(this->pin) == LOW)
|
||||
{
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "base.h"
|
||||
#include "./base.h"
|
||||
#include <Arduino.h>
|
||||
#include "../statechangeevent.h"
|
||||
#include "../common.h"
|
||||
|
||||
class Button
|
||||
{
|
||||
@ -10,13 +10,13 @@ public:
|
||||
|
||||
ButtonKind getKind();
|
||||
uint8_t getPin();
|
||||
StateChangeEvent<ButtonKind, ButtonState> *lastChange();
|
||||
StateChangeEvent<ButtonState> *lastChange();
|
||||
/// @brief Call this method in the main loop to update the button state.
|
||||
/// @return true if the button state changed, false otherwise.
|
||||
bool loop();
|
||||
|
||||
private:
|
||||
ButtonKind kind;
|
||||
private:
|
||||
uint8_t pin;
|
||||
WrappedState<ButtonKind, ButtonState> state;
|
||||
WrappedState<ButtonState> state;
|
||||
};
|
@ -12,17 +12,17 @@ void Buttons::setup()
|
||||
/**
|
||||
* Handle buttons state changes and return first button state change that occurred.
|
||||
*/
|
||||
StateChangeEvent<ButtonKind, ButtonState> *Buttons::handleButtons()
|
||||
Pair<ButtonKind, StateChangeEvent<ButtonState>>* Buttons::handleButtons()
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
// If a state change occurred, print it out and return the button that changed
|
||||
if (__buttons[i]->loop())
|
||||
{
|
||||
StateChangeEvent<ButtonKind, ButtonState> *change = __buttons[i]->lastChange();
|
||||
StateChangeEvent<ButtonState> *change = __buttons[i]->lastChange();
|
||||
|
||||
Serial.println(STATECHANGE_STR((*change)));
|
||||
return change;
|
||||
return new Pair<ButtonKind, StateChangeEvent<ButtonState>>(__buttons[i]->kind, *change);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <Arduino.h>
|
||||
#include "Button.h"
|
||||
#include "leds/leds.h"
|
||||
#include "../statechangeevent.h"
|
||||
#include "../common.h"
|
||||
// Button pins
|
||||
#define upButton 21
|
||||
#define downButton 22
|
||||
@ -18,7 +18,7 @@ public:
|
||||
// Constructor
|
||||
Buttons(){};
|
||||
|
||||
StateChangeEvent<ButtonKind, ButtonState> *handleButtons();
|
||||
Pair<ButtonKind, StateChangeEvent<ButtonState>> *handleButtons();
|
||||
void setup();
|
||||
};
|
||||
|
||||
|
47
src/common.h
Normal file
47
src/common.h
Normal file
@ -0,0 +1,47 @@
|
||||
#ifndef __basestatechange_h__
|
||||
#define __basestatechange_h__
|
||||
#include <Arduino.h>
|
||||
template <typename State>
|
||||
class StateChangeEvent
|
||||
{
|
||||
public:
|
||||
StateChangeEvent(State from, State to) : from(from), to(to) {}
|
||||
State from;
|
||||
State to;
|
||||
};
|
||||
|
||||
template <typename State>
|
||||
class WrappedState
|
||||
{
|
||||
public:
|
||||
WrappedState(State defaultState) : state(defaultState), lastChangeEvent(new StateChangeEvent<State>(defaultState, defaultState)) {}
|
||||
StateChangeEvent<State> *set(State state)
|
||||
{
|
||||
if (this->state == state)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
delete this->lastChangeEvent;
|
||||
lastChangeEvent = new StateChangeEvent<State>(this->state, state);
|
||||
this->state = state;
|
||||
this->lastStateChangeTime = millis();
|
||||
return lastChangeEvent;
|
||||
}
|
||||
State get()
|
||||
{
|
||||
return this->state;
|
||||
}
|
||||
State state;
|
||||
unsigned long lastStateChangeTime = 0;
|
||||
StateChangeEvent<State> *lastChangeEvent;
|
||||
};
|
||||
|
||||
template <typename First, typename Second>
|
||||
class Pair
|
||||
{
|
||||
public:
|
||||
Pair(First first, Second second) : first(first), second(second) {}
|
||||
First first;
|
||||
Second second;
|
||||
};
|
||||
#endif
|
@ -14,38 +14,38 @@ void LEDS::setup()
|
||||
pinMode(greenLED, OUTPUT);
|
||||
pinMode(redLED, OUTPUT);
|
||||
}
|
||||
void LEDS::handleButtonStateChange(StateChangeEvent<ButtonKind, ButtonState> change)
|
||||
void LEDS::handleButtonStateChange(Pair<ButtonKind, StateChangeEvent<ButtonState>> change)
|
||||
{
|
||||
switch (change.kind) {
|
||||
switch (change.first) {
|
||||
case ButtonKind::UP:
|
||||
if (change.to == ButtonState::PRESSED) {
|
||||
if (change.second.to == ButtonState::PRESSED) {
|
||||
digitalWrite(yellowLED, HIGH);
|
||||
} else if (!yellowLEDSeleOn) {
|
||||
digitalWrite(yellowLED, LOW);
|
||||
}
|
||||
yellowLEDUPOn = change.to == ButtonState::PRESSED;
|
||||
yellowLEDUPOn = change.second.to == ButtonState::PRESSED;
|
||||
break;
|
||||
case ButtonKind::DOWN:
|
||||
if (change.to == ButtonState::PRESSED) {
|
||||
if (change.second.to == ButtonState::PRESSED) {
|
||||
digitalWrite(greenLED, HIGH);
|
||||
} else {
|
||||
digitalWrite(greenLED, LOW);
|
||||
}
|
||||
break;
|
||||
case ButtonKind::BACK:
|
||||
if (change.to == ButtonState::PRESSED) {
|
||||
if (change.second.to == ButtonState::PRESSED) {
|
||||
digitalWrite(redLED, HIGH);
|
||||
} else {
|
||||
digitalWrite(redLED, LOW);
|
||||
}
|
||||
break;
|
||||
case ButtonKind::SELECT:
|
||||
if (change.to == ButtonState::PRESSED) {
|
||||
if (change.second.to == ButtonState::PRESSED) {
|
||||
digitalWrite(yellowLED, HIGH);
|
||||
} else if (!yellowLEDUPOn) {
|
||||
digitalWrite(yellowLED, LOW);
|
||||
}
|
||||
yellowLEDSeleOn = change.to == ButtonState::PRESSED;
|
||||
yellowLEDSeleOn = change.second.to == ButtonState::PRESSED;
|
||||
|
||||
break;
|
||||
default:
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef __leds_h__
|
||||
#define __leds_h__
|
||||
#include "../buttons/base.h"
|
||||
#include "../statechangeevent.h"
|
||||
#include "../common.h"
|
||||
|
||||
//If you didnt solder the LEDS in order, change the order here
|
||||
#define yellowLED 18
|
||||
@ -13,7 +13,7 @@ class LEDS
|
||||
public:
|
||||
LEDS();
|
||||
void setup();
|
||||
void handleButtonStateChange(StateChangeEvent<ButtonKind, ButtonState> change);
|
||||
void handleButtonStateChange(Pair<ButtonKind, StateChangeEvent<ButtonState>> change);
|
||||
|
||||
};
|
||||
#endif
|
@ -9,7 +9,7 @@
|
||||
#include "leds/leds.h"
|
||||
#include "reflow.h"
|
||||
#include "displays/oled.h"
|
||||
#include "statechangeevent.h"
|
||||
#include "common.h"
|
||||
|
||||
ReflowProcessState reflowProcessState = INITIALIZING;
|
||||
|
||||
@ -90,7 +90,7 @@ void loop()
|
||||
{
|
||||
|
||||
// Return the button that was pressed
|
||||
StateChangeEvent<ButtonKind, ButtonState>* k = buttons.handleButtons();
|
||||
Pair<ButtonKind, StateChangeEvent<ButtonState>> *k = buttons.handleButtons();
|
||||
|
||||
if (k != NULL) {
|
||||
leds.handleButtonStateChange(*k);
|
||||
|
@ -1,37 +0,0 @@
|
||||
#ifndef __basestatechange_h__
|
||||
#define __basestatechange_h__
|
||||
#include <Arduino.h>
|
||||
template <typename Kind, typename State>
|
||||
class StateChangeEvent {
|
||||
public:
|
||||
StateChangeEvent(Kind kind, State from, State to) : kind(kind), from(from), to(to) {}
|
||||
Kind kind;
|
||||
State from;
|
||||
State to;
|
||||
|
||||
};
|
||||
|
||||
|
||||
template <typename Kind, typename State>
|
||||
class WrappedState {
|
||||
public:
|
||||
WrappedState(Kind kind, State defaultState) : kind(kind), state(defaultState), lastChangeEvent(new StateChangeEvent<Kind, State>(kind, defaultState, defaultState)) {}
|
||||
Kind kind;
|
||||
StateChangeEvent<Kind, State> * set(State state) {
|
||||
if (this->state == state) {
|
||||
return NULL;
|
||||
}
|
||||
delete this->lastChangeEvent;
|
||||
lastChangeEvent = new StateChangeEvent<Kind, State>(kind, this->state, state);
|
||||
this->state = state;
|
||||
this->lastStateChangeTime = millis();
|
||||
return lastChangeEvent;
|
||||
}
|
||||
State get() {
|
||||
return this->state;
|
||||
}
|
||||
State state;
|
||||
unsigned long lastStateChangeTime = 0;
|
||||
StateChangeEvent<Kind, State> * lastChangeEvent;
|
||||
};
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user