|
-
- #ifndef ArduinoStream_h
- #define ArduinoStream_h
-
- #if defined(ARDUINO) || defined(DOXYGEN)
- #include <Arduino.h>
- #include "bufstream.h"
-
-
- class ArduinoInStream : public ibufstream {
- public:
-
-
- ArduinoInStream(Stream &hws, char* buf, size_t size) {
- m_hw = &hws;
- m_line = buf;
- m_size = size;
- }
-
- void readline() {
- size_t i = 0;
- uint32_t t;
- m_line[0] = '\0';
- while (!m_hw->available()) {}
-
- while (1) {
- t = millis();
- while (!m_hw->available()) {
- if ((millis() - t) > 10) goto done;
- }
- if (i >= (m_size - 1)) {
- setstate(failbit);
- return;
- }
- m_line[i++] = m_hw->read();
- m_line[i] = '\0';
- }
- done:
- init(m_line);
- }
-
- protected:
-
-
- bool seekoff(off_type off, seekdir way) {return false;}
-
-
- bool seekpos(pos_type pos) {return false;}
-
- private:
- char *m_line;
- size_t m_size;
- Stream* m_hw;
- };
-
-
- class ArduinoOutStream : public ostream {
- public:
-
-
- explicit ArduinoOutStream(Print& pr) : m_pr(&pr) {}
-
- protected:
-
-
-
- void putch(char c) {
- if (c == '\n') m_pr->write('\r');
- m_pr->write(c);
- }
- void putstr(const char* str) {m_pr->write(str);}
- bool seekoff(off_type off, seekdir way) {return false;}
- bool seekpos(pos_type pos) {return false;}
- bool sync() {return true;}
- pos_type tellpos() {return 0;}
-
- private:
- ArduinoOutStream() {}
- Print* m_pr;
- };
- #endif
- #endif
|