Cleaned code.
This commit is contained in:
parent
1f47db22f2
commit
6cd1eb7fe8
|
@ -1,15 +1,13 @@
|
|||
#define DEBUGOUTPUT 0
|
||||
#include <map> // Bluetooth
|
||||
#include <map> // Bluetooth
|
||||
#include <BluetoothSerial.h> // Bluetooth
|
||||
#include <M5Core2.h>
|
||||
#include "icons.cpp" // icons
|
||||
#include <Preferences.h>
|
||||
#include <Preferences.h> // Library for writing in the memory
|
||||
|
||||
// store screen mode and brightness in the memory of the Core
|
||||
Preferences preferences;
|
||||
const char* key1 = "lightDark";
|
||||
const char* key1 = "screenMode";
|
||||
const char* key2 = "brightness";
|
||||
int lightMode;
|
||||
|
||||
// UART communication variables
|
||||
#define RX_PIN 19
|
||||
|
@ -68,6 +66,7 @@ TaskHandle_t TaskHandle_2;
|
|||
|
||||
void setup() {
|
||||
M5.begin();
|
||||
|
||||
preferences.begin("Key");
|
||||
Serial.print(preferences.getUInt(key1));
|
||||
if (preferences.getUInt(key1) == 0) {
|
||||
|
@ -75,6 +74,7 @@ void setup() {
|
|||
} else {
|
||||
COLOR_MODE = LIGHT;
|
||||
}
|
||||
|
||||
M5.Axp.SetLcdVoltage(brightnessValues[preferences.getUInt(key2)]); // Set initial brightness
|
||||
Serial2.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); // UART communication
|
||||
|
||||
|
@ -133,7 +133,7 @@ void task1(void *pvParameters) {
|
|||
Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
|
||||
SerialBT.connect(addr, channel, sec_mask, role);
|
||||
isConnected = true;
|
||||
//drawGui(COLOR_MODE);
|
||||
drawGui(COLOR_MODE);
|
||||
}
|
||||
} else {
|
||||
Serial.println("Didn't find any devices");
|
||||
|
@ -142,6 +142,7 @@ void task1(void *pvParameters) {
|
|||
} else {
|
||||
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (!SerialBT.connected() && isConnected == true) {
|
||||
isConnected = false;
|
||||
|
@ -162,106 +163,98 @@ void task1(void *pvParameters) {
|
|||
|
||||
if (!SerialBT.isClosed() && SerialBT.connected()) {
|
||||
if (ReadOneByte() == 170) {
|
||||
if (ReadOneByte() == 170) {
|
||||
payloadLength = ReadOneByte();
|
||||
if (payloadLength > 169) //Payload length can not be greater than 169
|
||||
return;
|
||||
payloadLength = ReadOneByte();
|
||||
if (payloadLength > 169) //Payload length can not be greater than 169
|
||||
return;
|
||||
|
||||
generatedChecksum = 0;
|
||||
generatedChecksum = 0;
|
||||
|
||||
for (int i = 0; i < payloadLength; i++) {
|
||||
payloadData[i] = ReadOneByte(); //Read payload into memory
|
||||
generatedChecksum += payloadData[i];
|
||||
for (int i = 0; i < payloadLength; i++) {
|
||||
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
|
||||
|
||||
if (checksum == generatedChecksum) {
|
||||
|
||||
for (int i = 0; i < payloadLength; i++) { // Parse the payload
|
||||
switch (payloadData[i]) {
|
||||
case 2:
|
||||
i++;
|
||||
poorQuality = payloadData[i];
|
||||
bigPacket = true;
|
||||
break;
|
||||
case 4:
|
||||
i++;
|
||||
attention = payloadData[i];
|
||||
break;
|
||||
case 5:
|
||||
i++;
|
||||
meditation = payloadData[i];
|
||||
break;
|
||||
case 0x80:
|
||||
i++;
|
||||
raw = (payloadData[i++] << 8) | payloadData[i++];
|
||||
break;
|
||||
case 0x83:
|
||||
for (int j = 0; j < 8; j++) {
|
||||
uint8_t a, b, c;
|
||||
a = payloadData[++i];
|
||||
b = payloadData[++i];
|
||||
c = payloadData[++i];
|
||||
eegPower[j] = ((uint32_t)a << 16) | ((uint32_t)b << 8) | (uint32_t)c;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
checksum = ReadOneByte(); //Read checksum byte from stream
|
||||
generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum
|
||||
if (bigPacket) {
|
||||
updatePoorQuality();
|
||||
updateAttention();
|
||||
updateMeditation();
|
||||
|
||||
if (checksum == generatedChecksum) {
|
||||
|
||||
for (int i = 0; i < payloadLength; i++) { // Parse the payload
|
||||
switch (payloadData[i]) {
|
||||
case 2:
|
||||
i++;
|
||||
poorQuality = payloadData[i];
|
||||
bigPacket = true;
|
||||
break;
|
||||
case 4:
|
||||
i++;
|
||||
attention = payloadData[i];
|
||||
break;
|
||||
case 5:
|
||||
i++;
|
||||
meditation = payloadData[i];
|
||||
break;
|
||||
case 0x80:
|
||||
i++;
|
||||
raw = (payloadData[i++] << 8) | payloadData[i++];
|
||||
break;
|
||||
case 0x83:
|
||||
for (int j = 0; j < 8; j++) {
|
||||
uint8_t a, b, c;
|
||||
a = payloadData[++i];
|
||||
b = payloadData[++i];
|
||||
c = payloadData[++i];
|
||||
eegPower[j] = ((uint32_t)a << 16) | ((uint32_t)b << 8) | (uint32_t)c;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
} // switch
|
||||
} // for loop
|
||||
|
||||
#if !DEBUGOUTPUT
|
||||
if (bigPacket) {
|
||||
updatePoorQuality();
|
||||
updateAttention();
|
||||
updateMeditation();
|
||||
|
||||
// UART OUTPUT
|
||||
switch (valueReceived) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
Serial2.print(attention);
|
||||
break;
|
||||
case 2:
|
||||
Serial2.print(meditation);
|
||||
break;
|
||||
case 3:
|
||||
Serial2.print(poorQuality);
|
||||
break;
|
||||
}
|
||||
|
||||
// SERIAL OUTPUT
|
||||
Serial.print("Attention: ");
|
||||
Serial.print(attention);
|
||||
Serial.print("\n");
|
||||
Serial.print("Poor quality: ");
|
||||
Serial.print(poorQuality);
|
||||
Serial.print("\n");
|
||||
Serial.print("Meditation: ");
|
||||
Serial.print(meditation);
|
||||
Serial.print("\n");
|
||||
Serial.print("Raw: ");
|
||||
Serial.print(raw);
|
||||
Serial.print("\n");
|
||||
Serial.print("EEG POWER: ");
|
||||
Serial.print(String(eegPower[0]) + ", " + String(eegPower[1]) + ", " + String(eegPower[2]) + ", " + String(eegPower[3]) + ", " + String(eegPower[4]) + ", " + String(eegPower[5]) + ", " + String(eegPower[6]) + ", " + String(eegPower[7]));
|
||||
Serial.print("\n");
|
||||
} else {
|
||||
Serial.println("NOT!");
|
||||
// UART OUTPUT
|
||||
switch (valueReceived) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
Serial2.print(attention);
|
||||
break;
|
||||
case 2:
|
||||
Serial2.print(meditation);
|
||||
break;
|
||||
case 3:
|
||||
Serial2.print(poorQuality);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
bigPacket = false;
|
||||
} else {
|
||||
// Checksum Error
|
||||
} // end if else for checksum
|
||||
} // end if read 0xAA byte
|
||||
|
||||
// SERIAL OUTPUT
|
||||
Serial.print("Attention: ");
|
||||
Serial.print(attention);
|
||||
Serial.print("\n");
|
||||
Serial.print("Poor quality: ");
|
||||
Serial.print(poorQuality);
|
||||
Serial.print("\n");
|
||||
Serial.print("Meditation: ");
|
||||
Serial.print(meditation);
|
||||
Serial.print("\n");
|
||||
Serial.print("Raw: ");
|
||||
Serial.print(raw);
|
||||
Serial.print("\n");
|
||||
Serial.print("EEG POWER: ");
|
||||
Serial.print(String(eegPower[0]) + ", " + String(eegPower[1]) + ", " + String(eegPower[2]) + ", " + String(eegPower[3]) + ", " + String(eegPower[4]) + ", " + String(eegPower[5]) + ", " + String(eegPower[6]) + ", " + String(eegPower[7]));
|
||||
Serial.print("\n");
|
||||
}
|
||||
bigPacket = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Serial.println("NOT CONNECTED!");
|
||||
delay(1000);
|
||||
Serial.println("Bluetooth not connected!");
|
||||
vTaskDelete(TaskHandle_1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -332,10 +325,6 @@ byte ReadOneByte() {
|
|||
if (!SerialBT.isClosed() && SerialBT.connected()) {
|
||||
ByteRead = SerialBT.read();
|
||||
|
||||
#if DEBUGOUTPUT
|
||||
Serial.print((char)ByteRead); // echo the same byte out the USB serial (for debug purposes)
|
||||
#endif
|
||||
|
||||
return ByteRead;
|
||||
} else {
|
||||
Serial.println("not connected");
|
||||
|
|
Loading…
Reference in a new issue