Ghost Mouse / Mbed 2 deprecated ghost_mouse

Dependencies:   mbed

Revision:
22:99ff69f7111a
Parent:
21:a23f87a688b2
Child:
23:db56e04d555b
--- a/main.cpp	Wed Apr 12 21:04:40 2017 +0000
+++ b/main.cpp	Thu Apr 13 18:58:04 2017 +0000
@@ -55,29 +55,16 @@
 const int mouseMoveMult = 3;
 const double mouseMovePwr = 1.3;
 
-//counts for clicks
-int clickDeadzone = 15;
-int fingerDownCount = 0;
-int fingerUpCount = 0;
-int minLeftClickDur = 10;
-int minRightClickDur = 10;
-int delayForClick = 3;
-int instabilityLimit = 2;
-int instabilityCount = 0;
-
+//click state
+const int CLICK_DEAD_ZONE = 20;
+int clickBaseX;
+int clickBaseY;
+int clickDurCount = 0;
+bool readingClick = false;
+int minLeftClickDur = 0;
+int maxLeftClickDur = 100;
 
 
-//tracking info for click 
-bool readyForClick = false;
-bool readingClick = false;
-bool readyForClickRelease = false;
-int preClickDelayMin = 0;
-int preClickDelayMax = 100000;
-int preClickDelayCount = 0;
-
-int returnForClickX = 1023;
-int returnForClickY = 1023;
-
 
 //MOUSE STATE
 //implemented for ticker behavior
@@ -148,14 +135,13 @@
     updatey[0] = 0;
     
     //click
-    //TODO: uncomment
-//    if(toLeftClick){
-//        //send command to 
-//        mouseCommand(0x01, 0 , 0);
-//        
-//    } else if (toRightClick){
-//        mouseCommand(0x02, 0 , 0);
-//    }
+   if(toLeftClick){
+       //send command to 
+       mouseCommand(0x01, 0 , 0);
+       
+   } else if (toRightClick){
+       mouseCommand(0x02, 0 , 0);
+   }
     
     //fip clicking to false
     toLeftClick = false;
@@ -244,144 +230,67 @@
     bool xStable = false;
     bool yStable = false; 
     
-    //TODO: Do these need to be stable?
-    if(currx != 1023 && curry != 1023 && !readingClick){
-        //finger is down on the surface and you are not registering a click
-        readyForClick = true;
-        returnForClickX = currx;
-        returnForClickY = curry;
+
+    if(currx != 1023 && curry != 1023 && readingClick){
+        //finger is on surface and you are reading click
+
+        //test stability
 
-    } else if (currx == 1023 && curry == 1023 && readyForClick){
-        //finger is off of surface but had been on surface previously
-        //you can start looking to call a click now
-        
-        
-        readyForClick = false;
-        readingClick = true;
-        
-//        //increment count that tracks how long you have been delaying
-//        preClickDelayCount = preClickDelayCount + 1;
-//        
-//        if(preClickDelayCount > preClickDelayMax){
-//            //too much time has passed for a click
-//            readyForClick = false;
-//            readingClick = true;
-//            preClickDelayCount = 0;
-//        } else if(preClickDelayCount > preClickDelayMin){
-//            readyForClick = false;
-//            readingClick = false;
-//            preClickDelayCount = 0;
-//        }
-        
-        
-    } else if(currx != 1023 && curry != 1023 && readingClick){
-        //TODO: update to handle cases where just one is 1023
-        
-        //if finger is on plane and you can click, check if it is stable
-        //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
+        //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 > 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
+
+        //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;
         }
-        
-        //update finger down count
+
+        //if stable, increment count
         if(xStable && yStable){
-            //both are stable
-            fingerDownCount++;
-            
+            clickDurCount = clickDurCount + 1;
         } else{
-            //unstable, increase instability count
-            instabilityCount++;
-            if(instabilityCount > instabilityLimit){
-                //too many unstable points, reset to zero
-                fingerDownCount = 0;
-                instabilityCount = 0;
-                readingClick = false;
-            }
+            //if not stable, no longer reading click, counter to zero
+            readingClick = false;
+            clickDurCount = 0; 
         }
+
         
-        //if finger has been down and stable long enough to be a click
-        //set state to ready to get click release
-        if(fingerDownCount >= minLeftClickDur){
-            //let system know click can be released
-            readyForClickRelease = true;
-            readingClick = false;
-        }
-        
-        
-    } else if(currx == 1023 && curry == 1023 && readyForClickRelease) {
-        //both are up, increment up count
-        fingerUpCount++;
-        if(fingerUpCount >= delayForClick){
-            //finger was lifted for long enough to indicate click
-            
-            //indicate that you want to left click
+    } else if (currx != 1023 && curry != 1023 && prevx == 1023 && prevy == 1023 ){
+        //finger has been placed on surface
+
+        //set reading click to true
+        readingClick = true;
+
+        //save initial location
+        clickBaseX = currx;
+        clickBaseY = curry;
+
+    } else if (currx == 1023 && curry == 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;
-                
-            //TODO: remove. for debugging purposes only
             pc.printf("********LEFT mouse click \n");
-            
-            readingClick = false;
-            readyForClick = false;
-            readyForClickRelease = false;
-            
+        }
+
+
+        //no longer reading click
+        readingClick = false;
+        //reset counter
+        clickDurCount = 0;
+    }
             
-//>>>>>>>>>>>>> begin code for left-right diff
- //           if(fingerDownCount >= minRightClickDur){
-//                //initiate right click
-//                //mouseCommand(0x02, 0 , 0);
-//                
-//                //indicate that you want to right click
-//                toRightClick = true;
-//                
-//                //TODO: remove. for debugging purposes only
-//                pc.printf("#########RIGHT mouse click \t");
-//            } else {
-//                //initiate left click
-//                //mouseCommand(0x01, 0 , 0);
-//                
-//                //indicate that you want to left click
-//                toLeftClick = true;
-//                
-//                //TODO: remove. for debugging purposes only
-//                pc.printf("********LEFT mouse click \t");
-//            }
-//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< end code for left-right diff
-            
-            //reset counters
-            fingerUpCount = 0;
-            fingerDownCount = 0;
-        
-        }        
-    } 
-    
-        
 }
 
 
@@ -558,3 +467,4 @@
         
     }
 }
+ 
\ No newline at end of file