summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@yeah.nah.nz>2020-10-04 11:59:11 +1300
committerDavid Phillips <david@yeah.nah.nz>2020-10-04 12:11:00 +1300
commitdd62b9bfd982d53ec732135df76509fc532707b9 (patch)
treef15f474db6ac79d058c1bee47d007a228e13a7ad
downloadvim-clutch-improved-arduino-dd62b9bfd982d53ec732135df76509fc532707b9.tar.xz
Commit working codeHEADmaster
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.ino48
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;
+ }
+}