-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonBoxV2.ino.ino
149 lines (124 loc) · 3.76 KB
/
ButtonBoxV2.ino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <RotaryEncoder.h>
#include <Keypad.h>
#include <Joystick.h>
#define rot1IntPin1 0
#define rot1IntPin2 1
#define rot2IntPin1 2
#define rot2IntPin2 3
#define rot3IntPin1 8
#define rot3IntPin2 7
#define rot1Button 6
#define rot2Button 5
#define rot3Button 4
#define ENABLE_PULLUPS
#define NUMCOLS 5
#define NUMROWS 3
#define Rot1BtnNum 14
#define Rot2BtnNum 15
#define Rot3BtnNum 16
const bool initAutoSendState = true;
byte buttons[NUMROWS][NUMCOLS] = {
{4,3,2,1,0},
{9,8,7,6,5},
{10,11,12,13,14}
};
byte rowPins[NUMROWS] = {20,19,18};
byte colPins[NUMCOLS] = {9,10,11,12,13};
Joystick_ butJoystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, 24, 0,
false, false, false, false, false, false,
false, false, false, false, false);
Keypad buttbx = Keypad(makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);
RotaryEncoder encoder1(rot1IntPin1,rot1IntPin2);
RotaryEncoder encoder2(rot2IntPin1,rot2IntPin2);
RotaryEncoder encoder3(rot3IntPin1,rot3IntPin2);
void setup() {
pinMode(rot1Button, INPUT);
pinMode(rot2Button, INPUT);
pinMode(rot3Button, INPUT);
digitalWrite(rot1Button, HIGH);
digitalWrite(rot2Button, HIGH);
digitalWrite(rot3Button, HIGH);
attachInterrupt(digitalPinToInterrupt(rot1IntPin1), getTick1, CHANGE);
attachInterrupt(digitalPinToInterrupt(rot1IntPin2), getTick1, CHANGE);
attachInterrupt(digitalPinToInterrupt(rot2IntPin1), getTick2, CHANGE);
attachInterrupt(digitalPinToInterrupt(rot2IntPin2), getTick2, CHANGE);
attachInterrupt(digitalPinToInterrupt(rot3IntPin2), getTick3, CHANGE);
attachInterrupt(digitalPinToInterrupt(rot3IntPin1), getTick3, CHANGE);
butJoystick.begin(true);
}
void getTick1(){
encoder1.tick();
}
void getTick2(){
encoder2.tick();
}
void getTick3(){
encoder3.tick();
}
void loop() {
CheckEncoders();
CheckAllButtons();
}
void CheckAllButtons(void)
{
if (buttbx.getKeys())
{
for (int i=0; i<LIST_MAX; i++)
{
// Serial.println(buttbx.key[i].kstate);
if ( buttbx.key[i].stateChanged )
{
//Serial.println("Button Pressed: " + String(i));
switch (buttbx.key[i].kstate) {
case PRESSED:
case HOLD:
butJoystick.setButton(buttbx.key[i].kchar, 1);
break;
case RELEASED:
case IDLE:
butJoystick.setButton(buttbx.key[i].kchar, 0);
break;
}
}
}
}
if (digitalRead(rot1Button)==LOW) butJoystick.setButton(Rot1BtnNum,1);
else butJoystick.setButton(Rot1BtnNum,0);
if (digitalRead(rot2Button)==LOW) butJoystick.setButton(Rot2BtnNum,1);
else butJoystick.setButton(Rot2BtnNum,0);
if (digitalRead(rot3Button)==LOW) butJoystick.setButton(Rot3BtnNum,1);
else butJoystick.setButton(Rot3BtnNum,0);
}
void CheckEncoders(void)
{
int dir1= (int)encoder1.getDirection();
int dir2= (int)encoder2.getDirection();
int dir3= (int)encoder3.getDirection();
if (dir1 != 0) {
if (dir1 < 0) { butJoystick.setButton(18,1); delay(100); }
else if (dir1 > 0) { butJoystick.setButton(19,1); delay(100);}
}
else
{
butJoystick.setButton(18,0);
butJoystick.setButton(19,0);
}
if (dir2 != 0) {
if (dir2 < 0) { butJoystick.setButton(20,1); delay(100);}
else if (dir2 > 0) { butJoystick.setButton(21,1); delay(100);}
}
else
{
butJoystick.setButton(20,0);
butJoystick.setButton(21,0);
}
if (dir3 != 0) {
if (dir3 < 0) { butJoystick.setButton(22,1); delay(200);}
else if (dir3 > 0) { butJoystick.setButton(23,1); delay(100);}
}
else
{
butJoystick.setButton(22,0);
butJoystick.setButton(23,0);
}
}