-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
570 lines (422 loc) · 14.1 KB
/
main.cpp
File metadata and controls
570 lines (422 loc) · 14.1 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include "mbed.h"
#include <RawSerial.h>
//#include <math.h>
//******************************************************************
// All variables defined below
//******************************************************************
//communication
DigitalOut myled(LED1);
RawSerial pc(USBTX, USBRX);
RawSerial keyOut(p13, p14);
I2C camera1(p9, p10);
//initial camera data
int IRsensorAddress = 0xB0;
int slaveAddress;
char data_buf[16];
char s;
int i;
//point variables
short point1x = 0;
short point1y = 0;
short point2x = 0;
short point2y = 0;
short point3x = 0;
short point3y = 0;
short point4x = 0;
short point4y = 0;
//sensitivity
//Level 5: p0 = 0x96, p1 = 0xFE, p2 = 0xFE, p3 = 0x05
//highest sensitivity to more accurately detect points
int sen0 = 0x96;
int sen1 = 0xFE;
int sen2 = 0xFE;
int sen3 = 0x00;
//previous point values
short prevX = 1023;
short prevY = 1023;
//matrices of x and y coordinates from the first camera
short onex[4];
short oney[4];
//matrices of x and y coordinates from prev point
short prevx[4];
short prevy[4];
//movement
const short deadzone = 1;
const float mouseMoveMult = 1; //1 for accumulation, 3 for no accum
const float mouseMovePwr = 1.2; //was 1.2
const short MOVEMENT_CAP = 10; //working on the computer with 20
const short VALUES_TO_TOSS = 6;
short tossedValuesCounter = VALUES_TO_TOSS;
//click state
const short CLICK_DEAD_ZONE = 50;
short clickBaseX;
short clickBaseY;
short clickDurCount = 0;
bool readingClick = false;
short minLeftClickDur = 10;
short maxLeftClickDur = 50;
//MOUSE STATE
//implemented for ticker behavior
//ticker depends on these values to update the state/location of the mouse
Ticker mouseStateTicker;
short updatex[4];
short updatey[4];
bool toLeftClick = false;
bool toRightClick = false;
//READING FROM CAMERA VIA INTERRUPT
Ticker cameraReadTicker;
//LED
DigitalOut myled2(LED2);
//******************************************************************
// All methods defined below
//******************************************************************
//takes in values for the movement in the x and y direction
//also can indicate whether you want to "click"
//NOTE: hard coded wait of 0.1
void mouseCommand(char buttons, short x, short y) {
//x = rint((x > 0) ? powf(mouseMoveMult*( (float) x) , mouseMovePwr) : -powf(-mouseMoveMult*( (float) x) , mouseMovePwr));
// y = rint((y > 0) ? powf(mouseMoveMult*( (float) y) , mouseMovePwr) : -powf(-mouseMoveMult*( (float) y) , mouseMovePwr));
x = mouseMoveMult * ((x > 0) ? powf(( (float) x) , mouseMovePwr) : -powf(-( (float) x) , mouseMovePwr));
y = mouseMoveMult * ((y > 0) ? powf(( (float) y) , mouseMovePwr) : -powf(-( (float) y) , mouseMovePwr));
if(x>255){
x = 255;
} else if(x<-255){
x = -255;
}
if(y>255){
y = 255;
} else if (x<-255){
y = -255;
}
// pc.printf("%hd ", x);
// pc.printf("%hd\n", y);
// x = (x > 0) ? powf(( (float) x) , mouseMovePwr) : -powf(-( (float) x) , mouseMovePwr);
// y = (y > 0) ? powf(( (float) y) , mouseMovePwr) : -powf(-( (float) y) , mouseMovePwr);
//x = mouseMoveMult*x;
//y = mouseMoveMult*y;
//x = x*abs(x);
//y = y*abs(y);
//x = x*sqrt((float)abs(x));
//y = y*sqrt((float)abs(y));
keyOut.putc(0xFD);
keyOut.putc(0x00);
keyOut.putc(0x03);
keyOut.putc(buttons);
keyOut.putc(x);
keyOut.putc(y);
keyOut.putc(0x00);
keyOut.putc(0x00);
keyOut.putc(0x00);
//delay for pushing data
//wait(0.1); //how large does this need to be?
}
//the interrupt to update mouse state
//run every 100 us
void updateMouseState(){
myled2 = 1 - myled2;
//move mouse
//handles only single finger actions
mouseCommand(0, updatex[0], updatey[0]);
//clear out changes
updatex[0] = 0;
updatey[0] = 0;
//click
if(toLeftClick){
//send command to
//mouseCommand(0, clickBaseX - onex[0] , clickBaseY - oney[0]);
mouseCommand(0x01, 0, 0);
}
//TODO: right click
//else if (toRightClick){
// mouseCommand(0x02, 0 , 0);
// }
//fip clicking to false
toLeftClick = false;
toRightClick = false;
}
//moves mouse on screen from one finger input
//param
// current point (currx, curry)
// previous point (prevx, prevy)
//TODO: implement additional param to indicate which finger you are looking at
// current implementation defaults to zero (finger one)
void oneFingerResponse(short currx, short curry, short prevx, short prevy){
//look at delta btwn prev val and current
//TODO: moving average
if((prevx != 1023 || prevy != 1023) && (currx != 1023 && curry != 1023)){
short diffX = currx - prevx;
short diffY = -1*(curry - prevy);
//fix diffX
if(diffX < -MOVEMENT_CAP) {
diffX = -MOVEMENT_CAP;
} else if(diffX > MOVEMENT_CAP){
diffX = MOVEMENT_CAP;
} else if(diffX > deadzone){
diffX -= deadzone;
} else if (diffX < -1*deadzone){
diffX += deadzone;
} else{
diffX = 0;
}
//fix diffY
if(diffY < -MOVEMENT_CAP) {
diffY = -MOVEMENT_CAP;
} else if (diffY > MOVEMENT_CAP){
diffY = MOVEMENT_CAP;
} else if(diffY > deadzone){
diffY -= deadzone;
} else if (diffY < -1*deadzone){
diffY += deadzone;
} else{
diffY = 0;
}
////fix diffX
// if(abs(diffX) > MOVEMENT_CAP) {
// diffX = 0;
// } else if(diffX > deadzone){
// diffX -= deadzone;
// } else if (diffX < -1*deadzone){
// diffX += deadzone;
// } else{
// diffX = 0;
// }
// //fix diffY
// if(abs(diffY) > MOVEMENT_CAP) {
// diffY = 0;
// } else if(diffY > deadzone){
// diffY -= deadzone;
// } else if (diffY < -1*deadzone){
// diffY += deadzone;
// } else{
// diffY = 0;
// }
//mouseCommand(0, (char) diffX, (char) diffY);
//TODO: this is defaulting to first finger - need to fix this
//update target position to move x and y
//accumulates the diff until
//updatex[0] = updatex[0] + diffX;
// updatey[0] = updatey[0] + diffY;
updatex[0] += diffX;
updatey[0] += diffY;
// pc.printf("updating x to : %d", diffX);
// pc.printf("\t updating y to : %d \n", diffY);
// pc.printf("updating x to : %d", updatex[0]);
// pc.printf("\t updating y to : %d \n", updatex[0]);
}
}
//writes two bytes to the camera
void write2bytes(char data1, char data2){
char out[2];
out[0] = data1;
out[1] = data2;
camera1.write(slaveAddress, out, 2);
wait(0.01);
}
// Initialize WiiMote Camera
void initCamera(void){
write2bytes(0x30, 0x01);
write2bytes(0x00, 0x02);
write2bytes(0x00, 0x00);
write2bytes(0x71, 0x01);
write2bytes(0x07, 0x00);
write2bytes(sen1, 0x1A);
write2bytes(sen2, sen3);
write2bytes(0x33, 0x03);
write2bytes(0x30, 0x08);
//wait(0.1);
}
//update counts for click
void updateClickState(short currx, short curry, short prevx, short prevy){
bool xStable = false;
bool yStable = false;
if(currx != 1023 && curry != 1023 && readingClick){
//finger is on surface and you are reading click
//test stability
//check x stability
if (currx == clickBaseX){
//no movement in x direction
xStable = true;
} else if( abs(currx - clickBaseX) < CLICK_DEAD_ZONE){
//barely moved in x direction
xStable = true;
}
//check y stability
if( curry == clickBaseY){
//no movement in y direction
yStable = true;
} else if ( abs(curry - clickBaseY) < CLICK_DEAD_ZONE){
//barely moved in y direction
yStable = true;
}
//if stable, increment count
if(xStable && yStable){
clickDurCount = clickDurCount + 1;
} else{
//if not stable, no longer reading click, counter to zero
readingClick = false;
clickDurCount = 0;
}
} // rising edge
else if (currx != 1023 && curry != 1023 && prevx == 1023 && prevy == 1023 ){
//finger has been placed on surface
//tossedValuesCounter = VALUES_TO_TOSS;
//set reading click to true
readingClick = true;
//save initial location
clickBaseX = currx;
clickBaseY = curry;
//} else if (currx == 1023 && curry == 1023 && readingClick){
} else if (currx == 1023 && curry == 1023 && prevx == 1023 && prevy == 1023 && readingClick){
//stable click and finger was removed
//if within bounds, you want to click
if(clickDurCount > minLeftClickDur && clickDurCount < maxLeftClickDur){
//set state to indicate left click
toLeftClick = true;
// pc.printf("********LEFT mouse click \n");
//toss out accumulations of diffs
updatex[0] = 0;
updatey[0] = 0;
}
//no longer reading click
readingClick = false;
//reset counter
clickDurCount = 0;
}
}
//get data from camera one
//populates onex and oney with values depending on the measured points
//NOTE: 1023 means nothing was detected
void readCameraData(void){
//update previous values
//only updates for finger 1
prevX = onex[0];
prevY = oney[0];
//request data from camera
char out[1];
out[0] = 0x36;
camera1.write(slaveAddress, out, 1);
//wait(0.2); //do we need this?
//get data from camera
camera1.read(slaveAddress, data_buf, 16);
//POINT 1
//get data
point1x = data_buf[1];
point1y = data_buf[2];
s = data_buf[3];
//load x,y
onex[0] = point1x + ((s & 0x30) << 4);
oney[0] = point1y + ((s & 0xC0) << 2);
//>>>>>>>>>>>>>>>>>Begin unfinished code for moving
//if(tossedValuesCounter > 0){
// tossedValuesCounter -= 1;
// }else{
// oneFingerResponse(onex[0], oney[0], prevX, prevY);
// }
if(!readingClick){
oneFingerResponse(onex[0], oney[0], prevX, prevY);
}
updateClickState(onex[0], oney[0], prevX, prevY);
// //update prev values
// prevX = onex[0];
// prevY = oney[0];
//<<<<<<<<<<<<<<<<End unfinished code for moving averages
//>>>>>>>>>>>>>>>>Begin unused parsing for multiple fingers
// //POINT 2
// //get data
// point2x = data_buf[4];
// point2y = data_buf[5];
// s = data_buf[6];
// //load x,y
// onex[1] = point2x + ((s & 0x30) << 4);
// oney[1] = point2y + ((s & 0xC0) << 2);
//
// //POINT 3
// //get data
// point3x = data_buf[7];
// point3y = data_buf[8];
// s = data_buf[9];
// //load x,y
// onex[2] = point3x + ((s & 0x30) << 4);
// oney[2] = point3y + ((s & 0xC0) << 2);
//
// //POINT 4
// //get data
// point4x = data_buf[10];
// point4y = data_buf[11];
// s = data_buf[12];
// //load x,y
// onex[3] = point4x + ((s & 0x30) << 4);
// oney[3] = point4y + ((s & 0xC0) << 2);
//<<<<<<<<<<<<<<<<<<<<<<End unused parsing for multiple fingers
}
//print to serial monitor the coordinates of the points stored in
//the passed x and y arrays
void printCamData(short xcor[4], short ycor[4]){
for(int i = 0; i<4; i++){
short x = xcor[i];
short y = ycor[i];
//determine what to print
//x coordinate
pc.printf(" %d,", x);
//y coordinate
pc.printf(" %d\t", y);
}
//new line and delay
pc.printf("\n");
//wait(0.01);
}
//entrance to the code
int main() {
//i2c increase
camera1.frequency(400000);
//set values initially to zero
for(int i = 0; i < 4; i++){
updatex[i] = 0;
updatey[i] = 0;
}
myled = 0;
myled2 = 0;
//slaveAddress = IRsensorAddress >> 1;
slaveAddress = IRsensorAddress;
initCamera();
//update baud rate
pc.baud(115200);
//attach ticker for interrupt
//mouseStateTicker.attach_us(&updateMouseState, 100);
mouseStateTicker.attach(&updateMouseState, 0.05);
//attach ticker for reading camera interrupt
cameraReadTicker.attach(&readCameraData, 0.01);
//loop to search for new info using the camera
while(1) {
//pc.printf("while\n");
//toggle test LED
myled = 1 - myled;
//pc.printf("while2\n");
//DEPRECATED: now interrupt
//get the camera data
//readCameraData();
//printing clicking state -- FOR DEBUGGING
// pc.printf("readyForClick %s", readyForClick ? "true" : "false");
// pc.printf("\treadingClick %s", readingClick ? "true" : "false");
// pc.printf("\treadyForClickRelease %s\n", readyForClickRelease ? "true" : "false");
//printing mouse state -- FOR DEBUGGING
// pc.printf("update mouse %d, %d", updatex[0], updatey[0]);
// pc.printf("\tclick left %s", toLeftClick ? "true" : "false");
// pc.printf("\tclick right %s\n", toRightClick ? "true" : "false");
//print points
//printCamData(onex, oney);
//uncomment below to test infinite print
// keyOut.putc(0x41);
//uncomment below to infinitely move mouse in a square
// double delay = 0.1;
// int change = 75;
// mouseCommand(0,0, (char) -1*change);
// wait(delay);
// mouseCommand(0,(char) -1*change,0);
// wait(delay);
// mouseCommand(0,0, (char) change);
// wait(delay);
// mouseCommand(0,(char) change,0);
// wait(delay);
}
}