From d0ce4bf391f9a55574ea4dd0c897ee0546085c0e Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 21 Jun 2024 16:35:26 +0700 Subject: [PATCH 01/15] change debouce logic --- .../lib/ESPMegaPRO/DigitalInputCard.cpp | 45 ++++++++++++++----- .../lib/ESPMegaPRO/DigitalInputCard.hpp | 1 + 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp index 86ad5a1..5103c9b 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -137,25 +137,50 @@ void DigitalInputCard::handlePinChange(int pin, uint8_t ¤tBuffer, uint8_t // Handle Bank A if (((previousBuffer >> (7 - pin)) & 1) != ((currentBuffer >> (7 - pin)) & 1)) { - if (millis() - lastDebounceTime[pin] > debounceTime[pin]) - { + if (!pinChanged[pin]) { lastDebounceTime[pin] = millis(); - previousBuffer ^= (-((currentBuffer >> (7 - pin)) & 1) ^ previousBuffer) & (1UL << (7 - pin)); - for(const auto& callback : callbacks) - callback.second(virtualPin, ((currentBuffer >> (7 - pin)) & 1)); + pinChanged[pin] = true; + } else { + if (millis() - lastDebounceTime[pin] > debounceTime[pin]) + { + previousBuffer ^= (-((currentBuffer >> (7 - pin)) & 1) ^ previousBuffer) & (1UL << (7 - pin)); + for (const auto& callback : callbacks) + callback.second(virtualPin, ((currentBuffer >> (7 - pin)) & 1)); + pinChanged[pin] = false; + } } } // Handle Bank B if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1)) { - if (millis() - lastDebounceTime[pin] > debounceTime[pin]) - { + if (!pinChanged[pin]) { lastDebounceTime[pin] = millis(); - previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin)); - for (const auto& callback : callbacks) - callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1)); + pinChanged[pin] = true; + } else { + if (millis() - lastDebounceTime[pin] > debounceTime[pin]) + { + previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin)); + for (const auto& callback : callbacks) + callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1)); + pinChanged[pin] = false; + } } } + + + + // // Handle Bank B + // if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1)) + // { + // if (millis() - lastDebounceTime[pin] > debounceTime[pin]) + // { + // lastDebounceTime[pin] = millis(); + // previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin)); + // for (const auto& callback : callbacks) + // callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1)); + // } + // } + } /** diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp index a140029..3ba7805 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp @@ -54,6 +54,7 @@ class DigitalInputCard : public ExpansionCard { uint8_t previousInputBufferB; uint32_t debounceTime[16]; uint32_t lastDebounceTime[16]; + bool pinChanged[16]; // A map of the physical pin to the virtual pin uint8_t pinMap[16]; // A map of the virtual pin to the physical pin From 221d8e59f5318d2fe23b5b54d1a0595073b74697 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 21 Jun 2024 20:57:53 +0700 Subject: [PATCH 02/15] Update ESPMegaCommon.hpp --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp index ab46b85..3c1fa1d 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp @@ -1,3 +1,3 @@ #pragma once -#define SDK_VESRION "2.9.0" \ No newline at end of file +#define SDK_VESRION "2.9.1" \ No newline at end of file From b778fb6a02de660f34b5bddcabbe2f56ee5a75ff Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 21 Jun 2024 21:02:11 +0700 Subject: [PATCH 03/15] Update DigitalInputCard.cpp --- .../lib/ESPMegaPRO/DigitalInputCard.cpp | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp index 5103c9b..0cf87f0 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -167,20 +167,6 @@ void DigitalInputCard::handlePinChange(int pin, uint8_t ¤tBuffer, uint8_t } } - - - // // Handle Bank B - // if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1)) - // { - // if (millis() - lastDebounceTime[pin] > debounceTime[pin]) - // { - // lastDebounceTime[pin] = millis(); - // previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin)); - // for (const auto& callback : callbacks) - // callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1)); - // } - // } - } /** From 4ea2122f6eecc344fe253f47e37cc966e77e95c8 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 21 Jun 2024 21:34:51 +0700 Subject: [PATCH 04/15] allow input preload and state toggling --- .../lib/ESPMegaPRO/DigitalInputCard.cpp | 12 ++++++++++++ .../lib/ESPMegaPRO/DigitalInputCard.hpp | 2 ++ .../lib/ESPMegaPRO/DigitalOutputCard.cpp | 9 +++++++++ .../lib/ESPMegaPRO/DigitalOutputCard.hpp | 2 ++ ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp | 2 +- 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp index 0cf87f0..9ae678d 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -310,4 +310,16 @@ void DigitalInputCard::loadPinMap(uint8_t pinMap[16]) uint8_t DigitalInputCard::getType() { return CARD_TYPE_DIGITAL_INPUT; +} + +/** + * @brief Preload the previous input buffer and the input buffer + * + * @note This function is useful if you want to preload the input buffers with a run-time value + */ +void DigitalInputCard::preloadInputBuffer() { + refreshInputBankA(); + refreshInputBankB(); + previousInputBufferA = inputBufferA; + previousInputBufferB = inputBufferB; } \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp index 3ba7805..f5587ba 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp @@ -41,6 +41,8 @@ class DigitalInputCard : public ExpansionCard { void unregisterCallback(uint8_t handler); // Load a new pin map void loadPinMap(uint8_t pinMap[16]); + // Preload previousInputBuffer and inputBuffer + void preloadInputBuffer(); // Get type of card uint8_t getType(); private: diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalOutputCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalOutputCard.cpp index cefa8e8..d760dc1 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalOutputCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalOutputCard.cpp @@ -306,4 +306,13 @@ void DigitalOutputCard::saveStateToFRAM() { if(!framBinded) return; uint16_t packed = packStates(); this->fram->write16(framAddress, packed); +} + +/** + * @brief Toggle the state of the specified pin + * + * @param pin The pin to toggle + */ +void DigitalOutputCard::toggleState(uint8_t pin) { + this->setState(pin, !this->state_buffer[pin]); } \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalOutputCard.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalOutputCard.hpp index a3ed97c..88ebbaa 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalOutputCard.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalOutputCard.hpp @@ -58,6 +58,8 @@ public: void setValue(uint8_t pin, uint16_t value); // Get the state of the specified pin bool getState(uint8_t pin); + // Toggle the state of the specified pin + void toggleState(uint8_t pin); // Get the pwm value of the specified pin uint16_t getValue(uint8_t pin); // Register a callback function that will be called when the state of a pin changes diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp index 3c1fa1d..655573d 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp @@ -1,3 +1,3 @@ #pragma once -#define SDK_VESRION "2.9.1" \ No newline at end of file +#define SDK_VESRION "2.9.2" \ No newline at end of file From 8aeb6ab30a357911c70e8d727236046fa6921ae6 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 21 Jun 2024 23:11:08 +0700 Subject: [PATCH 05/15] improve debounce --- .../lib/ESPMegaPRO/DigitalInputCard.cpp | 152 ++++++++++++------ .../lib/ESPMegaPRO/ESPMegaCommon.hpp | 2 +- ESPMegaPRO-OS-SDK/src/main.cpp | 23 ++- 3 files changed, 119 insertions(+), 58 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp index 9ae678d..b68972a 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -3,8 +3,8 @@ /** * @brief Create a new Digital Input Card object with the specified address * @note If you are using the ESPMegaI/O board, you should use the dip switch constructor - * - * @param address_a The ESPMegaI/O address of bank A + * + * @param address_a The ESPMegaI/O address of bank A * @param address_b The ESPMegaI/O address of bank B */ DigitalInputCard::DigitalInputCard(uint8_t address_a, uint8_t address_b) : callbacks() @@ -16,11 +16,11 @@ DigitalInputCard::DigitalInputCard(uint8_t address_a, uint8_t address_b) : callb /** * @brief Create a new Digital Input Card object with the specified position on the dip switch - * + * * @note The bit 0 are at the left of the dip switch - * + * * @warning There are 6 switches on the dip switch, 3 for bank A and 3 for bank B, They should be unique for each bank accross all the cards - * + * * @param bit0 The position of the first switch on the dip switch * @param bit1 The position of the second switch on the dip switch * @param bit2 The position of the third switch on the dip switch @@ -50,18 +50,20 @@ DigitalInputCard::DigitalInputCard(bool bit0, bool bit1, bool bit2, bool bit3, b /** * @brief Initialize the Digital Input Card - * + * * @return True if the initialization is successful, false otherwise */ bool DigitalInputCard::begin() { this->inputBankA = PCF8574(this->address_a); this->inputBankB = PCF8574(this->address_b); - if (!this->inputBankA.begin()) { + if (!this->inputBankA.begin()) + { ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank A"); return false; } - if (!this->inputBankB.begin()) { + if (!this->inputBankB.begin()) + { ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank B"); return false; } @@ -82,7 +84,7 @@ bool DigitalInputCard::begin() /** * @brief Read the input from the specified pin, always refresh the input buffers - * + * * @param pin The pin to read from * @return True if the pin is HIGH, false if the pin is LOW */ @@ -93,7 +95,7 @@ bool DigitalInputCard::digitalRead(uint8_t pin) /** * @brief Read the input from the specified pin, also refresh the input buffers if refresh is true - * + * * @param pin The pin to read from * @param refresh If true, the input buffers will be refreshed before reading the pin * @return True if the pin is HIGH, false if the pin is LOW @@ -123,9 +125,9 @@ bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh) /** * @brief Check if the specified pin changed since the last call to this function - * + * * @note This function compares the current input buffer with the previous input buffer to detect changes - * + * * @param pin The pin to check * @param currentBuffer The current input buffer * @param previousBuffer The previous input buffer @@ -134,44 +136,89 @@ void DigitalInputCard::handlePinChange(int pin, uint8_t ¤tBuffer, uint8_t { // Get the index of the pin in the pin map uint8_t virtualPin = virtualPinMap[pin]; - // Handle Bank A - if (((previousBuffer >> (7 - pin)) & 1) != ((currentBuffer >> (7 - pin)) & 1)) + if (pin < 8) { - if (!pinChanged[pin]) { - lastDebounceTime[pin] = millis(); - pinChanged[pin] = true; - } else { - if (millis() - lastDebounceTime[pin] > debounceTime[pin]) + // Handle Bank A + if (((previousBuffer >> (7 - pin)) & 1) != ((currentBuffer >> (7 - pin)) & 1)) + { + // When the pin first change, store the time + if (!pinChanged[virtualPin]) { - previousBuffer ^= (-((currentBuffer >> (7 - pin)) & 1) ^ previousBuffer) & (1UL << (7 - pin)); - for (const auto& callback : callbacks) - callback.second(virtualPin, ((currentBuffer >> (7 - pin)) & 1)); - pinChanged[pin] = false; + ESP_LOGD("DigitalInputCard", "Pin %d changed", virtualPin); + pinChanged[virtualPin] = true; + lastDebounceTime[virtualPin] = millis(); + } + else + { + ESP_LOGD("DigitalInputCard", "Pin %d (%d>%d) debounce time: %d", virtualPin, ((previousBuffer >> (7 - pin)) & 1), ((currentBuffer >> (7 - pin)) & 1), millis() - lastDebounceTime[virtualPin]); + // If the pin was already changed, check if the debounce time has passed + if ((millis() - lastDebounceTime[virtualPin]) > debounceTime[virtualPin]) + { + ESP_LOGD("DigitalInputCard", "Pin %d triggered", virtualPin); + // Call the callback function + for (auto const &callback : callbacks) + { + callback.second(virtualPin, ((currentBuffer >> (7 - pin)) & 1)); + } + // Store the previous buffer at the specified pin (bitwise operation) + // new value : (currentBuffer >> (7 - pin)) & 1) + previousBuffer = (previousBuffer & ~(1 << (7 - pin))) | (((currentBuffer >> (7 - pin)) & 1) << (7 - pin)); + // Reset the pin changed flag + pinChanged[virtualPin] = false; + } } } - } - // Handle Bank B - if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1)) - { - if (!pinChanged[pin]) { - lastDebounceTime[pin] = millis(); - pinChanged[pin] = true; - } else { - if (millis() - lastDebounceTime[pin] > debounceTime[pin]) - { - previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin)); - for (const auto& callback : callbacks) - callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1)); - pinChanged[pin] = false; - } + else + { + // Pin bounce back to previous state, reset the debounce time + lastDebounceTime[virtualPin] = millis(); + pinChanged[virtualPin] = false; + } + } + else + { + // Handle Bank B + if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1)) + { + // When the pin first change, store the time + if (!pinChanged[virtualPin]) + { + ESP_LOGD("DigitalInputCard", "Pin %d changed", virtualPin); + pinChanged[virtualPin] = true; + lastDebounceTime[virtualPin] = millis(); + } + else + { + ESP_LOGD("DigitalInputCard", "Pin %d (%d>%d) debounce time: %d", virtualPin, ((previousBuffer >> (15 - pin)) & 1), ((currentBuffer >> (15 - pin)) & 1), millis() - lastDebounceTime[virtualPin]); + // If the pin was already changed, check if the debounce time has passed + if ((millis() - lastDebounceTime[virtualPin]) > debounceTime[virtualPin]) + { + ESP_LOGD("DigitalInputCard", "Pin %d triggered", virtualPin); + // Call the callback function + for (auto const &callback : callbacks) + { + callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1)); + } + // Store the previous buffer at the specified pin (bitwise operation) + // new value : (currentBuffer >> (15 - pin)) & 1) + previousBuffer = (previousBuffer & ~(1 << (15 - pin))) | (((currentBuffer >> (15 - pin)) & 1) << (15 - pin)); + // Reset the pin changed flag + pinChanged[virtualPin] = false; + } + } + } + else + { + // Pin bounce back to previous state, reset the debounce time + lastDebounceTime[virtualPin] = millis(); + pinChanged[virtualPin] = false; } } - } /** - * @brief A loop to refresh the input buffers and check for pin changes - * + * @brief A loop to refresh the input buffers and check for pin changes + * * @note Although this function can be called in the main loop, it is recommended install the card in ESPMega to automatically manage the loop */ // Preform a loop to refresh the input buffers @@ -197,7 +244,7 @@ void DigitalInputCard::loop() /** * @brief Get the input buffer for bank A (the first 8 pins) - * + * * @return The input buffer for bank A where the first bit is the first pin and the last bit is the last pin */ uint8_t DigitalInputCard::getInputBufferA() @@ -213,7 +260,7 @@ uint8_t DigitalInputCard::getInputBufferA() /** * @brief Get the input buffer for bank B (the last 8 pins) - * + * * @return The input buffer for bank B where the first bit is the first pin and the last bit is the last pin */ uint8_t DigitalInputCard::getInputBufferB() @@ -229,7 +276,7 @@ uint8_t DigitalInputCard::getInputBufferB() /** * @brief Register a callback function to be called when a pin changes - * + * * @param callback The callback function to be called * @return The handler of the callback function */ @@ -257,11 +304,11 @@ void DigitalInputCard::refreshInputBankB() /** * @brief Set the debounce time for the specified pin - * + * * Debounce is the time in milliseconds that the pin should be stable before the callback function is called * This is useful to prevent false triggers when the input is noisy * An example of this is when the input is connected to a mechanical switch - * + * * @param pin The pin to set the debounce time for * @param debounceTime The debounce time in milliseconds */ @@ -273,7 +320,7 @@ void DigitalInputCard::setDebounceTime(uint8_t pin, uint32_t debounceTime) /** * @brief Unregister a callback function - * + * * @param handler The handler of the callback function to unregister */ void DigitalInputCard::unregisterCallback(uint8_t handler) @@ -283,12 +330,12 @@ void DigitalInputCard::unregisterCallback(uint8_t handler) /** * @brief Load the pin map for the card - * + * * A pin map is an array of 16 elements that maps the physical pins to virtual pins * The virtual pins are the pins that are used in the callback functions and are used for all the functions in this class * The physical pins are the pins on the Input IC, This can be found on the schematic of the ESPMegaI/O board * This function is useful if you want to change the number identification of the pins to match your project needs - * + * * @param pinMap The pin map to load */ void DigitalInputCard::loadPinMap(uint8_t pinMap[16]) @@ -304,7 +351,7 @@ void DigitalInputCard::loadPinMap(uint8_t pinMap[16]) /** * @brief Get the type of the card - * + * * @return The type of the card */ uint8_t DigitalInputCard::getType() @@ -314,10 +361,11 @@ uint8_t DigitalInputCard::getType() /** * @brief Preload the previous input buffer and the input buffer - * + * * @note This function is useful if you want to preload the input buffers with a run-time value */ -void DigitalInputCard::preloadInputBuffer() { +void DigitalInputCard::preloadInputBuffer() +{ refreshInputBankA(); refreshInputBankB(); previousInputBufferA = inputBufferA; diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp index 655573d..de99223 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp @@ -1,3 +1,3 @@ #pragma once -#define SDK_VESRION "2.9.2" \ No newline at end of file +#define SDK_VESRION "2.9.3" \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/src/main.cpp b/ESPMegaPRO-OS-SDK/src/main.cpp index d73714b..98fd31d 100644 --- a/ESPMegaPRO-OS-SDK/src/main.cpp +++ b/ESPMegaPRO-OS-SDK/src/main.cpp @@ -12,15 +12,17 @@ #include #include +#define INPUT_DEBOUNCE_TIME_MS 1000 + // #define FRAM_DEBUG // #define MQTT_DEBUG // #define RTC_DEBUG -#define WRITE_DEFAULT_NETCONF -#define CLIMATE_CARD_ENABLE -#define MQTT_CARD_REGISTER +//#define WRITE_DEFAULT_NETCONF +//#define CLIMATE_CARD_ENABLE +//#define MQTT_CARD_REGISTER // #define DISPLAY_ENABLE -#define WEB_SERVER_ENABLE -#define LCD_OTA_ENABLE +//#define WEB_SERVER_ENABLE +//#define LCD_OTA_ENABLE // #define REMOTE_VARIABLE_ENABLE // #define CT_ENABLE // #define SMART_VARIABLE_ENABLE @@ -116,6 +118,7 @@ void input_change_callback(uint8_t pin, uint8_t value) Serial.print(pin); Serial.print(" "); Serial.println(value); + espmega.outputs.toggleState(pin); } void mqtt_callback(char *topic, char *payload) @@ -187,6 +190,16 @@ void setup() ESP_LOGI("Initializer", "Connecting to MQTT"); espmega.iot->connectToMqtt(); espmega.iot->registerMqttCallback(mqtt_callback); + // Set debounce time + for (uint8_t i = 0; i < 16; i++) + { + espmega.inputs.setDebounceTime(i, INPUT_DEBOUNCE_TIME_MS); + } + // Set all pin values to 4095 + for (uint8_t i = 0; i < 16; i++) + { + espmega.outputs.setValue(i, 4095); + } #ifdef MQTT_CARD_REGISTER ESP_LOGI("Initializer", "Registering cards 0"); espmega.iot->registerCard(0); From c2e45490b020f6b357510d2b1a8afb2b3eedfb1d Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 28 Jun 2024 22:16:44 +0700 Subject: [PATCH 06/15] energy load error handling --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp index f5ce161..d9f230c 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp @@ -84,6 +84,9 @@ void CurrentTransformerCard::saveEnergy(){ } void CurrentTransformerCard::loadEnergy(){ this->fram->read(this->framAddress, (uint8_t*)&this->energy, sizeof(this->energy)); + if (this->energy < 0 || isnan(this->energy)) { + this->energy = 0; + } } void CurrentTransformerCard::setEnergyAutoSave(bool autoSave){ From 520fd76b91dad93040b5df87b6819d23603e8ec6 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 28 Jun 2024 22:17:13 +0700 Subject: [PATCH 07/15] Update ESPMegaCommon.hpp --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp index de99223..3988dbc 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp @@ -1,3 +1,3 @@ #pragma once -#define SDK_VESRION "2.9.3" \ No newline at end of file +#define SDK_VESRION "2.9.4" \ No newline at end of file From 8ea85254a05c0213cf88248d541d13833be2e33b Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 7 Oct 2024 15:08:12 +0700 Subject: [PATCH 08/15] add uptime feature --- .../lib/ESPMegaPRO/ESPMegaCommon.hpp | 2 +- .../lib/ESPMegaPRO/ESPMegaWebServer.cpp | 1 + .../lib/ESPMegaPRO/html/ota.html | 19 +- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ota_html.h | 898 ++++++++++-------- 4 files changed, 503 insertions(+), 417 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp index 3988dbc..59e98d2 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp @@ -1,3 +1,3 @@ #pragma once -#define SDK_VESRION "2.9.4" \ No newline at end of file +#define SDK_VESRION "2.9.5" \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.cpp index d4791b7..00f6b31 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaWebServer.cpp @@ -482,6 +482,7 @@ void ESPMegaWebServer::getDeviceInfoHandler(AsyncWebServerRequest *request) { doc["software_version"] = SW_VERSION; doc["sdk_version"] = SDK_VESRION; doc["idf_version"] = IDF_VER; + doc["uptime"] = esp_timer_get_time() / 1000000; // Uptime in seconds char buffer[512]; serializeJson(doc, buffer); request->send(200, "application/json", buffer); diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/html/ota.html b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/html/ota.html index 51fc30a..a9fc9da 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/html/ota.html +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/html/ota.html @@ -14,6 +14,10 @@ MAC Address Loading ...

+

+ Uptime + Loading ... +

Model Loading ... @@ -60,7 +64,7 @@ SIWAT SYSTEM 2023