diff options
author | David Phillips <david@yeah.nah.nz> | 2020-10-04 11:59:11 +1300 |
---|---|---|
committer | David Phillips <david@yeah.nah.nz> | 2020-10-04 12:11:00 +1300 |
commit | dd62b9bfd982d53ec732135df76509fc532707b9 (patch) | |
tree | f15f474db6ac79d058c1bee47d007a228e13a7ad | |
download | vim-clutch-improved-arduino-dd62b9bfd982d53ec732135df76509fc532707b9.tar.xz |
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.
-rw-r--r-- | vim-clutch.ino | 48 |
1 files changed, 48 insertions, 0 deletions
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 <Keyboard.h> + +#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; + } +} |