-
Notifications
You must be signed in to change notification settings - Fork 163
/
Printers.h
156 lines (139 loc) · 7.15 KB
/
Printers.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Copyright (c) 2009 Andrew Rapp. All rights reserved.
*
* This file is part of XBee-Arduino.
*
* XBee-Arduino is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* XBee-Arduino is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XBee_Printers_h
#define XBee_Printers_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "XBee.h"
// Need to define global variables to allow PROGMEM pointers as default
// arguments below. Since these variables are const, there won't be any
// linker conflicts from defining these in a header file.
const char default_byte_sep_arr[] PROGMEM = " ";
const char default_group_sep_arr[] PROGMEM = "\r\n";
const __FlashStringHelper * const default_byte_sep = (const __FlashStringHelper*)default_byte_sep_arr;
const __FlashStringHelper * const default_group_sep = (const __FlashStringHelper*)default_group_sep_arr;
/**
* Print a buffer byte-by-byte. Each byte is separated by byte_sep and
* every group_by bytes are separated by group_sep instead.
*
* For example, to print 8 bytes per line, each byte separated by a
* newline, to Serial:
*
* printHex(Serial, buf, len, F(" "), F("\r\n"), 8);
*
* Values shown are also the defaults.
*
* Pass NULL as group_by or byte_sep to not have that separator.
*/
void printHex(Print& p, const uint8_t* buf, size_t len, const __FlashStringHelper* byte_sep = default_byte_sep, const __FlashStringHelper* group_sep = default_group_sep, size_t group_by = 8);
/**
* Print a single byte, in hex, using a leading zero if needed.
*/
inline void printHex(Print& p, uint8_t v) {
// Add leading zero if needed
if (v < 0x10)
p.write('0');
p.print(v, HEX);
}
/**
* Print a 16 bit integer, in hex, using leading zeroes if needed.
*/
inline void printHex(Print& p, uint16_t v) {
printHex(p, (uint8_t)(v >> 8));
printHex(p, (uint8_t)v);
}
/**
* Print a 32 bit integer, in hex, using leading zeroes if needed.
*/
inline void printHex(Print& p, uint32_t v) {
printHex(p, (uint16_t)(v >> 16));
printHex(p, (uint16_t)v);
}
/**
* Print a 64-bit address, in hex, using leading zeroes if needed.
*/
inline void printHex(Print& p, XBeeAddress64 v) {
printHex(p, v.getMsb());
printHex(p, v.getLsb());
}
// The following functions are intended to be used as callbacks, to
// print various information about received responses. All of the
// require a Print* to be passed as the data parameter. For example, to
// print to Serial any TxStatusResponses that contain errors, do:
//
// xbee.onTxStatusResponse(printErrorCb, (uintptr_t)(Print*)&Serial);
//
// Most of these callbacks can either be used as a response-specific
// callback, to only work that specific API response type, or as a
// generic callback (onResponse or onOtherResponse), in which case the
// relevant version of the callback will be called automatically.
// printErrorCb prints any error messages in status responses.
void printErrorCb(uint8_t code, uintptr_t data);
void printErrorCb(ZBTxStatusResponse& r, uintptr_t data);
void printErrorCb(TxStatusResponse& r, uintptr_t data);
void printErrorCb(AtCommandResponse& r, uintptr_t data);
void printErrorCb(RemoteAtCommandResponse& r, uintptr_t data);
void printErrorCb(XBeeResponse& r, uintptr_t data);
// printRawResponseCb prints the raw bytes of a response.
void printRawResponseCb(XBeeResponse& response, uintptr_t data);
// printResponseCb prints a human-readable version of a response, showing
// the values of all fields individually.
void printResponseCb(ZBTxStatusResponse& status, uintptr_t data);
void printResponseCb(ZBRxResponse& rx, uintptr_t data);
void printResponseCb(ZBExplicitRxResponse& rx, uintptr_t data);
void printResponseCb(ZBRxIoSampleResponse& rx, uintptr_t data);
void printResponseCb(TxStatusResponse& status, uintptr_t data);
void printResponseCb(Rx16Response& rx, uintptr_t data);
void printResponseCb(Rx64Response& rx, uintptr_t data);
void printResponseCb(Rx16IoSampleResponse& rx, uintptr_t data);
void printResponseCb(Rx64IoSampleResponse& rx, uintptr_t data);
void printResponseCb(ModemStatusResponse& status, uintptr_t data);
void printResponseCb(AtCommandResponse& at, uintptr_t data);
void printResponseCb(RemoteAtCommandResponse& at, uintptr_t data);
void printResponseCb(XBeeResponse& r, uintptr_t data);
// The following functions are non-callback version of the above,
// intended to be called with Print instance (such as Serial) directly,
// saving the casts.
// Print any error in these status response (prints nothing when the
// status is ok).
inline void printError(ZBTxStatusResponse& r, Print& print) { printErrorCb(r, (uintptr_t)(Print*)&print); }
inline void printError(TxStatusResponse& r, Print& print) { printErrorCb(r, (uintptr_t)(Print*)&print); }
inline void printError(AtCommandResponse& r, Print& print) { printErrorCb(r, (uintptr_t)(Print*)&print); }
inline void printError(RemoteAtCommandResponse& r, Print& print) { printErrorCb(r, (uintptr_t)(Print*)&print); }
inline void printError(XBeeResponse& r, Print& print) { printErrorCb(r, (uintptr_t)(Print*)&print); }
// Print the raw bytes of a response
inline void printRawResponse(XBeeResponse& r, Print& print) { printRawResponseCb(r, (uintptr_t)(Print*)&print); }
// Print a human-readable version of a response
inline void printResponse(ZBTxStatusResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(ZBRxResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(ZBExplicitRxResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(ZBRxIoSampleResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(TxStatusResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(Rx16Response& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(Rx64Response& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(Rx16IoSampleResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(Rx64IoSampleResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(ModemStatusResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(AtCommandResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(RemoteAtCommandResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
inline void printResponse(XBeeResponse& r, Print& print) { printResponseCb(r, (uintptr_t)(Print*)&print); }
#endif // XBee_Printers_h