summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;
+ }
+}