Sparkfun Analog Joystick

Dependents:   Lab4 4180final_receiver 4180final_sender DuelingTanks ... more

Fork of SparkfunAnalogJoystick by ECE4180

Revision:
0:ed9ee35481a9
Child:
1:ed0057aa2e31
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SparkfunAnalogJoystick.cpp	Sat Mar 07 23:28:06 2015 +0000
@@ -0,0 +1,48 @@
+#include "SparkfunAnalogJoystick.h"
+#include "math.h"
+
+#define M_PI 3.14159265358979323846
+
+SparkfunAnalogJoystick::SparkfunAnalogJoystick(PinName vert, PinName horz, PinName sel): VERT(vert), HORZ(horz), SEL(sel)
+{
+    SEL.mode(PullUp);
+}
+
+int SparkfunAnalogJoystick::button(){
+    return 1-SEL;
+}
+
+float SparkfunAnalogJoystick::xAxis(){
+    return -(HORZ-0.5)*2;
+}
+
+float SparkfunAnalogJoystick::yAxis(){
+    return (VERT-0.5)*2;
+}
+
+float SparkfunAnalogJoystick::angle(){
+    float horz=-(HORZ-0.5)*2;
+    float vert=(VERT-0.5)*2;
+    float angle=0;
+    if (vert==0&&horz>0) return 0;
+    if (vert==0&&horz<0) return 180;
+    if (horz==0&&vert>0) return 90;
+    if (horz==0&&vert<0) return 270;
+    if (horz>0) angle=atan(vert/horz)*180/M_PI;
+    else angle=180+atan(vert/horz)*180/M_PI;
+    if (angle<0) angle+=360;
+    return angle;
+}
+
+float SparkfunAnalogJoystick::distance(){
+    float horz=-(HORZ-0.5)*2;
+    float vert=(VERT-0.5)*2;
+    float angle=SparkfunAnalogJoystick::angle();
+    float oneAxis=tan(angle*M_PI/180.0);
+    if (oneAxis<0) oneAxis=-oneAxis;
+    if (oneAxis>1) oneAxis=1/oneAxis;
+    float maxdistance=sqrt(1+oneAxis*oneAxis);
+    float temp=horz*horz+vert*vert;
+    return sqrt(temp)/maxdistance;
+    
+}
\ No newline at end of file