USB Host Shield 2.0
printhex.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 
17 Contact information
18 -------------------
19 
20 Circuits At Home, LTD
21 Web : http://www.circuitsathome.com
22 e-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 
30 void E_Notifyc(char c, int lvl);
31 
32 template <class T>
33 void 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 
43 template <class T>
44 void 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 
52 template <class T>
53 void 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 
63 template <class T>
64 void 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 
76 template <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 
82 template <class T>
83 void 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__
PrintHex2
void PrintHex2(Print *prn, T val)
Definition: printhex.h:64
USB_HOST_SERIAL
#define USB_HOST_SERIAL
Definition: settings.h:49
SerialPrintHex
void SerialPrintHex(T val)
Definition: printhex.h:53
D_PrintHex
void D_PrintHex(T val, int lvl)
Definition: printhex.h:76
PrintBin
void PrintBin(T val, int lvl)
Definition: printhex.h:44
E_Notifyc
void E_Notifyc(char c, int lvl)
Definition: message.cpp:31
PrintHex
void PrintHex(T val, int lvl)
Definition: printhex.h:33
T
@ T
Definition: controllerEnums.h:111
D_PrintBin
void D_PrintBin(T val, int lvl)
Definition: printhex.h:83