Digital Joystick

Files at this revision

API Documentation at this revision

Comitter:
LeoHsueh
Date:
Thu Feb 26 11:50:10 2015 +0000
Parent:
5:31e3324d4c4b
Commit message:
Add function call.

Changed in this revision

Joystick.cpp Show annotated file Show diff for this revision Revisions of this file
Joystick.h Show annotated file Show diff for this revision Revisions of this file
--- a/Joystick.cpp	Sat Feb 21 05:20:01 2015 +0000
+++ b/Joystick.cpp	Thu Feb 26 11:50:10 2015 +0000
@@ -13,28 +13,61 @@
     
 }
 
-/** Function: getStatus
+void Joystick::functions(FunctionPointer *functionNone, FunctionPointer *functionUp, FunctionPointer *functionDown,
+        FunctionPointer *functionLeft, FunctionPointer *functionRight, FunctionPointer *functionPress) {
+    _functionNone = functionNone;
+    _functionUp = functionUp;
+    _functionDown = functionDown;
+    _functionLeft = functionLeft;
+    _functionRight = functionRight;
+    _functionPress = functionPress;
+}
+
+/** Get status
  * Read the joystick status
  *
- * Variables:
- *  returns - A uint8_t values representing the bits
+ * @returns Status of joystick
  */
-uint8_t Joystick::getStatus() {
-    uint8_t ret = 0;
+Joystick::Status Joystick::getStatus() {
     if (!_up) {
-        ret |= JOY_UP;
+        return up;
     }
     if (!_down) {
-        ret |= JOY_DOWN;
+        return down;
     }
     if (!_left) {
-        ret |= JOY_LEFT;
+        return left;
     }
     if (!_right) {
-        ret |= JOY_RIGHT;
+        return right;
     }
     if (!_press) {
-        ret |= JOY_PRESS;
+        return press;
     }
-    return ret;
+    return none;
 }
+
+void Joystick::poll() {
+    switch (getStatus()) {
+        case none:
+            _functionNone->call();
+            break;
+        case up:
+            _functionUp->call();
+            break;
+        case down:
+            _functionDown->call();
+            break;
+        case left:
+            _functionLeft->call();
+            break;
+        case right:
+            _functionRight->call();
+            break;
+        case press:
+            _functionPress->call();
+            break;
+        default:
+            break;
+    }
+}
--- a/Joystick.h	Sat Feb 21 05:20:01 2015 +0000
+++ b/Joystick.h	Thu Feb 26 11:50:10 2015 +0000
@@ -4,26 +4,44 @@
 
 //required to use mbed functions
 #include "mbed.h"
+#include "FunctionPointer.h"
 
-#define JOY_UP              0x01
-#define JOY_DOWN            0x02
-#define JOY_LEFT            0x04
-#define JOY_RIGHT           0x08
-#define JOY_PRESS           0x10
+#define JOYSTICK_NONE            0x00
+#define JOYSTICK_UP              0x01
+#define JOYSTICK_DOWN            0x02
+#define JOYSTICK_LEFT            0x04
+#define JOYSTICK_RIGHT           0x08
+#define JOYSTICK_PRESS           0x10
 
-/** Class: Joystick
+/** Digital Joystick class
  *
- * Used for reading from an digital joystick
+ * Used for an digital joystick.
  *
  * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "Joystick.h"
  *
- * > #include "mbed.h"
+ * Joystick joystick(P2_3, P0_15, P2_4, P0_16, P0_17);
  *
- * > Joystick joystick(P2_3, P0_15, P2_4, P0_16, P0_17);
+ * int main() {
+ *     joystick.getStatus();
+ * }
+ *
+ * @endcode
  */
 
 class Joystick {
     public:
+        typedef enum {
+            none = JOYSTICK_NONE,
+            up = JOYSTICK_UP,
+            down = JOYSTICK_DOWN,
+            left = JOYSTICK_LEFT,
+            right = JOYSTICK_RIGHT,
+            press = JOYSTICK_PRESS
+        } Status;
+
         /** Create a Joystick HID for using regular mbed pins
          *
          * @param up    Joystick Up
@@ -34,19 +52,44 @@
          */
         Joystick(PinName up, PinName down, PinName left, PinName right, PinName press);
 
-        /** Function: getStatus
+        void functions(FunctionPointer *functionNone, FunctionPointer *functionUp, FunctionPointer *functionDown,
+                FunctionPointer *functionLeft, FunctionPointer *functionRight, FunctionPointer *functionPress);
+
+        /** Get status
          * Read the joystick status
          *
-         * Variables:
-         *  returns - A uint8_t values representing the bits
+         * @code
+         * switch (joystick.getStatus()) {
+         *     case Joystick::none:
+         *         break;
+         *     case Joystick::up:
+         *         break;
+         *     case Joystick::down:
+         *         break;
+         *     case Joystick::left:
+         *         break;
+         *     case Joystick::right:
+         *         break;
+         *     case Joystick::press:
+         *         break;
+         *     default:
+         *         break;
+         * }
+         * @endcode
+         *
+         * @returns A uint8_t values representing the bits
          */
-        uint8_t getStatus();
+        Status getStatus();
+
+        void poll();
 
     private:
 
         /** Regular mbed pins bus
          */
         DigitalIn _up, _down, _left, _right, _press;
+
+        FunctionPointer *_functionNone, *_functionUp, *_functionDown, *_functionLeft, *_functionRight, *_functionPress;
 };
 
 #endif