-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathJoystickShield.cpp
executable file
·601 lines (513 loc) · 14.5 KB
/
JoystickShield.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/**
JoystickShield - Arduino Library for JoystickShield (http://hardwarefun.com/projects/joystick-shield)
Copyright 2011 Sudar Muthu (email : [email protected])
*
* ----------------------------------------------------------------------------
* "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 state button so multiple buttons can be pressed at one time
*/
#include "JoystickShield.h"
/**
* Constructor
*
*/
JoystickShield::JoystickShield() {
// define some threshold values. Change them if your Joystick is different
setThreshold(510, 530, 510, 540);
// Sparkfun Joystick shield connects the Joystick to Pins 0 and 1.
// Change it if you are using a different shield
setJoystickPins(0, 1);
// Sparkfun Joystick shield connects the buttons to the following pins.
// change it if you are using a different shield.
setButtonPins(8, 2, 3, 4, 5, 7, 6);
// by default set the position to centered
currentStatus = CENTER;
// initialize the button states array to all buttons not pressed
memcpy(buttonStates, ALL_BUTTONS_OFF, sizeof(ALL_BUTTONS_OFF));
// initialize all callback function pointers to NULL
initializeCallbacks();
}
/**
* Set Analyog pins which are connected to the Joystick
*
*/
void JoystickShield::setJoystickPins(byte pinX, byte pinY) {
pin_analog_x = pinX;
pin_analog_y = pinY;
// set Joystick pins to input mode
pinMode(pin_analog_x, INPUT);
pinMode(pin_analog_y, INPUT);
}
/**
* Set the pins used by the buttons
* to deactivate a button use a pin outside of the range of the arduino e.g. 255
*/
void JoystickShield::setButtonPins(byte pinSelect, byte pinUp, byte pinRight, byte pinDown, byte pinLeft, byte pinF, byte pinE) {
pin_joystick_button = pinSelect;
pin_up_button = pinUp;
pin_right_button = pinRight;
pin_down_button = pinDown;
pin_left_button = pinLeft;
pin_F_button = pinF;
pin_E_button = pinE;
// set Button pins to input mode
pinMode(pin_joystick_button, INPUT);
pinMode(pin_up_button , INPUT);
pinMode(pin_right_button , INPUT);
pinMode(pin_down_button , INPUT);
pinMode(pin_left_button , INPUT);
pinMode(pin_E_button , INPUT);
pinMode(pin_F_button , INPUT);
// buttons use pull-up resistors by default
setButtonPinsUnpressedState(HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH);
}
/**
* Configure if the input from an unpressed button is HIGH or LOW.
* This will enable a pull-up or a pull-down resistor for the pin of the button correspondingly.
*/
void JoystickShield::setButtonPinsUnpressedState(byte pinSelect, byte pinUp, byte pinRight, byte pinDown, byte pinLeft, byte pinF, byte pinE) {
// store unpressed states
pin_joystick_button_unpressed = pinSelect ? HIGH : LOW;
pin_up_button_unpressed = pinUp ? HIGH : LOW;
pin_right_button_unpressed = pinRight ? HIGH : LOW;
pin_down_button_unpressed = pinDown ? HIGH : LOW;
pin_left_button_unpressed = pinLeft ? HIGH : LOW;
pin_F_button_unpressed = pinF ? HIGH : LOW;
pin_E_button_unpressed = pinE ? HIGH : LOW;
// Enable pull-up or pull-down resistors for buttons
digitalWrite(pin_joystick_button, pin_joystick_button_unpressed);
digitalWrite(pin_up_button , pin_up_button_unpressed );
digitalWrite(pin_right_button , pin_right_button_unpressed );
digitalWrite(pin_down_button , pin_down_button_unpressed );
digitalWrite(pin_left_button , pin_left_button_unpressed );
digitalWrite(pin_F_button , pin_F_button_unpressed );
digitalWrite(pin_E_button , pin_E_button_unpressed );
}
/**
* Configure threshold values for Joystick
*
*/
void JoystickShield::setThreshold(int xLow, int xHigh, int yLow, int yHigh) {
x_threshold_low = xLow;
x_threshold_high = xHigh;
y_threshold_low = yLow;
y_threshold_high = yHigh;
}
/**
* Calibrate Joystick
*
*/
void JoystickShield::calibrateJoystick() {
byte i;
// calibrate x
int xCenter = 0;
for(i = 0; i<10; i++)
xCenter += analogRead(pin_analog_x);
xCenter /= i;
// calibrate y
int yCenter = 0;
for(i = 0; i<10; i++)
yCenter += analogRead(pin_analog_y);
yCenter /= i;
// save Stroke of Joystick
joystickStroke = max(pin_analog_x, pin_analog_y)*1.01;
// set Center with tolerance
setThreshold(xCenter-CENTERTOLERANCE, xCenter+CENTERTOLERANCE, yCenter-CENTERTOLERANCE, yCenter+CENTERTOLERANCE);
}
/**
* Process Events. This should be called in the loop()
*
*/
void JoystickShield::processEvents() {
int x_direction = 0;
int y_direction = 0;
// read from Joystick pins
x_position = analogRead(pin_analog_x);
y_position = analogRead(pin_analog_y);
// determine Joystick direction
if (x_position > x_threshold_high) {
x_direction = 1;
x_position = map(x_position, x_threshold_high,x_threshold_high+x_threshold_low,0,100);
x_position = constrain(x_position,0,100);
} else if (x_position < x_threshold_low) {
x_direction = -1;
x_position = map(x_position, 0,x_threshold_low,-100,0);
} else
x_position = 0;
if (y_position > y_threshold_high) {
y_direction = 1;
y_position = map(y_position, y_threshold_high,y_threshold_high+y_threshold_low,0,100);
y_position = constrain(y_position,0,100);
} else if (y_position < y_threshold_low) {
y_direction = -1;
y_position = map(y_position, 0,y_threshold_low,-100,0);
} else
y_position = 0;
if (x_direction == -1) {
if (y_direction == -1) {
currentStatus = LEFT_DOWN;
} else if (y_direction == 0) {
currentStatus = LEFT;
} else {
currentStatus = LEFT_UP;
}
} else if (x_direction == 0) {
if (y_direction == -1) {
currentStatus = DOWN;
} else if (y_direction == 0) {
currentStatus = CENTER;
} else {
currentStatus = UP;
}
} else {
if (y_direction == -1) {
currentStatus = RIGHT_DOWN;
} else if (y_direction == 0) {
currentStatus = RIGHT;
} else {
currentStatus = RIGHT_UP;
}
}
// Determine which buttons were pressed, set button states array values to true/false accordingly
buttonStates[0] = digitalRead(pin_up_button) != pin_up_button_unpressed;
buttonStates[1] = digitalRead(pin_right_button) != pin_right_button_unpressed;
buttonStates[2] = digitalRead(pin_down_button) != pin_down_button_unpressed;
buttonStates[3] = digitalRead(pin_left_button) != pin_left_button_unpressed;
buttonStates[4] = digitalRead(pin_E_button) != pin_E_button_unpressed;
buttonStates[5] = digitalRead(pin_F_button) != pin_F_button_unpressed;
buttonStates[6] = digitalRead(pin_joystick_button) != pin_joystick_button_unpressed;
}
void JoystickShield::processCallbacks() {
processEvents();
// Joystick Callbacks
if (isCenter() && centerCallback != NULL) {
centerCallback();
}
if (isUp() && upCallback != NULL) {
upCallback();
}
if (isRightUp() && rightUpCallback != NULL) {
rightUpCallback();
}
if (isRight() && rightCallback != NULL) {
rightCallback();
}
if (isRightDown() && rightDownCallback != NULL) {
rightDownCallback();
}
if (isDown() && downCallback != NULL) {
downCallback();
}
if (isLeftDown() && leftDownCallback != NULL) {
leftDownCallback();
}
if (isLeft() && leftCallback != NULL) {
leftCallback();
}
if (isLeftUp() && leftUpCallback != NULL) {
leftUpCallback();
}
if (isNotCenter() && notCenterCallback != NULL) {
notCenterCallback();
}
// Button Callbacks
if (isJoystickButton() && jsButtonCallback != NULL) {
jsButtonCallback();
}
if (isUpButton() && upButtonCallback != NULL) {
upButtonCallback();
}
if (isRightButton() && rightButtonCallback != NULL) {
rightButtonCallback();
}
if (isDownButton() && downButtonCallback != NULL) {
downButtonCallback();
}
if (isLeftButton() && leftButtonCallback != NULL) {
leftButtonCallback();
}
if (isFButton() && FButtonCallback != NULL) {
FButtonCallback();
}
if (isEButton() && EButtonCallback != NULL) {
EButtonCallback();
}
}
/**
* Joystick in Center status
*
*/
bool JoystickShield::isCenter() {
if (currentStatus == CENTER ) {
return true;
} else {
return false;
}
}
/**
* Joystick in Up status
*
*/
bool JoystickShield::isUp() {
if (currentStatus == UP ) {
return true;
} else {
return false;
}
}
/**
* Joystick in Right Up status
*
*/
bool JoystickShield::isRightUp() {
if (currentStatus == RIGHT_UP ) {
return true;
} else {
return false;
}
}
/**
* Joystick in Right status
*
*/
bool JoystickShield::isRight() {
if (currentStatus == RIGHT ) {
return true;
} else {
return false;
}
}
/**
* Joystick in Right Down status
*
*/
bool JoystickShield::isRightDown() {
if (currentStatus == RIGHT_DOWN ) {
return true;
} else {
return false;
}
}
/**
* Joystick in Down status
*
*/
bool JoystickShield::isDown() {
if (currentStatus == DOWN ) {
return true;
} else {
return false;
}
}
/**
* Joystick in Left Down status
*
*/
bool JoystickShield::isLeftDown() {
if (currentStatus == LEFT_DOWN ) {
return true;
} else {
return false;
}
}
/**
* Joystick in Left status
*
*/
bool JoystickShield::isLeft() {
if (currentStatus == LEFT ) {
return true;
} else {
return false;
}
}
/**
* Joystick in Left Up status
*
*/
bool JoystickShield::isLeftUp() {
if (currentStatus == LEFT_UP) {
return true;
} else {
return false;
}
}
/**
* Joystick in not Center status
*
*/
bool JoystickShield::isNotCenter() {
if (currentStatus != CENTER) {
return true;
} else {
return false;
}
}
/**
* Joystick x position from -100 to 100, 0 = center
*
*/
int JoystickShield::xAmplitude() {
return x_position;
}
/**
* Joystick y position from -100 to 100, 0 = center
*
*/
int JoystickShield::yAmplitude() {
return y_position;
}
/**
* Up button pressed
*
*/
bool JoystickShield::isUpButton() {
return buttonStates[0];
}
/**
* Right button pressed
*
*/
bool JoystickShield::isRightButton() {
return buttonStates[1];
}
/**
* Down button pressed
*
*/
bool JoystickShield::isDownButton() {
return buttonStates[2];
}
/**
* Left button pressed
*
*/
bool JoystickShield::isLeftButton() {
return buttonStates[3];
}
/**
* E button pressed
*
*/
bool JoystickShield::isEButton() {
return buttonStates[4];
}
/**
* F button pressed
*
*/
bool JoystickShield::isFButton() {
return buttonStates[5];
}
/**
* Joystick button pressed
*
*/
bool JoystickShield::isJoystickButton() {
return buttonStates[6];
}
/**
* Joystick Callbacks
*
*/
/****************************************************************** */
void JoystickShield::onJSCenter(void (*centerCallback)(void)) {
this->centerCallback = centerCallback;
}
void JoystickShield::onJSUp(void (*upCallback)(void)) {
this->upCallback = upCallback;
}
void JoystickShield::onJSRightUp(void (*rightUpCallback)(void)) {
this->rightUpCallback = rightUpCallback;
}
void JoystickShield::onJSRight(void (*rightCallback)(void)) {
this->rightCallback = rightCallback;
}
void JoystickShield::onJSRightDown(void (*rightDownCallback)(void)) {
this->rightDownCallback = rightDownCallback;
}
void JoystickShield::onJSDown(void (*downCallback)(void)) {
this->downCallback = downCallback;
}
void JoystickShield::onJSLeftDown(void (*leftDownCallback)(void)) {
this->leftDownCallback = leftDownCallback;
}
void JoystickShield::onJSLeft(void (*leftCallback)(void)) {
this->leftCallback = leftCallback;
}
void JoystickShield::onJSLeftUp(void (*leftUpCallback)(void)) {
this->leftUpCallback = leftUpCallback;
}
void JoystickShield::onJSnotCenter(void (*notCenterCallback)(void)) {
this->notCenterCallback = notCenterCallback;
}
/****************************************************************** */
/**
* Button Callbacks
*
*/
/****************************************************************** */
void JoystickShield::onJoystickButton(void (*jsButtonCallback)(void)) {
this->jsButtonCallback = jsButtonCallback;
}
void JoystickShield::onUpButton(void (*upButtonCallback)(void)) {
this->upButtonCallback = upButtonCallback;
}
void JoystickShield::onRightButton(void (*rightButtonCallback)(void)) {
this->rightButtonCallback = rightButtonCallback;
}
void JoystickShield::onDownButton(void (*downButtonCallback)(void)) {
this->downButtonCallback = downButtonCallback;
}
void JoystickShield::onLeftButton(void (*leftButtonCallback)(void)) {
this->leftButtonCallback = leftButtonCallback;
}
void JoystickShield::onFButton(void (*FButtonCallback)(void)) {
this->FButtonCallback = FButtonCallback;
}
void JoystickShield::onEButton(void (*EButtonCallback)(void)) {
this->EButtonCallback = EButtonCallback;
}
/****************************************************************** */
/**
* Initialize all function pointers to NULL
*
*/
void JoystickShield::initializeCallbacks() {
// Joystick callbacks
centerCallback = NULL;
upCallback = NULL;
rightUpCallback = NULL;
rightCallback = NULL;
rightDownCallback = NULL;
downCallback = NULL;
leftDownCallback = NULL;
leftCallback = NULL;
leftUpCallback = NULL;
notCenterCallback = NULL;
// Button callbacks
jsButtonCallback = NULL;
upButtonCallback = NULL;
rightButtonCallback = NULL;
downButtonCallback = NULL;
leftButtonCallback = NULL;
FButtonCallback = NULL;
EButtonCallback = NULL;
}