forked from sudar/JoystickShield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoystickShield.h
154 lines (127 loc) · 3.87 KB
/
JoystickShield.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
JoystickShield - Arduino Library for JoystickShield (http://sudarmuthu.com/arduino/joystickshield)
Copyright 2011 Sudar Muthu (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef JoystickShield_H
#define JoystickShield_H
#include <WProgram.h>
/**
* Enum to hold the different states of the Joystick
*
*/
enum JoystickStates {
CENTER, // 0
UP,
RIGHT_UP,
RIGHT,
RIGHT_DOWN,
DOWN,
LEFT_DOWN,
LEFT,
LEFT_UP //8
};
/**
* Enum to hold the button states
*
*/
enum ButtonStates {
NO_BUTTON, //0
JOYSTICK_BUTTON,
UP_BUTTON,
RIGHT_BUTTON,
DOWN_BUTTON,
LEFT_BUTTON //5
};
/**
* Class to encapsulate JoystickShield
*/
class JoystickShield {
public:
JoystickShield(); // constructor
void setJoystickPins (byte pinX, byte pinY);
void setButtonPins(byte pinSelect, byte pinUp, byte pinRight, byte pinDown, byte pinLeft);
void setThreshold(int xLow, int xHigh, int yLow, int yHigh);
void processEvents();
void processCallbacks();
// Joystick events
bool isCenter();
bool isUp();
bool isRightUp();
bool isRight();
bool isRightDown();
bool isDown();
bool isLeftDown();
bool isLeft();
bool isLeftUp();
// Button events
bool isJoystickButton();
bool isUpButton();
bool isRightButton();
bool isDownButton();
bool isLeftButton();
// Joystick callbacks
void onJSCenter(void (*centerCallback)(void));
void onJSUp(void (*upCallback)(void));
void onJSRightUp(void (*rightUpCallback)(void));
void onJSRight(void (*rightCallback)(void));
void onJSRightDown(void (*rightDownCallback)(void));
void onJSDown(void (*downCallback)(void));
void onJSLeftDown(void (*leftDownCallback)(void));
void onJSLeft(void (*leftCallback)(void));
void onJSLeftUp(void (*leftUpCallback)(void));
// Button callbacks
void onJoystickButton(void (*jsButtonCallback)(void));
void onUpButton(void (*upButtonCallback)(void));
void onRightButton(void (*rightButtonCallback)(void));
void onDownButton(void (*downButtonCallback)(void));
void onLeftButton(void (*leftButtonCallback)(void));
private:
// threshold values
int x_threshold_low;
int x_threshold_high;
int y_threshold_low;
int y_threshold_high;
// joystick pins
byte pin_analog_x;
byte pin_analog_y;
//button pins
byte pin_joystick_button;
byte pin_up_button;
byte pin_right_button;
byte pin_down_button;
byte pin_left_button;
//current states of Joystick
JoystickStates currentStatus;
//current button states
ButtonStates currentButton;
// Joystick callbacks
void (*centerCallback)(void);
void (*upCallback)(void);
void (*rightUpCallback)(void);
void (*rightCallback)(void);
void (*rightDownCallback)(void);
void (*downCallback)(void);
void (*leftDownCallback)(void);
void (*leftCallback)(void);
void (*leftUpCallback)(void);
// Button callbacks
void (*jsButtonCallback)(void);
void (*upButtonCallback)(void);
void (*rightButtonCallback)(void);
void (*downButtonCallback)(void);
void (*leftButtonCallback)(void);
// helper functions
void clearButtonStates();
void initializeCallbacks();
};
#endif