/* * * HMI Communication Script * * This script helps you to communicate with the HMI display * from */ // Send command to the UART HMI // Example usage: sendCommand("t3.txt=\"OUTPUT\""); bool nextReadIsPageNumber = false; //Next byte is page number byte incomingByte = 0; //Incoming byte buffer void sendCommand(String cmd){ if (debugMode){ //Mirror output to serial Serial.print(cmd); Serial.write(0XFF); Serial.write(0XFF); Serial.write(0XFF); } HMISerial.print(cmd); HMISerial.write(0XFF); HMISerial.write(0XFF); HMISerial.write(0XFF); delay(10); } //Set background brightness, range 0 - 100 in string void SetDisplayBrightness(String brightness){ sendCommand("dim=" + brightness); } void renderForcastField(String dayid, String dow, int minTemp, int maxTemp, int humd, int forcastIcon){ //Update the week of day, id = fcd* sendCommand("forcast.fcd" + dayid + ".txt=\"" + dow + "\""); //Update the temperature range String tempRange = String(minTemp) + " - " + String(maxTemp) + "C | " + String(humd) + "%"; sendCommand("forcast.fct" + dayid + ".txt=\"" + tempRange + "\""); //Update weather icon String hmiWeatherIcon = "14"; if (forcastIcon == 50){ //Sunny hmiWeatherIcon = "14"; }else if (forcastIcon == 51 || forcastIcon == 52){ //Cloudy with Sun hmiWeatherIcon = "18"; }else if (forcastIcon == 53 || forcastIcon == 54){ //Sun with rain hmiWeatherIcon = "19"; }else if (forcastIcon == 60 || forcastIcon == 61){ //Cloudy hmiWeatherIcon = "17"; }else if (forcastIcon == 62 || forcastIcon == 63 || forcastIcon == 64){ //Rain hmiWeatherIcon = "20"; }else if (forcastIcon == 65){ //Thunder hmiWeatherIcon = "25"; }else if (forcastIcon >= 70 && forcastIcon <= 75){ //Clear Night hmiWeatherIcon = "15"; }else if (forcastIcon == 76){ //Cloudy Night hmiWeatherIcon = "17"; }else if (forcastIcon == 80){ //Windy hmiWeatherIcon = "26"; }else if (forcastIcon == 81){ //Dry hmiWeatherIcon = "21"; }else if (forcastIcon == 82){ //Wet hmiWeatherIcon = "22"; }else if (forcastIcon >= 83 && forcastIcon <= 85){ //Fog hmiWeatherIcon = "16"; }else if (forcastIcon == 90 || forcastIcon == 91){ //Hot hmiWeatherIcon = "24"; }else if (forcastIcon == 92 || forcastIcon == 93){ //Cold hmiWeatherIcon = "23"; } sendCommand("forcast.fcp" + dayid + ".pic=" + hmiWeatherIcon); } //Send Float to the display, support -99 to +99 range, automatic turncat into 4 digit with unit //Example usage: printIntCommand("t6.txt=", 16); void printIntCommand(String cmd, int value){ if (debugMode){ //Mirror output to serial Serial.print(cmd + "\""); Serial.print(String(value)); Serial.print("\""); Serial.write(0XFF); Serial.write(0XFF); Serial.write(0XFF); } HMISerial.print(cmd + "\""); HMISerial.print(String(value)); HMISerial.print("\""); HMISerial.write(0XFF); HMISerial.write(0XFF); HMISerial.write(0XFF); delay(10); } //Send Float to the display, support -99 to +99 range, automatic turncat into 4 digit with unit //Example usage: printFloatCommand("t6.txt=", 16.8, "V"); void printFloatCommand(String cmd, float value, String unit){ if (debugMode){ //Mirror output to serial Serial.print(cmd + "\""); if (value <= -10){ Serial.print(value, 1); }else if (value < 0){ Serial.print(value, 2); }else if (value < 10){ Serial.print(value, 2); }else{ Serial.print(value, 1); } Serial.print(unit + "\""); Serial.write(0XFF); Serial.write(0XFF); Serial.write(0XFF); } HMISerial.print(cmd + "\""); if (value <= -10){ HMISerial.print(value, 1); }else if (value < 0){ HMISerial.print(value, 2); }else if (value < 10){ HMISerial.print(value, 2); }else{ HMISerial.print(value, 1); } HMISerial.print(unit + "\""); HMISerial.write(0XFF); HMISerial.write(0XFF); HMISerial.write(0XFF); delay(10); } void changePage(String pageid){ sendCommand("page " + pageid); } //Get the current page number on HMI int getPageNumber(){ //Request the UART HMI to return its current page number sendCommand("sendme"); //Try to parse and catch the return value bool nextReadIsPageNumber = false; if (debugMode){ //Debug mode while (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); if (nextReadIsPageNumber){ //This read is the page number nextReadIsPageNumber = false; return incomingByte; } if (incomingByte == 0x66){ //This is the start of a page response nextReadIsPageNumber = true; } } }else{ while (HMISerial.available() > 0) { // read the incoming byte: incomingByte = HMISerial.read(); if (nextReadIsPageNumber){ //This read is the page number nextReadIsPageNumber = false; return incomingByte; } if (incomingByte == 0x66){ //This is the start of a page response nextReadIsPageNumber = true; } } } return -1; }