Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.8 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. StreamInjector
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #pragma once
  16. #include <Stream.h>
  17. class StreamInjector : public Stream {
  18. public:
  19. typedef std::function<void(uint8_t ch)> writeCallback;
  20. StreamInjector(size_t buflen = 128) : _buffer_size(buflen) {
  21. _buffer = new char[buflen];
  22. }
  23. ~StreamInjector() {
  24. delete[] _buffer;
  25. }
  26. // ---------------------------------------------------------------------
  27. virtual uint8_t inject(char ch) {
  28. _buffer[_buffer_write] = ch;
  29. _buffer_write = (_buffer_write + 1) % _buffer_size;
  30. return 1;
  31. }
  32. virtual uint8_t inject(char *data, size_t len) {
  33. for (uint8_t i=0; i<len; i++) {
  34. inject(data[i]);
  35. }
  36. return len;
  37. }
  38. virtual void callback(writeCallback c) {
  39. _callback = c;
  40. }
  41. // ---------------------------------------------------------------------
  42. virtual size_t write(uint8_t ch) {
  43. if (_callback) _callback(ch);
  44. return 1;
  45. }
  46. virtual int read() {
  47. int ch = -1;
  48. if (_buffer_read != _buffer_write) {
  49. ch = _buffer[_buffer_read];
  50. _buffer_read = (_buffer_read + 1) % _buffer_size;
  51. }
  52. return ch;
  53. }
  54. virtual int available() {
  55. unsigned int bytes = 0;
  56. if (_buffer_read > _buffer_write) {
  57. bytes += (_buffer_write - _buffer_read + _buffer_size);
  58. } else if (_buffer_read < _buffer_write) {
  59. bytes += (_buffer_write - _buffer_read);
  60. }
  61. return bytes;
  62. }
  63. virtual int peek() {
  64. int ch = -1;
  65. if (_buffer_read != _buffer_write) {
  66. ch = _buffer[_buffer_read];
  67. }
  68. return ch;
  69. }
  70. virtual void flush() {
  71. _buffer_read = _buffer_write;
  72. }
  73. private:
  74. char * _buffer;
  75. unsigned char _buffer_size;
  76. unsigned char _buffer_write = 0;
  77. unsigned char _buffer_read = 0;
  78. writeCallback _callback = NULL;
  79. };