A class to handle reading, scaling and filtering horizontal and vertical position, and rise/fall interrupts for the button.
Joystick.h@2:8805f880e231, 2010-09-28 (annotated)
- Committer:
- alex89_2
- Date:
- Tue Sep 28 15:04:50 2010 +0000
- Revision:
- 2:8805f880e231
- Parent:
- 1:e55694d8a418
- Child:
- 3:83b40e07476f
Updated documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alex89_2 | 0:9c486d50434e | 1 | //remove repetition |
alex89_2 | 0:9c486d50434e | 2 | #ifndef MBED_JOYSTICK_H |
alex89_2 | 0:9c486d50434e | 3 | #define MBED_JOYSTICK_H |
alex89_2 | 0:9c486d50434e | 4 | |
alex89_2 | 0:9c486d50434e | 5 | //required to use mbed functions |
alex89_2 | 0:9c486d50434e | 6 | #include "mbed.h" |
alex89_2 | 0:9c486d50434e | 7 | |
alex89_2 | 2:8805f880e231 | 8 | /** Struct: joyhv |
alex89_2 | 2:8805f880e231 | 9 | * |
alex89_2 | 2:8805f880e231 | 10 | * Used for holding a horizontal and vertical position as doubles |
alex89_2 | 2:8805f880e231 | 11 | */ |
alex89_2 | 0:9c486d50434e | 12 | struct joyhv { |
alex89_2 | 0:9c486d50434e | 13 | double h; |
alex89_2 | 0:9c486d50434e | 14 | double v; |
alex89_2 | 0:9c486d50434e | 15 | }; |
alex89_2 | 0:9c486d50434e | 16 | |
alex89_2 | 1:e55694d8a418 | 17 | /** Class: Joystick |
alex89_2 | 2:8805f880e231 | 18 | * |
alex89_2 | 0:9c486d50434e | 19 | * Used for reading from an analog joystick |
alex89_2 | 0:9c486d50434e | 20 | * |
alex89_2 | 0:9c486d50434e | 21 | * Example: |
alex89_2 | 2:8805f880e231 | 22 | * |
alex89_2 | 0:9c486d50434e | 23 | * > #include "mbed.h" |
alex89_2 | 2:8805f880e231 | 24 | * |
alex89_2 | 0:9c486d50434e | 25 | * > Joystick joy(p20, p19, p18); |
alex89_2 | 0:9c486d50434e | 26 | */ |
alex89_2 | 0:9c486d50434e | 27 | |
alex89_2 | 0:9c486d50434e | 28 | class Joystick { |
alex89_2 | 0:9c486d50434e | 29 | public: |
alex89_2 | 1:e55694d8a418 | 30 | /** Constructor: Joystick |
alex89_2 | 0:9c486d50434e | 31 | * |
alex89_2 | 0:9c486d50434e | 32 | * Variables: |
alex89_2 | 0:9c486d50434e | 33 | * b - DigitalIn pin for button |
alex89_2 | 0:9c486d50434e | 34 | * h - AnalogIn pin for horizontal |
alex89_2 | 0:9c486d50434e | 35 | * v - AnalogIn pin for vertical |
alex89_2 | 0:9c486d50434e | 36 | */ |
alex89_2 | 0:9c486d50434e | 37 | Joystick(PinName b, PinName h, PinName v); |
alex89_2 | 0:9c486d50434e | 38 | |
alex89_2 | 2:8805f880e231 | 39 | /** Function: read |
alex89_2 | 0:9c486d50434e | 40 | * Read the joystick position, represented as a joyhv value - h and v are doubles in the range [0.0, 1.0] |
alex89_2 | 0:9c486d50434e | 41 | * |
alex89_2 | 0:9c486d50434e | 42 | * Variables: |
alex89_2 | 0:9c486d50434e | 43 | * returns - A structure of two double values representing the position of the joystick, |
alex89_2 | 0:9c486d50434e | 44 | * measured as a percentage vertically (joyhv.v) or horizontally (joyhv.h) |
alex89_2 | 0:9c486d50434e | 45 | */ |
alex89_2 | 0:9c486d50434e | 46 | joyhv read(); |
alex89_2 | 0:9c486d50434e | 47 | |
alex89_2 | 2:8805f880e231 | 48 | /** Function: getV |
alex89_2 | 0:9c486d50434e | 49 | * Read the joystick's vertical position, represented as a double value in the range [0.0, 1.0] |
alex89_2 | 0:9c486d50434e | 50 | * |
alex89_2 | 0:9c486d50434e | 51 | * Variables: |
alex89_2 | 0:9c486d50434e | 52 | * returns - A double values representing the vertical position of the joystick, |
alex89_2 | 0:9c486d50434e | 53 | * measured as a percentage |
alex89_2 | 0:9c486d50434e | 54 | */ |
alex89_2 | 0:9c486d50434e | 55 | double getV(); |
alex89_2 | 0:9c486d50434e | 56 | |
alex89_2 | 2:8805f880e231 | 57 | /** Function: getH |
alex89_2 | 0:9c486d50434e | 58 | * Read the joystick's horizontal position, represented as a double value in the range [0.0, 1.0] |
alex89_2 | 0:9c486d50434e | 59 | * |
alex89_2 | 0:9c486d50434e | 60 | * Variables: |
alex89_2 | 0:9c486d50434e | 61 | * returns - A double values representing the horizontal position of the joystick, |
alex89_2 | 0:9c486d50434e | 62 | * measured as a percentage |
alex89_2 | 0:9c486d50434e | 63 | */ |
alex89_2 | 0:9c486d50434e | 64 | double getH(); |
alex89_2 | 0:9c486d50434e | 65 | |
alex89_2 | 2:8805f880e231 | 66 | /** Function: rise |
alex89_2 | 0:9c486d50434e | 67 | * Attach a function to call when a rising edge occurs on the button input |
alex89_2 | 0:9c486d50434e | 68 | * |
alex89_2 | 0:9c486d50434e | 69 | * Variables: |
alex89_2 | 0:9c486d50434e | 70 | * fptr - A pointer to a void function, or 0 to set as none |
alex89_2 | 0:9c486d50434e | 71 | */ |
alex89_2 | 0:9c486d50434e | 72 | void rise (void (*fptr)(void)); |
alex89_2 | 0:9c486d50434e | 73 | |
alex89_2 | 2:8805f880e231 | 74 | /** Function: fall |
alex89_2 | 0:9c486d50434e | 75 | * Attach a function to call when a falling edge occurs on the button input |
alex89_2 | 0:9c486d50434e | 76 | * |
alex89_2 | 0:9c486d50434e | 77 | * Variables: |
alex89_2 | 0:9c486d50434e | 78 | * fptr - A pointer to a void function, or 0 to set as none |
alex89_2 | 0:9c486d50434e | 79 | */ |
alex89_2 | 0:9c486d50434e | 80 | void fall (void (*fptr)(void)); |
alex89_2 | 0:9c486d50434e | 81 | |
alex89_2 | 2:8805f880e231 | 82 | /** Function: operator joyhv |
alex89_2 | 0:9c486d50434e | 83 | * An operator shorthand for <read()> |
alex89_2 | 0:9c486d50434e | 84 | * |
alex89_2 | 0:9c486d50434e | 85 | * The joyhv() operator can be used as a shorthand for <read()> to simplify common code sequences |
alex89_2 | 0:9c486d50434e | 86 | * |
alex89_2 | 0:9c486d50434e | 87 | */ |
alex89_2 | 0:9c486d50434e | 88 | operator joyhv (); |
alex89_2 | 0:9c486d50434e | 89 | |
alex89_2 | 0:9c486d50434e | 90 | |
alex89_2 | 0:9c486d50434e | 91 | joyhv scale(joyhv read); |
alex89_2 | 0:9c486d50434e | 92 | joyhv filter(joyhv read, double factor); |
alex89_2 | 0:9c486d50434e | 93 | |
alex89_2 | 0:9c486d50434e | 94 | |
alex89_2 | 0:9c486d50434e | 95 | private: |
alex89_2 | 0:9c486d50434e | 96 | InterruptIn _b; |
alex89_2 | 0:9c486d50434e | 97 | AnalogIn _h; |
alex89_2 | 0:9c486d50434e | 98 | AnalogIn _v; |
alex89_2 | 0:9c486d50434e | 99 | }; |
alex89_2 | 0:9c486d50434e | 100 | |
alex89_2 | 0:9c486d50434e | 101 | |
alex89_2 | 0:9c486d50434e | 102 | #endif |