Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
*.exe
*.out
*.app

.vscode/
44 changes: 26 additions & 18 deletions src/EasyNextionLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ String EasyNex::readStr(String TextComponent){

String _Textcomp = TextComponent;
bool _endOfCommandFound = false;
char _tempChar;
uint8_t _tempChar;

_tmr1 = millis();
while(_serial->available()){ // Waiting for NO bytes on Serial,
// as other commands could be sent in that time.
if((millis() - _tmr1) > 1000UL){ // Waiting... But not forever...after the timeout
if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...after the timeout
_readString = "ERROR";
break; // Exit from the loop due to timeout. Return ERROR
}else{
Expand All @@ -110,7 +110,7 @@ String EasyNex::readStr(String TextComponent){
_tmr1 = millis();
while(_serial->available() < 4){ // Waiting for bytes to come to Serial, an empty Textbox will send 4 bytes
// and this the minimmum number that we are waiting for (70 FF FF FF)
if((millis() - _tmr1) > 400UL){ // Waiting... But not forever...after the timeout
if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...after the timeout
_readString = "ERROR";
break; // Exit the loop due to timeout. Return ERROR
}
Expand All @@ -125,7 +125,7 @@ String EasyNex::readStr(String TextComponent){
_start_char = _serial->read();
}

if((millis() - _tmr1) > 100UL){ // Waiting... But not forever......
if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever......
_readString = "ERROR"; // If the 0x70 is not found within the given time, break the while()
// to avoid being stuck inside the while() loop
break;
Expand All @@ -151,7 +151,7 @@ String EasyNex::readStr(String TextComponent){
}
}

if((millis() - _tmr1) > 1000UL){ // Waiting... But not forever......
if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever......
_readString = "ERROR"; // If the end of the command is NOT found in the time given,
// break the while
break;
Expand All @@ -175,14 +175,14 @@ uint32_t EasyNex::readNumber(String component){

_comp = component;
bool _endOfCommandFound = false;
char _tempChar;
uint8_t _tempChar;

_numberValue = 777777; // The function will return this number in case it fails to read the new number

_tmr1 = millis();
while(_serial->available()){ // Waiting for NO bytes on Serial,
// as other commands could be sent in that time.
if((millis() - _tmr1) > 1000UL){ // Waiting... But not forever...after the timeout
if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...after the timeout
_numberValue = 777777;
break; // Exit from the loop due to timeout. Return 777777
}else{
Expand All @@ -205,7 +205,7 @@ uint32_t EasyNex::readNumber(String component){
_tmr1 = millis();
while(_serial->available() < 8){ // Waiting for bytes to come to Serial,
// we are waiting for 8 bytes
if((millis() - _tmr1) > 400UL){ // Waiting... But not forever...after the timeout
if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...after the timeout
_numberValue = 777777;
break; // Exit the loop due to timeout. Return 777777
}
Expand All @@ -220,7 +220,7 @@ uint32_t EasyNex::readNumber(String component){
_start_char = _serial->read();
}

if((millis() - _tmr1) > 100UL){ // Waiting... But not forever......
if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever......
_numberValue = 777777; // If the 0x71 is not found within the given time, break the while()
// to avoid being stuck inside the while() loop
break;
Expand All @@ -240,19 +240,27 @@ uint32_t EasyNex::readNumber(String component){

while(_endOfCommandFound == false){ // As long as the three 0xFF bytes have NOT been found, run the commands inside the loop

_tempChar = _serial->read(); // Read the next byte of the Serial

if(_tempChar == 0xFF || _tempChar == 0xFFFFFFFF){ // If the read byte is the end command byte,
_endBytes++ ; // Add one to the _endBytes counter
if(_endBytes == 3){
_endOfCommandFound = true; // If the counter is equal to 3, we have the end command
}
}else{ // If the read byte is NOT the end command byte,
_tempChar = _serial->read(); // Read the next byte of the Serial

if (_tempChar == 0xFFFFFFFF)
{ // If the read byte is the end command byte,
_endOfCommandFound = true; // If the counter is equal to 3, we have the end command
break;
}
if (_tempChar == 0xFF)
{ // If the read byte is the end command byte,
_endBytes++; // Add one to the _endBytes counter
if (_endBytes == 3)
{
_endOfCommandFound = true; // If the counter is equal to 3, we have the end command
}
}
else{ // If the read byte is NOT the end command byte,
_numberValue = 777777;
break;
}

if((millis() - _tmr1) > 1000UL){ // Waiting... But not forever......
if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever......
_numberValue = 777777; // If the end of the command is NOT found in the time given,
// break the while
break;
Expand Down
4 changes: 3 additions & 1 deletion src/EasyNextionLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class EasyNex {
uint32_t readNumber(String);
String readStr(String);
int readByte();
void setReadTimeout(uint32_t timeout) { _readTimeout = timeout; }

//---------------------------------------
// public variables
Expand Down Expand Up @@ -118,7 +119,7 @@ class EasyNex {
HardwareSerial* _serial;
void readCommand(void);
void callTriggerFunction(void);

//----------------------------------------------
// for function writeNum() (write to numeric attribute)
//------------------------------------------------
Expand All @@ -136,6 +137,7 @@ class EasyNex {
String _comp;
uint8_t _numericBuffer[4];
uint32_t _numberValue;
uint32_t _readTimeout = 1000;

//---------------------------------------
// for General functions
Expand Down