Created
July 20, 2017 15:43
-
-
Save zeph1e/27a26a99635a34f13fde2e80b7557df8 to your computer and use it in GitHub Desktop.
Foot pedal prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Foot pedal prototype | |
*/ | |
#include <HID.h> | |
#include <Keyboard.h> | |
#define PIN_CTRL 2 | |
#define PIN_ALT 3 | |
#define PIN_SHIFT 4 | |
#define MASK_CTRL (1 << PIN_CTRL) | |
#define MASK_ALT (1 << PIN_ALT) | |
#define MASK_SHIFT (1 << PIN_SHIFT) | |
#define IDLE_DELAY 100 | |
#define READ_PIN(key) digitalRead(PIN_##key) == LOW ? MASK_##key : 0 | |
#define TOGGLE_KEY(key) \ | |
if (key ^ (pin_stat & MASK_##key)) { \ | |
if (key) Keyboard.press(KEY_LEFT_##key); \ | |
else Keyboard.release(KEY_LEFT_##key); \ | |
} | |
unsigned int pin_stat; | |
void setup() { | |
pin_stat = 0; | |
pinMode(PIN_CTRL, INPUT_PULLUP); | |
pinMode(PIN_ALT, INPUT_PULLUP); | |
pinMode(PIN_SHIFT, INPUT_PULLUP); | |
Keyboard.begin(); | |
} | |
void loop() { | |
unsigned int CTRL = READ_PIN(CTRL); | |
unsigned int ALT = READ_PIN(ALT); | |
unsigned int SHIFT = READ_PIN(SHIFT); | |
if ((CTRL | ALT | SHIFT) == pin_stat) { | |
delay(IDLE_DELAY); | |
return; | |
} | |
TOGGLE_KEY(CTRL); | |
TOGGLE_KEY(ALT); | |
TOGGLE_KEY(SHIFT); | |
pin_stat = CTRL | ALT | SHIFT; | |
delay(IDLE_DELAY); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment