-
Notifications
You must be signed in to change notification settings - Fork 10
/
example.c
137 lines (110 loc) · 3.85 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "pifacedigital.h"
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 interrupts_enabled; /**< 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).
* Reverse the return value of pifacedigital_enable_interrupts() to be consistent
* with the variable name "interrupts_enabled". (the function returns 0 on success)
*/
if (interrupts_enabled = !pifacedigital_enable_interrupts())
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.
* pifacedigital_wait_for_input returns a value <= 0 on failure.
*/
if (interrupts_enabled) {
printf("Waiting for input (press any button on the PiFaceDigital)\n");
if (pifacedigital_wait_for_input(&inputs, -1, hw_addr) > 0)
printf("Inputs: 0x%x\n", inputs);
else
printf("Can't wait for input. Something went wrong!\n");
}
else
printf("Interrupts disabled, skipping interrupt tests.\n");
/**
* Close the connection to the PiFace Digital
*/
pifacedigital_close(hw_addr);
}