I am interested in simulating a mouse pointer (say a square on a Cartesian coordinate system). I would need to convert DELTA_X & DELTA_Y data received from ADNS2610 Optical Mouse Sensor.
Looking at the datasheet, there is an 18 x 18 matrix of possible pixel read ranges. I divided the matrix into 4 parts, where each part can potentially mean a direction of movement:
I wrote a small app which lights up each pixle when data is receive just to get a visual understanding. And sure enough, parts of each quadrant would light up when the sensor is moved UP, Down, LEFT, or RIGHT.
So a method to get direction would be:
private int getDirectionInt(int val) //where val can be DELTA_X or DELTA_Y
{
if ((val >= 163 && val<= 171) ||
(val >= 181 && val<= 189) ||
(val >= 199 && val<= 207) ||
(val >= 217 && val<= 225) ||
(val >= 235 && val<= 243) ||
(val >= 253 && val<= 261) ||
(val>= 271 && val<= 279) ||
(val>= 289 && val<= 297) ||
(val>= 307 && val<= 315))
{
return 4; // Quadrant 4 (Lower Right)
}
else if ((val>= 1 && val<= 9) ||
(val>= 19 && val<= 27) ||
(val>= 37 && val<= 45) ||
(val>= 55 && val<= 63) ||
(val>= 73 && val<= 81) ||
(val>= 91 && val<= 99) ||
(val>= 109 && val<= 117) ||
(val>= 127 && val<= 135) ||
(val>= 145 && val<= 153))
{
return 2; // Quadrant 2 (Lower Left)
}
else if ((val>= 10 && val<= 18) ||
(val>= 28 && val<= 36) ||
(val>= 46 && val<= 54) ||
(val>= 64 && val<= 72) ||
(val>= 82 && val<= 90) ||
(val>= 100 && val<= 108) ||
(val>= 118 && val<= 126) ||
(val>= 136 && val<= 144) ||
(val>= 154 && val<= 162))
{
return 1; // Quadrant 1 (Upper Left)
}
else if ((val>= 172 && val<= 180) ||
(val>= 190 && val<= 198) ||
(val>= 208 && val<= 216) ||
(val>= 226 && val<= 234) ||
(val>= 244 && val<= 252) ||
(val>= 262 && val<= 270) ||
(val>= 280 && val<= 288) ||
(val>= 298 && val<= 306) ||
(val>= 316 && val<= 324))
{
return 3; // Quadrant 3 (Upper Right)
}
else
{
return 0; //Idle
}
}
I then have a method which moves the square:
private void moveSquare()
{
int dX = getDirectionInt(DELTA_X);
int dY = getDirectionInt(DELTA_Y);
int ddX, ddY;
int delta=5; // How much to move the square
if (dX == 4)
ddX = delta;
else if (dX == 3)
ddX = delta;
else if (dX == 2)
ddX = -delta;
else if (dX == 1)
ddX = -delta;
else
ddX = 0;
if (dY == 4)
ddY = -delta;
else if (dY == 3)
ddY = delta;
else if (dY == 2)
ddY = -delta;
else if (dY == 1)
ddY = delta;
else
ddY = 0;
squareX = squareX + ddX;
squareY = squareX + ddX;
mySquare.refresh(); //Re-draws the square with new center
}
Somehow I think I am walking the wrong path. This approach is not as fluid as I would like, ie. The mouse pointer moves much nicer than my square.
Any thoughts?