wobblyhead hg > arduino > Joystick
comparison Joystick.h @ 0:f31cf3175189
v1.0
| author | ark |
|---|---|
| date | 2017-01-26 00:48 +0100 |
| parents | |
| children | fd012dcc048d |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f31cf3175189 |
|---|---|
| 1 #ifndef Joystick_h | |
| 2 #define Joystick_h | |
| 3 | |
| 4 #include <Potentiometer.h> | |
| 5 #include <Button.h> | |
| 6 | |
| 7 | |
| 8 /** | |
| 9 utility class for interpreting joystick values. | |
| 10 combines two potentiometer and calibration information to provide a vector | |
| 11 of user selected direction with 0,0 in the center and +x,+y in top-right. | |
| 12 | |
| 13 (c) 2017 arkadiusz@wobblyhead.net | |
| 14 */ | |
| 15 class Joystick { | |
| 16 public: | |
| 17 // x-axis pot | |
| 18 Potentiometer x; | |
| 19 // y-axis pot | |
| 20 Potentiometer y; | |
| 21 // optional button | |
| 22 Button b; | |
| 23 | |
| 24 // if calibrated, current vector, x component | |
| 25 float vx; | |
| 26 // if calibrated, current vector, y component | |
| 27 float vy; | |
| 28 | |
| 29 // flag, if true joystick is calibrated. | |
| 30 bool isCalibrated; | |
| 31 | |
| 32 // calibrated center of x axis | |
| 33 int centerX; | |
| 34 // calibrated center of y axis | |
| 35 int centerY; | |
| 36 | |
| 37 /** center deadzone radius. if the joystick is not calibrated, | |
| 38 * positions within this distance from the center are considered in the | |
| 39 * center. | |
| 40 */ | |
| 41 const int CENTER_DEADZONE = 50; | |
| 42 | |
| 43 /** | |
| 44 create an uninitialised Joystick. | |
| 45 setup() must be called after this to make it usable. | |
| 46 */ | |
| 47 Joystick(); | |
| 48 | |
| 49 /** | |
| 50 setup joystick instance with flipped y axis | |
| 51 | |
| 52 @param {byte} pinX input pin for x axis | |
| 53 @param {byte} pinY input pin for y axis | |
| 54 @param {byte} pinB input pin for button | |
| 55 */ | |
| 56 void setup(byte pinX, byte pinY, byte pinB); | |
| 57 | |
| 58 /** | |
| 59 setup joystick instance | |
| 60 | |
| 61 @param {byte} pinX input pin for x axis | |
| 62 @param {byte} pinY input pin for y axis | |
| 63 @param {byte} pinB input pin for button | |
| 64 @param {bool} flipY flip y axis | |
| 65 */ | |
| 66 void setup(byte pinX, byte pinY, byte pinB, bool flipY); | |
| 67 | |
| 68 /** | |
| 69 check inputs. | |
| 70 implicitely calls millis(); | |
| 71 */ | |
| 72 void update(); | |
| 73 | |
| 74 /** | |
| 75 check inputs, update values. | |
| 76 if calibrated, calculate vector. | |
| 77 @param {unsigned long} ms current millis() value | |
| 78 */ | |
| 79 void update(unsigned long ms); | |
| 80 | |
| 81 private: | |
| 82 bool flipY; | |
| 83 | |
| 84 }; | |
| 85 | |
| 86 #endif |
