Cleaned code. Added comments.

This commit is contained in:
Vojtěch Pour 2022-12-16 22:48:53 +01:00
parent 7cb14f41cd
commit f62f3120fc

View file

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