-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.ino
executable file
·98 lines (79 loc) · 2.54 KB
/
keyboard.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
#define USE_TIMER_3 true
#include <TimerInterrupt.h>
#include <ISR_Timer.h>
#include <Keyboard.h>
#define TIMER_INTERVAL_MS 5
#define num_rows 8
#define num_cols 9
int rows[num_rows] = {21, 20, 19, 18, 15, 14, 16, 10 };
int cols[num_cols] = { 0, 1, 2, 3, 4, 5, 6, 7, 8};
char charmap[72] = {
/* 0 1 2 3 4 5 6 7 8 */
/* 0 */ 'z', 'a', 'q', 'o', '1', '9', ' ', KEY_F1, KEY_LEFT_SHIFT,
/* 1 */ 'x', 's', 'w', 'p', '2', '0', KEY_BACKSPACE, KEY_F2, KEY_LEFT_CTRL,
/* 2 */ 'c', 'd', 'e', ']', '3', '-', ' ', KEY_F3, ' ',
/* 3 */ 'v', 'f', 'r', ';', '4', '=', KEY_ESC, KEY_F4, ' ',
/* 4 */ 'b', 'g', 't', '\'', '5', KEY_DOWN_ARROW, ' ', KEY_F5, ' ',
/* 5 */ 'n', 'h', 'y', ',', '6', KEY_UP_ARROW, ' ', KEY_F6, ' ',
/* 6 */ 'm', 'j', 'u', '.', '7', KEY_LEFT_ARROW, ' ', KEY_F7, ' ',
/* 7 */ 'l', 'k', 'i', '/', '8', KEY_RIGHT_ARROW, KEY_RETURN, KEY_F8, ' ',
};
void timerHandler(void)
{
static int last_keys[num_rows * num_cols] = { 0 };
static int next_keys[num_rows * num_cols] = { 0 };
for (int c = 0; c < num_cols; c++)
{
digitalWrite(cols[c], LOW);
for (int r = 0; r < num_rows; r++)
{
if (digitalRead(rows[r]) == LOW)
{
int idx = r * num_cols + c;
next_keys[idx] = 1;
Serial.print(r);
Serial.print(",");
Serial.print(c);
Serial.print("\n");
}
}
digitalWrite(cols[c], HIGH);
}
for (int i = 0; i < num_rows * num_cols; i++)
{
char key = charmap[i];
// press if key is held down for two frames
if (next_keys[i] && last_keys[i])
{
Keyboard.press(key);
}
// release
else if (!next_keys[i] && last_keys[i])
{
Keyboard.release(key);
}
last_keys[i] = next_keys[i];
next_keys[i] = 0;
}
}
void setup()
{
// initialize keyboard library
Keyboard.begin();
// pull all columns high
for (int i = 0; i < num_cols; i++)
{
pinMode(cols[i], OUTPUT);
digitalWrite(cols[i], HIGH);
}
// enable internal pull-up for all rows
for (int i = 0; i < num_rows; i++)
{
pinMode(rows[i], INPUT_PULLUP);
}
ITimer3.init();
ITimer3.attachInterruptInterval(TIMER_INTERVAL_MS, timerHandler);
}
void loop()
{
}