forked from dalelyunas/ArduinoMouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoMouse.ino
More file actions
452 lines (375 loc) · 10.9 KB
/
ArduinoMouse.ino
File metadata and controls
452 lines (375 loc) · 10.9 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include <SPI.h>
/*
ArduinoMouse
Uses mouse input data to perform several functions
created 25 Jun 2013
by David Alelyunas
*/
// Require mouse control library
#include <MouseController.h>
#include <ads7843.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <tftlib.h> // Hardware-specific library
#define PSTR(a) a
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
/** ADS7843 pin map */
#ifdef ELECFREAKS_TFT_SHIELD_V2
#define DCLK 6
#define CS 5
#define DIN 4
#define DOUT 3
#define IRQ 2
#elif defined ELECHOUSE_DUE_TFT_SHIELD_V1
/** elechouse TFT shield pin map */
#define DCLK 25
#define CS 26
#define DIN 27
#define DOUT 29
#define IRQ 30
#endif
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
TFTLCD tft;
ADS7843 touch(CS, DCLK, DIN, DOUT, IRQ);
// Initialize USB Controller
USBHost usb;
Point p;
// Attach mouse controller to USB
MouseController mouse(usb);
//variables to control the keyboard
int enterValue = 0;
int prevEnterValue = 0;
boolean pressed = true;
//values for total x traveled and total y traveled
int totalX;
int totalY;
float totalMM;
float prevMM;
//distance from the sensor to the front of the smart template
float SYSTEM_DEPTH = 55.00;
//angle values
int totalDegrees = 0;
int prevDegrees;
float degreeCorrelation = 2.65;
//desired value for the leds to all be lit at -set to zero to enable input
float maxValue = 0;
//time keeping values
float startTime = 0;
float currentTime = 0;
float prevTime;
boolean startRecorded = false;
//other variables
float lastPercent;
boolean toggle = true;
// This function intercepts mouse movements
void mouseMoved() {
if(!startRecorded){
startTime = millis();
currentTime = (millis() - startTime)/1000;
startRecorded = true;
}
totalX += mouse.getXChange();
if(totalX > 360/degreeCorrelation){
totalX = 0;
}
else if(totalX < 0){
totalX = 360/degreeCorrelation;
}
totalDegrees = totalX * degreeCorrelation;
//Serial.println(totalX);
//Serial.print(", ");
totalY += mouse.getYChange();
totalMM = (-1 *(totalY/49.118)-SYSTEM_DEPTH);
Serial.println(totalMM);
}
//Checks to see which leds should light up based on a desired distance value
void checkLEDS() {
if((totalMM) < maxValue * 4/5) {
digitalWrite(4, HIGH);
}
else{
digitalWrite(4, LOW);
}
if((totalMM) >= (maxValue * 4/5) && (totalMM) < maxValue){
digitalWrite(3, HIGH);
}
else{
digitalWrite(3, LOW);
}
if((totalMM) >= maxValue){
digitalWrite(2, HIGH);
}
else{
digitalWrite(2, LOW);
}
}
void lcdDisplay(){
//writes the totalY value to the LCD screen
tft.setCursor(130,30);
tft.setTextColor(BLACK);
tft.print(prevMM);
tft.setCursor(130 ,30 );
tft.setTextColor(GREEN);
tft.print(totalMM);
//writes the degrees to the LCD screen
tft.setCursor(120, 120);
tft.setTextColor(BLACK);
tft.print(prevDegrees);
tft.setCursor(120,120);
tft.setTextColor(GREEN);
tft.print(totalDegrees);
//writes the time elapsed since the start of the program in seconds
if(startRecorded){
tft.setCursor(80,60);
tft.setTextColor(BLACK);
tft.print(prevTime);
tft.setCursor(80,60);
tft.setTextColor(GREEN);
tft.print(currentTime);
}
}
//displays a rectangle that displays progress towards the maxValue in 200ths
void displayProgress(){
float percent = (totalMM)/float(maxValue);
if(percent>0){
if(percent < 1){
tft.fillRect(20+ lastPercent*200,180,201 - lastPercent*200,20,BLACK);
tft.fillRect(20,180,200*percent,20,GREEN);
}
else if (percent > 1.00){
tft.setTextColor(RED);
tft.fillRect(20,180,200,20,RED);
}
lastPercent = percent;
}
//draws a line to represent the current angle of the needle
tft.drawLine(120,300, cos(prevDegrees * (PI/180)) * 80+120, sin(prevDegrees* (PI/180))*80+300, BLACK);
tft.drawLine(120,300, cos(totalDegrees* (PI/180)) * 80+120, sin(totalDegrees* (PI/180))*80+300, GREEN);
}
//displays the keyboard
void displayTouchKeyboard(){
int x = 20;
int y = 220;
int number = 0;
int side = 30;
tft.setCursor(20, 150);
tft.print("Enter Max Value:");
for(int i = 0;i<2;i++){
for(int z = 0; z<5;z++){
tft.drawRect(x+40*z,y+40*i,side,side,GREEN);
tft.setCursor(x+40*z+10,y+40*i+10);
tft.print(number);
number++;
}
}
tft.drawRect(x,y+80,90,side,GREEN);
tft.drawRect(x+100,y+80,90,side,GREEN);
tft.setCursor(x+10, y+90);
tft.print("Enter");
tft.setCursor(x+110, y+90);
tft.print("Back");
}
//determines when keys are pressed and what to do when a key is pressed
void interpretKeys(){
String temp = "";
temp += enterValue;
if(temp.length()<7){
if(((p.x-310)/14 < 42 && (p.x-310)/14 > 16) && ((p.y-150)/9> 229 && (p.y-150)/9< 253)){
if(pressed){
enterValue = enterValue*10 + 0;
pressed = false;
}
}
else if(((p.x-310)/14 < 83 && (p.x-310)/14 > 58) && ((p.y-150)/9> 229 && (p.y-150)/9< 253)){
if(pressed){
enterValue = enterValue*10 + 1;
pressed = false;
}
}
else if(((p.x-310)/14 < 127 && (p.x-310)/14 > 98) && ((p.y-150)/9> 229 && (p.y-150)/9< 253)){
if(pressed){
enterValue = enterValue*10 + 2;
pressed = false;
}
}
else if(((p.x-310)/14 < 171 && (p.x-310)/14 > 145) && ((p.y-150)/9> 229 && (p.y-150)/9< 253)){
if(pressed){
enterValue = enterValue*10 + 3;
pressed = false;
}
}
else if(((p.x-310)/14 < 212 && (p.x-310)/14 > 185) && ((p.y-150)/9> 229 && (p.y-150)/9< 253)){
if(pressed){
enterValue = enterValue*10 + 4;
pressed = false;
}
}
else if(((p.x-310)/14 < 42 && (p.x-310)/14 > 16) && ((p.y-150)/9> 270 && (p.y-150)/9< 294)){
if(pressed){
enterValue = enterValue*10 + 5;
pressed = false;
}
}
else if(((p.x-310)/14 < 83 && (p.x-310)/14 > 58) && ((p.y-150)/9> 270 && (p.y-150)/9< 294)){
if(pressed){
enterValue = enterValue*10 + 6;
pressed = false;
}
}
else if(((p.x-310)/14 < 127 && (p.x-310)/14 > 98) && ((p.y-150)/9> 270 && (p.y-150)/9< 294)){
if(pressed){
enterValue = enterValue*10 + 7;
pressed = false;
}
}
else if(((p.x-310)/14 < 171 && (p.x-310)/14 > 145) && ((p.y-150)/9> 270 && (p.y-150)/9< 294)){
if(pressed){
enterValue = enterValue*10 + 8;
pressed = false;
}
}
else if(((p.x-310)/14 < 212 && (p.x-310)/14 > 185) && ((p.y-150)/9> 270 && (p.y-150)/9< 294)){
if(pressed){
enterValue = enterValue*10 + 9;
pressed = false;
}
}
}
if(((p.x-310)/14 < 212 && (p.x-310)/14 > 123) && ((p.y-150)/9> 310 && (p.y-150)/9< 338)){
if(pressed){
enterValue = enterValue/10;
pressed = false;
}
}
if(((p.x-310)/14 < 108 && (p.x-310)/14 > 16) && ((p.y-150)/9> 310 && (p.y-150)/9< 338)){
if(pressed){
maxValue = enterValue;
pressed = false;
}
}
if(((p.x-310)/14 == -22) && ((p.y-150)/9 == -16)){
pressed = true;
}
}
//determines whether the screen is set to manual input or waits for external input
void determineToggle(){
if(((p.x-310)/14 < 106 && (p.x-310)/14 > 15) && ((p.y-150)/9> 371 && (p.y-150)/9< 397)){
if(pressed){
toggle = !toggle;
tft.fillScreen(BLACK);
pressed = false;
}
}
if(((p.x-310)/14 == -22) && ((p.y-150)/9 == -16)){
pressed = true;
}
}
void drawDegreeDisplay(){
tft.setCursor(225, 293);
tft.print("0");
tft.drawLine(200,300,215,300,GREEN);
tft.drawLine(120,380,120,395,GREEN);
tft.drawLine(40,300,25,300,GREEN);
tft.drawLine(120,220,120,205,GREEN);
tft.drawLine(cos(PI/6) * 80 + 120, sin(PI/6) * 80 + 300, cos(PI/6) * 95 + 120, sin(PI/6) * 95 + 300,GREEN);
tft.drawLine(cos(PI/3) * 80 + 120, sin(PI/3) * 80 + 300, cos(PI/3) * 95 + 120, sin(PI/3) * 95 + 300,GREEN);
tft.drawLine(cos(5*PI/6) * 80 + 120, sin(5*PI/6) * 80 + 300, cos(5*PI/6) * 95 + 120, sin(5*PI/6) * 95 + 300,GREEN);
tft.drawLine(cos(5*PI/3) * 80 + 120, sin(5*PI/3) * 80 + 300, cos(5*PI/3) * 95 + 120, sin(5*PI/3) * 95 + 300,GREEN);
tft.drawLine(cos(7*PI/6) * 80 + 120, sin(7*PI/6) * 80 + 300, cos(7*PI/6) * 95 + 120, sin(7*PI/6) * 95 + 300,GREEN);
tft.drawLine(cos(2*PI/3) * 80 + 120, sin(2*PI/3) * 80 + 300, cos(2*PI/3) * 95 + 120, sin(2*PI/3) * 95 + 300,GREEN);
tft.drawLine(cos(11*PI/6) * 80 + 120, sin(11*PI/6) * 80 + 300, cos(11*PI/6) * 95 + 120, sin(11*PI/6) * 95 + 300,GREEN);
tft.drawLine(cos(4*PI/3) * 80 + 120, sin(4*PI/3) * 80 + 300, cos(4*PI/3) * 95 + 120, sin(4*PI/3) * 95 + 300,GREEN);
}
void setup()
{
Serial.begin(115200);
tft.begin();
touch.begin();
//Serial.setTimeout(50);
Serial.println();
Serial.println();
Serial.println("Program started");
totalX = 0;
totalY = 0;
totalMM = 0;
tft.setTextColor(GREEN);
tft.setTextSize(2);
//reads a float from the keyboard interface or waits for input
Serial.println("Enter Max Value:");
while(maxValue == 0){
uint8_t flag;
p = touch.getpos(&flag);
if(toggle){
tft.drawRect(20,360, 90,30,GREEN);
tft.setCursor(30,370);
tft.print("Toggle");
tft.setCursor(20, 40);
tft.print("Waiting for input");
if(Serial.available()){
maxValue = Serial.parseFloat();
}
}
else{
tft.drawRect(20,360, 90,30,GREEN);
tft.setCursor(30,370);
tft.print("Toggle");
displayTouchKeyboard();
interpretKeys();
if(enterValue != prevEnterValue){
tft.fillScreen(BLACK);
}
tft.setCursor(20,180);
tft.print(enterValue);
prevEnterValue = enterValue;
}
determineToggle();
}
tft.fillScreen(BLACK);
Serial.print("Maximum Value: ");
Serial.println(maxValue);
Serial.println();
drawDegreeDisplay();
tft.setCursor(20, 90);
tft.print("EndDist: ");
tft.setCursor(120,90);
tft.print(maxValue);
tft.setCursor(20, 120);
tft.print("Degrees: ");
tft.setCursor(20,30);
tft.print("Distance: ");
tft.setCursor(20,60);
tft.print("Time: ");
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4,OUTPUT);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
}
void loop()
{
currentTime = (millis() - startTime)/1000;
//Process USB tasks
usb.Task();
uhd_set_vbof_active_high();
lcdDisplay();
displayProgress();
checkLEDS();
prevMM = totalMM;
prevTime = currentTime;
prevDegrees = totalDegrees;
}