-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathantiAFK_firmware.ino
287 lines (269 loc) · 8.5 KB
/
antiAFK_firmware.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* antiAFK_firmware.ino:
**
* © 2013-2014 Steven Casagrande (scasagrande@galvant.ca).
*
* This file is a part of the InstrumentKit project.
* Licensed under the AGPL version 3.
**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**/
#include "TimerOne.h"
#include <EEPROM.h>
#include "EEPROMAnything.h"
#include <Entropy.h>
const byte EEPROM_CODE = 0xBD;
const byte FIRMWARE_VERSION = 1;
const byte EEPROM_CODE_ADDRESS = 0x00;
const byte EEPROM_VERSION_ADDRESS = EEPROM_CODE_ADDRESS + 1; // 0x01
const byte EEPROM_PERIOD_ADDRESS = EEPROM_VERSION_ADDRESS + 1; // 0x02
const byte EEPROM_VARIANCE_ADDRESS = EEPROM_PERIOD_ADDRESS + 4; // 0x06
const byte EEPROM_VALID_KEYS_LENGTH_ADDRESS = EEPROM_VARIANCE_ADDRESS + 4; // 0xA0
const byte EEPROM_VALID_KEYS_ADDRESS = EEPROM_VALID_KEYS_LENGTH_ADDRESS + 1; // 0xA1
#define buttonPin 4
unsigned long period = 480000; // Delay between keyboard events in ms (default 8min)
unsigned long variance = 300000; // Maximum variance of period in ms (default 5min)
unsigned long duration = 20;
String valid_keys = "wasd ";
int valid_keys_length = valid_keys.length();
unsigned long nextKeyPress = period;
unsigned long counter = 0;
int prevButtonState = HIGH;
boolean running = false;
boolean debug = false;
boolean keyboard_enable = true;
byte eepromValue = 0;
String incomingCmd = "";
char nextKey = 0x00;
void setup() {
Keyboard.begin();
Serial.begin(9600);
if (debug) {
while(!Serial);
}
Entropy.Initialize();
pinMode(buttonPin, INPUT);
//Check EEPROM for stored settings
eepromValue = EEPROM.read(EEPROM_CODE_ADDRESS);
if (eepromValue == EEPROM_CODE) {
EEPROM_readAnything(EEPROM_PERIOD_ADDRESS, period);
EEPROM_readAnything(EEPROM_VARIANCE_ADDRESS, variance);
valid_keys_length = EEPROM.read(EEPROM_VALID_KEYS_LENGTH_ADDRESS);
valid_keys = "";
for (int i = 0; i < valid_keys_length; i++) {
valid_keys += char(EEPROM.read(EEPROM_VALID_KEYS_ADDRESS + i));
}
if (debug) {
Serial.println("Valid EEPROM code.");
Serial.print("Period: ");
Serial.println(period);
Serial.print("Variance: ");
Serial.println(variance);
}
}
else { // EEPROM not valid, so initialize it
EEPROM.write(EEPROM_CODE_ADDRESS, EEPROM_CODE);
EEPROM.write(EEPROM_VERSION_ADDRESS, FIRMWARE_VERSION);
EEPROM_writeAnything(EEPROM_PERIOD_ADDRESS, period);
EEPROM_writeAnything(EEPROM_VARIANCE_ADDRESS, variance);
valid_keys_length = valid_keys.length();
EEPROM.write(EEPROM_VALID_KEYS_LENGTH_ADDRESS, valid_keys_length);
for (int i = 0; i < valid_keys_length; i++) {
EEPROM.write(EEPROM_VALID_KEYS_ADDRESS + i, valid_keys[i]);
}
if (debug) {
Serial.println("Not valid EEPROM code.");
}
}
Timer1.initialize(1000); // Starts timer with 1000us interupt
Timer1.attachInterrupt(callback);
}
void callback() {
if (running == true) {
counter++;
if (debug) {
if (counter % 1000 == 0) {
Serial.print("Counter: ");
Serial.println(counter);
}
}
if (counter >= nextKeyPress) {
// Press the key
nextKey = valid_keys[random(valid_keys.length())];
if (debug) {
Serial.print("Key press event after ");
Serial.print(counter);
Serial.println(" milliseconds.");
Serial.print("Key pressed: ");
if (nextKey == 32) {
Serial.println("{space}");
} else {
Serial.println(nextKey);
}
}
if (keyboard_enable) {
Keyboard.press(nextKey);
delay(duration);
Keyboard.releaseAll();
}
// First, double check that variance is less than period
// if not, load from EEPROM
if (variance > period) {
variance = EEPROM.read(EEPROM_VARIANCE_ADDRESS);
}
// Generate next key press time
generateNextKeyPress();
}
}
}
void generateNextKeyPress() {
/*
* This method is used to determine the time until
* the next key press event will occur.
*/
if (Entropy.random(0,2) == 0){
nextKeyPress = period + Entropy.random(0, variance + 1);
} else {
nextKeyPress = period - Entropy.random(0, variance + 1);
}
duration = 20 + Entropy.random(0,512);
if (debug) {
Serial.print("Next key press set to: ");
Serial.println(nextKeyPress);
Serial.print("Duration will be: ");
Serial.println(duration);
}
counter = 0;
}
void toggleRunningState() {
/*
* Used to swap between running and standby modes of operation
* This is called when the "toggle" string is send from the attached
* PC, or the physical button is pushed.
*/
running = !running;
if (debug) {
Serial.print("Running state switched to: ");
Serial.println(running);
}
if (running) {
counter = nextKeyPress; // Gives quick feedback that device is running
}
else {
counter = 0;
}
}
String readLine() {
String value = "";
for (int i = 0; Serial.available() > 0; i++) {
value += (char)Serial.read();
delay(5);
}
return value;
}
void loop() {
// Get and parse messages from the attached PC
if (Serial.available() > 0) {
incomingCmd = readLine();
//period:N
if (incomingCmd.substring(0,7).equalsIgnoreCase("period:")) {
period = incomingCmd.substring(7,incomingCmd.length()).toInt();
EEPROM_writeAnything(EEPROM_PERIOD_ADDRESS, period);
generateNextKeyPress();
if (debug) {
Serial.print("Period set to: ");
Serial.println(period);
}
}
//period
else if (incomingCmd.substring(0,6).equalsIgnoreCase("period")) {
if (debug) {
Serial.print("Period set to: ");
Serial.println(period);
}
}
//variance:N
else if (incomingCmd.substring(0,9).equalsIgnoreCase("variance:")) {
variance = incomingCmd.substring(9,incomingCmd.length()).toInt();
if ((variance > period) || (variance < 0)){
if (debug) {
Serial.println("Variance invalid, loading from EEPROM.");
}
EEPROM_readAnything(EEPROM_VARIANCE_ADDRESS, variance);
}
else {
EEPROM_writeAnything(EEPROM_VARIANCE_ADDRESS, variance);
generateNextKeyPress();
}
if (debug) {
Serial.print("Variance set to: ");
Serial.println(variance);
}
}
//variance
else if (incomingCmd.substring(0,8).equalsIgnoreCase("variance")) {
if (debug) {
Serial.print("Variance set to: ");
Serial.println(variance);
}
}
//toggle
else if (incomingCmd.substring(0,6).equalsIgnoreCase("toggle")) {
toggleRunningState();
}
//keys:{set}
else if (incomingCmd.substring(0,5).equalsIgnoreCase("keys:")) {
if (incomingCmd.length() == 5) {
if (debug) {
Serial.println("Must specify at least 1 key.");
}
}
else {
valid_keys = incomingCmd.substring(5,incomingCmd.length());
valid_keys_length = valid_keys.length();
EEPROM.write(EEPROM_VALID_KEYS_LENGTH_ADDRESS, valid_keys_length);
for (int i = 0; i < valid_keys_length; i++) {
EEPROM.write(EEPROM_VALID_KEYS_ADDRESS + i, valid_keys[i]);
}
}
}
//debug
else if (incomingCmd.substring(0,5).equalsIgnoreCase("debug")) {
debug = !debug;
if (debug) {
Serial.println("Debug messages now enabled.");
}
}
//keyboard
else if (incomingCmd.substring(0,8).equalsIgnoreCase("keyboard")) {
keyboard_enable = !keyboard_enable;
if ((keyboard_enable) && (debug)) {
Serial.println("Keyboard output enabled");
}
else if (debug) {
Serial.println("Keyboard output disabled");
}
}
}
int buttonState = digitalRead(buttonPin);
// Button is active low
if ((buttonState != prevButtonState) && (buttonState == LOW)) {
delay(100); // protect against switch debounce
toggleRunningState();
}
else if ((buttonState != prevButtonState) && (buttonState == HIGH)) {
delay(100); // protect against switch debounce
}
prevButtonState = buttonState;
}