From af1844cafabb85fab068e59c7ff2a5c0a229ea3c Mon Sep 17 00:00:00 2001 From: Nico Stuhlmueller Date: Tue, 11 Apr 2023 17:59:25 +0200 Subject: [PATCH] added demo Arduino code --- hackpad.ino | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 hackpad.ino diff --git a/hackpad.ino b/hackpad.ino new file mode 100644 index 0000000..4fc4d63 --- /dev/null +++ b/hackpad.ino @@ -0,0 +1,102 @@ +//============================================================================= +// iR Hackpad Demo Firmware +// Compile for Arduino Leonardo +//============================================================================= + +// The Arduino Keyboard library creates a HID keyboard interface to send keyboard scancodes to the host. +// For convenience, the class takes ASCII characters via Keyboard::write(), which are automatically +// converted to the corresponding keboard scan-codes according to the keyboard layout passed to +// Keyboard::begin(). On the host side, the scan-codes are again converted to characters by the OS. +#include "Keyboard.h" + +//============================================================================= +// Our key matrix (see schematics) +const int N_rows = 5; +const int N_cols = 4; +const int gpio_rows[N_rows] = { 6, 7, 8, 9, 5 }; +const int gpio_cols[N_cols] = { 15, 14, 16, 10 }; + +// Key function assignment: ASCII characters or special function codes (see Keyboard.h) +const int key_matrix[N_rows][N_cols] = { + {'a', 'b', 'c', 'd'}, // 4x4 keypad + {'e', 'f', 'g', 'h'}, + {'i', 'j', 'k', 'l'}, + {'m', 'n', 'o', 'p'}, + {'_', '_', '_', 'x'} // rotery encoder button +}; + +// Key state +struct { + bool last; // Last debounced state + bool inhibit; // State change inhibited during debounce period + uint32_t since_ms; // Inhibited since timestamp +} key_state[N_rows][N_cols]; + +const uint32_t KEY_DEBOUNCE_MS = 50; // Key Debounce period +const uint32_t KEY_SCAN_DELAY_US = 200; // Delay between row scans + +//============================================================================= + +void setup() +{ + for (int col=0; col KEY_DEBOUNCE_MS)) { + key_state[row][col].inhibit = 0; + } + + if (pressed == true) { // Key is pressed? + if (key_state[row][col].last == false) { // Rising edge: Key has just been pressed? + key_state[row][col].last = true; + key_state[row][col].inhibit = true; + key_state[row][col].since_ms = millis(); + Keyboard.press(key_matrix[row][col]); + } + + // TODO: Repeat delay, repeat rate + + } + else { // Pressed == false + if (!key_state[row][col].inhibit) + key_state[row][col].last = false; + Keyboard.release(key_matrix[row][col]); + } +} + +//============================================================================= + +void loop() +{ + for (int row=0; row