Keyboard: Read digit (untested)

main
radex 2024-02-10 10:42:25 +01:00
parent c8f72826d1
commit a9e21296c8
Signed by: radex
SSH Key Fingerprint: SHA256:hvqRXAGG1h89yqnS+cyFTLKQbzjWD4uXIqw7Y+0ws30
1 changed files with 27 additions and 2 deletions

View File

@ -21,7 +21,14 @@ row/col 0 1 2 3
*/
typedef uint8_t Key;
const char *allKeys = "123A456B789C*0#D";
#define KEY_NONE 255
const uint8_t digits[16] =
{ 1, 2, 3, KEY_NONE,
4, 5, 6, KEY_NONE,
7, 8, 9, KEY_NONE,
KEY_NONE, 0, KEY_NONE, KEY_NONE};
bool setupKeyboard() {
if (!kbd.begin()) {
@ -37,16 +44,26 @@ bool setupKeyboard() {
}
Key getPressedKey();
uint8_t getPressedDigit(Key key);
#define KBD_COL_MASK 0b11110000
#define KBD_ROW_MASK 0b00001111
void demoKeyboard() {
String allKeys = F("123A456B789C*0#D");
auto key = getPressedKey();
if (key != KEY_NONE) {
Serial.print("Key pressed: ");
Serial.println(allKeys[key]);
Serial.print(allKeys[key]);
auto digit = getPressedDigit(key);
if (digit != KEY_NONE) {
Serial.print(", digit: ");
Serial.print(digit);
} else {
Serial.print(" (Not a digit)");
}
Serial.println();
}
}
@ -89,3 +106,11 @@ Key getPressedKey() {
// return key
return (rowPressed * 4) + colPressed;
}
/**
* Returns pressed digit or KEY_NONE if the key is not a digit
*/
uint8_t getPressedDigit(Key key) {
if (key == KEY_NONE) return KEY_NONE;
return digits[key];
}