-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuno-macro-device.ino
More file actions
151 lines (139 loc) · 3.19 KB
/
uno-macro-device.ino
File metadata and controls
151 lines (139 loc) · 3.19 KB
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
150
151
/*
* Arduino UNO Macro Device
* Extended for use when creating macros with autohotkey
* Source: http://mitchtech.net/arduino-usb-hid-keyboard/
* Source: http://www.freebsddiary.org/APC/usb_hid_usages.php
*/
#define KEY_LEFT_CTRL 0x01
#define KEY_LEFT_SHIFT 0x02
#define KEY_RIGHT_CTRL 0x10
#define KEY_RIGHT_SHIFT 0x20
#define KEY_LEFT_GUI 0x83
#define KEY_RIGHT_GUI 0x87
#define KEY_NUMPAD_DEL 0x63
#define KEY_NUMPAD_PLU 0x57
#define KEY_NUMPAD_MIN 0x56
#define KEY_NUMPAD_0 0x62
#define KEY_NUMPAD_1 0x59
#define KEY_NUMPAD_2 0x5A
#define KEY_NUMPAD_3 0x5B
#define KEY_NUMPAD_4 0x5C
#define KEY_NUMPAD_5 0x5D
#define KEY_NUMPAD_6 0x5E
#define KEY_NUMPAD_7 0x5F
#define KEY_NUMPAD_8 0x60
#define KEY_NUMPAD_9 0x61
uint8_t buf[8] = {
0 }; /* Keyboard report buffer */
#define PIN_CONTROL 6
#define PIN_M1 5
#define PIN_M2 4
#define PIN_M3 3
#define PIN_M4 2
int state = 1;
int controlMode = 0;
void setup()
{
Serial.begin(9600);
pinMode(PIN_CONTROL, INPUT_PULLUP);
pinMode(PIN_M1, INPUT_PULLUP);
pinMode(PIN_M2, INPUT_PULLUP);
pinMode(PIN_M3, INPUT_PULLUP);
pinMode(PIN_M4, INPUT_PULLUP);
delay(200);
}
void loop()
{
state = digitalRead(PIN_CONTROL);
if (state != 1) {
buf[0] = KEY_LEFT_CTRL; // Ctrl
buf[2] = KEY_NUMPAD_0; // Letter X
// buf[2] = 123; // Cut key: Less portable
Serial.write(buf, 8); // Ssend keypress
releaseKey();
controlMode = (controlMode + 1) % 3;
}
state = digitalRead(PIN_M1);
if (state != 1) {
buf[0] = KEY_LEFT_CTRL; // Ctrl
switch (controlMode) {
case 0:
buf[2] = KEY_NUMPAD_1;
break;
case 1:
buf[2] = KEY_NUMPAD_4;
break;
case 2:
buf[2] = KEY_NUMPAD_7;
break;
default:
buf[2] = KEY_NUMPAD_1;
}
Serial.write(buf, 8); // Send keypress
releaseKey();
}
state = digitalRead(PIN_M2);
if (state != 1) {
buf[0] = KEY_LEFT_CTRL; // Ctrl
switch (controlMode) {
case 0:
buf[2] = KEY_NUMPAD_2;
break;
case 1:
buf[2] = KEY_NUMPAD_5;
break;
case 2:
buf[2] = KEY_NUMPAD_8;
break;
default:
buf[2] = KEY_NUMPAD_2;
}
Serial.write(buf, 8); // Send keypress
releaseKey();
}
state = digitalRead(PIN_M3);
if (state != 1) {
buf[0] = KEY_LEFT_CTRL; // Ctrl
switch (controlMode) {
case 0:
buf[2] = KEY_NUMPAD_3;
break;
case 1:
buf[2] = KEY_NUMPAD_6;
break;
case 2:
buf[2] = KEY_NUMPAD_9;
break;
default:
buf[2] = KEY_NUMPAD_3;
}
Serial.write(buf, 8); // Send keypress
releaseKey();
}
state = digitalRead(PIN_M4);
if (state != 1) {
buf[0] = KEY_LEFT_CTRL; // Ctrl
switch (controlMode) {
case 0:
buf[2] = KEY_NUMPAD_DEL;
break;
case 1:
buf[2] = KEY_NUMPAD_PLU;
break;
case 2:
buf[2] = KEY_NUMPAD_MIN;
break;
default:
buf[2] = KEY_NUMPAD_DEL;
}
Serial.write(buf, 8); // Send keypress
releaseKey();
}
}
void releaseKey()
{
buf[0] = 0;
buf[2] = 0;
Serial.write(buf, 8); // Release key
delay(500);
}