From bfe092258662853144c2c77b9f224e17ec866847 Mon Sep 17 00:00:00 2001 From: Jyrki Berg <12322333+jyberg@users.noreply.github.com> Date: Fri, 24 Jul 2020 12:57:34 +0300 Subject: [PATCH] - STD_SERIAL_BAUD -> NEX_SERIAL_DEFAULT_BAUD - Version information updated --- NexConfig.h | 13 +- NexHardware.cpp | 6 +- NexHardware.h | 2 +- NexUpload.cpp | 480 +++++++++++++++++++++++------------------------ readme.md | 9 +- release_notes.md | 3 + version.txt | 2 +- 7 files changed, 265 insertions(+), 250 deletions(-) diff --git a/NexConfig.h b/NexConfig.h index 3479aa55..d1922ed4 100644 --- a/NexConfig.h +++ b/NexConfig.h @@ -29,17 +29,19 @@ // #define STD_SUPPORT /** - * Define serial communication baud + * Define serial communication default baud. + * it is recommended that do not change defaul baud on Nextion, because it can forgot it on re-start + * If changing this value check that vakue is the same as factory set default baud in the used display. * */ -#define STD_SERIAL_BAUD 9600 +#define NEX_SERIAL_DEFAULT_BAUD 9600 /** - * Define standard or fast timeout + * Define standard (dafault) or fast timeout, you may use fast timeout in case of baudrate higher than 115200 * */ -//#define NEX_TIMEOUT_STANDARD -#define NEX_TIMEOUT_FAST +#define NEX_TIMEOUT_STANDARD +//#define NEX_TIMEOUT_FAST #ifdef NEX_TIMEOUT_FAST #define NEX_TIMEOUT_CONNECT 10 @@ -50,6 +52,7 @@ #define NEX_TIMEOUT_COMMAND 200 #define NEX_TIMEOUT_RETURN 100 #endif + /** * Define NEX_DEBUG_SERIAL_ENABLE to enable debug serial. * Comment it to disable debug serial. diff --git a/NexHardware.cpp b/NexHardware.cpp index 11ef086b..bf6c5415 100644 --- a/NexHardware.cpp +++ b/NexHardware.cpp @@ -382,9 +382,11 @@ bool nexInit(const uint32_t baud) bool ret1 = false; bool ret2 = false; - nexSerial.begin(STD_SERIAL_BAUD); // default baud, it is recommended that do not change defaul baud on Nextion, because it can forgot it on re-start - if(baud!=STD_SERIAL_BAUD) + // try to connect first with default baud as display may have forgot set baud + nexSerial.begin(NEX_SERIAL_DEFAULT_BAUD); // default baud, it is recommended that do not change defaul baud on Nextion, because it can forgot it on re-start + if(baud!=NEX_SERIAL_DEFAULT_BAUD) { + // change baud to wanted char cmd[14]; sprintf(cmd,"baud=%i",baud); sendCommand(cmd); diff --git a/NexHardware.h b/NexHardware.h index 0b5fd9f2..6a823b95 100644 --- a/NexHardware.h +++ b/NexHardware.h @@ -196,7 +196,7 @@ bool RecvTransparendDataModeFinished(size_t timeout = 200); * * @return true if success, false for failure. */ -bool nexInit(const uint32_t baud=9600); +bool nexInit(const uint32_t baud=NEX_SERIAL_DEFAULT_BAUD); /** * Listen touch event and calling callbacks attached before. diff --git a/NexUpload.cpp b/NexUpload.cpp index 9f36c0eb..cf786315 100644 --- a/NexUpload.cpp +++ b/NexUpload.cpp @@ -1,240 +1,240 @@ -/** - * @file NexUpload.cpp - * - * The implementation of download tft file for nextion. - * - * @author Chen Zengpeng (email:) - * @date 2016/3/29 - * @author Jyrki Berg 1/25/2020 (https://github.com/jyberg) - * @copyright - * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - */ - -#include "NexUpload.h" - -#ifdef USE_SOFTWARE_SERIAL -#include -SoftwareSerial dbSerial(3, 2); /* RX:D3, TX:D2 */ -#define DEBUG_SERIAL_ENABLE -#endif - -#ifdef DEBUG_SERIAL_ENABLE -#define dbSerialPrint(a) dbSerial.print(a) -#define dbSerialPrintln(a) dbSerial.println(a) -#define dbSerialBegin(a) dbSerial.begin(a) -#else -#define dbSerialPrint(a) do{}while(0) -#define dbSerialPrintln(a) do{}while(0) -#define dbSerialBegin(a) do{}while(0) -#endif - -NexUpload::NexUpload(const char *file_name,const uint8_t SD_chip_select,uint32_t download_baudrate) -{ - _file_name = file_name; - _SD_chip_select = SD_chip_select; - _download_baudrate = download_baudrate; -} - -NexUpload::NexUpload(const String file_Name,const uint8_t SD_chip_select,uint32_t download_baudrate) -{ - NexUpload(file_Name.c_str(),SD_chip_select,download_baudrate); -} - -void NexUpload::upload(void) -{ - dbSerialBegin(9600); - if(!_checkFile()) - { - dbSerialPrintln("the file is error"); - return; - } - if(_getBaudrate() == 0) - { - dbSerialPrintln("get baudrate error"); - return; - } - if(!_setDownloadBaudrate(_download_baudrate)) - { - dbSerialPrintln("modify baudrate error"); - return; - } - if(!_downloadTftFile()) - { - dbSerialPrintln("download file error"); - return; - } - dbSerialPrintln("download ok\r\n"); -} - -uint16_t NexUpload::_getBaudrate(void) -{ - uint32_t baudrate_array[7] = {115200,19200,9600,57600,38400,4800,2400}; - for(uint8_t i = 0; i < 7; i++) - { - if(_searchBaudrate(baudrate_array[i])) - { - _baudrate = baudrate_array[i]; - dbSerialPrintln("get baudrate"); - break; - } - } - return _baudrate; -} - -bool NexUpload::_checkFile(void) -{ - dbSerialPrintln("start _checkFile"); - if(!SD.begin(_SD_chip_select)) - { - dbSerialPrintln("init sd failed"); - return 0; - } - if(!SD.exists(_file_name)) - { - dbSerialPrintln("file is not exit"); - } - _myFile = SD.open(_file_name); - _undownloadByte = _myFile.size(); - dbSerialPrintln("tft file size is:"); - dbSerialPrintln(_undownloadByte); - dbSerialPrintln("check file ok"); - return 1; -} - -bool NexUpload::_searchBaudrate(uint32_t baudrate) -{ - String string = String(""); - nexSerial.begin(baudrate); - this->sendCommand(""); - this->sendCommand("connect"); - this->recvRetString(string); - if(string.indexOf("comok") != -1) - { - return 1; - } - return 0; -} - -void NexUpload::sendCommand(const char* cmd) -{ - - while (nexSerial.available()) - { - nexSerial.read(); - } - - nexSerial.print(cmd); - nexSerial.write(0xFF); - nexSerial.write(0xFF); - nexSerial.write(0xFF); -} - -uint16_t NexUpload::recvRetString(String &string, size_t timeout,bool recv_flag) -{ - uint16_t ret = 0; - uint8_t c = 0; - long start; - bool exit_flag = false; - start = millis(); - while (millis() - start <= timeout) - { - while (nexSerial.available()) - { - c = nexSerial.read(); - if(c == 0) - { - continue; - } - string += (char)c; - if(recv_flag) - { - if(string.indexOf(0x05) != -1) - { - exit_flag = true; - } - } - } - if(exit_flag) - { - break; - } - } - ret = string.length(); - return ret; -} - -bool NexUpload::_setDownloadBaudrate(uint32_t baudrate) -{ - String string = String(""); - String cmd = String(""); - - String filesize_str = String(_undownloadByte,10); - String baudrate_str = String(baudrate,10); - cmd = "whmi-wri " + filesize_str + "," + baudrate_str + ",0"; - - dbSerialPrintln(cmd); - this->sendCommand(""); - this->sendCommand(cmd.c_str()); - delay(50); - nexSerial.begin(baudrate); - this->recvRetString(string,500); - if(string.indexOf(0x05) != -1) - { - return 1; - } - return 0; -} - -bool NexUpload::_downloadTftFile(void) -{ - uint8_t c; - uint16_t send_timer = 0; - uint16_t last_send_num = 0; - String string = String(""); - send_timer = _undownloadByte / 4096 + 1; - last_send_num = _undownloadByte % 4096; - - while(send_timer) - { - - if(send_timer == 1) - { - for(uint16_t j = 1; j <= 4096; j++) - { - if(j <= last_send_num) - { - c = _myFile.read(); - nexSerial.write(c); - } - else - { - break; - } - } - } - - else - { - for(uint16_t i = 1; i <= 4096; i++) - { - c = _myFile.read(); - nexSerial.write(c); - } - } - this->recvRetString(string,500,true); - if(string.indexOf(0x05) != -1) - { - string = ""; - } - else - { - return 0; - } - --send_timer; - } -} - +/** + * @file NexUpload.cpp + * + * The implementation of download tft file for nextion. + * + * @author Chen Zengpeng (email:) + * @date 2016/3/29 + * @author Jyrki Berg 1/25/2020 (https://github.com/jyberg) + * @copyright + * Copyright (C) 2014-2015 ITEAD Intelligent Systems Co., Ltd. \n + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ + +#include "NexUpload.h" + +#ifdef USE_SOFTWARE_SERIAL +#include +SoftwareSerial dbSerial(3, 2); /* RX:D3, TX:D2 */ +#define DEBUG_SERIAL_ENABLE +#endif + +#ifdef DEBUG_SERIAL_ENABLE +#define dbSerialPrint(a) dbSerial.print(a) +#define dbSerialPrintln(a) dbSerial.println(a) +#define dbSerialBegin(a) dbSerial.begin(a) +#else +#define dbSerialPrint(a) do{}while(0) +#define dbSerialPrintln(a) do{}while(0) +#define dbSerialBegin(a) do{}while(0) +#endif + +NexUpload::NexUpload(const char *file_name,const uint8_t SD_chip_select,uint32_t download_baudrate) +{ + _file_name = file_name; + _SD_chip_select = SD_chip_select; + _download_baudrate = download_baudrate; +} + +NexUpload::NexUpload(const String file_Name,const uint8_t SD_chip_select,uint32_t download_baudrate) +{ + NexUpload(file_Name.c_str(),SD_chip_select,download_baudrate); +} + +void NexUpload::upload(void) +{ + dbSerialBegin(NEX_SERIAL_DEFAULT_BAUD); + if(!_checkFile()) + { + dbSerialPrintln("the file is error"); + return; + } + if(_getBaudrate() == 0) + { + dbSerialPrintln("get baudrate error"); + return; + } + if(!_setDownloadBaudrate(_download_baudrate)) + { + dbSerialPrintln("modify baudrate error"); + return; + } + if(!_downloadTftFile()) + { + dbSerialPrintln("download file error"); + return; + } + dbSerialPrintln("download ok\r\n"); +} + +uint16_t NexUpload::_getBaudrate(void) +{ + uint32_t baudrate_array[7] = {115200,19200,9600,57600,38400,4800,2400}; + for(uint8_t i = 0; i < 7; i++) + { + if(_searchBaudrate(baudrate_array[i])) + { + _baudrate = baudrate_array[i]; + dbSerialPrintln("get baudrate"); + break; + } + } + return _baudrate; +} + +bool NexUpload::_checkFile(void) +{ + dbSerialPrintln("start _checkFile"); + if(!SD.begin(_SD_chip_select)) + { + dbSerialPrintln("init sd failed"); + return 0; + } + if(!SD.exists(_file_name)) + { + dbSerialPrintln("file is not exit"); + } + _myFile = SD.open(_file_name); + _undownloadByte = _myFile.size(); + dbSerialPrintln("tft file size is:"); + dbSerialPrintln(_undownloadByte); + dbSerialPrintln("check file ok"); + return 1; +} + +bool NexUpload::_searchBaudrate(uint32_t baudrate) +{ + String string = String(""); + nexSerial.begin(baudrate); + this->sendCommand(""); + this->sendCommand("connect"); + this->recvRetString(string); + if(string.indexOf("comok") != -1) + { + return 1; + } + return 0; +} + +void NexUpload::sendCommand(const char* cmd) +{ + + while (nexSerial.available()) + { + nexSerial.read(); + } + + nexSerial.print(cmd); + nexSerial.write(0xFF); + nexSerial.write(0xFF); + nexSerial.write(0xFF); +} + +uint16_t NexUpload::recvRetString(String &string, size_t timeout,bool recv_flag) +{ + uint16_t ret = 0; + uint8_t c = 0; + long start; + bool exit_flag = false; + start = millis(); + while (millis() - start <= timeout) + { + while (nexSerial.available()) + { + c = nexSerial.read(); + if(c == 0) + { + continue; + } + string += (char)c; + if(recv_flag) + { + if(string.indexOf(0x05) != -1) + { + exit_flag = true; + } + } + } + if(exit_flag) + { + break; + } + } + ret = string.length(); + return ret; +} + +bool NexUpload::_setDownloadBaudrate(uint32_t baudrate) +{ + String string = String(""); + String cmd = String(""); + + String filesize_str = String(_undownloadByte,10); + String baudrate_str = String(baudrate,10); + cmd = "whmi-wri " + filesize_str + "," + baudrate_str + ",0"; + + dbSerialPrintln(cmd); + this->sendCommand(""); + this->sendCommand(cmd.c_str()); + delay(50); + nexSerial.begin(baudrate); + this->recvRetString(string,500); + if(string.indexOf(0x05) != -1) + { + return 1; + } + return 0; +} + +bool NexUpload::_downloadTftFile(void) +{ + uint8_t c; + uint16_t send_timer = 0; + uint16_t last_send_num = 0; + String string = String(""); + send_timer = _undownloadByte / 4096 + 1; + last_send_num = _undownloadByte % 4096; + + while(send_timer) + { + + if(send_timer == 1) + { + for(uint16_t j = 1; j <= 4096; j++) + { + if(j <= last_send_num) + { + c = _myFile.read(); + nexSerial.write(c); + } + else + { + break; + } + } + } + + else + { + for(uint16_t i = 1; i <= 4096; i++) + { + c = _myFile.read(); + nexSerial.write(c); + } + } + this->recvRetString(string,500,true); + if(string.indexOf(0x05) != -1) + { + string = ""; + } + else + { + return 0; + } + --send_timer; + } +} + diff --git a/readme.md b/readme.md index 4df98f2f..8d060ed1 100644 --- a/readme.md +++ b/readme.md @@ -3,7 +3,14 @@ # Enhanced Nextion Library -------------------------------------------------------------------------------- -Jyrki Berg 4/5/2019 (https://github.com/jyberg) + +Jyrki Berg 7/24/2019 (https://github.com/jyberg) + +- NexConfig: standard (dafault) or fast timeout, you may use fast timeout in case of baudrate higher than 115200 +- bug in NexRtc::read_rtc_time() #12 Corrected +- appendText added to the text object + +Jyrki Berg 4/5/2019 -Backward compatibility issue: functions timeout parameters datatype changed from uint32_t to size_t - getValue and getText doesn't work #9 corrected diff --git a/release_notes.md b/release_notes.md index 2078318e..b4cffb19 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,6 +1,9 @@ # Release Notes -------------------------------------------------------------------------------- +# Release v0.11.9 +- NexConfig: standard (dafault) or fast timeout, you may use fast timeout in case of baudrate higher than 115200 + # Release v0.11.8 - bug in NexRtc::read_rtc_time() #12 Corrected diff --git a/version.txt b/version.txt index 4fc57536..61d147bd 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.11.8 +0.11.9