Skip to content

Instantly share code, notes, and snippets.

@biologist79
Created January 21, 2023 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biologist79/85544ec947c90480f5d2b1ea79a3975f to your computer and use it in GitHub Desktop.
Save biologist79/85544ec947c90480f5d2b1ea79a3975f to your computer and use it in GitHub Desktop.
PE
void Port_ExpanderHandler(void) {
static bool verifyChangeOfInput[2] = {false, false}; // Used to debounce once in case of input-change
static uint8_t inputChannelBuffer[2];
// If interrupt-handling is active, only read port-expander's register if interrupt was fired
#ifdef PE_INTERRUPT_PIN_ENABLE
if (!Port_AllowReadFromPortExpander && !Port_AllowInitReadFromPortExpander && !verifyChangeOfInput[0] && !verifyChangeOfInput[1]) {
//Serial.println("Interrupt false!");
return;
} else if (Port_AllowInitReadFromPortExpander) {
Port_AllowInitReadFromPortExpander = false;
} else if (Port_AllowReadFromPortExpander || Port_AllowInitReadFromPortExpander) {
//Serial.println("Interrupt true!");
Port_AllowReadFromPortExpander = false;
}
#endif
i2cBusTwo.beginTransmission(expanderI2cAddress);
for (uint8_t i = 0; i < 2; i++) {
i2cBusTwo.write(0x00 + i); // Pointer to input-register...
i2cBusTwo.endTransmission();
i2cBusTwo.requestFrom(expanderI2cAddress, 1u); // ...and read its byte
if (i2cBusTwo.available()) {
inputChannelBuffer[i] = i2cBusTwo.read();
if (inputChannelBuffer[i] != Port_ExpanderPortsInputChannelStatus[i] && millis() >= 10000 && !verifyChangeOfInput[i]) {
verifyChangeOfInput[i] = true;
Serial.println("Verify set!");
} else {
verifyChangeOfInput[i] = false;
Port_ExpanderPortsInputChannelStatus[i] = inputChannelBuffer[i];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment