-
Notifications
You must be signed in to change notification settings - Fork 908
/
Copy pathkeypad.c
96 lines (81 loc) · 2.65 KB
/
keypad.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
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include "pico/stdlib.h"
#define KEYPAD_NUM_ROWS 4
#define KEYPAD_NUM_COLUMNS 4
#define NO_KEY_PRESSED '\0' // Just a character that unlikely be
// present in keyboards, so it can by used
// to determine if no key is pressed.
#define READ_MS_DELAY 10 // Delay between lectures to avoid noise
const uint8_t keypad_rows_pins[KEYPAD_NUM_ROWS] = {
10,
11,
12,
13,
};
const uint8_t keypad_columns_pins[KEYPAD_NUM_COLUMNS] = {
18, // GP18
19, // GP19
20, // GP20
21, // GP21
};
const char keypad_keys[KEYPAD_NUM_ROWS][KEYPAD_NUM_COLUMNS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
void pico_keypad_init(void){
// Initialize GPIOs
//Initialize row pins as GPIO_OUT
for (int i=0; i < KEYPAD_NUM_ROWS; i++){
uint8_t pin_number = keypad_rows_pins[i];
gpio_init(pin_number);
gpio_set_dir(pin_number, GPIO_OUT);
gpio_put(pin_number, 0); // Make sure GPIO_OUT is LOW
}
//Initialize column pins as GPIO_IN
for (int i=0; i < KEYPAD_NUM_COLUMNS; i++){
uint8_t pin_number = keypad_columns_pins[i];
gpio_init(pin_number);
gpio_set_dir(pin_number, GPIO_IN);
}
}
char get_keypad_value(void){
// Iterate over key and rows to identify which key is pressed.
// When iterating rows, the GPIO_OUT associted to the row needs to be set
// to HIGH, and then iterate the columns to determine the GPIO_IN.
for (int row=0; row < KEYPAD_NUM_ROWS; row++){
gpio_put(keypad_rows_pins[row], 1);
for (int column=0; column < KEYPAD_NUM_COLUMNS; column++){
sleep_ms(READ_MS_DELAY);
if(gpio_get(keypad_columns_pins[column])){
// If the pin is HIGH, this means this is a matching row and
// column, so we put the row pin to LOW and return the pressed
// key by using the bidimensional array keypad_keys.
gpio_put(keypad_rows_pins[row], 0);
return keypad_keys[row][column];
}
}
gpio_put(keypad_rows_pins[row], 0);
}
return NO_KEY_PRESSED;
}
int main() {
stdio_init_all();
pico_keypad_init();
while (true) {
char key = get_keypad_value();
if (key == NO_KEY_PRESSED){
printf("No key pressed\n");
}
else{
printf("Key '%c' pressed\n", key);
}
sleep_ms(500);
}
}