USB Host Shield 2.0
Loading...
Searching...
No Matches
printhex.h
Go to the documentation of this file.
1/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License as published by
5the Free Software Foundation; either version 2 of the License, or
6(at your option) any later version.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program; if not, write to the Free Software
15Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17Contact information
18-------------------
19
20Circuits At Home, LTD
21Web : http://www.circuitsathome.com
22e-mail : support@circuitsathome.com
23 */
24
25#if !defined(_usb_h_) || defined(__PRINTHEX_H__)
26#error "Never include printhex.h directly; include Usb.h instead"
27#else
28#define __PRINTHEX_H__
29
30void E_Notifyc(char c, int lvl);
31
32template <class T>
33void PrintHex(T val, int lvl) {
34 int num_nibbles = sizeof (T) * 2;
35
36 do {
37 char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
38 if(v > 57) v += 7;
39 E_Notifyc(v, lvl);
40 } while(--num_nibbles);
41}
42
43template <class T>
44void PrintBin(T val, int lvl) {
45 for(T mask = (((T)1) << ((sizeof (T) << 3) - 1)); mask; mask >>= 1)
46 if(val & mask)
47 E_Notifyc('1', lvl);
48 else
49 E_Notifyc('0', lvl);
50}
51
52template <class T>
53void SerialPrintHex(T val) {
54 int num_nibbles = sizeof (T) * 2;
55
56 do {
57 char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
58 if(v > 57) v += 7;
59 USB_HOST_SERIAL.print(v);
60 } while(--num_nibbles);
61}
62
63template <class T>
64void PrintHex2(Print *prn, T val) {
65 T mask = (((T)1) << (((sizeof (T) << 1) - 1) << 2));
66
67 while(mask > 1) {
68 if(val < mask)
69 prn->print("0");
70
71 mask >>= 4;
72 }
73 prn->print((T)val, HEX);
74}
75
76template <class T> void D_PrintHex(T val __attribute__((unused)), int lvl __attribute__((unused))) {
77#ifdef DEBUG_USB_HOST
78 PrintHex<T > (val, lvl);
79#endif
80}
81
82template <class T>
83void D_PrintBin(T val, int lvl) {
84#ifdef DEBUG_USB_HOST
85 PrintBin<T > (val, lvl);
86#endif
87}
88
89
90
91#endif // __PRINTHEX_H__
void PrintHex(T val, int lvl)
Definition printhex.h:33
void D_PrintBin(T val, int lvl)
Definition printhex.h:83
void SerialPrintHex(T val)
Definition printhex.h:53
void PrintBin(T val, int lvl)
Definition printhex.h:44
void D_PrintHex(T val, int lvl)
Definition printhex.h:76
void E_Notifyc(char c, int lvl)
Definition message.cpp:31
void PrintHex2(Print *prn, T val)
Definition printhex.h:64
#define USB_HOST_SERIAL
Definition settings.h:49