From a542fbc931063324d61f22e7917c6941a2aacb85 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 16 May 2020 10:20:27 +0200 Subject: [PATCH] Yellow flash for AP mode if RGB LED is configured --- src/AmsToMqttBridge.ino | 10 +++++---- src/HwTools.cpp | 46 +++++++++++++++++++++++++++++------------ src/HwTools.h | 8 +++---- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 8516d27e..3a2ab24e 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -341,7 +341,11 @@ void loop() { } else { dnsServer.processNextRequest(); // Continously flash the LED when AP mode - if (now / 50 % 64 == 0) hw.ledBlink(LED_INTERNAL, 1); + if (now / 50 % 64 == 0) { + if(!hw.ledBlink(LED_YELLOW, 1)) { + hw.ledBlink(LED_INTERNAL, 1); + } + } } if(hanSerialPin != config.getHanPin()) { @@ -495,9 +499,7 @@ void readHanPort() { lastSuccessfulRead = millis(); if(config.getMeterType() > 0) { - if(config.getLedPinGreen() != 0xFF) - hw.ledBlink(LED_GREEN, 1); - else + if(!hw.ledBlink(LED_GREEN, 1)) hw.ledBlink(LED_INTERNAL, 1); AmsData data(config.getMeterType(), config.isSubstituteMissing(), hanReader); diff --git a/src/HwTools.cpp b/src/HwTools.cpp index 8fa2762a..f84eb965 100644 --- a/src/HwTools.cpp +++ b/src/HwTools.cpp @@ -110,25 +110,25 @@ void HwTools::setLedRgb(uint8_t ledPinRed, uint8_t ledPinGreen, uint8_t ledPinBl } } -void HwTools::ledOn(uint8_t color) { +bool HwTools::ledOn(uint8_t color) { if(color == LED_INTERNAL) { - writeLedPin(color, ledInverted ? LOW : HIGH); + return writeLedPin(color, ledInverted ? LOW : HIGH); } else { - writeLedPin(color, ledRgbInverted ? LOW : HIGH); + return writeLedPin(color, ledRgbInverted ? LOW : HIGH); } } -void HwTools::ledOff(uint8_t color) { +bool HwTools::ledOff(uint8_t color) { if(color == LED_INTERNAL) { - writeLedPin(color, ledInverted ? HIGH : LOW); + return writeLedPin(color, ledInverted ? HIGH : LOW); } else { - writeLedPin(color, ledRgbInverted ? HIGH : LOW); + return writeLedPin(color, ledRgbInverted ? HIGH : LOW); } } -void HwTools::ledBlink(uint8_t color, uint8_t blink) { +bool HwTools::ledBlink(uint8_t color, uint8_t blink) { for(int i = 0; i < blink; i++) { - ledOn(color); + if(!ledOn(color)) return false; delay(50); ledOff(color); if(i != blink) @@ -136,29 +136,49 @@ void HwTools::ledBlink(uint8_t color, uint8_t blink) { } } -void HwTools::writeLedPin(uint8_t color, uint8_t state) { +bool HwTools::writeLedPin(uint8_t color, uint8_t state) { switch(color) { case LED_INTERNAL: - if(ledPin != 0xFF) + if(ledPin != 0xFF) { digitalWrite(ledPin, state); + return true; + } else { + return false; + } break; case LED_RED: - if(ledPinRed != 0xFF) + if(ledPinRed != 0xFF) { digitalWrite(ledPinRed, state); + return true; + } else { + return false; + } break; case LED_GREEN: - if(ledPinGreen != 0xFF) + if(ledPinGreen != 0xFF) { digitalWrite(ledPinGreen, state); + return true; + } else { + return false; + } break; case LED_BLUE: - if(ledPinBlue != 0xFF) + if(ledPinBlue != 0xFF) { digitalWrite(ledPinBlue, state); + return true; + } else { + return false; + } break; case LED_YELLOW: if(ledPinRed != 0xFF && ledPinGreen != 0xFF) { digitalWrite(ledPinRed, state); digitalWrite(ledPinGreen, state); + return true; + } else { + return false; } break; } + return false; } diff --git a/src/HwTools.h b/src/HwTools.h index d6a9f957..e7b5aeb9 100644 --- a/src/HwTools.h +++ b/src/HwTools.h @@ -28,9 +28,9 @@ public: int getWifiRssi(); void setLed(uint8_t ledPin, bool ledInverted); void setLedRgb(uint8_t ledPinRed, uint8_t ledPinGreen, uint8_t ledPinBlue, bool ledRgbInverted); - void ledOn(uint8_t color); - void ledOff(uint8_t color); - void ledBlink(uint8_t color, uint8_t blink); + bool ledOn(uint8_t color); + bool ledOff(uint8_t color); + bool ledBlink(uint8_t color, uint8_t blink); HwTools() {}; private: @@ -43,7 +43,7 @@ private: OneWire *oneWire; DallasTemperature *tempSensor; - void writeLedPin(uint8_t color, uint8_t state); + bool writeLedPin(uint8_t color, uint8_t state); }; #endif