hmi.ino 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. *
  3. * HMI Communication Script
  4. *
  5. * This script helps you to communicate with the HMI display
  6. * from
  7. */
  8. // Send command to the UART HMI
  9. // Example usage: sendCommand("t3.txt=\"OUTPUT\"");
  10. bool nextReadIsPageNumber = false; //Next byte is page number
  11. byte incomingByte = 0; //Incoming byte buffer
  12. void sendCommand(String cmd){
  13. if (debugMode){
  14. //Mirror output to serial
  15. Serial.print(cmd);
  16. Serial.write(0XFF);
  17. Serial.write(0XFF);
  18. Serial.write(0XFF);
  19. }
  20. HMISerial.print(cmd);
  21. HMISerial.write(0XFF);
  22. HMISerial.write(0XFF);
  23. HMISerial.write(0XFF);
  24. delay(10);
  25. }
  26. //Set background brightness, range 0 - 100 in string
  27. void SetDisplayBrightness(String brightness){
  28. sendCommand("dim=" + brightness);
  29. }
  30. void renderForcastField(String dayid, String dow, int minTemp, int maxTemp, int humd, int forcastIcon){
  31. //Update the week of day, id = fcd*
  32. sendCommand("forcast.fcd" + dayid + ".txt=\"" + dow + "\"");
  33. //Update the temperature range
  34. String tempRange = String(minTemp) + " - " + String(maxTemp) + "C | " + String(humd) + "%";
  35. sendCommand("forcast.fct" + dayid + ".txt=\"" + tempRange + "\"");
  36. //Update weather icon
  37. String hmiWeatherIcon = "14";
  38. if (forcastIcon == 50){
  39. //Sunny
  40. hmiWeatherIcon = "14";
  41. }else if (forcastIcon == 51 || forcastIcon == 52){
  42. //Cloudy with Sun
  43. hmiWeatherIcon = "18";
  44. }else if (forcastIcon == 53 || forcastIcon == 54){
  45. //Sun with rain
  46. hmiWeatherIcon = "19";
  47. }else if (forcastIcon == 60 || forcastIcon == 61){
  48. //Cloudy
  49. hmiWeatherIcon = "17";
  50. }else if (forcastIcon == 62 || forcastIcon == 63 || forcastIcon == 64){
  51. //Rain
  52. hmiWeatherIcon = "20";
  53. }else if (forcastIcon == 65){
  54. //Thunder
  55. hmiWeatherIcon = "25";
  56. }else if (forcastIcon >= 70 && forcastIcon <= 75){
  57. //Clear Night
  58. hmiWeatherIcon = "15";
  59. }else if (forcastIcon == 76){
  60. //Cloudy Night
  61. hmiWeatherIcon = "17";
  62. }else if (forcastIcon == 80){
  63. //Windy
  64. hmiWeatherIcon = "26";
  65. }else if (forcastIcon == 81){
  66. //Dry
  67. hmiWeatherIcon = "21";
  68. }else if (forcastIcon == 82){
  69. //Wet
  70. hmiWeatherIcon = "22";
  71. }else if (forcastIcon >= 83 && forcastIcon <= 85){
  72. //Fog
  73. hmiWeatherIcon = "16";
  74. }else if (forcastIcon == 90 || forcastIcon == 91){
  75. //Hot
  76. hmiWeatherIcon = "24";
  77. }else if (forcastIcon == 92 || forcastIcon == 93){
  78. //Cold
  79. hmiWeatherIcon = "23";
  80. }
  81. sendCommand("forcast.fcp" + dayid + ".pic=" + hmiWeatherIcon);
  82. }
  83. //Send Float to the display, support -99 to +99 range, automatic turncat into 4 digit with unit
  84. //Example usage: printIntCommand("t6.txt=", 16);
  85. void printIntCommand(String cmd, int value){
  86. if (debugMode){
  87. //Mirror output to serial
  88. Serial.print(cmd + "\"");
  89. Serial.print(String(value));
  90. Serial.print("\"");
  91. Serial.write(0XFF);
  92. Serial.write(0XFF);
  93. Serial.write(0XFF);
  94. }
  95. HMISerial.print(cmd + "\"");
  96. HMISerial.print(String(value));
  97. HMISerial.print("\"");
  98. HMISerial.write(0XFF);
  99. HMISerial.write(0XFF);
  100. HMISerial.write(0XFF);
  101. delay(10);
  102. }
  103. //Send Float to the display, support -99 to +99 range, automatic turncat into 4 digit with unit
  104. //Example usage: printFloatCommand("t6.txt=", 16.8, "V");
  105. void printFloatCommand(String cmd, float value, String unit){
  106. if (debugMode){
  107. //Mirror output to serial
  108. Serial.print(cmd + "\"");
  109. if (value <= -10){
  110. Serial.print(value, 1);
  111. }else if (value < 0){
  112. Serial.print(value, 2);
  113. }else if (value < 10){
  114. Serial.print(value, 2);
  115. }else{
  116. Serial.print(value, 1);
  117. }
  118. Serial.print(unit + "\"");
  119. Serial.write(0XFF);
  120. Serial.write(0XFF);
  121. Serial.write(0XFF);
  122. }
  123. HMISerial.print(cmd + "\"");
  124. if (value <= -10){
  125. HMISerial.print(value, 1);
  126. }else if (value < 0){
  127. HMISerial.print(value, 2);
  128. }else if (value < 10){
  129. HMISerial.print(value, 2);
  130. }else{
  131. HMISerial.print(value, 1);
  132. }
  133. HMISerial.print(unit + "\"");
  134. HMISerial.write(0XFF);
  135. HMISerial.write(0XFF);
  136. HMISerial.write(0XFF);
  137. delay(10);
  138. }
  139. void changePage(String pageid){
  140. sendCommand("page " + pageid);
  141. }
  142. //Get the current page number on HMI
  143. int getPageNumber(){
  144. //Request the UART HMI to return its current page number
  145. sendCommand("sendme");
  146. //Try to parse and catch the return value
  147. bool nextReadIsPageNumber = false;
  148. if (debugMode){
  149. //Debug mode
  150. while (Serial.available() > 0) {
  151. // read the incoming byte:
  152. incomingByte = Serial.read();
  153. if (nextReadIsPageNumber){
  154. //This read is the page number
  155. nextReadIsPageNumber = false;
  156. return incomingByte;
  157. }
  158. if (incomingByte == 0x66){
  159. //This is the start of a page response
  160. nextReadIsPageNumber = true;
  161. }
  162. }
  163. }else{
  164. while (HMISerial.available() > 0) {
  165. // read the incoming byte:
  166. incomingByte = HMISerial.read();
  167. if (nextReadIsPageNumber){
  168. //This read is the page number
  169. nextReadIsPageNumber = false;
  170. return incomingByte;
  171. }
  172. if (incomingByte == 0x66){
  173. //This is the start of a page response
  174. nextReadIsPageNumber = true;
  175. }
  176. }
  177. }
  178. return -1;
  179. }