Added bluetooth recovery.
This commit is contained in:
parent
dcf83b2146
commit
23a2f48d27
|
@ -36,7 +36,7 @@ BluetoothSerial SerialBT;
|
||||||
esp_spp_sec_t sec_mask = ESP_SPP_SEC_NONE;
|
esp_spp_sec_t sec_mask = ESP_SPP_SEC_NONE;
|
||||||
esp_spp_role_t role = ESP_SPP_ROLE_SLAVE;
|
esp_spp_role_t role = ESP_SPP_ROLE_SLAVE;
|
||||||
boolean isConnected = false;
|
boolean isConnected = false;
|
||||||
boolean connecting = true;
|
boolean connectingBluetooth = true;
|
||||||
|
|
||||||
// checksum variables
|
// checksum variables
|
||||||
byte generatedChecksum = 0;
|
byte generatedChecksum = 0;
|
||||||
|
@ -98,50 +98,7 @@ void task1(void *pvParameters) {
|
||||||
// BLUETOOTH CONNECTION //
|
// BLUETOOTH CONNECTION //
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
|
|
||||||
if (!SerialBT.begin("ESP32test", true)) {
|
connectBluetooth();
|
||||||
Serial.println("========== serialBT failed!");
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Starting discoverAsync...");
|
|
||||||
BTScanResults *btDeviceList = SerialBT.getScanResults();
|
|
||||||
if (SerialBT.discoverAsync([](BTAdvertisedDevice *pDevice) {
|
|
||||||
Serial.printf(">>>>>>>>>>>Found a new device asynchronously: %s\n", pDevice->toString().c_str());
|
|
||||||
})) {
|
|
||||||
delay(BT_DISCOVER_TIME);
|
|
||||||
Serial.print("Stopping discoverAsync... ");
|
|
||||||
SerialBT.discoverAsyncStop();
|
|
||||||
Serial.println("discoverAsync stopped");
|
|
||||||
delay(5000);
|
|
||||||
if (btDeviceList->getCount() > 0) {
|
|
||||||
BTAddress addr;
|
|
||||||
int channel = 0;
|
|
||||||
Serial.println("Found devices:");
|
|
||||||
for (int i = 0; i < btDeviceList->getCount(); i++) {
|
|
||||||
BTAdvertisedDevice *device = btDeviceList->getDevice(i);
|
|
||||||
Serial.printf(" ----- %s %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI());
|
|
||||||
std::map<int, std::string> channels = SerialBT.getChannels(device->getAddress());
|
|
||||||
Serial.printf("scanned for services, found %d\n", channels.size());
|
|
||||||
for (auto const &entry : channels) {
|
|
||||||
Serial.printf(" channel %d (%s)\n", entry.first, entry.second.c_str());
|
|
||||||
}
|
|
||||||
if (channels.size() > 0) {
|
|
||||||
addr = device->getAddress();
|
|
||||||
channel = channels.begin()->first;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (addr) {
|
|
||||||
Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
|
|
||||||
SerialBT.connect(addr, channel, sec_mask, role);
|
|
||||||
isConnected = true;
|
|
||||||
drawGui(COLOR_MODE);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.println("Didn't find any devices");
|
|
||||||
connecting = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
|
|
||||||
}
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (!SerialBT.connected() && isConnected == true) {
|
if (!SerialBT.connected() && isConnected == true) {
|
||||||
|
@ -253,19 +210,19 @@ void task1(void *pvParameters) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Bluetooth not connected!");
|
delay(3000);
|
||||||
vTaskDelete(TaskHandle_1);
|
connectBluetooth();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void task2(void *pvParameters) {
|
void task2(void *pvParameters) {
|
||||||
bluetoothLoading();
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
bluetoothLoading();
|
||||||
if (!lock) {
|
if (!lock) {
|
||||||
drawBattery();
|
drawBattery();
|
||||||
}
|
}
|
||||||
delay(500);
|
delay(500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,7 +240,7 @@ void drawGui(screenMode COLOR_MODE) {
|
||||||
M5.lcd.drawBitmap(240, 5, 30, 50, (uint16_t *)bluetoothLight);
|
M5.lcd.drawBitmap(240, 5, 30, 50, (uint16_t *)bluetoothLight);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isConnected == false && !connecting) { // IF BLUETOOTH DISCONNECTED
|
if (isConnected == false && !connectingBluetooth) { // IF BLUETOOTH DISCONNECTED
|
||||||
for (int i = 0; i < 4; i++) { // thicker line
|
for (int i = 0; i < 4; i++) { // thicker line
|
||||||
M5.lcd.drawLine(240 + i, 55, 270 + i, 5, RED);
|
M5.lcd.drawLine(240 + i, 55, 270 + i, 5, RED);
|
||||||
}
|
}
|
||||||
|
@ -332,6 +289,54 @@ byte ReadOneByte() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void connectBluetooth() {
|
||||||
|
connectingBluetooth = true;
|
||||||
|
if (!SerialBT.begin("ESP32test", true)) {
|
||||||
|
Serial.println("========== serialBT failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("Starting discoverAsync...");
|
||||||
|
BTScanResults *btDeviceList = SerialBT.getScanResults();
|
||||||
|
if (SerialBT.discoverAsync([](BTAdvertisedDevice *pDevice) {
|
||||||
|
Serial.printf(">>>>>>>>>>>Found a new device asynchronously: %s\n", pDevice->toString().c_str());
|
||||||
|
})) {
|
||||||
|
delay(BT_DISCOVER_TIME);
|
||||||
|
Serial.print("Stopping discoverAsync... ");
|
||||||
|
SerialBT.discoverAsyncStop();
|
||||||
|
Serial.println("discoverAsync stopped");
|
||||||
|
delay(5000);
|
||||||
|
if (btDeviceList->getCount() > 0) {
|
||||||
|
BTAddress addr;
|
||||||
|
int channel = 0;
|
||||||
|
Serial.println("Found devices:");
|
||||||
|
for (int i = 0; i < btDeviceList->getCount(); i++) {
|
||||||
|
BTAdvertisedDevice *device = btDeviceList->getDevice(i);
|
||||||
|
Serial.printf(" ----- %s %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI());
|
||||||
|
std::map<int, std::string> channels = SerialBT.getChannels(device->getAddress());
|
||||||
|
Serial.printf("scanned for services, found %d\n", channels.size());
|
||||||
|
for (auto const &entry : channels) {
|
||||||
|
Serial.printf(" channel %d (%s)\n", entry.first, entry.second.c_str());
|
||||||
|
}
|
||||||
|
if (channels.size() > 0) {
|
||||||
|
addr = device->getAddress();
|
||||||
|
channel = channels.begin()->first;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (addr) {
|
||||||
|
Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
|
||||||
|
SerialBT.connect(addr, channel, sec_mask, role);
|
||||||
|
isConnected = true;
|
||||||
|
drawGui(COLOR_MODE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("Didn't find any devices");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
|
||||||
|
}
|
||||||
|
connectingBluetooth = false;
|
||||||
|
}
|
||||||
|
|
||||||
void screenModeHandler(Event &e) {
|
void screenModeHandler(Event &e) {
|
||||||
M5.Axp.SetLDOEnable(3, true);
|
M5.Axp.SetLDOEnable(3, true);
|
||||||
delay(125);
|
delay(125);
|
||||||
|
@ -345,7 +350,7 @@ void screenModeHandler(Event &e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
buttonPressed = true;
|
buttonPressed = true;
|
||||||
if (!connecting) {
|
if (!connectingBluetooth) {
|
||||||
lock = true;
|
lock = true;
|
||||||
drawGui(COLOR_MODE);
|
drawGui(COLOR_MODE);
|
||||||
lock = false;
|
lock = false;
|
||||||
|
@ -353,7 +358,7 @@ void screenModeHandler(Event &e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void bluetoothLoading() {
|
void bluetoothLoading() {
|
||||||
while (connecting) {
|
while (connectingBluetooth) {
|
||||||
if (buttonPressed) {
|
if (buttonPressed) {
|
||||||
drawGui(COLOR_MODE);
|
drawGui(COLOR_MODE);
|
||||||
buttonPressed = false;
|
buttonPressed = false;
|
||||||
|
@ -376,7 +381,7 @@ void bluetoothLoading() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!connecting) {
|
if (!connectingBluetooth) {
|
||||||
for (int i = 0; i < 4; i++) { // thicker line
|
for (int i = 0; i < 4; i++) { // thicker line
|
||||||
M5.lcd.drawLine(240 + i, 55, 270 + i, 5, RED);
|
M5.lcd.drawLine(240 + i, 55, 270 + i, 5, RED);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue