From b32b5dbdee2a3bf49f3c8e5a98b5691cd1071e34 Mon Sep 17 00:00:00 2001 From: Nis Wechselberg Date: Mon, 25 Sep 2017 21:44:53 +0200 Subject: [PATCH] Adjusted display timer in DS5 controller --- .../ESP8266-MQTT-Teufel-DS5.ino | 73 +++++++++++-------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/ESP8266-MQTT-Teufel-DS5/ESP8266-MQTT-Teufel-DS5.ino b/ESP8266-MQTT-Teufel-DS5/ESP8266-MQTT-Teufel-DS5.ino index be54b81..5b1ec7d 100644 --- a/ESP8266-MQTT-Teufel-DS5/ESP8266-MQTT-Teufel-DS5.ino +++ b/ESP8266-MQTT-Teufel-DS5/ESP8266-MQTT-Teufel-DS5.ino @@ -56,6 +56,8 @@ int curVol = 0; int targetVol = 0; unsigned long lastVolChange = 0; unsigned long volumeInterval = 500; +unsigned long volDisplayDelay = 7000; +uint8_t otherCommandSend = 0; // Process tracker for calibration. 0 = nothing, 1 = decreasing, 2 = increasing uint8_t recalibrateState = 0; int calibrateLowerLimit = -55; @@ -266,41 +268,54 @@ void updateState() { client.publish(mqtt_topic_volumeState, msg); } -void updateVolume() { - // Increase volume a notch if requested - if (targetVol > curVol) { +void updateVolume(unsigned long timeSinceChange) { + + if (otherCommandSend || timeSinceChange > volDisplayDelay) { + // Switch display to volume mode replicateNEC(irSendPin, 0x807F7A85, 32); - curVol += 1; - - snprintf(msg, 49, "%d", curVol); - client.publish(mqtt_topic_volumeState, msg); + } else { + // Increase volume a notch if requested + if (targetVol > curVol) { + replicateNEC(irSendPin, 0x807F7A85, 32); + curVol += 1; + + snprintf(msg, 49, "%d", curVol); + client.publish(mqtt_topic_volumeState, msg); + } + + // Decrease volume a notch if requested + if (targetVol < curVol) { + replicateNEC(irSendPin, 0x807F6A95, 32); + curVol -= 1; + + snprintf(msg, 49, "%d", curVol); + client.publish(mqtt_topic_volumeState, msg); + } + + // Check recalibration state + if (recalibrateState == 2 && curVol == calibrateUpperLimit) { + recalibrateState = 0; + } + if (recalibrateState == 1 && curVol == calibrateLowerLimit) { + recalibrateState = 2; + targetVol = calibrateUpperLimit; + } } - - // Decrease volume a notch if requested - if (targetVol < curVol) { - replicateNEC(irSendPin, 0x807F6A95, 32); - curVol -= 1; - - snprintf(msg, 49, "%d", curVol); - client.publish(mqtt_topic_volumeState, msg); - } - - // Check recalibration state - if (recalibrateState == 2 && curVol == calibrateUpperLimit) { - recalibrateState = 0; - } - if (recalibrateState == 1 && curVol == calibrateLowerLimit) { - recalibrateState = 2; - targetVol = calibrateUpperLimit; - } - } void replicateNEC(int pin, uint32_t data, int dataLength) { + + // Track if some other command than volume control has been sent in the meantime + if (data == 0x807F7A85 || data == 0x807F6A95) { + otherCommandSend = 0; + } else { + otherCommandSend = 1; + } + if (DEBUGTOSERIAL) { Serial.print("Sending code "); Serial.println(data, HEX); - } + } // Prepare mask uint32_t mask = 1L << (dataLength -1); // Write initial 9ms+4.5ms pulse @@ -341,9 +356,9 @@ void loop() { // Maybe update the volume unsigned long now = millis(); - if (now - lastVolChange > volumeInterval) { + if (targetVol != curVol && now - lastVolChange > volumeInterval) { + updateVolume(now - lastVolChange); lastVolChange = now; - updateVolume(); } // Maybe push the current status