A class to handle reading, scaling and filtering horizontal and vertical position, and rise/fall interrupts for the button.

Files at this revision

API Documentation at this revision

Comitter:
alex89_2
Date:
Tue Sep 28 14:59:57 2010 +0000
Child:
1:e55694d8a418
Commit message:
Release to public

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Joystick.cpp	Tue Sep 28 14:59:57 2010 +0000
@@ -0,0 +1,98 @@
+#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.51;
+    midmax = 0.56;
+
+    
+    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);
+}
+
+//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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Joystick.h	Tue Sep 28 14:59:57 2010 +0000
@@ -0,0 +1,96 @@
+//remove repetition
+#ifndef MBED_JOYSTICK_H
+#define MBED_JOYSTICK_H
+
+//required to use mbed functions
+#include "mbed.h"
+
+struct joyhv {
+    double h;
+    double v;
+};
+
+/* Class: Joystick
+ * Used for reading from an analog joystick
+ *
+ * Example:
+ * > #include "mbed.h"
+ * > Joystick joy(p20, p19, p18);
+ */
+ 
+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);
+
+    
+private:
+    InterruptIn _b;
+    AnalogIn _h;
+    AnalogIn _v;
+};
+
+
+#endif
\ No newline at end of file