hello All,
I have developpe a pcb for an lcd that display multi serial sensor . my question is about organize my code for best performance . The code work nicely but i want to improve it.
Hardware
1 esp32 and oled display (the gps in attached to this esp32
1 gps Serial frenquency every 1second
1 esp32 witch output Serial data of imu6050+ compass evrey 500ms
description : the esp32 screen have a switch to change view data display so i am mixing the
display with gps and imu data....
my code below:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(BTN_PIN, INPUT);
Serial.begin(115200);
SerialP.begin(115200, SERIAL_8N1, RXD2, TXD2);
int reading = digitalRead(BTN_PIN);
tft.begin();
tft.setRotation(45);
tft.setSwapBytes(true);
tft.pushImage(0,1,320,170,Back);
}
void Switch(){
int reading = digitalRead(BTN_PIN);
if (reading != btnState) {
btnState = reading;
if (btnState == HIGH) {
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
//Serial.print(ledState);
}
if(ledState==1){
SR1 = false;
tft.setRotation(45);
tft.setSwapBytes(true);
tft.pushImage(0,1,320,170,Back2);
SR2 = true;
}else {
SR1 = true;
//tft.begin();
tft.setRotation(45);
tft.setSwapBytes(true);
tft.pushImage(0,1,320,170,Back);
SR2 = false;
}
}
lastBtnState = reading;
}
//****************************************************************
//**************************************************************
void loop(void) {
Switch();
if(SR1) {
SR2=false;
IMU(roll,pitch,hdg);
Screen1();
screen2();
}
if(SR2) {
SR1=false;
DATAGPS(LAT,LAT2,LONG,LONG2,nb,sp,Cog,h,mn,s);
Screen4();
BlinkGreen();
}
}
//****************************SCREEN 1**************************************
void IMU(int &roll,int &pitch,int &hdg){
//while (Serial.available()) {
Data1 = Serial.readStringUntil('\n');
//Serial.println(Data1);
String readString =Data1;
int delimiter, delimiter_1, delimiter_2,delimiter_3,delimiter_4,delimiter_5;
delimiter = readString.indexOf(":");
delimiter_1 = readString.indexOf(":", delimiter_1 );
delimiter_2 = readString.indexOf(":", delimiter_1 +1);
delimiter_3 = readString.indexOf(":", delimiter_2 +1);
delimiter_4 = readString.indexOf(":", delimiter_3 +1);
delimiter_5 = readString.indexOf(":", delimiter_4 +1);
String A = readString.substring(delimiter -2 , delimiter_1);
String Roll = readString.substring(delimiter_1 + 1, delimiter_2);
String Pitch = readString.substring(delimiter_2 + 1, delimiter_3);
String Hdg = readString.substring(delimiter_3 + 1, delimiter_4);
String Yaw = readString.substring(delimiter_4 + 1, delimiter_4);
roll = (Roll.toInt());
pitch = (Pitch.toInt());
hdg=(Hdg.toInt());
yaw=(Yaw.toInt());
}
//***********************************************************************************
void Screen1(){
if (roll<0){
tft.setTextColor(TFT_RED, TFT_BLACK, true);
tft.loadFont(AA_FONT_LARGE);
tft.setCursor(60,72);
//tft.printf("%s%.2i",sp1,number22);
tft.printf("%.2i",roll);
tft.setCursor(115,60);
tft.loadFont(ARIAL);
tft.printf("o");
}
else {
//tft.setCursor(60,72);
tft.setTextColor(TFT_GREEN, TFT_BLACK, true);
//tft.print(" ");
tft.loadFont(AA_FONT_LARGE);
tft.setCursor(60,72);
tft.printf(" %.2i",roll);
tft.setCursor(115,60);
tft.loadFont(ARIAL);
tft.print("o");
}
if (pitch<0){
//tft.setCursor(245,72);
tft.setTextColor(TFT_RED, TFT_BLACK, true);
tft.loadFont(AA_FONT_LARGE);
tft.setCursor(245,72);
tft.printf("%.2i",pitch);
tft.loadFont(ARIAL);
tft.setCursor(300,60);
tft.printf("o");
}
else {
//tft.setCursor(245,72);
tft.setTextColor(TFT_GREEN, TFT_BLACK, true);
tft.loadFont(AA_FONT_LARGE);
tft.setCursor(245,72);
tft.printf(" %.2i",pitch);
tft.loadFont(ARIAL);
tft.setCursor(300,60);
tft.printf("o");
}
tft.setCursor(245,130);
tft.setTextColor(TFT_GREEN, TFT_BLACK, true);
tft.loadFont(AA_FONT_LARGE);
tft.printf("%.3i",hdg);
tft.loadFont(ARIAL);
tft.setCursor(308,120);
tft.printf("o");
}
//****************************************************************
void screen2(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
DATAGPS(LAT,LAT2,LONG,LONG2,nb,sp,Cog,h,mn,s);
tft.setCursor(55, 17);
tft.setTextColor(TFT_GREEN, TFT_BLACK,true);
tft.loadFont(AA_FONT_LARGE);
tft.printf("%.2i",sp);
tft.setCursor(235, 17);
tft.setTextColor(TFT_GREEN, TFT_BLACK, true);
tft.loadFont(AA_FONT_LARGE);
tft.printf("%.3i",Cog);
tft.loadFont(ARIAL);
tft.setCursor(300,10);
tft.printf("o");
//delay(200);
tft.setCursor(70, 130);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.loadFont(AA_FONT_LARGE);
tft.print("22");
tft.loadFont(ARIAL);
tft.setCursor(115,120);
tft.printf("o");
}
}
//*********************************On LINE*******************************************************
void BlinkGreen(){
if(millis()-timerTwo >=1000){
tft.setSwapBytes(true);
tft.fillCircle(310,18,7,TFT_GREEN);
timerTwo=millis();
}
if(millis()-timerOne >=2000){
tft.setSwapBytes(true);
// unsigned long currentMillis = millis();
// if(currentMillis - previousMillis > interval) {
// previousMillis = currentMillis;
tft.fillCircle(310,18,8,TFT_BLACK);
timerOne = millis();
//delay(500);
}
}
//********************************Off LINE*******************************************************
//void blinkRED(){
//if(millis()-timerTwo >=1000){
// tft.setSwapBytes(true);
// tft.fillCircle(300,18,8,TFT_RED);
//timerTwo=millis();
//}
// else if(millis()-timerOne >=1000){
//tft.setSwapBytes(true);
//// unsigned long currentMillis = millis();
//// if(currentMillis - previousMillis > interval) {
//// previousMillis = currentMillis;
//tft.fillCircle(300,18,10,TFT_BLACK);
//timerOne = millis();
//}
//}
//***************************************************************************************************
//GP DATAGPS(GP& DATA_GPS){
void DATAGPS(String &LAT,String &LAT2,String &LONG,String &LONG2,int &nb,int &sp,int &Cog,int &h,int &mn, int &s){
Data2 = SerialP.readStringUntil('\n');
//Serial.println(Data2);
//***************************************************
//**********************************************************
String readString =Data2;
int delimiter, delimiter_1, delimiter_2,delimiter_3,delimiter_4,delimiter_5,delimiter_6,delimiter_7,delimiter_8,delimiter_9,delimiter_10;
delimiter = readString.indexOf("-");
delimiter = readString.indexOf("-");
delimiter_1 = readString.indexOf("-", delimiter_1 );
delimiter_2 = readString.indexOf("-", delimiter_1 +1);
delimiter_3 = readString.indexOf("-", delimiter_2 +1);
delimiter_4 = readString.indexOf("-", delimiter_3 +1);
delimiter_5 = readString.indexOf("-", delimiter_4 +1);
delimiter_6 = readString.indexOf("-", delimiter_5 +1);
delimiter_7 = readString.indexOf("-", delimiter_6 +1);
delimiter_8 = readString.indexOf("-", delimiter_7 +1);
delimiter_9 = readString.indexOf("-", delimiter_8 +1);
delimiter_10 = readString.indexOf("-", delimiter_9 +1);
LAT = readString.substring(delimiter -2 , delimiter_1);
LAT2 = readString.substring(delimiter_1 + 1, delimiter_2);
LONG = readString.substring(delimiter_2 + 1, delimiter_3);
LONG2 = readString.substring(delimiter_3 + 1, delimiter_4);
String SP = readString.substring(delimiter_4 + 1, delimiter_5);
String NB = readString.substring(delimiter_5 + 1, delimiter_6);
String COG = readString.substring(delimiter_6 + 1, delimiter_7);
String H = readString.substring(delimiter_7 + 1, delimiter_8);
String MN = readString.substring(delimiter_8 + 1, delimiter_9);
String S = readString.substring(delimiter_9 + 1, delimiter_10);
nb=(NB.toInt());
sp=(SP.toInt());
Cog=(COG.toInt());
h=(H.toInt());
mn=(MN.toInt());
s=(S.toInt());
}
void Screen4 (){
tft.setCursor(55, 40); //X Y
tft.setTextColor(TFT_GREEN, TFT_BLACK,true);
tft.loadFont(AA_FONT_LARGE);
tft.printf("%s",LAT);
String Deg1=("o");
tft.loadFont(ARIAL);
tft.setCursor(100, 35);
tft.printf("%s",Deg1);
tft.setCursor(115, 40);
tft.loadFont(AA_FONT_LARGE);
// String LAT2=("00'34.268W");
tft.printf("%s\r\n",LAT2);
//********************************************************************
tft.setCursor(55, 80); //X Y
tft.setTextColor(TFT_GREEN, TFT_BLACK,true);
tft.loadFont(AA_FONT_LARGE);
tft.printf("%s",LONG);
tft.loadFont(ARIAL);
tft.setCursor(100, 75);
tft.printf("%s",Deg1);
tft.setCursor(115, 80);
tft.loadFont(AA_FONT_LARGE);
tft.printf("%s\r\n",LONG2);
//*********************************************************************
//time****************************************
tft.setCursor(38, 12); //X Y
tft.setTextColor(TFT_GREEN, TFT_BLACK,true);
tft.loadFont(ARIAL);
tft.printf(" %.2i:%.2i:%.2i",h,mn,s);
//sat number*********************************
tft.setCursor(210, 12); //X Y
tft.setTextColor(TFT_GREEN, TFT_BLACK,true);
tft.loadFont(ARIAL);
tft.printf(" %.2i",nb);
//speed***************************************
tft.setCursor(55, 130);
tft.setTextColor(TFT_GREEN, TFT_BLACK, true);
tft.loadFont(AA_FONT_LARGE);
tft.printf("%.2i",sp);
//cog****************************************
tft.setCursor(180, 130);
tft.setTextColor(TFT_GREEN, TFT_BLACK,true);
tft.loadFont(AA_FONT_LARGE);
tft.printf("%.3i",Cog);
tft.setCursor(245,120);
tft.loadFont(ARIAL);
tft.print("o");
} saisissez ou collez du code ici
My question is how to manage the best way the 2 serial fluxes focus on screen2() function that add some gps data on screen1().
The trick was to call again DATAGPS and put a millis function to add those data.
1 IMU(roll,pitch,hdg);
2 DATAGPS(LAT,LAT2,LONG,LONG2,nb,sp,Cog,h,mn,s);