From 724f8f42e9cece5b763407415971d8481b135449 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sun, 31 Dec 2023 13:32:38 +0700 Subject: [PATCH] return callback handler --- ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.cpp | 5 +++-- ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.hpp | 2 +- ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp | 10 ++++++---- ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp | 4 ++-- .../lib/ESPMegaPRO/DigitalInputCard.cpp | 5 +++-- .../lib/ESPMegaPRO/DigitalInputCard.hpp | 2 +- .../lib/ESPMegaPRO/DigitalOutputCard.cpp | 5 +++-- .../lib/ESPMegaPRO/DigitalOutputCard.hpp | 2 +- 8 files changed, 20 insertions(+), 15 deletions(-) diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.cpp index c7a60f4..a66293c 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.cpp @@ -65,8 +65,9 @@ void AnalogIoT::setADCsPublishEnabled(bool enabled) { adc_publish_enabled[i] = enabled; } } -void AnalogIoT::registerADCConversionCallback(std::function callback) { - this->adc_conversion_callbacks[this->adc_conversion_callback_index++] = callback; +uint8_t AnalogIoT::registerADCConversionCallback(std::function callback) { + this->adc_conversion_callbacks[this->adc_conversion_callback_index] = callback; + return this->adc_conversion_callback_index++; } void AnalogIoT::deregisterADCConversionCallback(uint8_t handler) { this->adc_conversion_callbacks.erase(handler); diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.hpp index 367a763..8633826 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.hpp @@ -25,7 +25,7 @@ class AnalogIoT : public IoTComponent { void publishDACValue(uint8_t pin); void setADCsPublishInterval(uint32_t interval); void setADCsPublishEnabled(bool enabled); - void registerADCConversionCallback(std::function callback); + uint8_t registerADCConversionCallback(std::function callback); void deregisterADCConversionCallback(uint8_t handler); void setADCConversionInterval(uint8_t pin, uint16_t interval); void setADCConversionEnabled(uint8_t pin, bool enabled); diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp index d9458ce..4e1deb3 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp @@ -137,9 +137,10 @@ void ClimateCard::setFanSpeed(uint8_t fan_speed) saveStateToFRAM(); } -void ClimateCard::registerChangeCallback(std::function callback) +uint8_t ClimateCard::registerChangeCallback(std::function callback) { - callbacks[callbacks_handler_count++] = callback; + callbacks[callbacks_handler_count] = callback; + return callbacks_handler_count++; } uint8_t ClimateCard::getType() @@ -237,9 +238,10 @@ uint8_t ClimateCard::getFanSpeed() return state.ac_fan_speed; } -void ClimateCard::registerSensorCallback(std::function callback) +uint8_t ClimateCard::registerSensorCallback(std::function callback) { - sensor_callbacks[sensor_callbacks_handler_count++] = callback; + sensor_callbacks[sensor_callbacks_handler_count] = callback; + return sensor_callbacks_handler_count++; } void ClimateCard::unregisterChangeCallback(uint8_t handler) diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp index 8f62d8b..3b2a9bd 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp @@ -58,8 +58,8 @@ class ClimateCard : public ExpansionCard { float getRoomTemperature(); float getHumidity(); uint8_t getSensorType(); - void registerChangeCallback(std::function callback); - void registerSensorCallback(std::function callback); + uint8_t registerChangeCallback(std::function callback); + uint8_t registerSensorCallback(std::function callback); void unregisterChangeCallback(uint8_t handler); void unregisterSensorCallback(uint8_t handler); uint8_t getType(); diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp index 56c9b5b..3bb7493 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -157,9 +157,10 @@ uint8_t DigitalInputCard::getInputBufferB() return inputBufferB_rearranged; } // Register a callback function to be called when a pin changes -void DigitalInputCard::registerCallback(std::function callback) +uint8_t DigitalInputCard::registerCallback(std::function callback) { - callbacks[this->callbacks_handler_index++] = callback; + callbacks[this->callbacks_handler_index] = callback; + return this->callbacks_handler_index++; } // Refresh the input buffer for bank A diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.hpp index 4a9e394..d428d30 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.hpp @@ -26,7 +26,7 @@ class DigitalInputCard : public ExpansionCard { // Set the debounce time for the specified pin void setDebounceTime(uint8_t pin, uint32_t debounceTime); // Register a callback function to be called when a pin changes - void registerCallback(std::function callback); + uint8_t registerCallback(std::function callback); // Unregister the callback function void unregisterCallback(uint8_t handler); // Load a new pin map diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.cpp index e9adf18..68daa41 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.cpp @@ -102,8 +102,9 @@ void DigitalOutputCard::setValue(uint8_t pin, uint16_t value) { } } -void DigitalOutputCard::registerChangeCallback(std::function callback) { - this->change_callbacks[this->callbacks_handler_index++] = callback; +uint8_t DigitalOutputCard::registerChangeCallback(std::function callback) { + this->change_callbacks[this->callbacks_handler_index] = callback; + return this->callbacks_handler_index++; } void DigitalOutputCard::deregisterChangeCallback(uint8_t handler) { diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.hpp index 2384626..072560f 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.hpp @@ -48,7 +48,7 @@ public: // 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 - void registerChangeCallback(std::function callback); + uint8_t registerChangeCallback(std::function callback); // Deregister the callback function void deregisterChangeCallback(uint8_t handler); // Load a new pin map