From fbda2c5eee78d182b2c5e4350048f1817c101fb8 Mon Sep 17 00:00:00 2001 From: Charles Hill Date: Mon, 13 Jun 2022 18:28:13 +0100 Subject: [PATCH] Better initialization state handling --- src/button.cpp | 42 ++++++++++++++++++++------------ src/coin-acceptor/dg600f.cpp | 47 ++++++++++++++++++++++++------------ src/coin-acceptor/hx616.cpp | 27 ++++++++++++++------- 3 files changed, 75 insertions(+), 41 deletions(-) diff --git a/src/button.cpp b/src/button.cpp index d0dc588..acff366 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -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. @@ -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; } } } diff --git a/src/coin-acceptor/dg600f.cpp b/src/coin-acceptor/dg600f.cpp index ed95f33..5d3fb7c 100644 --- a/src/coin-acceptor/dg600f.cpp +++ b/src/coin-acceptor/dg600f.cpp @@ -2,7 +2,13 @@ namespace { - bool initialized = false; + enum class State { + uninitialized, + initialized, + failed + }; + State state = State::uninitialized; + float accumulatedValue = 0.00; std::deque buffer; std::vector coinValues; @@ -55,7 +61,7 @@ namespace coinAcceptor_dg600f { } void loop() { - if (initialized) { + if (state == State::initialized) { while (Serial2.available()) { const int byteReceived = Serial2.read(); if (byteReceived > 0 && byteReceived < 254) { @@ -63,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; + } } } @@ -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); + } } } 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; + } } }