-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdpad.cpp
More file actions
93 lines (72 loc) · 1.71 KB
/
dpad.cpp
File metadata and controls
93 lines (72 loc) · 1.71 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
/*
*
* Dpad for Arduino TinyTetris
*
* Designed for https://github.com/AJRussell/Tiny-Tetris
*
* Clumsy code by tobozo (c+) 2016
* Apologies for coding horror
*
*/
#ifndef DPADCPP
#define DPADCPP
#include <Arduino.h>
#ifndef KEYPAD_PIN
#define KEYPAD_PIN A0
#endif
// this is for analog 5x momentary buttons dpad
// as seen here http://fritzing.org/projects/arduino-5-buttons-keypad
// put your resistor calibration values here
const int dpad[5][2] = {
{460, 480}, //KEY_MIDDLE 0
{122, 234}, //KEY_LEFT 1
{700, 750}, //KEY_RIGHT 2
{-1, 70}, //KEY_DOWN 3
{260, 350} //KEY_ROTATE 4
};
static int dpadwarp[5] = { 0,0,0,0,0 };
static volatile int Debounce = 0;
static volatile bool processKey = true;
static volatile int currentPos;
class Dpad
{
static const int DebounceMax = 10;
public:
static int getPos() {
currentPos = getPosition(KEYPAD_PIN)*1;
delay(100);
for(int i=0;i<5;i++) {
if(currentPos > dpad[i][0] && currentPos < dpad[i][1]) {
return i;
}
}
return -1;
}
static boolean DoDebounce() {
Debounce++;
if(Debounce > DebounceMax) {
return true;
}
return false;
}
static int setAccel(int acceleration, int offset) {
if(processKey) {
dpadwarp[currentPos] = millis();
}
if(millis() < dpadwarp[currentPos] + offset) {
processKey = false;
} else {
processKey = true;
acceleration = acceleration + 70;
if (acceleration > offset) {
acceleration = offset;
}
}
return acceleration;
}
private:
static int getPosition(int pin) {
return analogRead(pin);
}
};
#endif