HSUEH YU-MIN / Digital-Joystick

Files at this revision

API Documentation at this revision

Comitter:
LeoHsueh
Date:
Sat Feb 21 05:20:01 2015 +0000
Parent:
4:beeaa40d49fa
Child:
6:d354f6e3bd9b
Commit message:
joy lib

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	Tue Sep 28 16:32:20 2010 +0000
+++ b/Joystick.cpp	Sat Feb 21 05:20:01 2015 +0000
@@ -1,98 +1,40 @@
 #include "Joystick.h"
 
-AnalogIn hin(p19);
-AnalogIn vin(p18);
-
-joyhv last = {0.5,0.5};
-
-double scaleb(double min, double max, double bmin, double bmax, double num){
-    double shifted = num + (bmin - min);    //shifted bmin down to min
-    return (shifted - min) * (bmax-bmin)/(max-min) + min; //scaled bmax to max
-}
-
-double doscale(double pos){
-    double min, max, midmin, midmax, scaled;
-    
-    min = 0.087;
-    max = 0.98;
-    
-    midmin = 0.49;
-    midmax = 0.57;
-
+/** Create a Joystick HID for using regular mbed pins
+ *
+ * @param up    Joystick Up
+ * @param down  Joystick Down
+ * @param left  Joystick Left
+ * @param right Joystick Right
+ * @param press Joystick Press
+ */
+Joystick::Joystick(PinName up, PinName down, PinName left, PinName right, PinName press) :
+        _up(up), _down(down), _left(left), _right(right), _press(press) {
     
-    if(pos < midmin){           // lower than 0.5 tolerance - backwards
-        scaled = scaleb(min, midmin, 0, 0.5, pos);
-    } else if (pos > midmax){   //greater than 0.5 tolerance - forwards
-        scaled = scaleb(midmax, max, 0.5, 1, pos);
-    } else {
-        scaled = 0.5;
-    }
-    
-    //check if out of bounds [0,1]
-    
-    if(scaled < 0)
-        scaled = 0;
-    
-    if(scaled > 1)
-        scaled = 1;
-    
-    return scaled;
-}
-
-
-double dofilter(double last, double num, double factor){
-
-    return last*factor + num*(1-factor);
-
 }
 
-Joystick::Joystick(PinName b, PinName h, PinName v) : _b(b), _h(h), _v(v) {
-}
-
-Joystick::operator joyhv (){
-    joyhv data;
-    data.v = _v.read();
-    data.h = _h.read();
-    return data;
-}
-
-double Joystick::getV (){
-    return _v.read();
-}
-
-double Joystick::getH (){
-    return _h.read();
-}
-
-void Joystick::rise (void (*fptr)(void)){ 
-    _b.rise(fptr);
-}
-
-void Joystick::fall (void (*fptr)(void)){
-    _b.fall(fptr);
+/** Function: getStatus
+ * Read the joystick status
+ *
+ * Variables:
+ *  returns - A uint8_t values representing the bits
+ */
+uint8_t Joystick::getStatus() {
+    uint8_t ret = 0;
+    if (!_up) {
+        ret |= JOY_UP;
+    }
+    if (!_down) {
+        ret |= JOY_DOWN;
+    }
+    if (!_left) {
+        ret |= JOY_LEFT;
+    }
+    if (!_right) {
+        ret |= JOY_RIGHT;
+    }
+    if (!_press) {
+        ret |= JOY_PRESS;
+    }
+    return ret;
 }
-
-//simple filtering
-joyhv Joystick::filter(joyhv read, double factor){
-
-    joyhv filtered = {0.5,0.5};
-    
-    filtered.v = dofilter(last.v, read.v, factor);
-    filtered.h = dofilter(last.h, read.h, factor);
-    
-    last.v = filtered.v;
-    last.h = filtered.h;
-    
-    return filtered;
-}
-
-joyhv Joystick::scale(joyhv read){
-    
-    joyhv scaled = {0.5,0.5};
-    
-    scaled.v = doscale(read.v);
-    scaled.h = doscale(read.h);
-    
-    return scaled;
-    
-}
\ No newline at end of file
--- a/Joystick.h	Tue Sep 28 16:32:20 2010 +0000
+++ b/Joystick.h	Sat Feb 21 05:20:01 2015 +0000
@@ -5,97 +5,48 @@
 //required to use mbed functions
 #include "mbed.h"
 
-/** Struct: joyhv
- *
- * Used for holding a horizontal and vertical position as doubles
- */
-struct joyhv {
-    double h;
-    double v;
-};
+#define JOY_UP              0x01
+#define JOY_DOWN            0x02
+#define JOY_LEFT            0x04
+#define JOY_RIGHT           0x08
+#define JOY_PRESS           0x10
 
 /** Class: Joystick
  *
- * Used for reading from an analog joystick
+ * Used for reading from an digital joystick
  *
  * Example:
  *
  * > #include "mbed.h"
  *
- * > Joystick joy(p20, p19, p18);
+ * > Joystick joystick(P2_3, P0_15, P2_4, P0_16, P0_17);
  */
- 
-class Joystick {
-public:
-    /** Constructor: Joystick
-     *
-     * Variables: 
-     * b - DigitalIn pin for button
-     * h - AnalogIn pin for horizontal 
-     * v - AnalogIn pin for vertical 
-     */ 
-    Joystick(PinName b, PinName h, PinName v);
-    
-    /** Function: read
-     * Read the joystick position, represented as a joyhv value - h and v are doubles in the range [0.0, 1.0]
-     *
-     * Variables:
-     *  returns - A structure of two double values representing the position of the joystick,
-     *            measured as a percentage vertically (joyhv.v) or horizontally (joyhv.h)
-     */
-    joyhv read();
 
-    /** Function: getV
-     * Read the joystick's vertical position, represented as a double value in the range [0.0, 1.0]
-     *
-     * Variables:
-     *  returns - A double values representing the vertical position of the joystick,
-     *            measured as a percentage  
-     */
-    double getV();
-    
-    /** Function: getH
-     * Read the joystick's horizontal position, represented as a double value in the range [0.0, 1.0]
-     *
-     * Variables:
-     *  returns - A double values representing the horizontal position of the joystick,
-     *            measured as a percentage  
-     */
-    double getH();
-    
-    /** Function: rise
-     *  Attach a function to call when a rising edge occurs on the button input
-     *
-     * Variables:
-     *  fptr - A pointer to a void function, or 0 to set as none
-     */
-    void rise (void (*fptr)(void));
-    
-    /** Function: fall
-     *  Attach a function to call when a falling edge occurs on the button input
-     *
-     * Variables:
-     *  fptr - A pointer to a void function, or 0 to set as none
-     */
-    void fall (void (*fptr)(void));
-    
-    /** Function: operator joyhv
-     *  An operator shorthand for <read()>
-     *
-     * The joyhv() operator can be used as a shorthand for <read()> to simplify common code sequences
-     *
-     */
-    operator joyhv ();
-    
-    joyhv scale(joyhv read);
-    joyhv filter(joyhv read, double factor);
+class Joystick {
+    public:
+        /** Create a Joystick HID for using regular mbed pins
+         *
+         * @param up    Joystick Up
+         * @param down  Joystick Down
+         * @param left  Joystick Left
+         * @param right Joystick Right
+         * @param press Joystick Press
+         */
+        Joystick(PinName up, PinName down, PinName left, PinName right, PinName press);
 
-    
-private:
-    InterruptIn _b;
-    AnalogIn _h;
-    AnalogIn _v;
+        /** Function: getStatus
+         * Read the joystick status
+         *
+         * Variables:
+         *  returns - A uint8_t values representing the bits
+         */
+        uint8_t getStatus();
+
+    private:
+
+        /** Regular mbed pins bus
+         */
+        DigitalIn _up, _down, _left, _right, _press;
 };
 
-
-#endif
\ No newline at end of file
+#endif