-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButtonBox.ino
96 lines (87 loc) · 2.79 KB
/
ButtonBox.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
/**
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2016 Carl Hauser under the same license.
*/
#include "ButtonBox.h"
#include <Joystick.h>
// Keyboard state variables
boolean prevKeyReadings[ROWS*COLS];
boolean currentKeyReadings[ROWS*COLS];
Joystick_ Joystick = Joystick_(0x03, JOYSTICK_TYPE_JOYSTICK, 16, 0, false, false, false, false, false, false, false, false, false, false, false);
// This is called when the button box is connected and powers up
void setup() {
Joystick.begin(true);
for (int i = 0; i < COLS; i++)
pinMode(colPins[i], INPUT_PULLUP);
for (int i = 0; i < ROWS; i++) {
pinMode(rowPins[i], OUTPUT);
digitalWrite(rowPins[i], HIGH);
}
clearBooleanMatrixes();
}
// Read key states and handle all chord events
void loop() {
readKeys();
sendChanges();
delay(20);
}
int sendCount = 0;
void sendChanges() {
int always = 0;
if (sendCount==10) {
always = 1;
sendCount = 0;
}
else {
sendCount++;
};
for (int i = 0; i < ROWS*COLS; i++) {
bool currentKeyReading = currentKeyReadings[i];
if ((currentKeyReading != prevKeyReadings[i]) || always) {
if (currentKeyReading) {
Joystick.pressButton(switchMap[i]);
} else {
Joystick.releaseButton(switchMap[i]);
}
prevKeyReadings[i] = currentKeyReading;
}
}
}
// Set all values of all boolean matrixes to false
void clearBooleanMatrixes() {
clearBooleanMatrix(prevKeyReadings, false);
clearBooleanMatrix(currentKeyReadings, false);
}
// Set all values of the passed matrix to the given value
void clearBooleanMatrix(boolean booleanMatrix[ROWS*COLS], boolean value) {
for (int i = 0; i < ROWS*COLS; i++) {
booleanMatrix[i] = value;
}
}
// Read all keys
// Note that with the cathode (black bar end) of the diodes pointing
// to the Row pins, we drive the row pins low to activate a row
// and then sense the values of the column pins. A low value
// indicates that the button at the intersection of the row and column
// is pushed.
void readKeys() {
for (int i = 0; i < ROWS; i++) {
digitalWrite(rowPins[i], LOW);
for (int j = 0; j < COLS; j++)
currentKeyReadings[i*COLS+j] = !digitalRead(colPins[j]);
digitalWrite(rowPins[i], HIGH);
}
}