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 28a4fd4 commit ddd1e5c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 61 deletions.
51 changes: 32 additions & 19 deletions src/bill-acceptor.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;

std::deque<int> buffer;
float accumulatedValue = 0.00;
float escrowValue = 0.00;
Expand Down Expand Up @@ -91,10 +97,12 @@ namespace {
}

void serialWriteSIOCode(const char* key) {
const uint8_t byteOut = getSIOCodeByte(key);
if (byteOut > 0) {
logger::write("Sending SIO code to bill acceptor: \"" + std::string(key) + "\"");
Serial1.write(byteOut);
if (state == State::initialized) {
const uint8_t byteOut = getSIOCodeByte(key);
if (byteOut > 0) {
logger::write("Sending SIO code to bill acceptor: \"" + std::string(key) + "\"");
Serial1.write(byteOut);
}
}
}

Expand Down Expand Up @@ -154,7 +162,7 @@ namespace billAcceptor {
}

void loop() {
if (initialized) {
if (state == State::initialized) {
while (Serial1.available()) {
const uint8_t byteIn = Serial1.read();
if (byteIn > 0) {
Expand All @@ -166,19 +174,24 @@ namespace billAcceptor {
}
}
parseBuffer();
} else if (!(billRxPin > 0)) {
logger::write("Cannot initialize bill acceptor: \"billRxPin\" not set");
} else if (!(billTxPin > 0)) {
logger::write("Cannot initialize bill acceptor: \"billTxPin\" not set");
} else if (!(billBaudRate > 0)) {
logger::write("Cannot initialize bill acceptor: \"billBaudRate\" not set");
} else {
initialized = true;
logger::write("Initializing bill acceptor...");
Serial1.begin(billBaudRate, SERIAL_8N1, billTxPin, billRxPin);
billAcceptor::disinhibit();
serialWriteSIOCode("enable_escrow_mode");
serialWriteSIOCode("enable_escrow_timeout");
} else if (state == State::uninitialized) {
if (!(billRxPin > 0)) {
logger::write("Cannot initialize bill acceptor: \"billRxPin\" not set", "warn");
state = State::failed;
} else if (!(billTxPin > 0)) {
logger::write("Cannot initialize bill acceptor: \"billTxPin\" not set", "warn");
state = State::failed;
} else if (!(billBaudRate > 0)) {
logger::write("Cannot initialize bill acceptor: \"billBaudRate\" not set", "warn");
state = State::failed;
} else {
logger::write("Initializing bill acceptor...");
Serial1.begin(billBaudRate, SERIAL_8N1, billTxPin, billRxPin);
billAcceptor::disinhibit();
serialWriteSIOCode("enable_escrow_mode");
serialWriteSIOCode("enable_escrow_timeout");
state = State::initialized;
}
}
}

Expand Down
48 changes: 32 additions & 16 deletions src/button.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include "button.h"

namespace {

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 @@ -11,27 +19,35 @@ namespace {
namespace button {

void init() {
logger::write("Initializing button...");
debounceDelay = config::getUnsignedInt("buttonDebounce");
pinNumber = config::getUnsignedShort("buttonPin");
pinMode(pinNumber, INPUT);
debounceDelay = config::getUnsignedInt("buttonDebounce");
if (!(pinNumber > 0)) {
logger::write("Cannot initialize button: \"buttonPin\" not set", "warn");
state = State::failed;
} else {
logger::write("Initializing button...");
pinMode(pinNumber, INPUT);
state = State::initialized;
}
}

void loop() {
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");
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
51 changes: 34 additions & 17 deletions src/coin-acceptor/dg600f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

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;
unsigned short coinSignalPin;
unsigned short coinInhibitPin;
unsigned int coinBaudRate;
unsigned short fiatPrecision;

float getCoinValue(const int &byteIn) {
const int index = byteIn - 1;
Expand All @@ -34,7 +41,7 @@ namespace {
const int byte3 = buffer.front();
buffer.pop_front();
if (byte3 == (byte1 ^ byte2)) {
logger::write("Coin inserted with value = " + std::to_string(coinValue));
logger::write("Coin inserted with value = " + util::floatToStringWithPrecision(coinValue, fiatPrecision));
accumulatedValue += coinValue;
}
}
Expand All @@ -50,29 +57,35 @@ namespace coinAcceptor_dg600f {
coinInhibitPin = config::getUnsignedShort("coinInhibitPin");
coinBaudRate = config::getUnsignedInt("coinBaudRate");
coinValues = config::getFloatVector("coinValues");
fiatPrecision = config::getUnsignedShort("fiatPrecision");
}

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 @@ -85,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 ddd1e5c

Please sign in to comment.