Keyboard: Handle key presses once, debounce (untested)

main
radex 2024-02-14 14:37:32 +01:00
parent 771c135cd2
commit 45f84c58ad
Signed by: radex
SSH Key Fingerprint: SHA256:hvqRXAGG1h89yqnS+cyFTLKQbzjWD4uXIqw7Y+0ws30
5 changed files with 71 additions and 34 deletions

View File

@ -24,3 +24,6 @@
// --- Keyboard config
#define KBD_I2C_ADDR 0x21
// Used by keyboard and encoder switch
#define DEBOUNCE_MS 10

View File

@ -13,8 +13,6 @@ When rotated clockwise, CLK changes before DT; when rotated counter-clockwise, D
*/
#define SW_DEBOUNCE_MS 10
bool previousClk = false;
bool previousDt = false;
@ -40,8 +38,8 @@ SwitchEvent handleSwitch() {
}
// debounce
unsigned long now = millis();
if (now - lastChangeAt < SW_DEBOUNCE_MS) {
auto now = millis();
if (now - lastChangeAt < DEBOUNCE_MS) {
return SWITCH_NONE;
}

View File

@ -3,6 +3,7 @@
#include "constants.h"
#include "debug.h"
#include "keyboard.h"
PCF8574 kbd(KBD_I2C_ADDR, &Wire);
@ -15,15 +16,10 @@ row/col 0 1 2 3
1 4 5 6 B
2 7 8 9 C
3 * 0 # D
Keys, in order:
123A 456B 789C *0#D
*/
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,
@ -43,30 +39,35 @@ bool setupKeyboard() {
return true;
}
Key previousKey = KEY_NONE;
unsigned long lastPressedAt = 0;
Key getPressedKey();
uint8_t getPressedDigit(Key key);
Key handleKeyboard() {
auto key = getPressedKey();
// only handle change
if (key == previousKey) {
return KEY_NONE;
}
// debounce
auto now = millis();
if (now - lastPressedAt < DEBOUNCE_MS) {
return KEY_NONE;
}
// save state
previousKey = key;
lastPressedAt = now;
// return event
return key;
}
#define KBD_COL_MASK 0b11110000
#define KBD_ROW_MASK 0b00001111
void demoKeyboard() {
auto key = getPressedKey();
if (key != KEY_NONE) {
Serial.print("Key pressed: ");
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();
}
}
/**
* Returns pressed key or KEY_NONE if no key is pressed
*
@ -107,9 +108,6 @@ Key getPressedKey() {
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];

View File

@ -1,4 +1,26 @@
#pragma once
/*
Keys, in order:
123A 456B 789C *0#D
*/
typedef uint8_t Key;
#define KEY_NONE 255
extern const char *allKeys;
bool setupKeyboard();
void demoKeyboard();
/**
* Returns pressed key once upon its release
* or KEY_NONE if no key is pressed
*
* NOTE: Only one key can be pressed at a time;
* if multiple keys are pressed, the behavior is undefined
*/
Key handleKeyboard();
/**
* Returns pressed digit or KEY_NONE if the key is not a digit
*/
uint8_t getPressedDigit(Key key);

View File

@ -26,7 +26,23 @@ void loop() {
// debugScanI2C();
// demoScreen();
// demoKeyboard();
auto key = handleKeyboard();
if (key != KEY_NONE) {
tick();
Serial.print("Key pressed: ");
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();
}
if (auto event = handleSwitch()) {
tick();