change debouce logic

This commit is contained in:
Siwat Sirichai 2024-06-21 16:35:26 +07:00
parent b62ef700ed
commit d0ce4bf391
2 changed files with 36 additions and 10 deletions

View File

@ -137,25 +137,50 @@ void DigitalInputCard::handlePinChange(int pin, uint8_t &currentBuffer, uint8_t
// Handle Bank A
if (((previousBuffer >> (7 - pin)) & 1) != ((currentBuffer >> (7 - pin)) & 1))
{
if (!pinChanged[pin]) {
lastDebounceTime[pin] = millis();
pinChanged[pin] = true;
} else {
if (millis() - lastDebounceTime[pin] > debounceTime[pin])
{
lastDebounceTime[pin] = millis();
previousBuffer ^= (-((currentBuffer >> (7 - pin)) & 1) ^ previousBuffer) & (1UL << (7 - pin));
for(const auto& callback : callbacks)
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 (!pinChanged[pin]) {
lastDebounceTime[pin] = millis();
pinChanged[pin] = true;
} else {
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));
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));
// }
// }
}
/**

View File

@ -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