Skip to content

Commit 0da629f

Browse files
Add a number of printHex helper functions
These allow easily printing integers and XBeeAddress64 values with leading zeroes (which Arduino's print doesn't have any easy way to do). Also, a version is available that can print a buffer of bytes with configurable separators. These functions aren't used yet, but will be used in examples that will be added next.
1 parent e0af45e commit 0da629f

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

Printers.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "Printers.h"
2+
3+
void printHex(Print& p, const uint8_t* buf, size_t len, const __FlashStringHelper* byte_sep, const __FlashStringHelper* group_sep, size_t group_by) {
4+
size_t cur_group = 0;
5+
while (len--) {
6+
// Print the group separator whenever starting a new
7+
// group
8+
if (group_by && group_sep && cur_group == group_by) {
9+
p.print(group_sep);
10+
cur_group = 0;
11+
}
12+
13+
// Print the byte separator, except when at the start of
14+
// a new group (this also excludes the first byte)
15+
if (cur_group != 0 && byte_sep)
16+
p.print(byte_sep);
17+
18+
printHex(p, *buf);
19+
20+
buf++;
21+
cur_group++;
22+
}
23+
}

Printers.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright (c) 2009 Andrew Rapp. All rights reserved.
3+
*
4+
* This file is part of XBee-Arduino.
5+
*
6+
* XBee-Arduino is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* XBee-Arduino is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef XBee_Printers_h
21+
#define XBee_Printers_h
22+
23+
#if defined(ARDUINO) && ARDUINO >= 100
24+
#include "Arduino.h"
25+
#else
26+
#include "WProgram.h"
27+
#endif
28+
29+
#include "XBee.h"
30+
31+
// Need to define global variables to allow PROGMEM pointers as default
32+
// arguments below. Since these variables are const, there won't be any
33+
// linker conflicts from defining these in a header file.
34+
const char default_byte_sep_arr[] PROGMEM = " ";
35+
const char default_group_sep_arr[] PROGMEM = "\r\n";
36+
const __FlashStringHelper * const default_byte_sep = (const __FlashStringHelper*)default_byte_sep_arr;
37+
const __FlashStringHelper * const default_group_sep = (const __FlashStringHelper*)default_group_sep_arr;
38+
39+
/**
40+
* Print a buffer byte-by-byte. Each byte is separated by byte_sep and
41+
* every group_by bytes are separated by group_sep instead.
42+
*
43+
* For example, to print 8 bytes per line, each byte separated by a
44+
* newline, to Serial:
45+
*
46+
* printHex(Serial, buf, len, F(" "), F("\r\n"), 8);
47+
*
48+
* Values shown are also the defaults.
49+
*
50+
* Pass NULL as group_by or byte_sep to not have that separator.
51+
*/
52+
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);
53+
54+
/**
55+
* Print a single byte, in hex, using a leading zero if needed.
56+
*/
57+
inline void printHex(Print& p, uint8_t v) {
58+
// Add leading zero if needed
59+
if (v < 0x10)
60+
p.write('0');
61+
p.print(v, HEX);
62+
}
63+
64+
/**
65+
* Print a 16 bit integer, in hex, using leading zeroes if needed.
66+
*/
67+
inline void printHex(Print& p, uint16_t v) {
68+
printHex(p, (uint8_t)(v >> 8));
69+
printHex(p, (uint8_t)v);
70+
}
71+
72+
/**
73+
* Print a 32 bit integer, in hex, using leading zeroes if needed.
74+
*/
75+
inline void printHex(Print& p, uint32_t v) {
76+
printHex(p, (uint16_t)(v >> 16));
77+
printHex(p, (uint16_t)v);
78+
}
79+
80+
/**
81+
* Print a 64-bit address, in hex, using leading zeroes if needed.
82+
*/
83+
inline void printHex(Print& p, XBeeAddress64 v) {
84+
printHex(p, v.getMsb());
85+
printHex(p, v.getLsb());
86+
}
87+
88+
#endif // XBee_Printers_h

0 commit comments

Comments
 (0)