-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadWrite.h
48 lines (42 loc) · 895 Bytes
/
ReadWrite.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include <avr/pgmspace.h>
//////////////////////////////////////////////////
// read a sequence of chars from PROGMEM *or* Serial
struct Reader
{
virtual char Read() = 0;
virtual void Next() { _idx++; };
int _idx = 0;
};
struct PROGMEMReader: Reader
{
char Read() override {return pgm_read_byte_near(_basePtr + _idx);}
const char* _basePtr = NULL;
};
struct SerialReader: Reader
{
char Read() override
{
if (_char == -1)
Next();
return _char;
}
void Next() override
{
do
_char = Serial.read();
while (_char == -1);
}
int _char = -1;
};
//////////////////////////////////////
// write a sequence of chars to Serial
struct Writer
{
virtual void Write(char ch) = 0;
int _idx = 0;
};
struct SerialWriter: Writer
{
void Write(char ch) override { Serial.write(ch); }
};