diff --git a/firmware/src/constants.h b/firmware/src/constants.h index e5a4579..0e151ea 100644 --- a/firmware/src/constants.h +++ b/firmware/src/constants.h @@ -24,3 +24,6 @@ // --- Keyboard config #define KBD_I2C_ADDR 0x21 + +// Used by keyboard and encoder switch +#define DEBOUNCE_MS 10 diff --git a/firmware/src/encoder.cpp b/firmware/src/encoder.cpp index ddfc9a7..3f016d3 100644 --- a/firmware/src/encoder.cpp +++ b/firmware/src/encoder.cpp @@ -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; } diff --git a/firmware/src/keyboard.cpp b/firmware/src/keyboard.cpp index 6eacade..f934036 100644 --- a/firmware/src/keyboard.cpp +++ b/firmware/src/keyboard.cpp @@ -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]; diff --git a/firmware/src/keyboard.h b/firmware/src/keyboard.h index c04df60..55c24f8 100644 --- a/firmware/src/keyboard.h +++ b/firmware/src/keyboard.h @@ -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); diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 4638d6d..6c6a12d 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -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();