-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
263 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef AsyncStream_h | ||
#define AsyncStream_h | ||
|
||
template < uint16_t SIZE > | ||
class AsyncStream { | ||
public: | ||
AsyncStream(Stream* port, char ter = ';', uint16_t tout = 50) { | ||
_port = port; | ||
_tout = tout; | ||
_ter = ter; | ||
} | ||
bool available() { | ||
if (_port -> available()) { | ||
if (!_parseF) { | ||
_parseF = true; | ||
_count = 0; | ||
_tmr = millis(); | ||
} | ||
char ch = _port -> read(); | ||
if (ch == _ter) { | ||
buf[_count] = NULL; | ||
_parseF = false; | ||
return true; | ||
} else if (_count < SIZE - 1) buf[_count++] = ch; | ||
_tmr = millis(); | ||
} | ||
if (_parseF && millis() - _tmr >= _tout) { | ||
_parseF = false; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
char buf[SIZE]; | ||
|
||
private: | ||
Stream* _port; | ||
char _ter; | ||
uint16_t _tout, _count = 0; | ||
uint32_t _tmr = 0; | ||
bool _parseF = false; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// асинхронная читалка Serial | ||
// а также любых из класса Stream | ||
|
||
#include "AsyncStream.h" | ||
|
||
// указываем обработчик, терминатор (символ конца приёма) и таймаут в мс | ||
// в <> указан размер буфера! | ||
AsyncStream<40> serial(&Serial, '\n'); | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
} | ||
|
||
// строка для теста (отправь в сериал) | ||
// 1234,3.14,hello,4567,lolkek,qwerty | ||
void loop() { | ||
if (serial.available()) { // если данные получены | ||
Serial.println(serial.buf); // выводим их (как char*) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.