forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSArduino.h
81 lines (73 loc) · 1.66 KB
/
BSArduino.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef BS_ARDUINO_H
#define BS_ARDUINO_H
#include <Arduino.h>
namespace bs
{
class ArduinoIOHelper
{
public:
ArduinoIOHelper(Stream& stream) : m_stream(stream)
{
}
size_t printf(const char *format, ...)
{
va_list arg;
va_start(arg, format);
char temp[128];
char* buffer = temp;
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
va_end(arg);
if (len > sizeof(temp) - 1)
{
buffer = new char[len + 1];
if (!buffer)
{
return 0;
}
va_start(arg, format);
ets_vsnprintf(buffer, len + 1, format, arg);
va_end(arg);
}
len = m_stream.write((const uint8_t*) buffer, len);
if (buffer != temp)
{
delete[] buffer;
}
return len;
}
size_t read_line(char* dest, size_t dest_size)
{
size_t len = 0;
// Can't use Stream::readBytesUntil here because it can't tell the
// difference between timing out and receiving the terminator.
while (len < dest_size - 1)
{
int c = m_stream.read();
if (c < 0)
{
delay(1);
continue;
}
if (c == '\r')
{
continue;
}
if (c == '\n')
{
dest[len] = 0;
break;
}
dest[len++] = c;
}
return len;
}
protected:
Stream& m_stream;
};
typedef ArduinoIOHelper IOHelper;
inline void fatal()
{
ESP.restart();
}
} // namespace bs
#endif //BS_ARDUINO_H