88 lines
2.0 KiB
C++
88 lines
2.0 KiB
C++
#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);
|
|
|
|
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
|
|
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};
|
|
|
|
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);
|
|
}
|
|
|
|
Joystick.begin();
|
|
}
|
|
|
|
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;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
readMatrix();
|
|
readEncoders();
|
|
}
|