From 4af3bbc9be47a1408664fd347e65060e5ab6a5ee Mon Sep 17 00:00:00 2001 From: BigTire Date: Sat, 17 Dec 2022 11:19:05 +0100 Subject: [PATCH] Added MAC_ADRESS checking and fixed some GUI bugs. --- projectEEG/projectEEG/projectEEG.ino | 56 ++++++++++++++-------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/projectEEG/projectEEG/projectEEG.ino b/projectEEG/projectEEG/projectEEG.ino index 984484c..ec0da93 100644 --- a/projectEEG/projectEEG/projectEEG.ino +++ b/projectEEG/projectEEG/projectEEG.ino @@ -4,6 +4,9 @@ #include "icons.cpp" // icons #include // Library for writing in the memory +// MAC address of EEG headset device +String MAC_ADDRESS = "98:07:2d:7f:ea:15"; + // store screen mode and brightness in the memory of the Core Preferences preferences; const char* key1 = "screenMode"; @@ -27,6 +30,7 @@ boolean buttonPressed = false; boolean connectingBluetooth = true; enum screenMode {DARK, LIGHT}; screenMode COLOR_MODE; +boolean setupGUI = false; uint16_t COLORS[2] = {WHITE, BLACK}; @@ -104,14 +108,13 @@ void loop() { connectBluetooth(); } - M5.update(); - drawBattery(); - - if (!SerialBT.connected() && isConnected == true) { - isConnected = false; + if (!setupGUI && isConnected) { + setupGUI = true; drawGui(); } + M5.update(); + // read value from UART if(Serial2.available()){ char c = Serial2.read(); @@ -127,18 +130,18 @@ void loop() { if (!SerialBT.isClosed() && SerialBT.connected()) { if ((readOneByte() == 170) && readOneByte() == 170) { payloadLength = readOneByte(); - if (payloadLength > 169) //Payload length can not be greater than 169 + if (payloadLength > 169) // Payload length can not be greater than 169 return; generatedChecksum = 0; for (int i = 0; i < payloadLength; i++) { - payloadData[i] = readOneByte(); //Read payload into memory + payloadData[i] = readOneByte(); // Read payload into memory generatedChecksum += payloadData[i]; } - checksum = readOneByte(); //Read checksum byte from stream - generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum + checksum = readOneByte(); // Read checksum byte from stream + generatedChecksum = 255 - generatedChecksum; if(checksum == generatedChecksum) { for(int i = 0; i < payloadLength; i++) { // Parse the payload @@ -171,7 +174,7 @@ void loop() { break; default: break; - } // switch + } } if(bigPacket) { @@ -270,15 +273,17 @@ void connectBluetooth() { 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 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 (String(device->getAddress().toString().c_str()) == MAC_ADDRESS) { + Serial.printf(" ----- %s %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI()); + std::map 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) { @@ -286,7 +291,6 @@ void connectBluetooth() { SerialBT.connect(addr, channel, sec_mask, role); isConnected = true; wasConnected = true; - drawGui(); } } else { Serial.println("Didn't find any devices"); @@ -330,12 +334,6 @@ void drawGui() { M5.lcd.drawBitmap(240, 5, 30, 50, (uint16_t *)bluetoothLight); } - if (isConnected == false) { // IF BLUETOOTH DISCONNECTED - for (int i = 0; i < 4; i++) { // thicker line - M5.lcd.drawLine(240 + i, 55, 270 + i, 5, RED); - } - } - drawBattery(); M5.Lcd.drawLine(0, 60, 320, 60, COLORS[COLOR_MODE]); @@ -381,13 +379,17 @@ void drawBluetoothConnecting() { void drawBluetoothFailed() { M5.lcd.fillRect(0, 65, 320, 110, (COLORS[COLOR_MODE] - 1) % 2); if (wasConnected) { - M5.lcd.fillRect(0, 0, 150, 55, (COLORS[COLOR_MODE] - 1) % 2); + M5.lcd.fillRect(0, 0, 150, 60, (COLORS[COLOR_MODE] - 1) % 2); M5.lcd.fillRect(0, 185, 320, 240, (COLORS[COLOR_MODE] - 1) % 2); M5.lcd.fillRect(0, 185, 320, 240, (COLORS[COLOR_MODE] - 1) % 2); M5.Lcd.drawString("Bluetooth: disconnected", 20, 110, 4); } else { M5.Lcd.drawString("connecting failed", 50, 110, 4); } + + for (int i = 0; i < 4; i++) { // thicker line + M5.lcd.drawLine(240 + i, 55, 270 + i, 5, RED); + } } void bluetoothBlinking() {