Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Revision 12:e127fa20d609, committed 2017-04-02
- Comitter:
- jennabarton
- Date:
- Sun Apr 02 20:57:25 2017 +0000
- Parent:
- 11:cd450ce343a8
- Child:
- 13:4f2181174071
- Commit message:
- initial attempt at clicking
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat Apr 01 22:13:22 2017 +0000 +++ b/main.cpp Sun Apr 02 20:57:25 2017 +0000 @@ -17,10 +17,6 @@ char s; int i; -//previous point values -int prevX = 1023; -int prevY = 1023; - //point variables int point1x = 0; int point1y = 0; @@ -39,26 +35,37 @@ int sen2 = 0xFE; int sen3 = 0x00; +//previous point values +int prevX = 1023; +int prevY = 1023; + //matrices of x and y coordinates from the first camera int onex[4]; int oney[4]; +//matrices of x and y coordinates from prev point +int prevx[4]; +int prevy[4]; + +//movement +int deadzone = 5; + +//counts for clicks +int clickDeadzone = 10; +int fingerDownCount = 0; +int fingerUpCount = 0; +int minLeftClickDur = 4; +int minRightClickDur = 10; +int delayForClick = 2; +int instabilityLimit = 3; +int instabilityCount = 0; //****************************************************************** // All methods defined below //****************************************************************** - -//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); -} - -//TODO: get rid of these b/c they don't work. instead use printc -// +//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, char x, char y) { keyOut.putc(0xFD); keyOut.putc(0x00); @@ -71,10 +78,55 @@ keyOut.putc(0x00); //delay for pushing data - wait(0.1); + wait(0.1); //how large does this need to be? } +//moves mouse on screen from one finger input +//param +// current point (currx, curry) +// previous point (prevx, prevy) +void oneFingerResponse(int currx, int curry, int prevx, int prevy){ + //look at delta btwn prev val and current + //TODO: moving average + if((prevx != 1023 || prevy != 1023) && (currx != 1023 && curry != 1023)){ + int diffX = currx - prevx; + int diffY = -1*(curry - prevy); + + //fix diffX + if(diffX > deadzone){ + diffX -= deadzone; + } else if (diffX < -1*deadzone){ + diffX += deadzone; + } else{ + diffX = 0; + } + //fix diffY + if(diffY > deadzone){ + diffY -= deadzone; + } else if (diffX < -1*deadzone){ + diffY += deadzone; + } else{ + diffY = 0; + } + + //move x and y + mouseCommand(0, (char) diffX, (char) diffY); + } +} + + +//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); @@ -91,6 +143,93 @@ } + +//update counts for click +void updateClickState(int currx, int curry, int prevx, int prevy){ + bool xStable = false; + bool yStable = false; + + //TODO: update to handle cases where just one is 1023 + if(currx != 1023 || curry != 1023){ + //check x stability + if( currx > prevx ){ + if(currx-prevx < clickDeadzone){ + //barely moved in x dir + xStable = true; + } + } else if( currx < prevx) { + if(prevx-currx < clickDeadzone){ + //barely moved in x dir + xStable = true; + } + } else { + //no movement in x + xStable = true; + } + + //check y stability + if( curry > prevy ){ + if(curry-prevy < clickDeadzone){ + //barely moved in y dir + yStable = true; + } + } else if( curry < prevy) { + if(prevy-curry < clickDeadzone){ + //barely moved in y dir + yStable = true; + } + } else { + //no movement in y + yStable = true; + } + + //update finger down count + if(xStable && yStable){ + //both are stable + fingerDownCount++; + //pc.printf("finger down \t"); + + } else{ + //unstable, increase instability count + instabilityCount++; + if(instabilityCount > instabilityLimit){ + //too many instable points, reset to zero + fingerDownCount = 0; + instabilityCount = 0; + } + } + + } else { + //both are up, increment up count + fingerUpCount++; + if(fingerUpCount >= delayForClick && fingerDownCount >= minLeftClickDur){ + //finger was down for long enough to be a click + //finger was lifted for long enough to indicate click + if(fingerDownCount >= minRightClickDur){ + //initiate right click + mouseCommand(0x02, 0 , 0); + + //TODO: remove. for debugging purposes only + pc.printf("#########RIGHT mouse click \t"); + } else { + //initiate left click + mouseCommand(0x01, 0 , 0); + + //TODO: remove. for debugging purposes only + pc.printf("********LEFT mouse click \t"); + } + + //reset counters + fingerUpCount = 0; + fingerDownCount = 0; + + } + } + + +} + + //get data from camera one //populates onex and oney with values depending on the measured points //NOTE: 1023 means nothing was detected @@ -117,33 +256,9 @@ //>>>>>>>>>>>>>>>>>Begin unfinished code for moving - int deadzone = 5; - //look at delta btwn prev val and current - //TODO: moving average - if((prevX != 1023 || prevY != 1023) && (onex[0] != 1023 && oney[0] != 1023)){ - int diffX = onex[0] - prevX; - int diffY = -1*(oney[0] - prevY); + oneFingerResponse(onex[0], oney[0], prevX, prevY); + updateClickState(onex[0], oney[0], prevX, prevY); - //fix diffX - if(diffX > deadzone){ - diffX -= deadzone; - } else if (diffX < -1*deadzone){ - diffX += deadzone; - } else{ - diffX = 0; - } - //fix diffY - if(diffY > deadzone){ - diffY -= deadzone; - } else if (diffX < -1*deadzone){ - diffY += deadzone; - } else{ - diffY = 0; - } - - //move x and y - mouseCommand(0, (char) diffX, (char) diffY); - } //update prev values prevX = onex[0]; @@ -217,7 +332,7 @@ while(1) { //wait - wait(0.04); + //wait(0.04); //toggle test LED myled = 1 - myled; @@ -232,7 +347,7 @@ //keyOut.putc(0x41); - //uncomment below to infinitely move mouse up and down + //uncomment below to infinitely move mouse in a square // double delay = 0.1; // int change = 75; // mouseCommand(0,0, (char) -1*change); @@ -243,8 +358,6 @@ // wait(delay); // mouseCommand(0,(char) change,0); // wait(delay); - //mouseCommand(0,0,(char) 100); - //wait(.05); }