function io_check_hardware($in1, $in2, $in3, $in4, $out1, $out2) { # set all pins as inputs on the input boards $error = 0; if ($i1 = I2C_Write($in1, 255) < 0) { $error++; } //chip is bidirectional, set outputs to 1 to read as input if ($i2 = I2C_Write($in2, 255) < 0) { $error++; } //chip is bidirectional, set outputs to 1 to read as input if ($i3 = I2C_Write($in3, 255) < 0) { $error++; } //chip is bidirectional, set outputs to 1 to read as input if ($i4 = I2C_Write($in4, 255) < 0) { $error++; } //chip is bidirectional, set outputs to 1 to read as input if ($i1) { logformat(sprintf("Opto input board 1 with address: 0x%X is missing or not functioning!\n", PCF8574_ADDR1), 1); } if ($i2) { logformat(sprintf("Opto input board 2 with address: 0x%X is missing or not functioning!\n", PCF8574_ADDR2), 1); } if ($i3) { logformat(sprintf("Opto input board 3 with address: 0x%X is missing or not functioning!\n", PCF8574_ADDR3), 1); } if ($i4) { logformat(sprintf("Opto input board 4 with address: 0x%X is missing or not functioning!\n", PCF8574_ADDR4), 1); } if ($error) { return $error; } #set all pins as outputs on the output boards and set all outputs high if ($o1 = I2C_WriteReg16($out1, PCA9555_CONFPORT0, 0) < 0) { $error++; } //sends to CONFPORT0 and 1 if ($o2 = I2C_WriteReg16($out2, PCA9555_CONFPORT0, 0) < 0) { $error++; } //sends to CONFPORT0 and 1 if ($o1) { logformat(sprintf("PCA9555 based output board 1 with address: 0x%X is missing or not functioning!\n", PCA9555_ADDR1), 1); } if ($o2) { logformat(sprintf("PCA9555 based output board 2 with address: 0x%X is missing or not functioning!\n", PCA9555_ADDR2), 1); } if ($error) { return $error; } return 0; }
$in1 = I2C_Setup(PCF8574_ADDR1); $in2 = I2C_Setup(PCF8574_ADDR2); $in3 = I2C_Setup(PCF8574_ADDR3); $in4 = I2C_Setup(PCF8574_ADDR4); $out1 = I2C_Setup(PCA9555_ADDR1); $out2 = I2C_Setup(PCA9555_ADDR2); // set all pins as inputs on the input boards // set all pins as outputs on the output boards $error = io_check_hardware($in1, $in2, $in3, $in4, $out1, $out2); if ($error) { exit(1); } // and set all outputs high $o = I2C_WriteReg16($out1, PCA9555_OUTPORT0, 65535); //sends to OUTPORT0 and 1 $o = I2C_WriteReg16($out2, PCA9555_OUTPORT0, 65535); //sends to OUTPORT0 and 1 $pc_up = array_fill(0, IN_PINS, 0); $pc_dn = array_fill(0, IN_PINS, 0); $out_states = ''; //these will be 64 2 digit hex items $in_states = ''; $in_programs = ''; $in_levels = ''; $inames = array(); //input names id=>name $onames = array(); //output names id=>name $itypes = array(); //input switch type $otypes = array();
function set_outputs(&$out_states) { global $out1, $out2; $o = ''; for ($i = 0; $i < 64; $i++) { $o .= hexdec(substr($out_states, $i * 2, 2)) ? '1' : '0'; } $r = I2C_WriteReg16($out1, PCA9555_OUTPORT0, bindec(substr($o, 0, 16))); //sends to OUTPORT0 and 1 $r = I2C_WriteReg16($out2, PCA9555_OUTPORT0, bindec(substr($o, 16, 16))); //sends to OUTPORT0 and 1 }