Ghost Mouse / Mbed 2 deprecated ghost_mouse

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
jennabarton
Date:
Fri Apr 14 21:52:46 2017 +0000
Parent:
25:d7418ded06aa
Commit message:
demo ready;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Apr 14 19:03:16 2017 +0000
+++ b/main.cpp	Fri Apr 14 21:52:46 2017 +0000
@@ -21,14 +21,14 @@
 int i;
 
 //point variables
-int point1x = 0;
-int point1y = 0;
-int point2x = 0;
-int point2y = 0;
-int point3x = 0;
-int point3y = 0;
-int point4x = 0;
-int point4y = 0;
+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
@@ -39,33 +39,33 @@
 int sen3 = 0x00;
 
 //previous point values
-int prevX = 1023;
-int prevY = 1023;
+short prevX = 1023;
+short prevY = 1023;
 
 //matrices of x and y coordinates from the first camera
-int onex[4];
-int oney[4];
+short onex[4];
+short oney[4];
 
 //matrices of x and y coordinates from prev point
-int prevx[4];
-int prevy[4];
+short prevx[4];
+short prevy[4];
 
 //movement
-const int deadzone = 1;
-const int mouseMoveMult = 1;
-const double mouseMovePwr = 1.2;
-const int MOVEMENT_CAP = 20; //working on the computer with 20
-const int VALUES_TO_TOSS = 6;
-int tossedValuesCounter = VALUES_TO_TOSS;
+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 int CLICK_DEAD_ZONE = 50;
-int clickBaseX;
-int clickBaseY;
-int clickDurCount = 0;
+const short CLICK_DEAD_ZONE = 50;
+short clickBaseX;
+short clickBaseY;
+short clickDurCount = 0;
 bool readingClick = false;
-int minLeftClickDur = 10;
-int maxLeftClickDur = 50;
+short minLeftClickDur = 10;
+short maxLeftClickDur = 50;
 
 
 
@@ -98,11 +98,40 @@
 //NOTE: hard coded wait of 0.1
 void mouseCommand(char buttons, short x, short y) {
   
-  x = (x > 0) ? pow(mouseMoveMult*x, mouseMovePwr) : -pow(-mouseMoveMult*x, mouseMovePwr);
+  
+  //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 > 0) ? pow(mouseMoveMult*y, mouseMovePwr) : -pow(-mouseMoveMult*y, mouseMovePwr);
-  //y = y*abs(y);
   //y = y*sqrt((float)abs(y));
   
   keyOut.putc(0xFD);
@@ -162,7 +191,7 @@
 // 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(int currx, int curry, int prevx, int prevy){
+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)){
@@ -170,8 +199,10 @@
         short diffY = -1*(curry - prevy);
     
         //fix diffX
-        if(abs(diffX) > MOVEMENT_CAP) {
-            diffX = 0;
+        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){
@@ -179,25 +210,58 @@
         } else{
             diffX = 0;
         }
+         
+        
+        
+        
         //fix diffY
-        if(abs(diffY) > MOVEMENT_CAP) {
-            diffY = 0;
+        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] = 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);  
@@ -237,7 +301,7 @@
 
 
 //update counts for click 
-void updateClickState(int currx, int curry, int prevx, int prevy){
+void updateClickState(short currx, short curry, short prevx, short prevy){
     bool xStable = false;
     bool yStable = false; 
     
@@ -400,10 +464,10 @@
 
 //print to serial monitor the coordinates of the points stored in
 //the passed x and y arrays
-void printCamData(int xcor[4], int ycor[4]){
+void printCamData(short xcor[4], short ycor[4]){
     for(int i = 0; i<4; i++){
-        int x = xcor[i];
-        int y = ycor[i];
+        short x = xcor[i];
+        short y = ycor[i];
         //determine what to print
         //x coordinate
         pc.printf(" %d,", x);
@@ -427,8 +491,10 @@
     camera1.frequency(400000);
     
     //set values initially to zero
-    updatex[0] = 0;
-    updatey[0] = 0;
+    for(int i = 0; i < 4; i++){
+        updatex[i] = 0;
+        updatey[i] = 0;
+    }
     
     myled = 0;
     myled2 = 0;
@@ -476,7 +542,7 @@
 //        pc.printf("\tclick right %s\n", toRightClick ? "true" : "false");
         
         //print points
-        printCamData(onex, oney);  
+        //printCamData(onex, oney);