Nahrát soubory do „M2Ext“
This commit is contained in:
parent
9535ca9ed4
commit
a964068d70
344
M2Ext/M2Ext.ino
Normal file
344
M2Ext/M2Ext.ino
Normal file
|
@ -0,0 +1,344 @@
|
|||
#define BAUDRATE 115200
|
||||
#define DEBUGOUTPUT 0
|
||||
|
||||
#include <M5Core2.h>
|
||||
#include "icons.cpp"
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
//WIFI AND MQTT SETUP
|
||||
const char* ssid = "poco";
|
||||
const char* password = "lucifer101";
|
||||
const char* mqtt_server = "test.mosquitto.org";
|
||||
const char* deviceid = "ESP32Client";
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
//MQTT MESSAGE SETUP
|
||||
#define MSG_BUFFER_SIZE (50)
|
||||
char msgAtt[MSG_BUFFER_SIZE];
|
||||
char msgMed[MSG_BUFFER_SIZE];
|
||||
char msgQua[MSG_BUFFER_SIZE];
|
||||
char msgRaw[MSG_BUFFER_SIZE];
|
||||
|
||||
void setupWifi();
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
///UART SETUP
|
||||
#define RX_PIN 19
|
||||
#define TX_PIN 27
|
||||
|
||||
///UI SETUP
|
||||
|
||||
int brightnessValue = 1;
|
||||
ButtonColors noDraw = {NODRAW, NODRAW, NODRAW};
|
||||
enum screenMode {DARK, LIGHT};
|
||||
screenMode COLOR_MODE = DARK;
|
||||
enum dataMode {ATTENTION, MEDITATION, QUALITY, RAW, STOP};
|
||||
dataMode DATA_MODE = RAW;
|
||||
|
||||
Button screenModeButton(0, 190, 50, 50, false, "", noDraw);
|
||||
Button brightnessButton(55, 190, 50, 50, false, "", noDraw);
|
||||
Button restartButton(200, 191, 45, 50, false, "", noDraw);
|
||||
Button powerOffButton(260, 186, 55, 55, false, "", noDraw);
|
||||
|
||||
Button attentionButton(0, 2, 48, 48, false, "", noDraw);
|
||||
Button meditationButton(90, 2, 48, 48, false, "", noDraw);
|
||||
Button qualityButton(180, 2, 48, 48, false, "", noDraw);
|
||||
Button rawButton(270, 2, 48, 48, false, "", noDraw);
|
||||
|
||||
uint16_t COLORS[2] = {WHITE, BLACK};
|
||||
|
||||
///SETUP
|
||||
void setup(){
|
||||
Serial.begin(BAUDRATE);
|
||||
Serial2.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN);
|
||||
M5.begin();
|
||||
setupWifi();
|
||||
client.setServer(mqtt_server,1883);
|
||||
if (client.connect(deviceid)) {
|
||||
Serial.println("connected to MQTT");
|
||||
}
|
||||
else {
|
||||
Serial.print("failed to connect ");
|
||||
Serial.print(client.state()); delay(2000);
|
||||
}
|
||||
|
||||
|
||||
M5.Axp.SetLcdVoltage(3150);
|
||||
|
||||
// GUI SETUP
|
||||
screenModeButton.addHandler(screenModeHandler, E_TOUCH);
|
||||
brightnessButton.addHandler(brightnessHandler, E_TOUCH);
|
||||
restartButton.addHandler(restartHandler, E_TOUCH);
|
||||
powerOffButton.addHandler(powerOffHandler, E_TOUCH);
|
||||
|
||||
attentionButton.addHandler(attentionHandler, E_TOUCH);
|
||||
meditationButton.addHandler(meditationHandler, E_TOUCH);
|
||||
qualityButton.addHandler(qualityHandler, E_TOUCH);
|
||||
rawButton.addHandler(rawHandler, E_TOUCH);
|
||||
|
||||
drawGui(COLOR_MODE);
|
||||
|
||||
|
||||
//Serial2.print("4");
|
||||
}
|
||||
|
||||
//WIFI CONNECT
|
||||
void setupWifi() {
|
||||
delay(10);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
delay(500);
|
||||
if(WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("Couldn't get a wifi connection");
|
||||
}
|
||||
else{
|
||||
Serial.println("Successfully connected");
|
||||
}
|
||||
}
|
||||
|
||||
//MQTT CONNECTION CHECK AND RECONECT
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
if (client.connect(deviceid)) {
|
||||
Serial.println("connected");
|
||||
client.publish("EEGStart", deviceid);
|
||||
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
///DRAW GUI
|
||||
void drawGui(screenMode COLOR_MODE) {
|
||||
M5.Lcd.clear();
|
||||
M5.Lcd.fillScreen((COLORS[COLOR_MODE] - 1) % 2);
|
||||
M5.Lcd.setTextColor(COLORS[COLOR_MODE]);
|
||||
|
||||
M5.Lcd.drawLine(0, 60, 320, 60, COLORS[COLOR_MODE]);
|
||||
|
||||
// BUTTONS
|
||||
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(55, 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);
|
||||
|
||||
M5.lcd.drawBitmap(0, 2, 48, 48, (uint16_t *) attention_white);
|
||||
M5.lcd.drawBitmap(90, 2, 48, 48, (uint16_t *) meditation_white);
|
||||
M5.lcd.drawBitmap(180, 2, 48, 48, (uint16_t *) quality_white);
|
||||
M5.lcd.drawBitmap(270, 2, 48, 48, (uint16_t *) raw_white);
|
||||
} else {
|
||||
M5.lcd.drawBitmap(0, 191, 50, 50, (uint16_t *) modeSwitchLight);
|
||||
M5.lcd.drawBitmap(55, 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);
|
||||
|
||||
M5.lcd.drawBitmap(0, 2, 48, 48, (uint16_t *) attention);
|
||||
M5.lcd.drawBitmap(90, 2, 48, 48, (uint16_t *) meditation);
|
||||
M5.lcd.drawBitmap(180, 2, 48, 48, (uint16_t *) quality);
|
||||
M5.lcd.drawBitmap(270, 2, 48, 48, (uint16_t *) raw);
|
||||
}
|
||||
}
|
||||
|
||||
/// BUTTON HANDLERS
|
||||
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;
|
||||
}
|
||||
|
||||
drawGui(COLOR_MODE);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void attentionHandler(Event& e) {
|
||||
M5.Axp.SetLDOEnable(3, true);
|
||||
delay(80);
|
||||
M5.Axp.SetLDOEnable(3, false);
|
||||
if(DATA_MODE == ATTENTION){
|
||||
DATA_MODE = STOP;
|
||||
}else{
|
||||
DATA_MODE = ATTENTION;
|
||||
Serial.println("in ATTENTION");
|
||||
}
|
||||
drawGui(COLOR_MODE);
|
||||
}
|
||||
|
||||
void meditationHandler(Event& e) {
|
||||
M5.Axp.SetLDOEnable(3, true);
|
||||
delay(80);
|
||||
M5.Axp.SetLDOEnable(3, false);
|
||||
if(DATA_MODE == MEDITATION){
|
||||
DATA_MODE = STOP;
|
||||
}else{
|
||||
DATA_MODE = MEDITATION;
|
||||
Serial.println("in MEDITATION");
|
||||
}
|
||||
drawGui(COLOR_MODE);
|
||||
}
|
||||
|
||||
void qualityHandler(Event& e) {
|
||||
M5.Axp.SetLDOEnable(3, true);
|
||||
delay(80);
|
||||
M5.Axp.SetLDOEnable(3, false);
|
||||
if(DATA_MODE == QUALITY){
|
||||
DATA_MODE = STOP;
|
||||
}else{
|
||||
DATA_MODE = QUALITY;
|
||||
Serial.println("in QUALITY");
|
||||
}
|
||||
drawGui(COLOR_MODE);
|
||||
}
|
||||
|
||||
void rawHandler(Event& e) {
|
||||
M5.Axp.SetLDOEnable(3, true);
|
||||
delay(80);
|
||||
M5.Axp.SetLDOEnable(3, false);
|
||||
if(DATA_MODE == RAW){
|
||||
DATA_MODE = STOP;
|
||||
}else{
|
||||
DATA_MODE = RAW;
|
||||
Serial.println("in RAW");
|
||||
}
|
||||
drawGui(COLOR_MODE);
|
||||
}
|
||||
|
||||
/// WRITING DATA ON SCREEN
|
||||
void writeScreenData(dataMode DATA_MODE, screenMode COLOR_MODE, int data[4]){
|
||||
|
||||
M5.Lcd.setTextColor(COLORS[COLOR_MODE]);
|
||||
|
||||
if(DATA_MODE == ATTENTION){
|
||||
M5.Lcd.drawString("ATTENTION", 90, 80, 4);
|
||||
}else if(DATA_MODE ==MEDITATION){
|
||||
M5.Lcd.drawString("MEDITATION", 84, 80, 4);
|
||||
}else if(DATA_MODE == QUALITY){
|
||||
M5.Lcd.drawString("SIGNAL QUALITY", 60, 80, 4);
|
||||
}else if(DATA_MODE == RAW){
|
||||
M5.Lcd.drawString("RAW", 132, 80, 4);
|
||||
}else{
|
||||
M5.Lcd.drawString("...", 148, 100, 4);
|
||||
}
|
||||
|
||||
if(DATA_MODE != STOP){
|
||||
if(COLOR_MODE == DARK) {
|
||||
M5.Lcd.fillRect(132,116,64,64,COLORS[LIGHT]);
|
||||
} else {
|
||||
M5.Lcd.fillRect(132,116,64,64,COLORS[DARK]);
|
||||
}
|
||||
M5.Lcd.drawString(String(data[DATA_MODE]), 148 - ((countDigit(data[DATA_MODE]) - 1) * 5), 128, 4);
|
||||
}
|
||||
}
|
||||
|
||||
int countDigit(int n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
while (n != 0) {
|
||||
n = n / 10;
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
String stringReceived = "";
|
||||
int valuesReceived[4];
|
||||
|
||||
void loop() {
|
||||
//CHECK IF MQTT IS CONNECTED
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
|
||||
M5.update();
|
||||
//WAITS FOR DATA IN UART
|
||||
while(Serial2.available())
|
||||
{
|
||||
char c = Serial2.read();
|
||||
stringReceived += c;
|
||||
|
||||
//ONCE LONG ENOUGH DATA HAS BEEN RECIEVED PROCEEDS
|
||||
if (stringReceived.length() == 17)
|
||||
{
|
||||
|
||||
// POPULATES ARRAY OF VALUES
|
||||
valuesReceived[RAW] = stringReceived.substring(9).toInt();
|
||||
valuesReceived[QUALITY] = stringReceived.substring(6,9).toInt();
|
||||
valuesReceived[MEDITATION] = stringReceived.substring(3,6).toInt();
|
||||
valuesReceived[ATTENTION] = stringReceived.substring(0,3).toInt();
|
||||
|
||||
// PRINTS VALUES INTO USB SERIAL CONSOLE
|
||||
Serial.printf("\t\tAT: %3d",valuesReceived[ATTENTION]);
|
||||
Serial.printf("\t\t\tMD: %3d",valuesReceived[MEDITATION]);
|
||||
Serial.printf("\t\t\tSQ: %3d",valuesReceived[QUALITY]);
|
||||
Serial.printf("\t\t\tRW: %+8d\n",valuesReceived[RAW]);
|
||||
|
||||
// PUBLISHES DATA BY MQTT
|
||||
snprintf(msgAtt, MSG_BUFFER_SIZE, "%ld",valuesReceived[ATTENTION]);
|
||||
client.publish("EEGATT",msgAtt);
|
||||
snprintf(msgMed, MSG_BUFFER_SIZE, "%ld",valuesReceived[MEDITATION]);
|
||||
client.publish("EEGMED",msgMed);
|
||||
snprintf(msgQua, MSG_BUFFER_SIZE, "%ld",valuesReceived[QUALITY]);
|
||||
client.publish("EEGQUA",msgQua);
|
||||
snprintf(msgRaw, MSG_BUFFER_SIZE, "%ld",valuesReceived[RAW]);
|
||||
client.publish("EEGRAW",msgRaw);
|
||||
|
||||
//RESETS STRING
|
||||
stringReceived = "";
|
||||
|
||||
//WRITES LATEST DATA ON SCREEN
|
||||
writeScreenData(DATA_MODE, COLOR_MODE, valuesReceived);
|
||||
}
|
||||
}
|
||||
}
|
2493
M2Ext/icons.cpp
Normal file
2493
M2Ext/icons.cpp
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue