My Embedded Systems Project Ihsian Mulla (el14imfm@leeds.ac.uk 200839613

Files at this revision

API Documentation at this revision

Comitter:
Ihsianmulla
Date:
Wed May 04 13:01:09 2016 +0000
Commit message:
Joystick Functions

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
diff -r 000000000000 -r f79fe9dc65ee joystick.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/joystick.cpp	Wed May 04 13:01:09 2016 +0000
@@ -0,0 +1,49 @@
+/** Temperature Sensor Functions
+* Used for printing and updating the temperature at 1Hz with unit conversion capabilities
+@file joystick.cpp
+@brief contains functions for joystick update/calibration and direction names with specification
+@author Craig Evans
+@date May 2016
+*/
+
+#include "joystick.h"
+
+extern Joystick joystick;
+
+void calibrateJoystick()
+{
+    button.mode(PullDown);// additional pull down for external button
+    Jbutton.mode(PullDown);
+    // must not move during calibration
+    joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
+    joystick.y0 = yPot;
+}
+
+Joystick return_data(){
+    return joystick;
+}
+void updateJoystick()
+{
+    // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
+    joystick.x = xPot - joystick.x0;
+    joystick.y = yPot - joystick.y0;
+    // read button state
+    joystick.button = button;
+
+    // calculate direction depending on x,y values
+    // tolerance allows a little lee-way in case joystick not exactly in the stated direction
+    if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+        joystick.direction = CENTRE;
+    } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+        joystick.direction = UP;
+    } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+        joystick.direction = DOWN;
+    } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
+        joystick.direction = RIGHT;
+    } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
+        joystick.direction = LEFT;
+    } else {
+        joystick.direction = UNKNOWN;
+    }
+
+}
\ No newline at end of file
diff -r 000000000000 -r f79fe9dc65ee joystick.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/joystick.h	Wed May 04 13:01:09 2016 +0000
@@ -0,0 +1,47 @@
+/** Joystick Functions
+* Used for reading and updating the joystick position and value for its button.
+@file joystick.h
+@brief
+@brief
+@author Craig Evans
+@date April 2016
+*/
+#ifndef _JOYSTICK_H_
+
+#define _JOYSTICK_H_
+
+#define DIRECTION_TOLERANCE 0.25L // change this to alter tolerance of joystick direction
+#include "mbed.h"
+extern InterruptIn button;
+extern AnalogIn xPot;
+extern AnalogIn yPot;
+extern DigitalIn Jbutton;
+extern Ticker pollJoystick;
+
+enum DirectionName {
+    UP,
+    DOWN,
+    LEFT,
+    RIGHT,
+    CENTRE,
+    UNKNOWN
+};
+
+typedef struct JoyStick Joystick;
+struct JoyStick {
+    float x;    // current x value
+    float x0;   // 'centred' x value
+    float y;    // current y value
+    float y0;   // 'centred' y value
+    int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
+    DirectionName direction;  // current direction
+};
+
+
+
+// function prototypes
+extern void calibrateJoystick();
+extern void updateJoystick();
+extern Joystick return_data();
+
+#endif
\ No newline at end of file