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

88 lines
2.0 KiB
Arduino
Raw Permalink Normal View History

2021-01-30 14:22:10 +01:00
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
21, 0,
false, false, false,
false, false, false,
false, false,
false, false, false);
2021-01-30 14:22:10 +01:00
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] = {{0,1,2},{3,4,5},{6,7,8}}; // Buttons controller keys
2021-01-30 15:10:31 +01:00
bool Bstate[3][3] = {{0,0,0},{0,0,0},{0,0,0}}; // Buttons state
2021-01-30 14:22:10 +01:00
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};
2021-01-30 14:22:10 +01:00
void setup() {
2021-01-30 15:10:31 +01:00
for(int i=0; i<12; i++) {
pinMode(RE[i/2][i%2], INPUT_PULLUP);
2021-01-30 14:22:10 +01:00
}
2021-01-30 15:10:31 +01:00
for(int i=0; i<3; i++) {
2021-01-30 14:22:10 +01:00
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);
}
2021-01-30 15:10:31 +01:00
Joystick.begin();
2021-01-30 14:22:10 +01:00
}
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) {
Joystick.setButton(BK[j][i], state);
} else {
Joystick.setButton(BK[j][i], !state);
}
Bstate[j][i] = state;
}
2021-01-30 14:22:10 +01:00
pinMode(BR[j], INPUT);
}
pinMode(BC[i], INPUT);
}
}
void readEncoders() {
for (int i=0; i<6; i++) {
if (millis() - REchange[i] > 50) {
Joystick.releaseButton(REK[i][0]);
Joystick.releaseButton(REK[i][1]);
REchange[i] = millis();
}
}
for (int i=0; i<6; i++) {
bool Astate = digitalRead(RE[i][0]);
if (Astate != REstate[i]) {
if(digitalRead(RE[i][1]) == Astate) {
Joystick.pressButton(REK[i][0]);
} else {
Joystick.pressButton(REK[i][1]);
}
REchange[i] = millis();
REstate[i] = Astate;
}
}
}
2021-01-30 14:22:10 +01:00
void loop() {
readMatrix();
readEncoders();
2021-01-30 15:10:31 +01:00
}