diff options
Diffstat (limited to '12key-arduino/ModeKeyboard.h')
-rw-r--r-- | 12key-arduino/ModeKeyboard.h | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/12key-arduino/ModeKeyboard.h b/12key-arduino/ModeKeyboard.h new file mode 100644 index 0000000..b17f3f5 --- /dev/null +++ b/12key-arduino/ModeKeyboard.h @@ -0,0 +1,131 @@ +#pragma once + +#include <Arduino.h> +#include <Keyboard.h> +#include "Mode.h" + +class ModeKeyboard : public Mode { + protected: + byte code; + bool stroke2; + bool special; + + bool control_hold; + bool super_hold; + bool alt_hold; + bool shift_hold; + + bool nextReset; + + public: + ModeKeyboard() { + reset(); + } + + void reset() { + stroke2 = false; + special = false; + nextReset = false; + digitalWrite(LED, 1); + } + + Mode * update(unsigned int keys) { + digitalWrite(LED, 1); + if (keys & KEY_STAR) return Mode::modes[0]; + + if (keys & KEY_HASH) { + if (nextReset) { + /* hash was pressed twice, reset */ + reset(); + return NULL; + } + nextReset = true; + if (!special) { + special = true; + } + return NULL; + } + nextReset = false; + + if (special) { + stroke2 = false; + special = false; + handleSpecial(keys); + return NULL; + } + + if (!stroke2) { + digitalWrite(LED, 0); + code = 10 * keyToNumber(keys); + stroke2 = true; + } + else { + stroke2 = false; + code += keyToNumber(keys); + transmitCode(code); + if (control_hold) { + Keyboard.release(KEY_LEFT_CTRL); + control_hold = false; + } + if (alt_hold) { + Keyboard.release(KEY_LEFT_ALT); + alt_hold = false; + } + if (super_hold) { + Keyboard.release(KEY_LEFT_GUI); + super_hold = false; + } + if (shift_hold) { + Keyboard.release(KEY_LEFT_SHIFT); + shift_hold = false; + } + } + + return NULL; + } + + + byte keyToNumber(unsigned int keys) { + if (keys & KEY_0) return 0; + if (keys & KEY_1) return 1; + if (keys & KEY_2) return 2; + if (keys & KEY_3) return 3; + if (keys & KEY_4) return 4; + if (keys & KEY_5) return 5; + if (keys & KEY_6) return 6; + if (keys & KEY_7) return 7; + if (keys & KEY_8) return 8; + if (keys & KEY_9) return 9; + return 0; + } + + + void transmitCode(byte code) { + if (code == 99) Keyboard.write(KEY_BACKSPACE); + else if (code == 98) Keyboard.write(KEY_TAB); + else if (code == 97) Keyboard.write(KEY_ESC); + else if (code == 96) Keyboard.write(KEY_RETURN); + else if (code == 95) Keyboard.write(KEY_TAB); + else Keyboard.write(code + 32); + } + + + void handleSpecial(unsigned int keys) { + if (keys & KEY_4) { + Keyboard.press(KEY_LEFT_SHIFT); + shift_hold = true; + } + if (keys & KEY_7) { + Keyboard.press(KEY_LEFT_CTRL); + control_hold = true; + } + if (keys & KEY_8) { + Keyboard.press(KEY_LEFT_GUI); + super_hold = true; + } + if (keys & KEY_9) { + Keyboard.press(KEY_LEFT_ALT); + alt_hold = true; + } + } +}; |