-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model_F_BLE.ino
254 lines (222 loc) · 5.68 KB
/
Model_F_BLE.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
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
#include <BleKeyboard.h>
#include "driver/adc.h"
#define clk_pin 18
#define data_pin 21
#define led LED_BUILTIN // Used for NumLock
// FSM states defining
#define START_BITS_START 0x00
#define START_BITS_END 0x01
#define PAYLOAD_RECEIVING 0x02
BleKeyboard bk("Keyboard Model F XT", "IBM"); // Bluetooth Keyboard
uint8_t val;
uint8_t lastVal;
int received_bits = 0;
uint8_t state = START_BITS_START;
//size_t res = 0; // Sending result (0 --> error; 1 --> OK)
//uint8_t sending_counter = 0; // This variable prevents infinite sending loop
unsigned char translationTable[128] = {
0, // Not Used
KEY_ESC,
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'0',
'-', // ' and ?
0xb6,
KEY_BACKSPACE,
KEY_TAB,
'q',
'w',
'e',
'r',
't',
'y',
'u',
'i',
'o',
'p',
0xb7,
0xb8,
KEY_RETURN,
KEY_LEFT_CTRL,
'a',
's',
'd',
'f',
'g',
'h',
'j',
'k',
'l',
0xbb,
0xbc,
'ù',
KEY_LEFT_SHIFT,
0x60,
'z',
'x',
'c',
'v',
'b',
'n',
'm',
',',
'.',
0xc0,
KEY_RIGHT_SHIFT,
KEY_RIGHT_GUI,
KEY_LEFT_ALT,
' ',
KEY_CAPS_LOCK,
KEY_F1,
KEY_F2,
KEY_F3,
KEY_F4,
KEY_F5,
KEY_F6,
KEY_F7,
KEY_F8,
KEY_F9,
KEY_F10,
0xdb, // Num Lock
KEY_RIGHT_CTRL,
0xe7,
0xe8,
0xe9,
0xde,
0xe4,
0xe5,
0xe6,
0xdf,
0xe1,
0xe2,
0xe3,
0xea,
0xeb
};
void IRAM_ATTR clk_down() {
switch (state) {
case START_BITS_START:
if (!digitalRead(data_pin))
state = START_BITS_END;
else
state = START_BITS_START;
break;
case START_BITS_END:
if (digitalRead(data_pin))
state = PAYLOAD_RECEIVING;
else
state = START_BITS_END;
break;
case PAYLOAD_RECEIVING:
if (received_bits < 7) { // Receiving
val |= (digitalRead(data_pin) << received_bits);
received_bits++;
}
else { // Out Key
val |= (digitalRead(data_pin) << received_bits);
if (val != lastVal && (val & 0x7f) <= 83) {
pinMode(data_pin, OUTPUT); // These instructions prevent Keyboard from sending data during time-consuming operations (BLE connection)
digitalWrite(data_pin, LOW);
int msb = val & 0x80; // Only the byte's MSB is on
/*char buf[256];
if (msb)
sprintf(buf, "Released 0x%02x\n", val & 0x7f); // Debugging purpose
else
sprintf(buf, "Pressed 0x%02x\n", val & 0x7f); // Only the byte's MSB is off
Serial.print(buf);*/
/*if (bk.isConnected()) {
unsigned char key = translationTable[val & 0x7f];
if (msb) { // msb == 1 --> release
if (key >= 0x80) // If it is a modifier key
bk.release(key);
}
else { // msb == 0 --> press
if (key >= 0x80) { // If it is a modifier key
sending_counter = 0;
do {
res = bk.press(key);
sending_counter++;
} while (res == 0 && sending_counter < 2);
}
else {
sending_counter = 0;
do {
res = bk.write(key);
sending_counter++;
} while (res == 0 && sending_counter < 2);
}
}
if ((val & 0x7f) == 0x45 and !msb)
digitalWrite(led, !digitalRead(led));
}*/
if (bk.isConnected()) {
unsigned char key = translationTable[val & 0x7f];
if (msb) { // msb == 1 --> release
if (key >= 0x80) // If it is a modifier key
bk.release(key);
}
else { // msb == 0 --> press
if (key >= 0x80 && key != KEY_BACKSPACE && key != KEY_DELETE) // If it is a modifier key
bk.press(key);
else
bk.write(key);
}
if ((val & 0x7f) == 0x45 and !msb)
digitalWrite(led, !digitalRead(led));
}
lastVal = val;
pinMode(data_pin, INPUT_PULLUP); // Re-activate Keyboard sending data
}
received_bits = 0;
val = 0x00;
state = START_BITS_START;
}
break;
}
}
void goToDeepSleep()
{
adc_power_off();
//esp_wifi_stop();
}
void setup() {
/* BATTERY-SAVE */
//setCpuFrequencyMhz(80);
goToDeepSleep();
/* LED INIT */
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
/* BLE INIT */
bk.begin();
bk.setBatteryLevel(50); /// Future update
bk.releaseAll();
//remove
/*Serial.begin(115200);
Serial.println("start");*/
//end remove
/* SOFT RESET */
pinMode(data_pin, INPUT); // Data Line Hi
digitalWrite(data_pin, HIGH);
pinMode(clk_pin, INPUT);
digitalWrite(clk_pin, HIGH);
delay(5);
digitalWrite(clk_pin, LOW); // Falling Edge
delay(21); // Wait ~20ms
digitalWrite(clk_pin, HIGH); // Rising Edge
/* RECEIVING PIN MODE */
pinMode(clk_pin, INPUT_PULLUP);
pinMode(data_pin, INPUT_PULLUP);
/* WAIT */
//delay(100);
attachInterrupt(digitalPinToInterrupt(clk_pin), clk_down, FALLING);
}
void loop() {
;;
}