From e74849a71d9fff58a65bf11a14dafde1b32a8a1d Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Mon, 4 Jul 2022 09:20:54 +0200 Subject: [PATCH] Control box with Keyboard F-keys --- ControlBox.ino | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ sketch.json | 6 ++++ 2 files changed, 97 insertions(+) create mode 100644 ControlBox.ino create mode 100644 sketch.json diff --git a/ControlBox.ino b/ControlBox.ino new file mode 100644 index 0000000..9f1584c --- /dev/null +++ b/ControlBox.ino @@ -0,0 +1,91 @@ +#include + +const byte BR[3] = {9,5,13}; // Button array rows +const byte BC[3] = {10,11,12}; // Button array columns +/*const byte BK[3][3] = {{KEY_KP_1,KEY_KP_2,KEY_KP_3}, + {KEY_KP_4,KEY_KP_5,KEY_KP_6}, + {KEY_KP_7,KEY_KP_8,KEY_KP_9} +}; // Buttons controller keys */ +const byte BK[3][3] = {{KEY_F15,KEY_F16,KEY_F17}, + {KEY_F18,KEY_F19,KEY_F20}, + {KEY_F21,KEY_F22,KEY_F23} +}; // Buttons controller keys +bool Bstate[3][3] = {{0,0,0},{0,0,0},{0,0,0}}; // Buttons state + +const byte RE[6][2] = {{8,2},{7,3},{6,4},{19,18},{21,20},{22,23}}; // Rotary encoders pins {A,B} +const byte REK[6][2] = {{9,10},{11,12},{13,14},{15,16},{17,18},{19,20}}; // Rotary encoders keys {-,+} +unsigned long REchange[6] = {0,0,0,0,0,0}; // Last RE change time +bool REstate[6] = {0,0,0,0,0,0}; // Rotary encoders state + +void setup() { + for (int i = 0; i < 12; i++) { + pinMode(RE[i / 2][i % 2], INPUT_PULLUP); + } + for (int i = 0; i < 3; i++) { + pinMode(BR[i], INPUT); + pinMode(BC[i], INPUT_PULLUP); + } + for (int i = 0; i < 6; i++) { + pinMode(RE[i][0], INPUT_PULLUP); + pinMode(RE[i][1], INPUT_PULLUP); + } + + Keyboard.begin(); +} + +void loop() { + readMatrix(); + readEncoders(); +} + + +// Read temporary button matrix +void readMatrix() { + for (int i = 0; i < 3; i++) { + pinMode(BC[i], OUTPUT); + digitalWrite(BC[i], LOW); + + for (int j = 0; j < 3; j++) { + pinMode(BR[j], INPUT_PULLUP); + int state = digitalRead(BR[j]); + if (state != Bstate[j][i]) { + if (i == 0 && j == 0) { + setKey(BK[j][i], state); + } else { + setKey(BK[j][i], !state); + } + Bstate[j][i] = state; + } + pinMode(BR[j], INPUT); + } + pinMode(BC[i], INPUT); + } +} + +// Press key +void setKey(byte key, bool state) { + if (state) { + Keyboard.press(key); + } else { + Keyboard.release(key); + } +} + +void readEncoders() { + for (int i = 0; i < 6; i++) { + bool Astate = digitalRead(RE[i][0]); + + if (Astate != REstate[i]) { + if (digitalRead(RE[i][1]) == Astate) { + Keyboard.press(KEY_LEFT_CTRL); + Keyboard.write(BK[1 + i / 3][i % 3]); + Keyboard.release(KEY_LEFT_CTRL); + } else { + Keyboard.press(KEY_LEFT_SHIFT); + Keyboard.write(BK[1 + i / 3][i % 3]); + Keyboard.release(KEY_LEFT_SHIFT); + } + REstate[i] = Astate; + } + } +} diff --git a/sketch.json b/sketch.json new file mode 100644 index 0000000..fccbf79 --- /dev/null +++ b/sketch.json @@ -0,0 +1,6 @@ +{ + "cpu": { + "fqbn": "arduino:avr:micro", + "port": "" + } +} \ No newline at end of file