-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gkeypad.h
117 lines (89 loc) · 2.53 KB
/
Gkeypad.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
//#define NUMBER_PIN 11
#include <Arduino.h>
#define makeKeymap(x) ((char*)x)
#ifndef NULL
#define NULL 0
#endif
typedef enum{ IDLE, PRESSED, HOLD, RELEASED } KeyState;
class Gkeypad {
public:
Gkeypad(char *Keymap, int *Keypins, int NUM_PIN, uint16_t Dbounce=100, uint16_t htime=700){
NUMBER_PIN=NUM_PIN;
keyPins= Keypins;
giantKeymap = Keymap;
holdTime=htime;
Bounce=Dbounce;
};
void setHoldTime(uint16_t htime){
holdTime=htime;
}
void setDebounceTime(uint16_t Dbounce){
Bounce=Dbounce;
}
int *keyPins;
char *giantKeymap;
int NUMBER_PIN;
int I,J,numkey,oldKey;
uint16_t holdTime,Bounce;
uint64_t pushTime;
int correctNum(int ii , int jj){
int small,big,counter=0;
if (ii <= jj) {small=ii;big=jj;}//return ((I*NUMBER_PIN)+J);
else if (jj < ii) {small=jj;big=ii;}// return ((J*NUMBER_PIN)+I);
for(int i=0; i<small ; i++){
counter+=(NUMBER_PIN)-i-1;
}
counter+=big;
return (counter);
}
int getKey(){
oldKey= numkey;
// check pin GND //
inputEX(-1); //make all of the pins as output
for(int j=0 ; j< NUMBER_PIN ;j++){
if (!digitalRead(keyPins[j])) {I=NUMBER_PIN; J=j;return(numkey=correctNum(I,J));}
}
//end check pin GND //
int k=NUMBER_PIN;
for(int i=0 ; i< NUMBER_PIN ;i++){
inputEX(keyPins[i]);
for(int j=0 ; j< k ;j++){
int newj= NUMBER_PIN-j;
if (!digitalRead(keyPins[newj])) {
digitalWrite(keyPins[i],HIGH);
// delay(1);
if (!digitalRead(keyPins[newj]))goto skip; //wrong check! GND pin is attached
else I=i; J=newj ;
return(numkey=correctNum(I,J));
}
}
skip:
k--; //for romoving mirror situation.
}
pushTime=millis();
I=-1;
J=0;
return(NULL);
}
char getCharKey(){
if(getKey()){
numkey=correctNum(I,J);
return(giantKeymap[numkey-1]);
}else return(NULL);
}
KeyState getState(){
int key=getKey();
if (key== NULL) {pushTime=millis(); return RELEASED;}
else if (key== oldKey && millis()-pushTime<Bounce) return IDLE;
else if (key== oldKey && millis()-pushTime<holdTime)return PRESSED;
else if (key== oldKey && millis()-pushTime>holdTime)return HOLD;
}
protected:
void inputEX(int GPIO){
for(int i=0 ; i< NUMBER_PIN ;i++){
if (keyPins[i] !=GPIO) pinMode(keyPins[i],INPUT_PULLUP);
}
pinMode(GPIO,OUTPUT);
digitalWrite(GPIO,LOW);
}
};