#ifdef ENABLE_I2C_PORTEXPANDER

void portexpander_setup() {
    #ifdef FIRMWARE_VARIANT_HEATCONTROL
    pcf8574.pinMode(P0, OUTPUT, HIGH);  // LED red, status testmode active
    pcf8574.pinMode(P1, OUTPUT, HIGH);  // LED red, status heating locked
    pcf8574.pinMode(P2, INPUT_PULLUP);   // switch input - bypass control for heating
    pcf8574.pinMode(P3, INPUT_PULLUP);   // switch input - bypass control for pump
    pcf8574.pinMode(P4, INPUT_PULLUP);   // input status heating active (return line)
    pcf8574.pinMode(P5, INPUT_PULLUP);   // input heat request from room thermostat
    pcf8574.pinMode(P6, OUTPUT, HIGH);  // output - control pump
    pcf8574.pinMode(P7, OUTPUT, HIGH);  // output - control heating
    #else
    pcf8574.pinMode(P0, INPUT_PULLUP);
    pcf8574.pinMode(P1, INPUT_PULLUP);
    pcf8574.pinMode(P2, INPUT_PULLUP);
    pcf8574.pinMode(P3, INPUT_PULLUP);
    pcf8574.pinMode(P4, INPUT_PULLUP);
    pcf8574.pinMode(P5, INPUT_PULLUP);
    pcf8574.pinMode(P6, INPUT_PULLUP);
    pcf8574.pinMode(P7, INPUT_PULLUP);
    #endif

    sendLog(F("PCF8574 init..."));
	if (pcf8574.begin())
    {
		sendLog(F("    OK"));
	}
    else
    {
		sendLog(F("    ERROR"));
	}
}

void portexpander_readInputs() {
    PCF8574::DigitalInput _di = pcf8574.digitalReadAll();
    
    #ifdef FIRMWARE_VARIANT_HEATCONTROL
    // read inputs - inverse logic as active is LOW...

    bool _changes = false;

    if(heatcontrol_in_sw_disableControl_heating != !_di.p2) {
        heatcontrol_in_sw_disableControl_heating = !_di.p2;
        heatcontrol_publish_sw_disableControl_heating();
        _changes = true;
    }

    if(heatcontrol_in_sw_disableControl_pump != !_di.p3) {
        heatcontrol_in_sw_disableControl_pump = !_di.p3;
        heatcontrol_publish_sw_disableControl_pump();
        _changes = true;
    }

    if(heatcontrol_in_heat_active != !_di.p4) {
        heatcontrol_in_heat_active = !_di.p4;
        heatcontrol_publish_in_heat_active();
        _changes = true;
    }
    
    if(heatcontrol_in_heat_request != !_di.p5) {
        heatcontrol_in_heat_request = !_di.p5;
        heatcontrol_publish_in_heat_request();
        _changes = true;
    }

    if(_changes && debug > 0) {
        snprintf(logBuf, LOG_BUFFER_SIZE, "PCF8574 inputs changed: SW_H_DIR=%d, SW_P_DIR=%d, STAT_HEAT=%d, IN_RT=%d", heatcontrol_in_sw_disableControl_heating, heatcontrol_in_sw_disableControl_pump, heatcontrol_in_heat_active, heatcontrol_in_heat_request);
        sendLog(logBuf, LOGLEVEL_DEBUG);
    }

    #endif
    
}


void portexpander_toggleOutputs() {
    #ifdef FIRMWARE_VARIANT_HEATCONTROL
    heatcontrol_out_stat_testmode = !heatcontrol_out_stat_testmode;
    pcf8574.digitalWrite(P0, heatcontrol_out_stat_testmode);

    heatcontrol_out_stat_locked = !heatcontrol_out_stat_locked;
    pcf8574.digitalWrite(P1, heatcontrol_out_stat_locked);

    heatcontrol_out_heat = !heatcontrol_out_heat;
    pcf8574.digitalWrite(P6, heatcontrol_out_heat);

    heatcontrol_out_pump = !heatcontrol_out_pump;
    pcf8574.digitalWrite(P7, heatcontrol_out_pump);
    #endif
}

#endif
