-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeyboard_driver.ino
69 lines (60 loc) · 1.39 KB
/
keyboard_driver.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
#include <Keyboard.h>
// Constants
const unsigned long debounceDelay = 50000;
// Structure for Button
struct Button {
int pin;
char key;
bool pressed;
unsigned long lastRead;
};
// Button instances
Button buttons[] = {
{2, '7', false, 0},
{3, '8', false, 0},
{4, '9', false, 0},
{5, 'L', false, 0},
{6, '4', false, 0},
{7, '5', false, 0},
{8, '6', false, 0},
{9, 'G', false, 0},
{10, '1', false, 0},
{11, '2', false, 0},
{12, '3', false, 0},
{A0, 'O', false, 0},
{A1, 'W', false, 0},
{A2, '0', false, 0},
{A3, '.', false, 0},
{A5, 'E', false, 0}
};
//SETUP
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("Sheldor ist wieder online");
for (Button &button : buttons) {
pinMode(button.pin, INPUT_PULLUP);
}
Keyboard.begin();
}
// TIME_PASSED
bool time_passed(unsigned long pre, unsigned long next, unsigned long threshold) {
return (next - pre) >= threshold;
}
// UPDATE_BUTTON
void updateButton(Button &button) {
if (digitalRead(button.pin) == HIGH && button.pressed && time_passed(button.lastRead, micros(), debounceDelay)) {
button.pressed = false;
Keyboard.release(button.key);
} else if (digitalRead(button.pin) == LOW && !button.pressed) {
button.lastRead = micros();
button.pressed = true;
Keyboard.press(button.key);
}
}
// LOOP
void loop() {
for (Button &button : buttons) {
updateButton(button);
}
}