-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSBprint.h
113 lines (100 loc) · 3.19 KB
/
USBprint.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* USBprint.h
*
* Author: Andrew D. Horchler, horchler @ gmail . com
* Created: 8-24-14, modified: 5-8-16
*/
#ifndef USBPRINT_H_
#define USBPRINT_H_
#include "HardwareSerial.h"
#include "Print.h"
#ifndef USBPRINT_BUFFER_SIZE
#define USBPRINT_BUFFER_SIZE (8*80)
#endif
/*
* Return 1 if USB serial is connected and configured, 0 otherwise.
* TODO: Check that Serial.begin() has been called and USB serial connection is active.
*/
inline int USBprintReady() __attribute__((always_inline));
inline int USBprintReady()
{
return 1;
}
/*
* Fast general print functions without floating-point support using vsniprintf to
* replace Serial.print(). These functions require about 6,320 additional bytes compared
* to Serial.print(). The number of bytes written is returned.
*/
int USBprint(const char *fmt, ...);
int USBprintln(const char *fmt, ...);
int USBsprint(char *str, size_t n, const char *fmt, ...);
/*
* Fast general print functions using vsnprintf to replace Serial.print(). These
* functions require a considerable amount of memory to support floating-point: about
* 14,684 additional bytes compared to Serial.printf(). The number of bytes written is
* returned.
*/
int USBprintf(const char *fmt, ...);
int USBprintfln(const char *fmt, ...);
int USBsprintf(char *str, size_t n, const char *fmt, ...);
/*
* Fast print functions using ee_vsprint() to replace Serial.print(). These functions
* use much less memory than USBprint() and only about 1,280 bytes more than
* Serial.print(). However, not all format specifications are supported. The number of
* bytes written is returned.
*/
int USBprintLite(const char *fmt, ...);
int USBsprintLite(char *str, const char *fmt, ...);
/*
* Fast print functions using ee_vsprintf() to replace Serial.print(). These functions
* use less memory than USBprintf() and about 9,840 bytes more than Serial.print().
* However, not all format specifications are supported. The number of bytes written is
* returned.
*/
int USBprintfLite(const char *fmt, ...);
int USBsprintfLite(char *str, const char *fmt, ...);
/*
* Fast string-only print function to replace Serial.print(). This very lightweight
* function only supports character string inputs. The number of bytes written is
* returned.
*/
inline int USBprintStr(const char *str)
{
int len;
len = strlen(str);
if (USBprintReady() && len > 0) {
Serial.write((const uint8_t*)str, len);
return len;
} else {
return 0;
}
}
/*
* Print single raw uint8_t byte. This low-level function only supports character input
* that has already been converted to raw unsigned 8-bit data. The number of bytes
* successfully written is returned (0 or 1).
*/
inline int USBprintRaw(const uint8_t dat)
{
if (USBprintReady()) {
Serial.write(&dat, 1);
return 1;
} else {
return 0;
}
}
/*
* Print array of raw uint8_t bytes of given length. This low-level function only
* supports character string inputs that have already been converted to raw unsigned
* 8-bit data. The number of bytes successfully written is returned.
*/
inline int USBprintRaw(const uint8_t *dat, const int datlen)
{
if (USBprintReady() && datlen > 0) {
Serial.write(dat, datlen);
return datlen;
} else {
return 0;
}
}
#endif /* USBPRINT_H_ */