/** * Perform a periodic task for the PiFace card */ static void piface_task(void) { unsigned i = 0; BACNET_BINARY_PV present_value = BINARY_INACTIVE; bool pin_status = false; for (i = 0; i < MAX_BINARY_INPUTS; i++) { if (!Binary_Input_Out_Of_Service(i)) { present_value = Binary_Input_Present_Value(i); pin_status = false; if (pifacedigital_digital_read(i)) { pin_status = true; } if (pin_status != PiFace_Pin_Status[i]) { PiFace_Pin_Status[i] = pin_status; if (pin_status) { /* toggle the input only when button is pressed */ if (present_value == BINARY_INACTIVE) { present_value = BINARY_ACTIVE; } else { present_value = BINARY_INACTIVE; } Binary_Input_Present_Value_Set(i, present_value); } } } } for (i = 0; i < MAX_BINARY_OUTPUTS; i++) { if (!Binary_Output_Out_Of_Service(i)) { present_value = Binary_Output_Present_Value(i); if (present_value == BINARY_INACTIVE) { pifacedigital_digital_write(i, 0); } else { pifacedigital_digital_write(i, 1); } } } }
int main( int argc, char *argv[] ) { uint8_t i = 0; /**< Loop iterator */ uint8_t inputs; /**< Input bits (pins 0-7) */ int hw_addr = 0; /**< PiFaceDigital hardware address */ int intenable = 1; /**< Whether or not interrupts are enabled */ /** * Read command line value for which PiFace to connect to */ if (argc > 1) { hw_addr = atoi(argv[1]); } /** * Open piface digital SPI connection(s) */ printf("Opening piface digital connection at location %d\n", hw_addr); pifacedigital_open(hw_addr); /** * Enable interrupt processing (only required for all * blocking/interrupt methods) */ intenable = pifacedigital_enable_interrupts(); if ( intenable == 0) printf("Interrupts enabled.\n"); else printf("Could not enable interrupts. Try running using sudo to enable PiFaceDigital interrupts.\n"); /** * Bulk set all 8 outputs at once using a hexidecimal * representation of the inputs as an 8-bit binary * number, where each bit represents an output from * 0-7 */ /* Set all outputs off (00000000) */ printf("Setting all outputs off\n"); pifacedigital_write_reg(0x00, OUTPUT, hw_addr); sleep(1); /* Set output states to alternating on/off (10101010) */ printf("Setting outputs to 10101010\n"); pifacedigital_write_reg(0xaa, OUTPUT, hw_addr); sleep(1); /* Set output states to alternating off/on (01010101) */ printf("Setting outputs to 01010101\n"); pifacedigital_write_reg(0x55, OUTPUT, hw_addr); sleep(1); /* Set all outputs off (000000) */ printf("Setting all outputs off\n"); pifacedigital_write_reg(0x00, OUTPUT, hw_addr); /** * Read/write single input bits */ uint8_t bit = pifacedigital_read_bit(0, OUTPUT, hw_addr); printf("Reading bit 0: %d\n", bit); sleep(1); printf("Writing bit 0 to 0\n", bit); pifacedigital_write_bit(0, 0, OUTPUT, hw_addr); /** * Set input pullups (must #include "mcp23s17.h") */ /* pifacedigital_write_reg(0xff, GPPUB, hw_addr); */ /** * Bulk read all inputs at once */ inputs = pifacedigital_read_reg(INPUT, hw_addr); printf("Inputs: 0x%x\n", inputs); /** * Write each output pin individually */ for (i = 0; i < 8; i++) { const char *desc; if (i <= 1) desc = "pin with attached relay"; else desc = "pin"; /* Turn output pin i high */ printf("Setting output %s %d HIGH\n", desc, (int)i); pifacedigital_digital_write(i, 1); sleep(1); /* Turn output pin i low */ printf("Setting output %s %d LOW\n", desc, (int)i); pifacedigital_digital_write(i, 0); sleep(1); } /** * Read each input pin individually * A return value of 0 is pressed. */ for (i = 0; i < 8; i++) { uint8_t pinState = pifacedigital_digital_read(i); printf("Input %d value: %d\n", (int)i, (int)pinState); } /** * Wait for input change interrupt */ if( intenable ) { printf("Interrupts disabled, skipping interrupt tests.\n"); } else { printf("Waiting for input (press any button on the PiFaceDigital)\n"); inputs = pifacedigital_wait_for_input(-1, hw_addr); printf("Inputs: 0x%x\n", inputs); } /** * Close the connection to the PiFace Digital */ pifacedigital_close(hw_addr); }