forked from sudar/JoystickShield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoystickShield.h
More file actions
163 lines (138 loc) · 4.03 KB
/
Copy pathJoystickShield.h
File metadata and controls
163 lines (138 loc) · 4.03 KB
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
155
156
157
158
159
160
161
162
163
/**
JoystickShield - Arduino Library for JoystickShield (http://hardwarefun.com/projects/joystick-shield)
*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <sudar@sudarmuthu.com> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer or coffee in return - Sudar
* ----------------------------------------------------------------------------
*/
#ifndef JoystickShield_H
#define JoystickShield_H
// Compatibility for Arduino 1.0
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
/**
* 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,
SELECT_BUTTON,
START_BUTTON //7
};
/**
* Class to encapsulate JoystickShield
*/
class JoystickShield {
public:
JoystickShield(); // constructor
void setJoystickPins (byte pinX, byte pinY);
void setButtonPins(byte pinJoy, byte pinUp, byte pinRight, byte pinDown, byte pinLeft, byte pinSelect, byte pinStart);
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();
bool isSelectButton();
bool isStartButton();
// 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));
void onSelectButton(void (*SelectButtonCallback)(void));
void onStartButton(void (*StartButtonCallback)(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;
byte pin_select_button;
byte pin_start_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);
void (*selectButtonCallback)(void);
void (*startButtonCallback)(void);
// helper functions
void clearButtonStates();
void initializeCallbacks();
};
#endif