-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathJoystickShield.h
executable file
·192 lines (160 loc) · 5.13 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
JoystickShield - Arduino Library for JoystickShield (http://hardwarefun.com/projects/joystick-shield)
*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> 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
* ----------------------------------------------------------------------------
* 2014 edit by Markus Mücke, muecke.ma(a)gmail.com
* Changes for JoystickShield V1.2
* added a function to read the amplitude of the joystick
* added a auto calibrate function for 3.3V and 5V mode
*
* Added functions:
* Functions for F and E Button
* Calibrate Joystick
* xAmplitude
* yAmplitude
*
* 20th October 2015 edit by Lindsay Ward, https://github.com/lindsaymarkward
* made buttons not mutually exclusive
* functions report the current button state so multiple buttons can be pressed at one time
*/
#ifndef JoystickShield_H
#define JoystickShield_H
#define CENTERTOLERANCE 5
// 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
};
static const bool ALL_BUTTONS_OFF[7] = {false, false, false, false, false, false, false};
/**
* 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, byte pinF, byte pinE);
void setButtonPinsUnpressedState(byte pinSelect, byte pinUp, byte pinRight, byte pinDown, byte pinLeft, byte pinF, byte pinE);
void setThreshold(int xLow, int xHigh, int yLow, int yHigh);
void processEvents();
void processCallbacks();
void calibrateJoystick();
// Joystick events
bool isCenter();
bool isUp();
bool isRightUp();
bool isRight();
bool isRightDown();
bool isDown();
bool isLeftDown();
bool isLeft();
bool isLeftUp();
bool isNotCenter();
// Joystick coordinates
int xAmplitude();
int yAmplitude();
// Button events
bool isJoystickButton();
bool isUpButton();
bool isRightButton();
bool isDownButton();
bool isLeftButton();
bool isFButton();
bool isEButton();
// 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));
void onJSnotCenter(void (*notCenterCallback)(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 onFButton(void (*FButtonCallback)(void));
void onEButton(void (*EButtonCallback)(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_F_button;
byte pin_E_button;
byte pin_joystick_button_unpressed;
byte pin_up_button_unpressed;
byte pin_right_button_unpressed;
byte pin_down_button_unpressed;
byte pin_left_button_unpressed;
byte pin_F_button_unpressed;
byte pin_E_button_unpressed;
// joystick
byte joystickStroke;
int x_position;
int y_position;
//current states of Joystick
JoystickStates currentStatus;
// array of button states to allow multiple buttons to be pressed concurrently
// order is up, right, down, left, e, f, joystick
bool buttonStates[7];
// 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);
void (*notCenterCallback)(void);
// Button callbacks
void (*jsButtonCallback)(void);
void (*upButtonCallback)(void);
void (*rightButtonCallback)(void);
void (*downButtonCallback)(void);
void (*leftButtonCallback)(void);
void (*FButtonCallback)(void);
void (*EButtonCallback)(void);
// helper functions
void clearButtonStates();
void initializeCallbacks();
};
#endif