#include "pins.hpp" MCP23S08 MCP(SS, MISO, MOSI, SCK); volatile byte ISR_FLAG = 0; struct pins PINS; void IRAM_ATTR isr() { ISR_FLAG = 1; } void initializeMCP() { pinMode(ISR_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(ISR_PIN), isr, CHANGE); SPI.begin(); MCP.begin(); // Enable inputs and interrupts on pins 0-2 MCP.pinMode8(0b00000111); MCP.enableInterrupt(0, CHANGE); MCP.enableInterrupt(1, CHANGE); MCP.enableInterrupt(2, CHANGE); Serial.println("MCP23S08 Started"); } void isr_check_loop() { if (ISR_FLAG == 1) { PINS.sib = false; PINS.plc = false; PINS.ups = false; PINS.last_triggered = millis(); Serial.print("Interrupt: "); uint8_t regval = MCP.getInterruptCaptureRegister(); // INTCAP Serial.print(regval, BIN); uint8_t mask = 1 << 0; if (!(regval & mask)) { Serial.print(" UPS"); PINS.ups = true; } mask = 1 << 1; if (!(regval & mask)) { Serial.print(" PLC"); PINS.plc = true; } mask = 1 << 2; if (!(regval & mask)) { Serial.print(" SIB"); PINS.sib = true; } Serial.println(); ISR_FLAG = 0; } } void poll_port_expander() { PINS.sib = false; PINS.plc = false; PINS.ups = false; PINS.last_triggered = millis(); int regval = MCP.read8(); uint8_t mask = 1 << 0; if (!(regval & mask)) { Serial.print(" UPS"); PINS.ups = true; } mask = 1 << 1; if (!(regval & mask)) { Serial.print(" PLC"); PINS.plc = true; } mask = 1 << 2; if (!(regval & mask)) { Serial.print(" SIB"); PINS.sib = true; } Serial.println(); } bool on = true; unsigned long toggle_start = 0; void toggle_status_led() { if (millis() - toggle_start >= 1000) { toggle_start = millis(); on = !on; MCP.write1(STATUS_LED_PIN, on); } }