This repository has been archived on 2022-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
controlbox/ControlBox.ino

92 lines
2.2 KiB
Arduino
Raw Normal View History

2022-07-04 09:20:54 +02:00
#include <Keyboard.h>
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;
}
}
}