forked from sudar/JoystickShield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoystickShield.cpp
More file actions
559 lines (485 loc) · 12.8 KB
/
JoystickShield.cpp
File metadata and controls
559 lines (485 loc) · 12.8 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
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
/**
JoystickShield - Arduino Library for JoystickShield (http://hardwarefun.com/projects/joystick-shield)
Copyright 2011 Sudar Muthu (email : sudar@sudarmuthu.com)
*
* ----------------------------------------------------------------------------
* "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
* ----------------------------------------------------------------------------
Confirgured for Nokia 5110 Joystick shield v2.4. (http://www.elecfreaks.com/wiki/index.php?title=Joystick_Shield)
remapped button pins. added 2 additional 'START' and "SELECT' buttons
modified yLow threshold
danielelijahsmith@gmail.com
https://github.com/RoboDonut
*/
#include "JoystickShield.h"
/**
* Constructor
*
*/
JoystickShield::JoystickShield() {
// define some threshold values. Change them if your Joystick is different
//int xLow, int xHigh, int yLow, int yHigh
setThreshold(510, 530, 490, 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.
// pinSelect (joystick), pinUp, pinRight, pinDown, pinLeft,
setButtonPins(8, 2, 3, 4, 5,6,7); // 6 is button E and 7 is button F
// by default set the position to centered
currentStatus = CENTER;
// by default set the button state to NO_BUTTON
currentButton = NO_BUTTON;
// 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
* modify setbutton pins function inputs to: byte pinJoy, byte pinUp, byte pinRight, byte pinDown, byte pinLeft, byte pinSelect, byte pinStart
*/
void JoystickShield::setButtonPins(byte pinJoy, byte pinUp, byte pinRight, byte pinDown, byte pinLeft, byte pinSelect, byte pinStart) {
pin_joystick_button = pinJoy;
pin_up_button = pinUp;
pin_right_button = pinRight;
pin_down_button = pinDown;
pin_left_button = pinLeft;
pin_select_button = pinSelect;
pin_start_button = pinStart;
// 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_select_button , INPUT);
pinMode(pin_start_button , INPUT);
// Enable "pull-up resistors" for buttons
digitalWrite(pin_joystick_button, HIGH);
digitalWrite(pin_up_button , HIGH);
digitalWrite(pin_right_button , HIGH);
digitalWrite(pin_down_button , HIGH);
digitalWrite(pin_left_button , HIGH);
digitalWrite(pin_select_button , HIGH);
digitalWrite(pin_start_button , HIGH);
}
/**
* 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;
}
/**
* Process Events. This should be called in the loop()
*
*/
void JoystickShield::processEvents() {
int x_direction = 0;
int y_direction = 0;
// read from Joystick pins
int x_position = analogRead(pin_analog_x);
int y_position = analogRead(pin_analog_y);
// determine Joystick direction
if (x_position > x_threshold_high) {
x_direction = 1;
} else if (x_position < x_threshold_low) {
x_direction = -1;
}
if (y_position > y_threshold_high) {
y_direction = 1;
} else if (y_position < y_threshold_low) {
y_direction = -1;
}
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
if (digitalRead(pin_joystick_button) == LOW) {
currentButton = JOYSTICK_BUTTON;
}
if (digitalRead(pin_up_button) == LOW) {
currentButton = UP_BUTTON;
}
if (digitalRead(pin_right_button) == LOW) {
currentButton = RIGHT_BUTTON;
}
if (digitalRead(pin_down_button) == LOW) {
currentButton = DOWN_BUTTON;
}
if (digitalRead(pin_left_button) == LOW) {
currentButton = LEFT_BUTTON;
}
if (digitalRead(pin_select_button) == LOW) {
currentButton = SELECT_BUTTON;
}
if (digitalRead(pin_start_button) == LOW) {
currentButton = START_BUTTON;
}
}
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();
}
// 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 (isSelectButton() && selectButtonCallback != NULL) {
selectButtonCallback();
}
if (isStartButton() && startButtonCallback != NULL) {
startButtonCallback();
}
}
/**
* 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 button pressed
*
*/
bool JoystickShield::isJoystickButton() {
if (currentButton == JOYSTICK_BUTTON) {
clearButtonStates();
return true;
} else {
return false;
}
}
/**
* Up button pressed
*
*/
bool JoystickShield::isUpButton() {
if (currentButton == UP_BUTTON) {
clearButtonStates();
return true;
} else {
return false;
}
}
/**
* Right button pressed
*
*/
bool JoystickShield::isRightButton() {
if (currentButton == RIGHT_BUTTON) {
clearButtonStates();
return true;
} else {
return false;
}
}
/**
* Down button pressed
*
*/
bool JoystickShield::isDownButton() {
if (currentButton == DOWN_BUTTON) {
clearButtonStates();
return true;
} else {
return false;
}
}
/**
* Left button pressed
*
*/
bool JoystickShield::isLeftButton() {
if (currentButton == LEFT_BUTTON) {
clearButtonStates();
return true;
} else {
return false;
}
}
/**
* Select button pressed
*
*/
bool JoystickShield::isSelectButton() {
if (currentButton == SELECT_BUTTON) {
clearButtonStates();
return true;
} else {
return false;
}
}
/**
* Start button pressed
*
*/
bool JoystickShield::isStartButton() {
if (currentButton == START_BUTTON) {
clearButtonStates();
return true;
} else {
return false;
}
}
/**
* 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;
}
/****************************************************************** */
/**
* 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::onSelectButton(void (*selectButtonCallback)(void)) {
this->selectButtonCallback = selectButtonCallback;
}
void JoystickShield::onStartButton(void (*startButtonCallback)(void)) {
this->startButtonCallback = startButtonCallback;
}
/****************************************************************** */
/**
* Clear the current button state
*
*/
void JoystickShield::clearButtonStates() {
currentButton = NO_BUTTON;
}
/**
* 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;
// Button callbacks
jsButtonCallback = NULL;
upButtonCallback = NULL;
rightButtonCallback = NULL;
downButtonCallback = NULL;
leftButtonCallback = NULL;
selectButtonCallback = NULL;
startButtonCallback = NULL;
}