#define ledEnable 2
#define buttonEnable 7
#define columnCount 2
#define rowCount 2
#define keyCount 37
#define debug false
int enablePins[columnCount] = {
21,
22
};
int keyPins[rowCount] = {
11,
12
};
bool keys[columnCount][rowCount];
char chars[keyCount] = "abcdefghijklmnopqrstuvwxyz0123456789";
void initArrays(void) {
for (int col = 0; col < columnCount; col++) {
for (int r = 0; r < rowCount; r++) {
keys[col][r] = false;
}
}
}
void initKeyPins(void) {
for (int key = 0; key < rowCount; key ++) {
Serial1.print("Setting input_pullup - gpio ");
Serial1.println(keyPins[key]);
pinMode(keyPins[key], INPUT_PULLUP);
}
}
void initColumnPins(void) {
for (int i = 0; i < columnCount; i ++) {
Serial1.print("Setting output (LOW) - gpio ");
Serial1.println(enablePins[i]);
pinMode(enablePins[i], OUTPUT);
digitalWrite(enablePins[i], HIGH);
}
}
void setup() {
initArrays();
// put your setup code here, to run once:
Serial1.begin(115200);
Serial1.println("Hello, Raspberry Pi Pico!");
// enable key press LEDs
pinMode(ledEnable, OUTPUT);
digitalWrite(ledEnable, HIGH);
initColumnPins();
initKeyPins();
}
void loop() {
for (int col = 0; col < columnCount; col ++) {
digitalWrite(enablePins[col], LOW);
for (int row = 0; row < rowCount; row ++) {
if (digitalRead(keyPins[row]) == LOW) {
keyDown(col, row);
} else {
keyUp(col, row);
}
}
digitalWrite(enablePins[col], HIGH);
}
delay(10);
}
int getKeyCode(int c, int r) {
return (c*rowCount) + r;
}
void keyDown(int col, int row) {
if (keys[col][row])
return;
int code = getKeyCode(col,row);
if (debug) {
Serial1.print(code);
Serial1.print('_');
Serial1.print(col);
Serial1.print(':');
Serial1.print(row);
Serial1.println(" pressed");
}
Serial1.print(chars[code]);
keys[col][row] = true;
}
void keyUp(int col, int row) {
if (!keys[col][row])
return;
if (debug) {
Serial1.print(getKeyCode(col,row));
Serial1.print('_');
Serial1.print(col);
Serial1.print(':');
Serial1.print(row);
Serial1.println(" released");
}
keys[col][row] = false;
}