diff --git a/src/bill-acceptor.cpp b/src/bill-acceptor.cpp index 45bd769..a96b1d2 100644 --- a/src/bill-acceptor.cpp +++ b/src/bill-acceptor.cpp @@ -2,7 +2,13 @@ namespace { - bool initialized = false; + enum class State { + uninitialized, + initialized, + failed + }; + State state = State::uninitialized; + std::deque buffer; float accumulatedValue = 0.00; float escrowValue = 0.00; @@ -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); + } } } @@ -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) { @@ -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; + } } } diff --git a/src/button.cpp b/src/button.cpp index d3b6ac8..acff366 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -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. @@ -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; } } } diff --git a/src/coin-acceptor/dg600f.cpp b/src/coin-acceptor/dg600f.cpp index 02c06cc..5d3fb7c 100644 --- a/src/coin-acceptor/dg600f.cpp +++ b/src/coin-acceptor/dg600f.cpp @@ -2,13 +2,20 @@ namespace { - bool initialized = false; + enum class State { + uninitialized, + initialized, + failed + }; + State state = State::uninitialized; + float accumulatedValue = 0.00; std::deque buffer; std::vector coinValues; unsigned short coinSignalPin; unsigned short coinInhibitPin; unsigned int coinBaudRate; + unsigned short fiatPrecision; float getCoinValue(const int &byteIn) { const int index = byteIn - 1; @@ -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; } } @@ -50,10 +57,11 @@ 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) { @@ -61,18 +69,23 @@ namespace coinAcceptor_dg600f { } } 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; + } } } @@ -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); + } } } diff --git a/src/coin-acceptor/hx616.cpp b/src/coin-acceptor/hx616.cpp index cc2a501..3bdcf8c 100644 --- a/src/coin-acceptor/hx616.cpp +++ b/src/coin-acceptor/hx616.cpp @@ -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; @@ -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. @@ -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; + } } }