Stream.cpp 803 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "Stream.h"
  2. #include "trace.h"
  3. #include <iostream>
  4. #include <Arduino.h>
  5. Stream::Stream() {
  6. this->expectBuffer = new Buffer();
  7. this->_error = false;
  8. this->_written = 0;
  9. }
  10. size_t Stream::write(uint8_t b) {
  11. this->_written++;
  12. TRACE(std::hex << (unsigned int)b);
  13. if (this->expectBuffer->available()) {
  14. uint8_t expected = this->expectBuffer->next();
  15. if (expected != b) {
  16. this->_error = true;
  17. TRACE("!=" << (unsigned int)expected);
  18. }
  19. } else {
  20. this->_error = true;
  21. }
  22. TRACE("\n"<< std::dec);
  23. return 1;
  24. }
  25. bool Stream::error() {
  26. return this->_error;
  27. }
  28. void Stream::expect(uint8_t *buf, size_t size) {
  29. this->expectBuffer->add(buf,size);
  30. }
  31. uint16_t Stream::length() {
  32. return this->_written;
  33. }