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