Erik Kallen / PositionModule
Revision:
0:886002922b6a
diff -r 000000000000 -r 886002922b6a PositionModule.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PositionModule.cpp	Tue Oct 19 10:04:34 2010 +0000
@@ -0,0 +1,40 @@
+#include "PositionModule.h"
+#include "Point.h"
+
+PositionModule::PositionModule(PinName tx, PinName rx):_mouse(tx,rx) {
+    _mouse.frequency(100000);
+}
+
+long PositionModule::getX() {
+    updatePosition();
+    return -_ypos;
+}
+
+long PositionModule::getY() {
+    updatePosition();
+    return -_xpos;    
+}
+
+void PositionModule::setZero() {
+    char cmd[1] = {2};
+    _mouse.write(4,cmd,1);
+}
+
+long PositionModule::getDistance() {
+    updatePosition();
+    return sqrt((long double)(getX()*getX())+(getY()*getY())); 
+}
+
+float PositionModule::getHeading() {
+    return Point::XYToDegrees(Point(0,0),Point(getX(),getY()));
+}
+
+void PositionModule::updatePosition() {
+    char * buf = new char[8];
+     char cmd[1] = {3};
+    _mouse.read(5,buf,8);
+    _mouse.write(4,cmd,1);        
+    _xpos = (long)((buf[0] << 24) + (buf[1] << 16) + ( buf[2] << 8) + buf[3]);
+    _ypos = (long)((buf[4] << 24) + (buf[5] << 16) + ( buf[6] << 8) + buf[7]);
+    wait(0.1);
+}
\ No newline at end of file