Cleaned code. Added comments.
This commit is contained in:
parent
7cb14f41cd
commit
f62f3120fc
|
@ -1,11 +1,9 @@
|
||||||
#define BAUDRATE 115200
|
|
||||||
#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> // Library for writing in the memory
|
#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 = "screenMode";
|
const char* key1 = "screenMode";
|
||||||
|
@ -27,15 +25,15 @@ Button restartButton(200, 191, 45, 50, false, "", noDraw);
|
||||||
Button powerOffButton(260, 186, 55, 55, false, "", noDraw);
|
Button powerOffButton(260, 186, 55, 55, false, "", noDraw);
|
||||||
boolean buttonPressed = false;
|
boolean buttonPressed = false;
|
||||||
boolean connectingBluetooth = true;
|
boolean connectingBluetooth = true;
|
||||||
|
|
||||||
enum screenMode {DARK, LIGHT};
|
enum screenMode {DARK, LIGHT};
|
||||||
screenMode COLOR_MODE;
|
screenMode COLOR_MODE;
|
||||||
|
|
||||||
uint16_t COLORS[2] = {WHITE, BLACK};
|
uint16_t COLORS[2] = {WHITE, BLACK};
|
||||||
|
|
||||||
// Bluetooth Serial, variables
|
// Bluetooth Serial, variables
|
||||||
BluetoothSerial SerialBT;
|
|
||||||
#define BT_DISCOVER_TIME 10000
|
#define BT_DISCOVER_TIME 10000
|
||||||
|
#define BAUDRATE 115200
|
||||||
|
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;
|
||||||
|
@ -46,24 +44,29 @@ byte generatedChecksum = 0;
|
||||||
byte checksum = 0;
|
byte checksum = 0;
|
||||||
int payloadLength = 0;
|
int payloadLength = 0;
|
||||||
byte payloadData[64] = {0};
|
byte payloadData[64] = {0};
|
||||||
|
boolean bigPacket = false;
|
||||||
|
|
||||||
|
// EEG variables
|
||||||
byte poorQuality = 0;
|
byte poorQuality = 0;
|
||||||
byte attention = 0;
|
byte attention = 0;
|
||||||
byte meditation = 0;
|
byte meditation = 0;
|
||||||
short raw;
|
short raw;
|
||||||
uint32_t eegPower[8];
|
uint32_t eegPower[8];
|
||||||
|
|
||||||
// system variables
|
// battery level variables
|
||||||
long lastReceivedPacket = 0;
|
|
||||||
boolean bigPacket = false;
|
|
||||||
float batVoltage;
|
float batVoltage;
|
||||||
float batPercentage;
|
float batPercentage;
|
||||||
|
|
||||||
// Multi threading
|
// Multi threading
|
||||||
TaskHandle_t TaskHandle_1;
|
TaskHandle_t TaskHandle_1;
|
||||||
|
|
||||||
//////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Microprocessor Setup //
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
//////////////////////////
|
||||||
|
// Microprocessor Setup //
|
||||||
|
//////////////////////////
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
Serial.begin(BAUDRATE);
|
Serial.begin(BAUDRATE);
|
||||||
|
@ -81,49 +84,35 @@ void setup(){
|
||||||
|
|
||||||
M5.Axp.SetLcdVoltage(brightnessValues[preferences.getUInt(key2)]); // Set initial brightness
|
M5.Axp.SetLcdVoltage(brightnessValues[preferences.getUInt(key2)]); // Set initial brightness
|
||||||
|
|
||||||
// GUI
|
|
||||||
screenModeButton.addHandler(screenModeHandler, E_TOUCH);
|
screenModeButton.addHandler(screenModeHandler, E_TOUCH);
|
||||||
brightnessButton.addHandler(brightnessHandler, E_TOUCH);
|
brightnessButton.addHandler(brightnessHandler, E_TOUCH);
|
||||||
restartButton.addHandler(restartHandler, E_TOUCH);
|
restartButton.addHandler(restartHandler, E_TOUCH);
|
||||||
powerOffButton.addHandler(powerOffHandler, E_TOUCH);
|
powerOffButton.addHandler(powerOffHandler, E_TOUCH);
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
|
||||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
|
||||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Read data from Serial Bluetooth //
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
/////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
byte ReadOneByte() {
|
///////////////
|
||||||
int ByteRead;
|
// Main loop //
|
||||||
if(! SerialBT.isClosed() && SerialBT.connected()) {
|
///////////////
|
||||||
ByteRead = SerialBT.read();
|
|
||||||
|
|
||||||
return ByteRead;
|
|
||||||
} else {
|
|
||||||
Serial.println("not connected");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/////////////
|
|
||||||
//MAIN LOOP//
|
|
||||||
/////////////
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (!isConnected) {
|
if (!isConnected) {
|
||||||
drawBluetoothLoading();
|
drawBluetoothConnecting();
|
||||||
connectBluetooth();
|
connectBluetooth();
|
||||||
}
|
}
|
||||||
|
|
||||||
M5.update();
|
M5.update();
|
||||||
drawBattery();
|
drawBattery();
|
||||||
|
|
||||||
if (!SerialBT.connected() && isConnected == true) {
|
if (!SerialBT.connected() && isConnected == true) {
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
drawGui(COLOR_MODE);
|
drawGui(COLOR_MODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// UART communication
|
// read value from UART
|
||||||
if(Serial2.available()){
|
if(Serial2.available()){
|
||||||
char c = Serial2.read();
|
char c = Serial2.read();
|
||||||
stringReceived += c;
|
stringReceived += c;
|
||||||
|
@ -135,25 +124,24 @@ void loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(! SerialBT.isClosed() && SerialBT.connected()) {
|
if (!SerialBT.isClosed() && SerialBT.connected()) {
|
||||||
if((ReadOneByte() == 170) && ReadOneByte() == 170) {
|
if ((readOneByte() == 170) && 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
|
checksum = readOneByte(); //Read checksum byte from stream
|
||||||
generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum
|
generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum
|
||||||
|
|
||||||
if(checksum == generatedChecksum) {
|
if(checksum == generatedChecksum) {
|
||||||
|
for(int i = 0; i < payloadLength; i++) { // Parse the payload
|
||||||
for(int i = 0; i < payloadLength; i++) { // Parse the payload
|
|
||||||
switch (payloadData[i]) {
|
switch (payloadData[i]) {
|
||||||
case 2:
|
case 2:
|
||||||
i++;
|
i++;
|
||||||
|
@ -184,7 +172,7 @@ void loop() {
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
} // switch
|
} // switch
|
||||||
} // for loop
|
}
|
||||||
|
|
||||||
if(bigPacket) {
|
if(bigPacket) {
|
||||||
updatePoorQuality();
|
updatePoorQuality();
|
||||||
|
@ -228,9 +216,6 @@ void loop() {
|
||||||
}
|
}
|
||||||
bigPacket = false;
|
bigPacket = false;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// Checksum Error
|
|
||||||
} // end if else for checksum
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
drawBluetoothFailed();
|
drawBluetoothFailed();
|
||||||
|
@ -241,6 +226,27 @@ void loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////
|
||||||
|
// Task 1 for blinking while connecting //
|
||||||
|
//////////////////////////////////////////
|
||||||
|
|
||||||
|
void task1(void *pvParameters) {
|
||||||
|
while (1) {
|
||||||
|
delay(500);
|
||||||
|
if (connectingBluetooth) {
|
||||||
|
bluetoothBlinking();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
//////////////////////////
|
||||||
|
// Bluetooth connection //
|
||||||
|
//////////////////////////
|
||||||
|
|
||||||
void connectBluetooth() {
|
void connectBluetooth() {
|
||||||
connectingBluetooth = true;
|
connectingBluetooth = true;
|
||||||
if(! SerialBT.begin("ESP32test", true) ) {
|
if(! SerialBT.begin("ESP32test", true) ) {
|
||||||
|
@ -291,10 +297,24 @@ void connectBluetooth() {
|
||||||
connectingBluetooth = false;
|
connectingBluetooth = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
/////////////////////////////////////////
|
||||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
// Read one byte from Serial Bluetooth //
|
||||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
/////////////////////////////////////////
|
||||||
// GUI
|
|
||||||
|
byte readOneByte() {
|
||||||
|
int ByteRead;
|
||||||
|
if(! SerialBT.isClosed() && SerialBT.connected()) {
|
||||||
|
ByteRead = SerialBT.read();
|
||||||
|
|
||||||
|
return ByteRead;
|
||||||
|
} else {
|
||||||
|
Serial.println("not connected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////
|
||||||
|
// GUI functions //
|
||||||
|
///////////////////
|
||||||
|
|
||||||
void drawGui(screenMode COLOR_MODE) {
|
void drawGui(screenMode COLOR_MODE) {
|
||||||
M5.Lcd.clear();
|
M5.Lcd.clear();
|
||||||
|
@ -343,7 +363,7 @@ void drawGui(screenMode COLOR_MODE) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawBluetoothLoading() {
|
void drawBluetoothConnecting() {
|
||||||
M5.Lcd.clear();
|
M5.Lcd.clear();
|
||||||
M5.Lcd.fillScreen((COLORS[COLOR_MODE] - 1) % 2);
|
M5.Lcd.fillScreen((COLORS[COLOR_MODE] - 1) % 2);
|
||||||
M5.Lcd.setTextColor(COLORS[COLOR_MODE]);
|
M5.Lcd.setTextColor(COLORS[COLOR_MODE]);
|
||||||
|
@ -370,16 +390,7 @@ void drawBluetoothFailed() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void task1(void *pvParameters) {
|
void bluetoothBlinking() {
|
||||||
while (1) {
|
|
||||||
delay(500);
|
|
||||||
if (connectingBluetooth) {
|
|
||||||
bluetoothLoading();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void bluetoothLoading() {
|
|
||||||
while (connectingBluetooth) {
|
while (connectingBluetooth) {
|
||||||
if (COLOR_MODE == DARK) {
|
if (COLOR_MODE == DARK) {
|
||||||
M5.lcd.fillRect(240, 5, 35, 51, BLACK);
|
M5.lcd.fillRect(240, 5, 35, 51, BLACK);
|
||||||
|
@ -403,7 +414,6 @@ void bluetoothLoading() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void updatePoorQuality() {
|
void updatePoorQuality() {
|
||||||
if (COLOR_MODE == DARK) {
|
if (COLOR_MODE == DARK) {
|
||||||
M5.lcd.fillRect(0, 0, 110, 59, BLACK);
|
M5.lcd.fillRect(0, 0, 110, 59, BLACK);
|
||||||
|
@ -501,16 +511,13 @@ void drawBattery() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int countDigit(int n) {
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
if (n == 0)
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
return 1;
|
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
int count = 0;
|
|
||||||
while (n != 0) {
|
/////////////////////
|
||||||
n = n / 10;
|
// Button handlers //
|
||||||
++count;
|
/////////////////////
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
void screenModeHandler(Event &e) {
|
void screenModeHandler(Event &e) {
|
||||||
M5.Axp.SetLDOEnable(3, true);
|
M5.Axp.SetLDOEnable(3, true);
|
||||||
|
@ -561,4 +568,15 @@ void powerOffHandler(Event& e) {
|
||||||
delay(500);
|
delay(500);
|
||||||
M5.Axp.SetLDOEnable(3, false);
|
M5.Axp.SetLDOEnable(3, false);
|
||||||
M5.shutdown();
|
M5.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
int countDigit(int n) {
|
||||||
|
if (n == 0)
|
||||||
|
return 1;
|
||||||
|
int count = 0;
|
||||||
|
while (n != 0) {
|
||||||
|
n = n / 10;
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
Loading…
Reference in a new issue