-
Notifications
You must be signed in to change notification settings - Fork 1
/
bt.cpp
273 lines (224 loc) · 10.6 KB
/
bt.cpp
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
#include "bt.hpp"
#include "headerimages/gh/gh_american.jpg.h"
#include "headerimages/gh/gh_anser.jpg.h"
#include "headerimages/gh/gh_barnacle.jpg.h"
#include "headerimages/gh/gh_chinese.jpg.h"
#include "headerimages/gh/gh_cotton.jpg.h"
#include "headerimages/gh/gh_dansk.jpg.h"
#include "headerimages/gh/gh_embden.jpg.h"
#include "headerimages/gh/gh_oie.jpg.h"
#include "headerimages/gh/gh_pokeri.jpg.h"
#include "headerimages/gh/gh_pomm.jpg.h"
#include "headerimages/gh/gh_roman.jpg.h"
#include "headerimages/gh/gh_whiteseb.jpg.h"
#include "headerimages/gh/gh_goosen.jpg.h"
#ifdef ESP32
void MyAdvertisedDeviceCallbacks::onResult(BLEAdvertisedDevice advertisedDevice)
{
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
//advertisedDevice.toString().c_str();
}
#endif
#define SERVICE_UUID "fff7d507-93d5-4b90-bed3-c730a28c9e33"
#define CHARACTERISTIC_UUID "3587bc19-07f7-4766-8dd2-345c8f0404a1"
#define SCAN_NAME_SIZE (20)
#ifdef ESP32
BLEScan *pBLEScan = nullptr;
#endif
int hapticPin = 35;
unsigned long btFrameTime_last = 0;
bool initup = false;
bool scanStarted = false;
bool btLoop(GameBuff *gameBuff)
{
if (!initup)
{
initup = true;
int i = 0;
displayClear(gameBuff, 0x00);
drawString(gameBuff, (char *)"Wifi ON!", 0, 8 * i++, 0xFF, 0);
#ifdef ESP32
Serial.println("Scanning...");
pinMode(hapticPin, OUTPUT);
digitalWrite(hapticPin, HIGH);
Serial.println("D");
BLEDevice::init("Badge McBadgeFace");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
/*
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
Serial.println("C");
pCharacteristic->setValue("Hello World says Neil");
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
*/
#endif
btFrameTime_last = gameBuff->timeInMillis;
}
if (gameBuff->playerKeys.debouncedInput && gameBuff->playerKeys.start && gameBuff->playerKeys.select)
{
return true;
}
if (gameBuff->timeInMillis - btFrameTime_last > 3000)
{
btFrameTime_last = gameBuff->timeInMillis;
#ifdef ESP32
Serial.print("MAIN Executing on core ");
Serial.println(xPortGetCoreID());
#endif
}
displayClear(gameBuff, 0x00);
//drawString(gameBuff, (char*)"BLUETOOTH", 0, 0, 0xFF, 0);
#ifdef ESP32
BLEScanResults foundDevices = pBLEScan->start(1, false);
Serial.println("found device count:");
Serial.println(foundDevices.getCount());
int withNameCount = 0;
for(int i = 0; i < foundDevices.getCount(); i++) {
if(foundDevices.getDevice(i).haveName()) {
withNameCount++;
}
}
/*
fEdvb3NlfExlZ2VufGlkWSBm
fCBpcyBsfGQgaXMgfHJwak1t
fG9vc2UgfEBmbHVmfGUgUFhv
fFRoZXkgfGZ5cG9ufCBRUSBq
fGhhdmUgfHkgbG9zfENyIFVh
fHRoZSBrfHQgaGlzfCB0aCBh
fGV5cyBGfCBwdnQgfHVNSWJl
fGluZCB0fGtleXMgfCBwSk0g
fGhlbSBhfGluIGJvfFdhZWxr
fG5kIGRlfGF0aW5nfCBkYWdi
fHN0cm95fCBhY2NpfG91ciBY
fCB0aGVtfGRlbnQgfGZjVWFp
*/
int threshold = -55;
for (int j = 0; j < foundDevices.getCount(); j++)
{
BLEAdvertisedDevice foundDevice = foundDevices.getDevice(j);
if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "b4:e6:2d:9d:bb:07", 17) == 0)) {
Serial.println("Roman");
TJpgDec.drawJpg(0, 0, gh_roman_jpg, sizeof(gh_roman_jpg));
drawString(gameBuff, (char *)"fEdvb3NlfExlZ2VufGlkWSBm", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "b4:e6:2d:9d:bf:e3", 17) == 0)) {
Serial.println("CottonPatch");
TJpgDec.drawJpg(0, 0, gh_cotton_jpg, sizeof(gh_cotton_jpg));
drawString(gameBuff, (char *)"fCBpcyBsfGQgaXMgfHJwak1t", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "a4:cf:12:6a:4e:4a", 17) == 0)) {
Serial.println("Toulouse");
TJpgDec.drawJpg(0, 0, gh_oie_jpg, sizeof(gh_oie_jpg));
drawString(gameBuff, (char *)"fG9vc2UgfEBmbHVmfGUgUFhv", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "24:6f:28:16:53:4a", 17) == 0)) {
Serial.println("AmericanBuff");
TJpgDec.drawJpg(0, 0, gh_american_jpg, sizeof(gh_american_jpg));
drawString(gameBuff, (char *)"fFRoZXkgfGZ5cG9ufCBRUSBq", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "a4:cf:12:45:cc:5e", 17) == 0)) {
Serial.println("Emden");
TJpgDec.drawJpg(0, 0, gh_embden_jpg, sizeof(gh_embden_jpg));
drawString(gameBuff, (char *)"fGhhdmUgfHkgbG9zfENyIFVh", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "24:6f:28:16:d8:ea", 17) == 0)) {
Serial.println("Faroese");
TJpgDec.drawJpg(0, 0, gh_pokeri_jpg, sizeof(gh_pokeri_jpg));
drawString(gameBuff, (char *)"fHRoZSBrfHQgaGlzfCB0aCBh", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "24:6f:28:16:df:76", 17) == 0)) {
Serial.println("Pomeranian");
TJpgDec.drawJpg(0, 0, gh_pomm_jpg, sizeof(gh_pomm_jpg));
drawString(gameBuff, (char *)"fGV5cyBGfCBwdnQgfHVNSWJl", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "a4:cf:12:45:d8:4e", 17) == 0)) {
Serial.println("Sebastopol");
TJpgDec.drawJpg(0, 0, gh_whiteseb_jpg, sizeof(gh_whiteseb_jpg));
drawString(gameBuff, (char *)"fGluZCB0fGtleXMgfCBwSk0g", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "24:6f:28:18:23:d6", 17) == 0)) {
Serial.println("Barnacle");
TJpgDec.drawJpg(0, 0, gh_barnacle_jpg, sizeof(gh_barnacle_jpg));
drawString(gameBuff, (char *)"fGhlbSBhfGluIGJvfFdhZWxr", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "24:6f:28:18:22:ba", 17) == 0)) {
Serial.println("Chinese");
TJpgDec.drawJpg(0, 0, gh_chinese_jpg, sizeof(gh_chinese_jpg));
drawString(gameBuff, (char *)"fG5kIGRlfGF0aW5nfCBkYWdi", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "24:6f:28:18:26:ca", 17) == 0)) {
Serial.println("Landrace");
TJpgDec.drawJpg(0, 0, gh_dansk_jpg, sizeof(gh_dansk_jpg));
drawString(gameBuff, (char *)"fHN0cm95fCBhY2NpfG91ciBY", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "24:6f:28:16:56:56", 17) == 0)) {
Serial.println("PinkFooted");
TJpgDec.drawJpg(0, 0, gh_anser_jpg, sizeof(gh_anser_jpg));
drawString(gameBuff, (char *)"fCB0aGVtfGRlbnQgfGZjVWFp", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
} else if((foundDevice.getRSSI() > threshold) && (strncmp(foundDevice.getAddress().toString().c_str(), "24:6f:28:16:d9:3e", 17) == 0)) {
Serial.println("Goosen");
TJpgDec.drawJpg(0, 0, gh_goosen_jpg, sizeof(gh_goosen_jpg));
drawString(gameBuff, (char *)"The Golden Gooose", 0, 8, 0xFF, 0);
drawString(gameBuff, (char *)foundDevice.getName().c_str(), 0, 220, 0xFF, 0);
return false;
}
}
drawString(gameBuff, (char *)": WHICH GOOSE?", 0, 8, 0xFF, 0);
if (withNameCount > 0)
{
int y = 18;
for (int i = 0; i < foundDevices.getCount(); i++)
{
char scanDisplayText[40];
BLEAdvertisedDevice foundDevice = foundDevices.getDevice(i);
if (!foundDevice.haveName())
{
continue;
}
sprintf(scanDisplayText, "%s (%d)\n", foundDevice.getName().c_str(), foundDevice.getRSSI());
/*
else
{
sprintf(scanDisplayText, "%s (%d)\n", foundDevice.getAddress().toString().c_str(), foundDevice.getRSSI());
}
*/
int textColour = 0xFF;
drawString(gameBuff, scanDisplayText, 10, y, textColour, 0);
y += 8;
}
}
else
{
drawString2x(gameBuff, "NO GOOSE EGGS", 20, 120, 0xFF, 0);
}
#endif
return false;
}