From dd62b9bfd982d53ec732135df76509fc532707b9 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sun, 4 Oct 2020 11:59:11 +1300 Subject: Commit working code Emulates a two-key keyboard, with each key being activated on alternate edges of the input line. I.e. one keypress+release emulated when the switch is pressed, and a different keypress+release when they switch is released. The input is polled with a naive software loop, and debounced with a simple settling state machine to help fight off false triggers. --- vim-clutch.ino | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 vim-clutch.ino diff --git a/vim-clutch.ino b/vim-clutch.ino new file mode 100644 index 0000000..8c23b83 --- /dev/null +++ b/vim-clutch.ino @@ -0,0 +1,48 @@ +#include + +#define KEYCODE_PRESS KEY_F11 +#define KEYCODE_RELEASE KEY_F12 +#define LED_PIN 17 +#define SWITCH_PIN 2 +#define DEBOUNCE_MS 25 +#define POLL_MS 5 +#define COUNTER_RESET (DEBOUNCE_MS / POLL_MS) + +#define DEBUG_MARK_BEGIN (digitalWrite(LED_PIN, LOW)) +#define DEBUG_MARK_END (digitalWrite(LED_PIN, HIGH)) + +void setup() { + pinMode(SWITCH_PIN, INPUT_PULLUP); + pinMode(LED_PIN, OUTPUT); +} + +void loop() { + static long last_poll = millis(); + static bool debounced = LOW; + static bool raw = LOW; + static int counter = COUNTER_RESET; + + if (millis() >= last_poll + POLL_MS) { + last_poll = millis(); + DEBUG_MARK_BEGIN; + raw = digitalRead(SWITCH_PIN); + if (raw == debounced) { + counter = COUNTER_RESET; + } else { + if (--counter == 0) { + debounced = raw; + if (debounced) { + // Key was just released + Keyboard.press(KEYCODE_RELEASE); + Keyboard.release(KEYCODE_RELEASE); + } else { + // Key was just pressed + Keyboard.press(KEYCODE_PRESS); + Keyboard.release(KEYCODE_PRESS); + } + counter = COUNTER_RESET; + } + } + DEBUG_MARK_END; + } +} -- cgit v1.1