Added version multithreading version.
This commit is contained in:
parent
44820e656f
commit
18afb7c559
2936
multithreading/multiThread/icons.cpp
Normal file
2936
multithreading/multiThread/icons.cpp
Normal file
File diff suppressed because it is too large
Load diff
470
multithreading/multiThread/multiThread.ino
Normal file
470
multithreading/multiThread/multiThread.ino
Normal file
|
@ -0,0 +1,470 @@
|
|||
#define DEBUGOUTPUT 0
|
||||
#include <map> // Bluetooth
|
||||
#include <BluetoothSerial.h> // Bluetooth
|
||||
#include <M5Core2.h>
|
||||
#include "icons.cpp" // icons
|
||||
|
||||
// UART communication variables
|
||||
#define RX_PIN 19
|
||||
#define TX_PIN 27
|
||||
String stringReceived = "";
|
||||
int valueReceived = 0;
|
||||
|
||||
// GUI variables
|
||||
int brightnessValue = 1;
|
||||
ButtonColors noDraw = { NODRAW, NODRAW, NODRAW };
|
||||
Button screenModeButton(0, 190, 50, 50, false, "", noDraw);
|
||||
Button brightnessButton(65, 190, 50, 50, false, "", noDraw);
|
||||
Button restartButton(200, 191, 45, 50, false, "", noDraw);
|
||||
Button powerOffButton(260, 186, 55, 55, false, "", noDraw);
|
||||
boolean buttonPressed = false;
|
||||
|
||||
enum screenMode { DARK,
|
||||
LIGHT };
|
||||
screenMode COLOR_MODE = LIGHT;
|
||||
|
||||
uint16_t COLORS[2] = { WHITE, BLACK };
|
||||
|
||||
// Bluetooth Serial, variables
|
||||
BluetoothSerial SerialBT;
|
||||
#define BT_DISCOVER_TIME 10000
|
||||
esp_spp_sec_t sec_mask = ESP_SPP_SEC_NONE;
|
||||
esp_spp_role_t role = ESP_SPP_ROLE_SLAVE;
|
||||
boolean isConnected = false;
|
||||
boolean connecting = true;
|
||||
|
||||
// checksum variables
|
||||
byte generatedChecksum = 0;
|
||||
byte checksum = 0;
|
||||
int payloadLength = 0;
|
||||
byte payloadData[64] = { 0 };
|
||||
byte poorQuality = 200;
|
||||
byte attention = 0;
|
||||
byte meditation = 0;
|
||||
short raw;
|
||||
uint32_t eegPower[8];
|
||||
|
||||
// system variables
|
||||
long lastReceivedPacket = 0;
|
||||
boolean bigPacket = false;
|
||||
TaskHandle_t TaskHandle_1;
|
||||
TaskHandle_t TaskHandle_2;
|
||||
|
||||
//////////////////////////
|
||||
// Microprocessor Setup //
|
||||
//////////////////////////
|
||||
|
||||
void setup() {
|
||||
M5.begin();
|
||||
M5.Axp.SetLcdVoltage(3150); // Set initial brightness
|
||||
Serial2.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); // UART communication
|
||||
|
||||
// MULTITHREADING
|
||||
xTaskCreatePinnedToCore(task1, "task1", 4096, NULL, 1, &TaskHandle_1, 0);
|
||||
xTaskCreatePinnedToCore(task2, "task2", 4096, NULL, 2, &TaskHandle_2, 0);
|
||||
|
||||
screenModeButton.addHandler(screenModeHandler, E_TOUCH);
|
||||
brightnessButton.addHandler(brightnessHandler, E_TOUCH);
|
||||
restartButton.addHandler(restartHandler, E_TOUCH);
|
||||
powerOffButton.addHandler(powerOffHandler, E_TOUCH);
|
||||
drawGui(COLOR_MODE);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
M5.update();
|
||||
}
|
||||
|
||||
void task1(void *pvParameters) {
|
||||
//////////////////////////
|
||||
// BLUETOOTH CONNECTION //
|
||||
//////////////////////////
|
||||
|
||||
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");
|
||||
connecting = false;
|
||||
}
|
||||
} else {
|
||||
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
|
||||
}
|
||||
while (1) {
|
||||
if (!SerialBT.connected() && isConnected == true) {
|
||||
isConnected = false;
|
||||
drawGui(COLOR_MODE);
|
||||
}
|
||||
|
||||
// UART communication
|
||||
if (Serial2.available()) {
|
||||
char c = Serial2.read();
|
||||
stringReceived += c;
|
||||
if (stringReceived.length() == 1) {
|
||||
valueReceived = stringReceived.toInt();
|
||||
stringReceived = "";
|
||||
} else {
|
||||
stringReceived = "";
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
generatedChecksum = 0;
|
||||
|
||||
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;
|
||||
} // 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;
|
||||
} else {
|
||||
// Checksum Error
|
||||
} // end if else for checksum
|
||||
} // end if read 0xAA byte
|
||||
}
|
||||
} else {
|
||||
Serial.println("NOT CONNECTED!");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void task2(void *pvParameters) {
|
||||
bluetoothLoading();
|
||||
vTaskDelete(TaskHandle_2);
|
||||
}
|
||||
|
||||
void drawGui(screenMode COLOR_MODE) {
|
||||
M5.Lcd.clear();
|
||||
M5.Lcd.fillScreen((COLORS[COLOR_MODE] - 1) % 2);
|
||||
M5.Lcd.setTextColor(COLORS[COLOR_MODE]);
|
||||
|
||||
// HEADER
|
||||
updatePoorQuality();
|
||||
|
||||
if (COLOR_MODE == DARK) {
|
||||
M5.lcd.drawBitmap(280, 5, 30, 50, (uint16_t *)bluetooth_dark);
|
||||
} else {
|
||||
M5.lcd.drawBitmap(280, 5, 30, 50, (uint16_t *)bluetooth_light);
|
||||
}
|
||||
|
||||
if (isConnected == false && !connecting) { // IF BLUETOOTH DISCONNECTED
|
||||
for (int i = 0; i < 4; i++) { // thicker line
|
||||
M5.lcd.drawLine(280 + i, 55, 310 + i, 5, RED);
|
||||
}
|
||||
}
|
||||
|
||||
M5.Lcd.drawLine(0, 60, 320, 60, COLORS[COLOR_MODE]);
|
||||
|
||||
// BODY
|
||||
M5.Lcd.drawString("Attention", 20, 80, 4);
|
||||
M5.Lcd.drawString("Meditation", 160, 80, 4);
|
||||
|
||||
updateMeditation();
|
||||
updateAttention();
|
||||
|
||||
// BOTTOM
|
||||
M5.Lcd.drawLine(0, 180, 320, 180, COLORS[COLOR_MODE]);
|
||||
screenModeButton.draw();
|
||||
if (COLOR_MODE == DARK) {
|
||||
M5.lcd.drawBitmap(0, 191, 50, 50, (uint16_t *)modeSwitchDark);
|
||||
M5.lcd.drawBitmap(65, 191, 50, 50, (uint16_t *)brightness_white);
|
||||
M5.lcd.drawBitmap(200, 191, 45, 50, (uint16_t *)restart_white);
|
||||
M5.lcd.drawBitmap(260, 186, 55, 55, (uint16_t *)power_off_white);
|
||||
} else {
|
||||
M5.lcd.drawBitmap(0, 191, 50, 50, (uint16_t *)modeSwitchLight);
|
||||
M5.lcd.drawBitmap(65, 191, 50, 50, (uint16_t *)brightness);
|
||||
M5.lcd.drawBitmap(200, 191, 45, 50, (uint16_t *)restart);
|
||||
M5.lcd.drawBitmap(260, 186, 55, 55, (uint16_t *)power_off);
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////
|
||||
// Read data from Serial Bluetooth //
|
||||
/////////////////////////////////////
|
||||
|
||||
byte ReadOneByte() {
|
||||
int ByteRead;
|
||||
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");
|
||||
M5.update();
|
||||
}
|
||||
}
|
||||
|
||||
void screenModeHandler(Event &e) {
|
||||
M5.Axp.SetLDOEnable(3, true);
|
||||
delay(125);
|
||||
M5.Axp.SetLDOEnable(3, false);
|
||||
if (COLOR_MODE == DARK) {
|
||||
COLOR_MODE = LIGHT;
|
||||
} else {
|
||||
COLOR_MODE = DARK;
|
||||
}
|
||||
|
||||
buttonPressed = true;
|
||||
if (!connecting) {
|
||||
drawGui(COLOR_MODE);
|
||||
}
|
||||
}
|
||||
|
||||
void bluetoothLoading() {
|
||||
while (connecting) {
|
||||
if (buttonPressed) {
|
||||
drawGui(COLOR_MODE);
|
||||
buttonPressed = false;
|
||||
}
|
||||
if (COLOR_MODE == DARK) {
|
||||
M5.lcd.fillRect(280, 5, 35, 51, BLACK);
|
||||
delay(500);
|
||||
if (!buttonPressed) {
|
||||
M5.lcd.drawBitmap(280, 5, 30, 50, (uint16_t *)bluetooth_dark);
|
||||
delay(500);
|
||||
}
|
||||
} else {
|
||||
M5.lcd.fillRect(280, 5, 35, 51, WHITE);
|
||||
delay(500);
|
||||
if (!buttonPressed) {
|
||||
M5.lcd.drawBitmap(280, 5, 30, 50, (uint16_t *)bluetooth_light);
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!connecting) {
|
||||
for (int i = 0; i < 4; i++) { // thicker line
|
||||
M5.lcd.drawLine(280 + i, 55, 310 + i, 5, RED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updatePoorQuality() {
|
||||
if (COLOR_MODE == DARK) {
|
||||
M5.lcd.fillRect(0, 0, 110, 59, BLACK);
|
||||
M5.Lcd.drawString(String(poorQuality), 60, 20, 4);
|
||||
|
||||
if (poorQuality < 50) {
|
||||
M5.lcd.drawBitmap(5, 5, 50, 50, (uint16_t *)signal_white_3_3);
|
||||
} else if ((50 < poorQuality) && (poorQuality < 150)) {
|
||||
M5.lcd.drawBitmap(5, 5, 50, 50, (uint16_t *)signal_white_2_3);
|
||||
} else if ((poorQuality == 200) || (poorQuality > 230)) {
|
||||
M5.lcd.drawBitmap(5, 5, 50, 50, (uint16_t *)signal_white_0_3);
|
||||
} else {
|
||||
M5.lcd.drawBitmap(5, 5, 50, 50, (uint16_t *)signal_white_1_3);
|
||||
}
|
||||
} else {
|
||||
M5.lcd.fillRect(0, 0, 110, 59, WHITE);
|
||||
M5.Lcd.drawString(String(poorQuality), 60, 20, 4);
|
||||
|
||||
if (poorQuality < 50) {
|
||||
M5.lcd.drawBitmap(5, 5, 50, 50, (uint16_t *)signal_3_3);
|
||||
} else if ((50 < poorQuality) && (poorQuality < 150)) {
|
||||
M5.lcd.drawBitmap(5, 5, 50, 50, (uint16_t *)signal_2_3);
|
||||
} else if ((poorQuality == 200) || (poorQuality > 230)) {
|
||||
M5.lcd.drawBitmap(5, 5, 50, 50, (uint16_t *)signal_0_3);
|
||||
} else {
|
||||
M5.lcd.drawBitmap(5, 5, 50, 50, (uint16_t *)signal_1_3);
|
||||
}
|
||||
}
|
||||
|
||||
if (poorQuality == 200) { // if headset not connected to person
|
||||
for (int i = 0; i < 4; i++) { // thicker line
|
||||
M5.lcd.drawLine(5 + i, 55, 55 + i, 5, RED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateAttention() {
|
||||
int x = 60 - ((countDigit(attention) - 1) * 5);
|
||||
if (COLOR_MODE == DARK) {
|
||||
M5.lcd.fillRect(60, 130, 50, 40, BLACK);
|
||||
M5.Lcd.drawString(String(attention), x, 130, 4);
|
||||
} else {
|
||||
M5.lcd.fillRect(60, 130, 50, 40, WHITE);
|
||||
M5.Lcd.drawString(String(attention), x, 130, 4);
|
||||
}
|
||||
}
|
||||
|
||||
void updateMeditation() {
|
||||
int x = 210 - ((countDigit(meditation) - 1) * 5);
|
||||
if (COLOR_MODE == DARK) {
|
||||
M5.lcd.fillRect(210, 130, 50, 40, BLACK);
|
||||
M5.Lcd.drawString(String(meditation), x, 130, 4);
|
||||
} else {
|
||||
M5.lcd.fillRect(210, 130, 50, 40, WHITE);
|
||||
M5.Lcd.drawString(String(meditation), x, 130, 4);
|
||||
}
|
||||
}
|
||||
|
||||
int countDigit(int n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
while (n != 0) {
|
||||
n = n / 10;
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void brightnessHandler(Event &e) {
|
||||
M5.Axp.SetLDOEnable(3, true);
|
||||
delay(125);
|
||||
M5.Axp.SetLDOEnable(3, false);
|
||||
switch (brightnessValue) {
|
||||
case 1:
|
||||
M5.Axp.SetLcdVoltage(2500);
|
||||
brightnessValue += 1;
|
||||
break;
|
||||
case 2:
|
||||
M5.Axp.SetLcdVoltage(2750);
|
||||
brightnessValue += 1;
|
||||
break;
|
||||
case 3:
|
||||
M5.Axp.SetLcdVoltage(2900);
|
||||
brightnessValue += 1;
|
||||
break;
|
||||
case 4:
|
||||
M5.Axp.SetLcdVoltage(3150);
|
||||
brightnessValue = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void restartHandler(Event &e) {
|
||||
M5.Axp.SetLDOEnable(3, true);
|
||||
delay(250);
|
||||
M5.Axp.SetLDOEnable(3, false);
|
||||
M5.shutdown(1);
|
||||
}
|
||||
|
||||
void powerOffHandler(Event &e) {
|
||||
M5.Axp.SetLDOEnable(3, true);
|
||||
delay(500);
|
||||
M5.Axp.SetLDOEnable(3, false);
|
||||
M5.shutdown();
|
||||
}
|
|
@ -26,7 +26,7 @@ uint16_t COLORS[2] = {WHITE, BLACK};
|
|||
|
||||
// Bluetooth Serial, variables
|
||||
BluetoothSerial SerialBT;
|
||||
#define BT_DISCOVER_TIME 1000
|
||||
#define BT_DISCOVER_TIME 10000
|
||||
esp_spp_sec_t sec_mask=ESP_SPP_SEC_NONE;
|
||||
esp_spp_role_t role=ESP_SPP_ROLE_SLAVE;
|
||||
boolean isConnected = false;
|
||||
|
@ -36,7 +36,7 @@ byte generatedChecksum = 0;
|
|||
byte checksum = 0;
|
||||
int payloadLength = 0;
|
||||
byte payloadData[64] = {0};
|
||||
byte poorQuality = 0;
|
||||
byte poorQuality = 200;
|
||||
byte attention = 0;
|
||||
byte meditation = 0;
|
||||
short raw;
|
||||
|
@ -85,7 +85,7 @@ void setup(){
|
|||
Serial.print("Stopping discoverAsync... ");
|
||||
SerialBT.discoverAsyncStop();
|
||||
Serial.println("discoverAsync stopped");
|
||||
delay(1000);
|
||||
delay(5000);
|
||||
if(btDeviceList->getCount() > 0) {
|
||||
BTAddress addr;
|
||||
int channel=0;
|
||||
|
@ -182,10 +182,6 @@ void loop() {
|
|||
|
||||
if(checksum == generatedChecksum) {
|
||||
|
||||
poorQuality = 200;
|
||||
attention = 0;
|
||||
meditation = 0;
|
||||
|
||||
for(int i = 0; i < payloadLength; i++) { // Parse the payload
|
||||
switch (payloadData[i]) {
|
||||
case 2:
|
||||
|
@ -202,8 +198,8 @@ void loop() {
|
|||
meditation = payloadData[i];
|
||||
break;
|
||||
case 0x80:
|
||||
i = i + 3;
|
||||
raw = (payloadData[0]<<8) | payloadData[1];
|
||||
i++;
|
||||
raw = (payloadData[i++]<<8) | payloadData[i++];
|
||||
break;
|
||||
case 0x83:
|
||||
for (int j = 0; j < 8; j++) {
|
||||
|
@ -251,8 +247,8 @@ void loop() {
|
|||
Serial.print("Raw: ");
|
||||
Serial.print(raw);
|
||||
Serial.print("\n");
|
||||
Serial.print("EEG POWER: ")
|
||||
Serial.print(eegPower[0] + ", " + eegPower[1] + ", " + eegPower[2] + ", " + eegPower[3] + ", " + eegPower[4] + ", " + eegPower[5] + ", " + eegPower[6] + ", " + eegPower[7]);
|
||||
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");
|
||||
}
|
||||
#endif
|
||||
|
@ -299,7 +295,7 @@ void drawGui(screenMode COLOR_MODE) {
|
|||
}
|
||||
}
|
||||
|
||||
if (poorQuality == 200) { //
|
||||
if (poorQuality == 200) { // if headset not connected to person
|
||||
for (int i = 0; i < 4; i++) { // thicker line
|
||||
M5.lcd.drawLine(5 + i, 55, 55 + i, 5, RED);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue