Skip to content

Commit c56503e

Browse files
authored
Implemented the enhanced keypad changes and README.adoc (#3)
* Implemented the enhanced keypad changes * Create README.adoc file (#4) * Change wiring to make it easier * Include first version of README.adoc * Fix image syntax * Fix number of columns in table * Remove 'Installation' section * Put terminal output as a note * Remove NOTE * Use plain text Code Block instead * Fix URLs to LICENSE and CONTRIBUTING * Another attempt of fixing contributing and licensing * Put links in line
1 parent 52a7f7b commit c56503e

File tree

3 files changed

+216
-14
lines changed

3 files changed

+216
-14
lines changed

gpio/keypad/README.adoc

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
= Keypad Example for Raspberry Pi Pico
2+
3+
This document provides details about the 4x4 keypad example included in the larger repository of examples for Raspberry Pi Pico. This example demonstrates how to interface a 4x4 matrix keypad with the Pico and includes both simple and enhanced key detection modes.
4+
5+
== Introduction
6+
7+
This example shows how to use a 4x4 matrix keypad with the Raspberry Pi Pico. It has been enhanced to support detecting multiple key presses simultaneously, alongside the original single key press functionality.
8+
9+
== Features
10+
11+
* Simple key detection (one key press at a time)
12+
* Enhanced key detection (multiple keys pressed simultaneously)
13+
* Configurable maximum number of simultaneous key presses
14+
* Mode switching via macro definition
15+
16+
== Hardware Requirements
17+
18+
* Raspberry Pi Pico
19+
* 4x4 matrix keypad
20+
* Connecting wires
21+
22+
== Wiring Instructions
23+
24+
Follow these wiring instructions to connect the 4x4 matrix keypad to the Raspberry Pi Pico:
25+
26+
[image2]
27+
image::pico_keypad_connection.png[Keypad Connection Diagram]
28+
29+
=== Wiring Table
30+
31+
The following table shows the GPIO pins used for the rows and columns of the keypad:
32+
33+
[width="50%",cols="1,1,1",options="header"]
34+
|===
35+
| Keypad Row/Column | GPIO Pin | Pin Number
36+
37+
| Row 0 | GP9 | 9
38+
| Row 1 | GP8 | 8
39+
| Row 2 | GP7 | 7
40+
| Row 3 | GP6 | 6
41+
42+
| Column 0 | GP5 | 5
43+
| Column 1 | GP4 | 4
44+
| Column 2 | GP2 | 2
45+
| Column 3 | GP1 | 1
46+
|===
47+
48+
Ensure that the keypad rows and columns are connected to the GPIO pins on the Pico as indicated in the table.
49+
50+
== Usage
51+
52+
This example can be compiled and run to interact with the keypad.
53+
54+
=== Simple Mode
55+
56+
In simple mode, the `get_keypad_value` function returns a single character corresponding to the pressed key. If no key is pressed, it returns a constant value indicating no key press.
57+
58+
[source,c]
59+
----
60+
char get_keypad_value();
61+
----
62+
63+
=== Enhanced Mode
64+
65+
In enhanced mode, the `get_keypad_result` function returns a `KeypadResult` structure containing the count of pressed keys and a list of those keys.
66+
67+
[source,c]
68+
----
69+
KeypadResult get_keypad_result();
70+
----
71+
72+
The `KeypadResult` structure is defined as follows:
73+
74+
[source,c]
75+
----
76+
typedef struct {
77+
int count; // Number of pressed keys
78+
char keys[MAX_MULTI_KEYS]; // Pressed keys
79+
} KeypadResult;
80+
----
81+
82+
== Example Output
83+
84+
=== Simple Mode
85+
86+
If a key is pressed, the output will be:
87+
88+
[source, plain]
89+
----
90+
Key 'A' pressed
91+
----
92+
93+
If no key is pressed, the output will be:
94+
95+
[source, plain]
96+
----
97+
No key pressed
98+
----
99+
100+
=== Enhanced Mode
101+
102+
If multiple keys are pressed, the output will be:
103+
104+
[source, plain]
105+
----
106+
Bang!!! '2' key(s) are pressed. Keys: A, B
107+
----
108+
109+
If no keys are pressed, the output will be:
110+
111+
[source, plain]
112+
----
113+
No key pressed
114+
----
115+
116+
== Additional Information
117+
118+
For contributing to the repository, refer to the link:../../CONTRIBUTING.md[CONTRIBUTING.md] file.
119+
120+
For licensing information, see the link:../../LICENSE.TXT[LICENSE.TXT] file.

gpio/keypad/keypad.c

+96-14
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,41 @@
1515
// to determine if no key is pressed.
1616
#define READ_MS_DELAY 10 // Delay between read to avoid noise
1717

18+
// Enable enhanced mode to allow reading multiple keys simultaneously.
19+
#define ENHANCED_KEYPAD
20+
21+
22+
#ifdef ENHANCED_KEYPAD
23+
/*
24+
* When using the Enhanced Keypad mode, the get_keypad_value function
25+
* returns a KeypadResult structure instead of a single character.
26+
*
27+
* KeypadResult includes:
28+
* - count: The number of keys pressed (up to MAX_MULTI_KEYS).
29+
* - keys: An array containing the pressed keys (up to MAX_MULTI_KEYS).
30+
*/
31+
#define MAX_MULTI_KEYS 6 // Maximum number of keys that can be pressed simultaneously.
32+
33+
typedef struct {
34+
int count; // Number of keys pressed.
35+
char keys[MAX_MULTI_KEYS]; // Array of pressed keys.
36+
} KeypadResult;
37+
#endif // ENHANCED_KEYPAD
38+
1839

1940
const uint8_t keypad_rows_pins[KEYPAD_NUM_ROWS] = {
20-
10,
21-
11,
22-
12,
23-
13,
41+
9, // GP9
42+
8, // GP8
43+
7, // GP7
44+
6, // GP6
2445
};
2546

2647

2748
const uint8_t keypad_columns_pins[KEYPAD_NUM_COLUMNS] = {
28-
18, // GP18
29-
19, // GP19
30-
20, // GP20
31-
21, // GP21
49+
5, // GP5
50+
4, // GP4
51+
3, // GP2
52+
2, // GP1
3253
};
3354

3455

@@ -58,39 +79,100 @@ void pico_keypad_init(void) {
5879
}
5980

6081

61-
char get_keypad_value(void) {
82+
#ifdef ENHANCED_KEYPAD
83+
KeypadResult get_keypad_result(void) {
84+
KeypadResult result; // Define the result structure.
85+
result.count = 0; // Initialize count to 0.
86+
87+
// Iterate over key and rows to identify which key(s) are pressed.
88+
for (int row=0; row < KEYPAD_NUM_ROWS; row++) {
89+
gpio_put(keypad_rows_pins[row], 1);
90+
for (int column=0; column < KEYPAD_NUM_COLUMNS; column++) {
91+
sleep_ms(READ_MS_DELAY);
92+
if(gpio_get(keypad_columns_pins[column])) {
93+
// If the column pin is HIGH, a key is pressed.
94+
// Save the key in the KeypadResult structure and increase
95+
// count.
96+
result.keys[result.count] = keypad_keys[row][column];
97+
result.count++;
98+
}
99+
}
100+
gpio_put(keypad_rows_pins[row], 0);
101+
}
102+
103+
// Return the structure containing all pressed keys.
104+
return result;
105+
}
106+
#else
107+
char get_keypad_result(void) {
62108
// Iterate over key and rows to identify which key is pressed.
63-
// When iterating rows, the GPIO_OUT associted to the row needs to be set
109+
// When iterating rows, the GPIO_OUT associated to the row needs to be set
64110
// to HIGH, and then iterate the columns to determine the GPIO_IN.
65111
for (int row=0; row < KEYPAD_NUM_ROWS; row++) {
66112
gpio_put(keypad_rows_pins[row], 1);
67113
for (int column=0; column < KEYPAD_NUM_COLUMNS; column++) {
68114
sleep_ms(READ_MS_DELAY);
69115
if(gpio_get(keypad_columns_pins[column])) {
70-
// If the pin is HIGH, this means this is a matching row and
71-
// column, so we put the row pin to LOW and return the pressed
72-
// key by using the bidimensional array keypad_keys.
116+
// If the column pin is HIGH, a key is pressed.
117+
// Put the row pin to low and return the pressed key.
73118
gpio_put(keypad_rows_pins[row], 0);
74119
return keypad_keys[row][column];
75120
}
76121
}
77122
gpio_put(keypad_rows_pins[row], 0);
78123
}
124+
125+
// Return a constant indicating no key was pressed
79126
return NO_KEY_PRESSED;
80127
}
128+
#endif //ENHANCED_KEYPAD
81129

82130

83131
int main() {
84132
stdio_init_all();
85133
pico_keypad_init();
86134
while (true) {
87-
char key = get_keypad_value();
135+
#ifdef ENHANCED_KEYPAD
136+
// Call the enhanced function to get the result structure
137+
// containing the number of keys pressed and the keys themselves.
138+
KeypadResult result = get_keypad_result();
139+
140+
// Check if no keys are pressed.
141+
if (result.count == 0) {
142+
// Inform the user that no keys are pressed.
143+
printf("No key pressed\n");
144+
}
145+
else{
146+
// If one or more keys are pressed, print the number of pressed keys.
147+
printf("Bang!!! '%d' key(s) are pressed. Keys: ", result.count);
148+
149+
// Iterate through the pressed keys and print them.
150+
for (int i = 0; i < result.count; i++) {
151+
printf("%c", result.keys[i]);
152+
153+
// If it's not the last key, print a comma separator.
154+
if (i != (result.count - 1)) {
155+
printf(", ");
156+
} else {
157+
// If it's the last key, move to the next line.
158+
printf("\n");
159+
}
160+
}
161+
}
162+
#else
163+
// Call the simple function to get a single pressed key.
164+
char key = get_keypad_result();
165+
166+
// Check if no key is pressed.
88167
if (key == NO_KEY_PRESSED) {
168+
// Inform the user that no keys are pressed.
89169
printf("No key pressed\n");
90170
}
91171
else{
172+
// If a key is pressed, print the key.
92173
printf("Key '%c' pressed\n", key);
93174
}
175+
#endif
94176
sleep_ms(500);
95177
}
96178
}
78.1 KB
Loading

0 commit comments

Comments
 (0)