-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_reading.ino
152 lines (145 loc) · 2.61 KB
/
fast_reading.ino
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
// https://git.dejvino.cz/dejvino/xt-keyboard-adapter/src/branch/master/XtKeyboard.ino
// https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
int clockPin = 3;
int dataPin = 2;
volatile byte data = B0;
int test = 0;
volatile int counter = 0;
int numbits = 9;
unsigned long repeatTimeout = 0;
unsigned long repeatTimeoutValue = 100;
const int keyReleaseBit = 0x80;
char m[255];
void processByte(byte data) {
bool isRelease = data & keyReleaseBit;
int key = data - (isRelease ? keyReleaseBit : 0);
Serial.print("Scan: <" + String(data) + "> " + "Key: <" + String(key) + "> " + "<" + String(m[key]) + "> ");
if (isRelease) {
Serial.print("Release: " + String(key));
} else {
Serial.print("Press: " + String(key));
}
Serial.println(" Button: " + String(m[key]) );
}
void setupKeyMapping() {
m[1]='-';
m[2]='1';
m[3]='2';
m[4]='3';
m[5]='4';
m[6]='5';
m[7]='6';
m[8]='7';
m[9]='8';
m[10]='9';
m[11]='0';
m[12]='-';
m[13]='=';
m[14]='-';
m[15]='-';
m[16]='q';
m[17]='w';
m[18]='e';
m[19]='r';
m[20]='t';
m[21]='y';
m[22]='u';
m[23]='i';
m[24]='o';
m[25]='p';
m[26]='[';
m[27]=']';
m[28]='-';
m[29]='-';
m[30]='a';
m[31]='s';
m[32]='d';
m[33]='f';
m[34]='g';
m[35]='h';
m[36]='j';
m[37]='k';
m[38]='l';
m[39]=';';
m[40]='\'';
m[41]='-';
m[42]='-';
m[43]='\\';
m[44]='z';
m[45]='x';
m[46]='c';
m[47]='v';
m[48]='b';
m[49]='n';
m[50]='m';
m[51]=',';
m[52]='.';
m[53]='/';
m[54]='-';
m[55]='-';
m[56]='-';
m[57]='-';
m[58]='-';
m[59]='f';
m[60]='f';
m[61]='f';
m[62]='f';
m[63]='f';
m[64]='f';
m[65]='f';
m[66]='f';
m[67]='f';
m[68]='f';
m[69]='-';
m[70]='-';
m[71]='7';
m[72]='8';
m[73]='9';
m[74]='-';
m[75]='4';
m[76]='5';
m[77]='6';
m[78]='+';
m[79]='1';
m[80]='2';
m[81]='3';
m[82]='0';
m[83]='.';
m[101]='-';
m[102]='-';
m[107]='-';
m[108]='-';
m[109]='-';
m[110]='-';
m[111]='-';
m[112]='-';
}
void clockInterrupt() {
int clockValue = digitalRead(clockPin);
if (clockValue == LOW && test == 0 && counter < numbits) {
test = 1;
data = data >> 1;
if (digitalRead(dataPin) == HIGH) {
bitSet(data, 7);
}
counter++;
}
if (clockValue == HIGH && test == 1) {
test = 0;
}
}
void setup() {
pinMode(dataPin, INPUT);
pinMode(clockPin, INPUT_PULLUP);
Serial.begin(115200);
setupKeyMapping();
attachInterrupt(digitalPinToInterrupt(clockPin), clockInterrupt, CHANGE);
}
void loop() {
if (counter >= numbits) {
processByte(data);
data = B0;
counter = 0;
repeatTimeout = millis() + repeatTimeoutValue;
}
}