Skip to content

Commit

Permalink
Better initialization state handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chill117 committed Jun 13, 2022
1 parent d39db36 commit fbda2c5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 41 deletions.
42 changes: 26 additions & 16 deletions src/button.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#include "button.h"

namespace {
bool initialized = false;

enum class State {
uninitialized,
initialized,
failed
};
State state = State::uninitialized;

bool pressed = false;
int lastState;
unsigned long lastStateChangeTime = 0;// Last time the button pin was toggled.
Expand All @@ -15,29 +22,32 @@ namespace button {
pinNumber = config::getUnsignedShort("buttonPin");
debounceDelay = config::getUnsignedInt("buttonDebounce");
if (!(pinNumber > 0)) {
logger::write("Cannot initialize button: \"buttonPin\" not set");
logger::write("Cannot initialize button: \"buttonPin\" not set", "warn");
state = State::failed;
} else {
logger::write("Initializing button...");
pinMode(pinNumber, INPUT);
initialized = true;
state = State::initialized;
}
}

void loop() {
if (initialized && (millis() - lastStateChangeTime) > debounceDelay) {
const int state = digitalRead(pinNumber);
if (state != lastState) {
if (state == HIGH) {
pressed = true;
logger::write("Button pressed");
} else {
pressed = false;
logger::write("Button released");
if (state == State::initialized) {
if (millis() - lastStateChangeTime > debounceDelay) {
const int state = digitalRead(pinNumber);
if (state != lastState) {
if (state == HIGH) {
pressed = true;
logger::write("Button pressed");
} else {
pressed = false;
logger::write("Button released");
}
// Reset the debouncing timer.
// We track time in order to avoid noise state changes.
lastStateChangeTime = millis();
lastState = state;
}
// Reset the debouncing timer.
// We track time in order to avoid noise state changes.
lastStateChangeTime = millis();
lastState = state;
}
}
}
Expand Down
47 changes: 31 additions & 16 deletions src/coin-acceptor/dg600f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

namespace {

bool initialized = false;
enum class State {
uninitialized,
initialized,
failed
};
State state = State::uninitialized;

float accumulatedValue = 0.00;
std::deque<int> buffer;
std::vector<float> coinValues;
Expand Down Expand Up @@ -55,26 +61,31 @@ namespace coinAcceptor_dg600f {
}

void loop() {
if (initialized) {
if (state == State::initialized) {
while (Serial2.available()) {
const int byteReceived = Serial2.read();
if (byteReceived > 0 && byteReceived < 254) {
buffer.push_back(byteReceived);
}
}
parseBuffer();
} else if (!(coinSignalPin > 0)) {
logger::write("Cannot initialize coin acceptor: \"coinSignalPin\" not set");
} else if (!(coinInhibitPin > 0)) {
logger::write("Cannot initialize coin acceptor: \"coinInhibitPin\" not set");
} else if (!(coinBaudRate > 0)) {
logger::write("Cannot initialize coin acceptor: \"coinBaudRate\" not set");
} else {
logger::write("Initializing DG600F coin acceptor...");
initialized = true;
Serial2.begin(coinBaudRate, SERIAL_8E1, coinSignalPin, 0);
pinMode(coinInhibitPin, OUTPUT);
coinAcceptor_dg600f::disinhibit();
} else if (state == State::uninitialized) {
if (!(coinSignalPin > 0)) {
logger::write("Cannot initialize coin acceptor: \"coinSignalPin\" not set", "warn");
state = State::failed;
} else if (!(coinInhibitPin > 0)) {
logger::write("Cannot initialize coin acceptor: \"coinInhibitPin\" not set", "warn");
state = State::failed;
} else if (!(coinBaudRate > 0)) {
logger::write("Cannot initialize coin acceptor: \"coinBaudRate\" not set", "warn");
state = State::failed;
} else {
logger::write("Initializing DG600F coin acceptor...");
Serial2.begin(coinBaudRate, SERIAL_8E1, coinSignalPin, 0);
pinMode(coinInhibitPin, OUTPUT);
coinAcceptor_dg600f::disinhibit();
state = State::initialized;
}
}
}

Expand All @@ -87,10 +98,14 @@ namespace coinAcceptor_dg600f {
}

void inhibit() {
digitalWrite(coinInhibitPin, LOW);
if (state == State::initialized) {
digitalWrite(coinInhibitPin, LOW);
}
}

void disinhibit() {
digitalWrite(coinInhibitPin, HIGH);
if (state == State::initialized) {
digitalWrite(coinInhibitPin, HIGH);
}
}
}
27 changes: 18 additions & 9 deletions src/coin-acceptor/hx616.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

namespace {

bool initialized = false;
enum class State {
uninitialized,
initialized,
failed
};
State state = State::uninitialized;

float valueIncrement = 1.00;
float accumulatedValue = 0.00;
uint8_t lastPinReadState;
Expand Down Expand Up @@ -37,7 +43,7 @@ namespace coinAcceptor_hx616 {
}

void loop() {
if (initialized) {
if (state == State::initialized) {
if (pinStateHasChanged()) {
if (coinWasInserted()) {
// This code executes once for each pulse from the HX-616.
Expand All @@ -46,13 +52,16 @@ namespace coinAcceptor_hx616 {
}
flipPinState();
}
} else if (!(coinSignalPin > 0)) {
logger::write("Cannot initialize coin acceptor: \"coinSignalPin\" not set");
} else {
logger::write("Initializing HX616 coin acceptor...");
initialized = true;
pinMode(coinSignalPin, INPUT_PULLUP);
lastPinReadState = readPin();
} else if (state == State::uninitialized) {
if (!(coinSignalPin > 0)) {
logger::write("Cannot initialize coin acceptor: \"coinSignalPin\" not set", "warn");
state = State::failed;
} else {
logger::write("Initializing HX616 coin acceptor...");
pinMode(coinSignalPin, INPUT_PULLUP);
lastPinReadState = readPin();
state = State::initialized;
}
}
}

Expand Down

0 comments on commit fbda2c5

Please sign in to comment.