|
| 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