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 software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16  */
17 
18 #if !defined(_usb_h_) || defined(__PRINTHEX_H__)
19 #error "Never include printhex.h directly; include Usb.h instead"
20 #else
21 #define __PRINTHEX_H__
22 
23 void E_Notifyc(char c, int lvl);
24 
25 template <class T>
26 void PrintHex(T val, int lvl) {
27  int num_nibbles = sizeof (T) * 2;
28 
29  do {
30  char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
31  if(v > 57) v += 7;
32  E_Notifyc(v, lvl);
33  } while(--num_nibbles);
34 }
35 
36 template <class T>
37 void PrintBin(T val, int lvl) {
38  for(T mask = (((T)1) << ((sizeof (T) << 3) - 1)); mask; mask >>= 1)
39  if(val & mask)
40  E_Notifyc('1', lvl);
41  else
42  E_Notifyc('0', lvl);
43 }
44 
45 template <class T>
46 void SerialPrintHex(T val) {
47  int num_nibbles = sizeof (T) * 2;
48 
49  do {
50  char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
51  if(v > 57) v += 7;
52  USB_HOST_SERIAL.print(v);
53  } while(--num_nibbles);
54 }
55 
56 template <class T>
57 void PrintHex2(Print *prn, T val) {
58  T mask = (((T)1) << (((sizeof (T) << 1) - 1) << 2));
59 
60  while(mask > 1) {
61  if(val < mask)
62  prn->print("0");
63 
64  mask >>= 4;
65  }
66  prn->print((T)val, HEX);
67 }
68 
69 template <class T> void D_PrintHex(T val, int lvl) {
70 #ifdef DEBUG_USB_HOST
71  PrintHex<T > (val, lvl);
72 #endif
73 }
74 
75 template <class T>
76 void D_PrintBin(T val, int lvl) {
77 #ifdef DEBUG_USB_HOST
78  PrintBin<T > (val, lvl);
79 #endif
80 }
81 
82 
83 
84 #endif // __PRINTHEX_H__
void SerialPrintHex(T val)
Definition: printhex.h:46
void PrintBin(T val, int lvl)
Definition: printhex.h:37
#define USB_HOST_SERIAL
Definition: settings.h:34
void D_PrintBin(T val, int lvl)
Definition: printhex.h:76
void E_Notifyc(char c, int lvl)
Definition: message.cpp:24
void PrintHex(T val, int lvl)
Definition: printhex.h:26
void PrintHex2(Print *prn, T val)
Definition: printhex.h:57
void D_PrintHex(T val, int lvl)
Definition: printhex.h:69