USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 #if !defined(__PRINTHEX_H__)
18 #define __PRINTHEX_H__
19 
20 #if defined(ARDUINO) && ARDUINO >=100
21 #include "Arduino.h"
22 #else
23 #include <WProgram.h>
24 #endif
25 
26 #ifndef USB_HOST_SERIAL
27 #define USB_HOST_SERIAL Serial
28 #endif
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, int lvl) {
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__