Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed N5110 ShiftReg PinDetect
Diff: Joystick.cpp
- Revision:
- 0:1f92de30d43e
- Child:
- 17:d6a3b29cab31
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Joystick.cpp Sun Apr 26 17:20:09 2015 +0000
@@ -0,0 +1,45 @@
+#include "Joystick.h"
+#include "mbed.h"
+#include <cmath>
+
+Joystick::Joystick(PinName x, PinName y, PinName button)
+{
+ xPot = new AnalogIn(x);
+ yPot = new AnalogIn(y);
+ btn = new DigitalIn(button);
+}
+
+Joystick::~Joystick()
+{
+ delete xPot;
+ delete yPot;
+ delete btn;
+}
+
+
+// Make sure that the Joystick is centered while calibrating
+void Joystick::calibrate()
+{
+ btn->mode(PullDown);
+ centerX = *xPot;
+ centerY = *yPot;
+}
+
+
+void Joystick::update()
+{
+ // Get position of joystick compared to the center values (delta x and delta y).
+ dx = *xPot - centerX;
+ dy = *yPot - centerY;
+
+ if (abs(dx) < DEAD_ZONE && abs(dy) < DEAD_ZONE)
+ dir = CENTER;
+ else if (abs(dx) < DEAD_ZONE) // Inside horizontal dead zone.
+ dir = (dy > DEAD_ZONE) ? UP : DOWN;
+ else if (abs(dy) < DEAD_ZONE) // Inside vertical dead zone.
+ dir = (dx > DEAD_ZONE) ? LEFT : RIGHT;
+ else if (dx > 0) // To the left, outside both deadzones.
+ dir = (dy > 0) ? UP_LEFT : DOWN_LEFT;
+ else // To the right, outside both deadzones
+ dir = (dy > 0) ? UP_RIGHT : DOWN_RIGHT;
+}