|
15 | 15 | // to determine if no key is pressed.
|
16 | 16 | #define READ_MS_DELAY 10 // Delay between read to avoid noise
|
17 | 17 |
|
| 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 | + |
18 | 39 |
|
19 | 40 | 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 |
24 | 45 | };
|
25 | 46 |
|
26 | 47 |
|
27 | 48 | 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 |
32 | 53 | };
|
33 | 54 |
|
34 | 55 |
|
@@ -58,39 +79,100 @@ void pico_keypad_init(void) {
|
58 | 79 | }
|
59 | 80 |
|
60 | 81 |
|
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) { |
62 | 108 | // 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 |
64 | 110 | // to HIGH, and then iterate the columns to determine the GPIO_IN.
|
65 | 111 | for (int row=0; row < KEYPAD_NUM_ROWS; row++) {
|
66 | 112 | gpio_put(keypad_rows_pins[row], 1);
|
67 | 113 | for (int column=0; column < KEYPAD_NUM_COLUMNS; column++) {
|
68 | 114 | sleep_ms(READ_MS_DELAY);
|
69 | 115 | 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. |
73 | 118 | gpio_put(keypad_rows_pins[row], 0);
|
74 | 119 | return keypad_keys[row][column];
|
75 | 120 | }
|
76 | 121 | }
|
77 | 122 | gpio_put(keypad_rows_pins[row], 0);
|
78 | 123 | }
|
| 124 | + |
| 125 | + // Return a constant indicating no key was pressed |
79 | 126 | return NO_KEY_PRESSED;
|
80 | 127 | }
|
| 128 | +#endif //ENHANCED_KEYPAD |
81 | 129 |
|
82 | 130 |
|
83 | 131 | int main() {
|
84 | 132 | stdio_init_all();
|
85 | 133 | pico_keypad_init();
|
86 | 134 | 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. |
88 | 167 | if (key == NO_KEY_PRESSED) {
|
| 168 | + // Inform the user that no keys are pressed. |
89 | 169 | printf("No key pressed\n");
|
90 | 170 | }
|
91 | 171 | else{
|
| 172 | + // If a key is pressed, print the key. |
92 | 173 | printf("Key '%c' pressed\n", key);
|
93 | 174 | }
|
| 175 | + #endif |
94 | 176 | sleep_ms(500);
|
95 | 177 | }
|
96 | 178 | }
|
0 commit comments