-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton_matrix.c
96 lines (76 loc) · 1.96 KB
/
button_matrix.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
/*****************************************************************************
Program: Button matrix
This example program shows the use of the button matrix on
CrowPi.
Hardware: CrowPi (with Raspberry Pi 3B+)
CrowPi's built-in button matrix
Monitor
Software: Raspbian system with WiringPi library
Others: Compile with:
$ gcc -o example example_button_matrix.c -lwiringPi -Wall
Run with:
$ ./example
Jan 2020
******************************************************************************/
#include <stdio.h>
#include <wiringPi.h>
// Function prototypes
int setup_button_matrix();
int get_button_matrix();
// Button matrix configuration for key numbers
int BMrow[] = { 13, 15, 29, 31 }; // Pin #s for the rows
int BMcol[] = { 22, 37, 35, 33 }; // Pin #s for the columns
// ---------------------------------------------------------------------------
// Main function
//
int main( int argc, char **argv )
{
int k;
setup_button_matrix();
printf("Press button matrix...\n");
while(1) {
do {
k = get_button_matrix();
if( k ) printf("Key = %d\n", k);
} while( k==0 );
delay(200);
}
return 0;
}
// ---------------------------------------------------------------------------
// Function: to setup button matrix
//
int setup_button_matrix()
{
int r, c;
// Setup GPIO
wiringPiSetupPhys();
for( c=0; c < 4; c++ ) {
pinMode( BMcol[c], OUTPUT );
digitalWrite( BMcol[c], HIGH );
}
for( r=0; r < 4; r++ ) {
pinMode( BMrow[r], PUD_UP );
pullUpDnControl( BMrow[r], PUD_UP );
}
return 0;
}
// ---------------------------------------------------------------------------
// Function: Read button matrix and return corresponding key number
//
int get_button_matrix()
{
int r, c, key;
for( c=0; c < 4; c++ ) {
digitalWrite( BMcol[c], LOW );
for( r=0; r < 4; r++ ) {
if( !digitalRead(BMrow[r]) ) {
key = r*4 + c + 1;
delay(50);
return key;
}
}
digitalWrite( BMcol[c], HIGH );
}
return 0;
}