-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
467 lines (397 loc) · 12.5 KB
/
main.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/**
* A BLE client example that is rich in capabilities.
* There is a lot new capabilities implemented.
* author unknown
* updated by chegewara
*/
#include "Arduino.h"
//#include "BLEDevice.h"
//#include "BluetoothSerial.h"
#include <WiFi.h>
#include <WiFiAP.h>
//#include <User_Setups/Setup25_TTGO_T_Display.h>
#include <TFT_eSPI.h>
#include <Button2.h>
#include <EBYTE.h>
#include <CircularBuffer.h>
//#include <WebServer.h>
// The remote service we wish to connect to.
/*
static BLEUUID serviceUUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
// The characteristic of the remote service we are interested in.
static BLEUUID charUUID("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
static BLEUUID rxUUID("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
*/
/*
static boolean doBleConnect = false;
static boolean bleConnected = false;
static boolean doScan = false;
static BLERemoteCharacteristic *pRemoteCharacteristic;
static BLERemoteCharacteristic *rxRemoteCharacteristic;
static BLEAdvertisedDevice *myDevice;
*/
#define ADC_EN 14
#define ADC_PIN 34
#define BUTTON_1 35
#define BUTTON_2 0
// EByte module connection
#define EB_M0 21
#define EB_M1 22
#define EB_RX 17
#define EB_TX 32
#define EB_AUX 33
// Vesc serial connection
#define VESC_RX 26
#define VESC_TX 25
EBYTE eb(&Serial2, EB_M0, EB_M1, EB_AUX);
TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library
Button2 btn1(BUTTON_1);
Button2 btn2(BUTTON_2);
const char *ssid = "vesc_extender";
const char *password = "12345678";
const int vescDefaultPort = 65102;
static WiFiServer server(vescDefaultPort);
static WiFiClient client;
// VESC produces about 800 bytes of configs data on vesc_tool connection
// We need to store it, while sending over slow LoRa chanel
static CircularBuffer<uint8_t, 2048> loraToSend;
const size_t max_buf = 2048;
uint8_t buf[max_buf];
/*
static void notifyCallback(
BLERemoteCharacteristic *pBLERemoteCharacteristic,
uint8_t *pData,
size_t length,
bool isNotify)
{
if (client.connected())
client.write(pData, length);
Serial.print(length);
Serial.print('*');
}
class MyClientCallback : public BLEClientCallbacks
{
void onConnect(BLEClient *pclient)
{
Serial.println("onConnect");
}
void onDisconnect(BLEClient *pclient)
{
bleConnected = false;
//doScan = true;
Serial.println("onDisconnect");
}
};
bool connectToBleUartServer()
{
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient *bleClient = BLEDevice::createClient();
Serial.println(" - Created client");
bleClient->setClientCallbacks(new MyClientCallback());
// Connect to the remove BLE Server.
bleClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
Serial.println(" - Connected to server");
BLEDevice::setPower(ESP_PWR_LVL_P9);
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService *pRemoteService = bleClient->getService(serviceUUID);
if (pRemoteService == nullptr)
{
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
bleClient->disconnect();
return false;
}
Serial.println(" - Found our service");
// Check remote device has RX characteristic
rxRemoteCharacteristic = pRemoteService->getCharacteristic(rxUUID);
if (rxRemoteCharacteristic == nullptr)
{
Serial.print("Failed to find BLE rx characteristic UUID: ");
Serial.println(rxUUID.toString().c_str());
bleClient->disconnect();
return false;
}
Serial.println(" - Found our rx characteristic");
if (rxRemoteCharacteristic->canRead())
{
std::string value = rxRemoteCharacteristic->readValue();
Serial.print("The rx canRead() value was: ");
Serial.println(value.c_str());
}
if (rxRemoteCharacteristic->canNotify())
{
Serial.println("rxUUID can notify");
//rxCharacteristic->registerForNotify(notifyCallback);
}
// Check remote device has TX characteristic
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr)
{
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
bleClient->disconnect();
return false;
}
Serial.println(" - Found our tx characteristic");
// Read the value of the characteristic.
if (pRemoteCharacteristic->canRead())
{
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if (pRemoteCharacteristic->canNotify())
{
Serial.println("charUUID can notify");
pRemoteCharacteristic->registerForNotify(notifyCallback);
}
BLERemoteDescriptor *d = pRemoteCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902));
if (d)
{
// Allow receive data notifications from Vesc
const uint8_t notificationOn[] = {0x1, 0x0};
d->writeValue((uint8_t *)notificationOn, 2, true);
Serial.println("Wrote TXON");
}
bleConnected = true;
return true;
}
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
// We have found a device, let us now see if it contains the service we are looking for.
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID))
{
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doBleConnect = true;
doScan = false;
Serial.println("doBleConnect");
} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks
*/
void initTDisplay()
{
tft.init();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setFreeFont(&FreeMono9pt7b);
}
class DisplayOffMessage
{
int countDown;
public:
DisplayOffMessage(int countDownStartsFrom = 10) : countDown(countDownStartsFrom) {}
void step()
{
if (countDown < 0)
return;
else if (countDown > 0)
{
String s = String("Display off - ") + countDown;
// Clear bottom line
tft.setTextDatum(BC_DATUM);
tft.setTextPadding(tft.textWidth(s) + 20);
tft.drawString(s, tft.width() / 2, tft.height());
}
else
{
tft.fillScreen(TFT_BLACK); // Clear to reduce blinking on reset
digitalWrite(TFT_BL, !TFT_BACKLIGHT_ON);
}
countDown--;
}
} displayOff;
void initEBYTE()
{
// EBYTE module always use 9600 Baud to read/set configuration
Serial2.begin(9600, SERIAL_8N1, EB_TX, EB_RX);
eb.init();
Serial.println("Read EBYTE parametes with 9600 serial");
eb.PrintParameters();
eb.SetUARTBaudRate(UDR_115200);
eb.SetAirDataRate(ADR_4800);
eb.SaveParameters();
Serial2.updateBaudRate(115200);
}
void eatEBYTEshit()
{
// Module sends packet after initialization, read it to prevent forwarding to vesc
const int shitBytes = 6;
uint8_t buf[shitBytes];
int len = Serial2.readBytes(buf, shitBytes);
if (len == shitBytes)
Serial.printf("EBYTE unwanted reply %d bytes: %02x:%02x:%02x:%02x:%02x:%02x\n", len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
else
Serial.printf("EBYTE unwanted reply %d bytes!\n", len);
}
void setup()
{
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");
// This shit will send first packet with his parameters instead of data
initEBYTE();
initTDisplay();
// Vesc serial
Serial1.begin(115200, SERIAL_8N1, VESC_TX, VESC_RX); // Start Vesc serial
// WiFi Access Point
WiFi.softAP(ssid, password, 2, 0, 4);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
tft.drawString(String("WiFi: ") + ssid, 0, 0);
tft.drawString(String("Pass: ") + password, 0, 20);
tft.drawString(myIP.toString() + String(':') + vescDefaultPort, 0, 40);
eatEBYTEshit();
/*
BLEDevice::init("Vesc Extender");
// Retrieve a Scanner and set the callback we want to use to be informed when we
// have detected a new device. Specify that we want active scanning and start the
// scan to run for 5 seconds.
BLEScan *pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(5, false);
*/
} // End of setup.
// This is the Arduino main loop function.
void appendToLora(uint8_t *buf, size_t len)
{
if (loraToSend.available() >= len) // Otherwise append to large enough LoRa buf
{
for (int i = 0; i < len; i++)
loraToSend.push(buf[i]);
}
else // Drop packet, not enough buffer space
Serial.printf("loraToSend BUFFER OVERFLOW! %d bytes available in buffer, %d received\n", loraToSend.available(), len);
}
static int loopStep = 0;
void debugPacket(uint8_t *buf, int len)
{
Serial.print('(');
for (int i = 0; i < len; i++)
i == len - 1 ? Serial.printf("%02x", buf[i]) : Serial.printf("%02x:", buf[i]);
Serial.println(")");
}
void loop()
{
// If the flag "doBleConnect" is true then we have scanned for and found the desired
// BLE Server with which we wish to connect. Now we connect to it. Once we are
// connected we set the connected flag to be true.
/*
if (doBleConnect == true)
{
if (connectToBleUartServer())
{
Serial.println("We are now connected to the BLE Server.");
}
else
{
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
doBleConnect = false;
}
// If we are connected to a peer BLE Server, update the characteristic each time we are reached
// with the current time since boot.
if (doScan)
{
Serial.println("doScan");
BLEDevice::init("Vesc Extender");
BLEScan *pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(5); // this is just example to start scan after disconnect, most likely there is better way to do it in arduino
}
*/
if (!(loopStep % 200000))
displayOff.step();
loopStep++;
if (!client)
{
client = server.available();
if (client)
{
client.setNoDelay(true);
Serial.println();
Serial.print(client.remoteIP());
Serial.println(" connected!");
}
}
size_t len, avail;
// Wifi client data available?
avail = client.available();
if (avail)
{
len = client.readBytes(buf, avail < max_buf ? avail : max_buf);
Serial.print(len);
Serial.print('W');
size_t written = Serial1.write(buf, len); // Send to Vesc
Serial.print(written);
Serial.print("V ");
}
// Vesc data available?
avail = Serial1.available();
if (avail)
{
len = Serial1.readBytes(buf, avail < max_buf ? avail : max_buf);
Serial.print(len);
Serial.print("V");
if (client) // Send to WiFi, if connected
{
size_t written = client.write(buf, len);
Serial.print(written);
Serial.print("W ");
if (written != len) // WiFi client seems lost
{
Serial.println();
Serial.println("WiFi client lost, repeating to LoRa!");
client.stop();
// Accidentially append to lora
appendToLora(buf, len);
}
}
else
appendToLora(buf, len);
}
// LoRa data available?
avail = Serial2.available();
if (avail)
{
len = Serial2.readBytes(buf, avail < max_buf ? avail : max_buf);
Serial.print(len);
Serial.print('L');
size_t written = Serial1.write(buf, len); // Send to Vesc
Serial.print(written);
Serial.print("V ");
debugPacket(buf, len);
}
int loraAvailableForWrite = Serial2.availableForWrite();
if (loraToSend.size() && loraAvailableForWrite >= 58)
{
//Serial.printf("loraToSend.size() %d, loraAvailableForWrite %d\n", loraToSend.size(), loraAvailableForWrite);
len = loraToSend.size() < loraAvailableForWrite ? loraToSend.size() : loraAvailableForWrite;
bool isLoraModuleReady = digitalRead(EB_AUX) == HIGH;
//Serial.printf("isLoraModuleReady: %d\n", isLoraModuleReady);
if (isLoraModuleReady)
{
for (int i = 0; i < len; i++)
buf[i] = loraToSend.shift();
size_t written = Serial2.write(buf, len); // Send to Vesc
Serial.print(written);
Serial.print("L");
debugPacket(buf, len);
}
}
//delay(10);
} // End of loop