diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp
index d9f230c..f5ce161 100644
--- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp
+++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/CurrentTransformerCard.cpp
@@ -84,9 +84,6 @@ 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){
diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp
index fc9f2cf..86ad5a1 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,23 +50,19 @@ 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");
- this->initOk = false;
return false;
}
- if (!this->inputBankB.begin())
- {
+ if (!this->inputBankB.begin()) {
ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank B");
- this->initOk = false;
return false;
}
// Set the debounce time for all pins to 50ms
@@ -81,13 +77,12 @@ bool DigitalInputCard::begin()
this->pinMap[i] = i;
this->virtualPinMap[i] = i;
}
- this->initOk = true;
return true;
}
/**
* @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
*/
@@ -98,18 +93,13 @@ 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
*/
bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh)
{
- if(!this->initOk)
- {
- ESP_LOGE("DigitalInputCard", "Input Card ERROR: Card not initialized");
- return false;
- }
pin = pinMap[pin];
// First check if the pin is in bank A or B
if (pin >= 0 && pin <= 7)
@@ -128,14 +118,14 @@ bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh)
// Extract the bit from the buffer
return ((inputBufferB >> (15 - pin)) & 1);
}
- return false;
+ return 255;
}
/**
* @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
@@ -144,89 +134,33 @@ 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];
- if (pin < 8)
+ // Handle Bank A
+ if (((previousBuffer >> (7 - pin)) & 1) != ((currentBuffer >> (7 - pin)) & 1))
{
- // Handle Bank A
- if (((previousBuffer >> (7 - pin)) & 1) != ((currentBuffer >> (7 - pin)) & 1))
+ if (millis() - lastDebounceTime[pin] > debounceTime[pin])
{
- // 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 >> (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;
- }
- }
- }
- else
- {
- // Pin bounce back to previous state, reset the debounce time
- lastDebounceTime[virtualPin] = millis();
- pinChanged[virtualPin] = false;
+ lastDebounceTime[pin] = millis();
+ previousBuffer ^= (-((currentBuffer >> (7 - pin)) & 1) ^ previousBuffer) & (1UL << (7 - pin));
+ for(const auto& callback : callbacks)
+ callback.second(virtualPin, ((currentBuffer >> (7 - pin)) & 1));
}
}
- else
+ // Handle Bank B
+ if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1))
{
- // Handle Bank B
- if (((previousBuffer >> (15 - pin)) & 1) != ((currentBuffer >> (15 - pin)) & 1))
+ if (millis() - lastDebounceTime[pin] > debounceTime[pin])
{
- // 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;
+ lastDebounceTime[pin] = millis();
+ previousBuffer ^= (-((currentBuffer >> (15 - pin)) & 1) ^ previousBuffer) & (1UL << (15 - pin));
+ for (const auto& callback : callbacks)
+ callback.second(virtualPin, ((currentBuffer >> (15 - pin)) & 1));
}
}
}
/**
- * @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
@@ -252,7 +186,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()
@@ -268,7 +202,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()
@@ -284,7 +218,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
*/
@@ -299,11 +233,6 @@ uint8_t DigitalInputCard::registerCallback(std::function
- Uptime - Loading ... -
Model Loading ... @@ -64,7 +60,7 @@ SIWAT SYSTEM 2023