Skip to content

Commit

Permalink
fix deviceType()
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Nov 10, 2023
1 parent e3347ed commit 7a818c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- add example, Kudos to chlordk (#16)
- add **screenWidth()**
- add **screenHeight()**
- refactor **deviceType()** size.
- update readme.md
- update keywords.txt

Expand Down
30 changes: 17 additions & 13 deletions ansi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,23 @@ int ANSI::deviceType(uint32_t timeout)
int type = -1; // -1 = unknown
print("\033[0c");

char buffer[4];
int len = 0;
char c;
uint32_t start = millis();
int read_len = 0;
char buffer[8];
while ((read_len != 3) && ((millis() - start) < timeout))
while ((len < 3) && ((millis() - start) < timeout))
{
delay(1);
read_len = Serial.readBytes(buffer, 3);
if ((buffer[0] == '1') && (buffer[1] == ';'))
if (_stream->available())
{
type = buffer[2] - '0';
c = _stream->read();
buffer[len++] = c;
buffer[len] = 0;
}
// Serial.write(buffer, 3);
// Serial.println();
}

if ((buffer[0] == '1') && (buffer[1] == ';'))
{
type = buffer[2] - '0';
}
return type;
}
Expand All @@ -239,10 +243,10 @@ bool ANSI::readCursorPosition(uint16_t &w, uint16_t &h, uint32_t timeout)
}
}
// do we have enough chars
if (len < 7) { // 8 = \e[24;80R
return false;
}
// last char must be R to have all of them.
// typical (8) = \e[24;80R
// minimal (6) = \e[1;1R
if (len < 6) return false;
// last char must be R to have them all.
if (c != 'R') return false;

// parse the buffer
Expand Down
16 changes: 11 additions & 5 deletions ansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ class ANSI : public Stream


// EXPERIMENTAL SECTION
// use at own risk
// META
// deviceType is discussed
// use at own risk

// TERMINAL TYPE
// - https://github.com/RobTillaart/ANSI/issues/9
// timeout in milliseconds.
// note this function blocks for timeout or less.
// -1 = unknown;
// 1 = VT52, 2 = VT100, 3 = VT220,
enum {
UNKNOWN = -1,
// known types
VT52 = 1,
VT100 = 2,
VT220 = 3,
// add others if needed.
};
int deviceType(uint32_t timeout = 100);


Expand Down

0 comments on commit 7a818c1

Please sign in to comment.