Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
NextionLCD.cpp
00001 #include <cstdlib> 00002 #include <string> 00003 #include "main.h" 00004 #include "Functions.h" 00005 #include "Definitions.h" 00006 #include "Boolean.h" 00007 #include "NextionLCD.h" 00008 #include "mbed.h" 00009 #include "Languages.h" 00010 #include "NVM.h" 00011 #include "Controls.h" 00012 #include "FastPWM.h" 00013 #include <cstring> 00014 00015 /////////////////////////////////////////////////////////////////////////////// 00016 // MBED 00017 /////////////////////////////////////////////////////////////////////////////// 00018 00019 /////////////////////////////////////////////////////////////////////////////// 00020 // VARIABLES 00021 /////////////////////////////////////////////////////////////////////////////// 00022 bool scrnUpdate = true;//Update the Nexion screen 00023 00024 volatile bool keyScanFlag; 00025 00026 uint8_t nexDataLSB; 00027 uint8_t nexDataMSB; 00028 volatile uint8_t page = 0; 00029 volatile uint8_t id= 0; 00030 volatile bool pushPop = 0; 00031 00032 /////////////////////////////////////////////////////////////////////////////// 00033 // CONSTANTS 00034 /////////////////////////////////////////////////////////////////////////////// 00035 bool push = 1; 00036 bool pop = 0; 00037 00038 /////////////////////////////////////////////////////////////////////////////// 00039 // DEBUG 00040 /////////////////////////////////////////////////////////////////////////////// 00041 volatile char dbgNexMsg[20]; 00042 volatile int dbgMRxIdx; 00043 volatile bool debugFlag; 00044 volatile bool tx_true; 00045 00046 NextionLCD::NextionLCD(PinName Tx, PinName Rx) : lcd(Tx, Rx){ 00047 lcd.baud(9600); 00048 mTouch=false; 00049 mRxIdx=0; 00050 lcd.printf("rest%c%c%c", 0xff, 0xff, 0xff); 00051 lcd.printf("rest%c%c%c", 0xff, 0xff, 0xff); 00052 00053 #ifdef DEBUG_LCD 00054 pc.printf("\r\n\r\nNextionLCD Rst\r\n"); 00055 #endif 00056 00057 wait(1.0); 00058 lcd.attach(callback(this,&NextionLCD::RxInterrupt),Serial::RxIrq); 00059 } 00060 00061 void NextionLCD::nexRst(void){ 00062 lcd.printf("rest%c%c%c", 0xff, 0xff, 0xff); 00063 lcd.printf("rest%c%c%c", 0xff, 0xff, 0xff); 00064 00065 #ifdef DEBUG_LCD 00066 pc.printf("\r\n\r\nnexRst\r\n"); 00067 #endif 00068 } 00069 00070 void NextionLCD::nexSetFont(string id, uint8_t val){ 00071 static string newId = 0; 00072 static uint8_t newVal = 0; 00073 00074 if((id != newId)||(val != newVal)){ 00075 lcd.printf("%s.font=%d%c%c%c",id.c_str(),val, 0xff, 0xff, 0xff); 00076 newId = id; 00077 newVal = val; 00078 00079 #ifdef DEBUG_LCD 00080 pc.printf("\r\n\r\nnexSetFont %s %d\r\n",id.c_str(),val); 00081 #endif 00082 } 00083 } 00084 00085 void NextionLCD::nexSetBckLite(uint8_t val){//ok 00086 /* 00087 dim : use dim to instanteniously change backlight intensity, this is not saved in NVM and forgotten once power cycled 00088 dims: use dims to instanteniously change backlight intensity, this is saved in NVM and remembered after power cycle. 00089 */ 00090 static uint8_t newVal = 0; 00091 00092 if(val != newVal){ 00093 lcd.printf("dims=%d%c%c%c",val, 0xff, 0xff, 0xff); 00094 newVal = val; 00095 00096 #ifdef DEBUG_LCD 00097 pc.printf("\r\n\r\nnexSetBcklite = %d\r\n",val); 00098 #endif 00099 } 00100 } 00101 00102 void NextionLCD::nexSetBaud(uint32_t baud){//ok 00103 static uint32_t newBaud = 0; 00104 00105 if(baud != newBaud){ 00106 lcd.printf("baud=%d%c%c%c",baud, 0xff, 0xff, 0xff);//Set Nextion display to high baud 00107 wait(0.5); 00108 lcd.baud(baud);//Set Mbed target to high baud 00109 wait(0.5); 00110 newBaud = baud; 00111 00112 #ifdef DEBUG_LCD 00113 pc.printf("\r\n\r\nnexSetBaud = %u\r\n",baud); 00114 #endif 00115 } 00116 } 00117 00118 void NextionLCD::nexChgPage(uint8_t nexPage){//ok 00119 lcd.printf("page %d%c%c%c",nexPage,0xff, 0xff, 0xff); 00120 00121 #ifdef DEBUG_LCD 00122 pc.printf("\r\n\r\nnexChgPage = %d\r\n",nexPage); 00123 #endif 00124 } 00125 00126 void NextionLCD::nexSetDSBtn(string id, uint8_t on_off){ 00127 00128 if(on_off == ON) 00129 lcd.printf("%s.val=1%c%c%c",id.c_str(),0xff, 0xff, 0xff); 00130 else 00131 if(on_off == OFF) 00132 lcd.printf("%s.val=0%c%c%c",id.c_str(),0xff, 0xff, 0xff); 00133 00134 #ifdef DEBUG_LCD 00135 pc.printf("\r\n\r\nnexSetDSBtn %s %d\r\n",id.c_str(),on_off); 00136 #endif 00137 } 00138 00139 void NextionLCD::nexRotArrow(bool on_off){//ok 00140 static bool newOnOff = 0; 00141 00142 if(on_off != newOnOff){ 00143 00144 if(on_off == ON){ 00145 lcd.printf("arrow.picc=19%c%c%c", 0xff, 0xff, 0xff); 00146 lcd.printf("arrowTimer.en=1%c%c%c", 0xff, 0xff, 0xff); 00147 } 00148 else 00149 if(on_off == OFF){ 00150 lcd.printf("arrowTimer.en=0%c%c%c", 0xff, 0xff, 0xff); 00151 lcd.printf("arrow.picc=18%c%c%c", 0xff, 0xff, 0xff); 00152 } 00153 00154 newOnOff = on_off; 00155 00156 00157 #ifdef DEBUG_LCD 00158 pc.printf("\r\n\r\nnexRotArrow = %d\r\n", on_off); 00159 #endif 00160 } 00161 } 00162 00163 void NextionLCD::nexDispSymbol(string id, bool on_off){ 00164 static string newId = 0; 00165 static bool newOnOff = 0; 00166 00167 if((id != newId)||(on_off!= newOnOff)){ 00168 00169 if(on_off == ON) 00170 nexSetCrop(id, 1);//display symbol 00171 else 00172 if(on_off == OFF) 00173 nexSetCrop(id, 16);//white page 00174 00175 newId = id; 00176 newOnOff = on_off; 00177 00178 #ifdef DEBUG_LCD 00179 pc.printf("\r\n\r\nnexDispSymbol %s %d\r\n", id.c_str(),on_off ); 00180 #endif 00181 } 00182 } 00183 00184 void NextionLCD::nexSetCrop(string id, uint16_t picc){//ok 00185 /* 00186 q0 = arrow crop image 00187 picc = 1//Turn rotation arrow off 00188 picc = 2// pos1 displayed 00189 picc = 3// pos2 displayed 00190 picc = 4// pos3 displayed 00191 picc = 5// pos4 displayed 00192 00193 */ 00194 static string newId = 0; 00195 static uint16_t newPicc = 0; 00196 00197 if((id != newId)||(picc!= newPicc)){ 00198 lcd.printf("%s.picc=%d%c%c%c",id.c_str(),picc,0xff, 0xff, 0xff); 00199 newId = id; 00200 newPicc = picc; 00201 00202 #ifdef DEBUG_LCD 00203 pc.printf("\r\n\r\nnexSetCrop %s %d\r\n", id.c_str(),picc); 00204 #endif 00205 } 00206 } 00207 00208 void NextionLCD::nexSetTimer(string id,uint16_t tim, uint16_t on_off){//ok 00209 lcd.printf("%s.tim=%d%c%c%c",id.c_str(),tim,0xff, 0xff, 0xff); 00210 lcd.printf("%s.en=%d%c%c%c",id.c_str(),on_off,0xff, 0xff, 0xff); 00211 00212 #ifdef DEBUG_LCD 00213 pc.printf("\r\n\r\nnexSetTimer %s %d %d \r\n", id.c_str(),tim,on_off); 00214 #endif 00215 } 00216 00217 void NextionLCD::nexSetSlider(string id, uint16_t sliderVal){//ok 00218 static string newId = 0; 00219 static uint16_t newSliderVal = 0; 00220 00221 if((id != newId)||(sliderVal!= newSliderVal)){ 00222 lcd.printf("%s.val=%d%c%c%c",id.c_str(),sliderVal,0xff, 0xff, 0xff); 00223 newId = id; 00224 newSliderVal = sliderVal; 00225 00226 #ifdef DEBUG_LCD 00227 pc.printf("\r\n\r\nnexSetSlider %s %d\r\n", id.c_str(),sliderVal); 00228 #endif 00229 } 00230 } 00231 00232 void NextionLCD::nexSetPrgBar(string id, uint16_t progBarVal){//ok 00233 static string newId = 0; 00234 static uint16_t newProgBarVal = 0; 00235 00236 if((id != newId)||(progBarVal!= newProgBarVal)){ 00237 lcd.printf("%s.val=%d%c%c%c",id.c_str(),progBarVal,0xff, 0xff, 0xff); 00238 newId = id; 00239 newProgBarVal = progBarVal; 00240 00241 #ifdef DEBUG_LCD 00242 pc.printf("\r\n\r\nnexSetPrgBar %s %d\r\n", id.c_str(),progBarVal); 00243 #endif 00244 } 00245 } 00246 00247 void NextionLCD::nexSendTxt(string id,string txt){//ok 00248 static string newId = 0; 00249 static string newTxt = 0; 00250 00251 if((id != newId)||(txt != newTxt)){ 00252 lcd.printf("%s.txt=\"%s\"%c%c%c",id.c_str(),txt.c_str(),0xff, 0xff, 0xff); 00253 newId = id; 00254 newTxt = txt; 00255 00256 #ifdef DEBUG_LCD 00257 pc.printf("\r\n\r\nnexSendTxt %s %s\r\n",id.c_str(),txt.c_str()); 00258 #endif 00259 } 00260 } 00261 00262 void NextionLCD::nexSendFloat(string id, float val ,uint8_t precision){ 00263 /* 00264 In C, the printf() statement allows the precision lengths to be supplied in the parameter list. 00265 00266 printf("%*.*f", 7, 3, floatValue); 00267 00268 where the asterisks are replaced with first and second values, respectively. 00269 */ 00270 static string newId = 0; 00271 static float newVal = 0; 00272 00273 if((id != newId)||(val != newVal)){ 00274 lcd.printf("%s.txt=\"%.*f\"%c%c%c",id.c_str(), precision, val, 0xff, 0xff, 0xff); 00275 newId = id; 00276 newVal = val; 00277 00278 #ifdef DEBUG_LCD 00279 pc.printf("\r\n\r\nnexSendFloat string=%s, pre=%d, val=%f\r\n",id.c_str(), precision, val); 00280 #endif 00281 } 00282 } 00283 00284 void NextionLCD::nexSetFontCol(string id, uint16_t color){ 00285 static string newId = 0; 00286 static uint16_t newColor = 0; 00287 00288 if((id != newId)||(color!= newColor)){ 00289 lcd.printf("%s.pco=%d%c%c%c",id.c_str(),color,0xff, 0xff, 0xff); 00290 newId = id; 00291 newColor = color; 00292 00293 #ifdef DEBUG_LCD 00294 pc.printf("\r\n\r\nnexSetFontCol %s %d\r\n", id.c_str(),color); 00295 #endif 00296 } 00297 } 00298 00299 void NextionLCD::nexSendVal(string id,uint16_t val){ 00300 static string newId = 0; 00301 static uint16_t newVal = 0; 00302 00303 if((id != newId)||(val!= newVal)){ 00304 lcd.printf("%s.txt=\"%d\"%c%c%c",id.c_str(), val, 0xff, 0xff, 0xff); 00305 newId = id; 00306 newVal = val; 00307 00308 #ifdef DEBUG_LCD 00309 pc.printf("\r\n\r\nnexSendVal %s %d\r\n", id.c_str(),val); 00310 #endif 00311 } 00312 } 00313 00314 void NextionLCD::nexSendGetVal(string id){ 00315 static string newId = 0; 00316 00317 if(id != newId){ 00318 lcd.printf("get %s.val%c%c%c",id.c_str(),0xff, 0xff, 0xff); 00319 newId = id; 00320 00321 #ifdef DEBUG_LCD 00322 pc.printf("\r\n\r\nnexSendGetVal %s\r\n",id.c_str()); 00323 #endif 00324 } 00325 } 00326 00327 void NextionLCD::ClrScr(uint16_t color){//ok 00328 lcd.printf("cls %d%c%c%c", color, 0xff, 0xff, 0xff); 00329 00330 #ifdef DEBUG_LCD 00331 pc.printf("\r\n\r\nClrScr %d\r\n",color); 00332 #endif 00333 } 00334 00335 void NextionLCD::nexLoadSD(void){ 00336 nexPwrCont = ON;//Nextion power is on 00337 wait(0.50); 00338 nexSetBckLite(5);//program backlight to be off at power up 00339 wait(5.0); 00340 nexPwrCont = OFF; 00341 wait(5.0); 00342 nexPwrCont = ON;//Nextion power is on 00343 wait(0.25); 00344 00345 #ifdef DEBUG_LCD 00346 pc.printf("\r\n\r\nnexLoadSD\r\n"); 00347 #endif 00348 } 00349 00350 void NextionLCD::nexDrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) { 00351 static uint16_t newX1,newY1,newX2,newY2,newColor = 0; 00352 00353 if((x1 != newX1)||(y1 != newY1)||(x2 != newX2)||(y2 != newY2)||(color != newColor)){ 00354 lcd.printf("line %d,%d,%d,%d,%d%c%c%c", x1, y1, x2, y2, color, 0xff, 0xff, 0xff); 00355 newX1 = x1; 00356 newY1 = y1; 00357 newX2 = x2; 00358 newY2 = y2; 00359 newColor = color; 00360 00361 #ifdef DEBUG_LCD 00362 pc.printf("\r\n\r\nnexDrawLine x1=%d y1=%d x2=%d y2=%d col=%d\r\n",x1, y1, x2, y2, color); 00363 #endif 00364 } 00365 } 00366 00367 void NextionLCD::RxInterrupt(void){ 00368 /* 00369 Decode Nextion messages 00370 */ 00371 char c; 00372 00373 if(lcd.readable()){ 00374 00375 debugFlag = true;//Debug all 00376 00377 c = lcd.getc(); 00378 mRxMsg[mRxIdx] = c; 00379 dbgNexMsg[mRxIdx] = c;//make a copy of the message in dbgNexMsg[] 00380 dbgMRxIdx = mRxIdx++;//Save a copy of the index counter in dbgMRxIdx 00381 00382 00383 /* 00384 These are touch screen evernt and by there nature slow so 00385 it should no be necessary to have to process these received touch event that quickly 00386 so no need to just set a flag in the RX ISR and let a thread do the processing it can 00387 ALL be done with the RX ISR 00388 */ 00389 00390 00391 if((mRxIdx >= 3) && (mRxMsg[mRxIdx-1] == 0xff) && (mRxMsg[mRxIdx-2] == 0xff) && (mRxMsg[mRxIdx-3] == 0xff)){//valid rx message 00392 00393 mRxIdx=0; 00394 //////////////////////////////debugFlag = true; 00395 00396 //led1=1; 00397 00398 if(mRxMsg[0] == TOUCH_EVENT){//Nextion RX Touch Event 00399 00400 page = mRxMsg[PAGE];//get the page 00401 id = mRxMsg[ID];//get the component ID 00402 pushPop = mRxMsg[PUSHPOP];//get the push/pop state 00403 00404 keyScanFlag = true;//set key scn flag for main routine 00405 00406 } 00407 00408 if(mRxMsg[0] == 0x71){//Nextion RX Numeric Data 00409 //led1=1; 00410 scrnUpdate = true; 00411 nexDataLSB = mRxMsg[1]; 00412 nexDataMSB = mRxMsg[2]; 00413 } 00414 } 00415 } 00416 }
Generated on Tue Jul 19 2022 00:58:42 by
1.7.2