#include <stdio.h>
#include "pico/stdlib.h"
uint8_t Key_col[4] = { 8, 9, 10, 11 };
uint8_t Key_row[4] = { 12, 13, 14, 15 };
uint8_t Key_value[4][4] = { { 1,2,3,10},
{ 4,5,6,11},{ 7,8,9,12},{ 14,0,15,13}
};
uint8_t readKey();
int main() {
stdio_init_all();
for (uint8_t index=0; index < 4; index++){
gpio_init( Key_col[index] );
gpio_set_dir( Key_col[index], GPIO_OUT );
gpio_init( Key_row[index] );
gpio_set_dir( Key_row[index], GPIO_IN );
gpio_pull_up( Key_row[index] );
}
for (uint8_t index=0; index < 4; index++){
gpio_put(Key_col[index], 1);
}
uint8_t key;
while (true) {
key = readKey();
if ( key <16){
printf("KEY: %i\n", key);
}
sleep_ms(20);
}
}
uint8_t readKey(){
uint8_t col, row, keycol, keyrow, value;
bool tmpEA, tmpEF, flag;
keycol=4;
keyrow=4;
flag=0;
value = 16;
for ( col = 0 ; col < 4; col++ ){
gpio_put(Key_col[col], 0);
sleep_ms(1);
for ( row = 0 ; row < 4; row++ ){
sleep_ms(10);
tmpEA = gpio_get( Key_row[row] );
if ( !tmpEA){
if ( ( keycol != col) || ( keyrow != row) ) {
keycol = col;
keyrow = row;
value = Key_value[keyrow][keycol];
printf("KEY[%i][%i] = %i\n", keycol, keyrow, value);
flag=1;
}
break;
}
if (flag){
break;
}
}
gpio_put(Key_col[col], 1);
}
return value;
}