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 #define USB_HOST_SERIAL Serial
27 
28 void E_Notifyc(char c, int lvl);
29 
30 template <class T>
31 void PrintHex(T val, int lvl) {
32 //#ifdef DEBUG_USB_HOST
33  int num_nibbles = sizeof(T) * 2;
34 
35  do {
36  char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
37  if(v > 57) v += 7;
38  E_Notifyc(v, lvl);
39  } while(--num_nibbles);
40 //#endif
41 }
42 
43 template <class T>
44 void PrintBin(T val, int lvl) {
45 //#ifdef DEBUG_USB_HOST
46  for(T mask = (((T) 1) << ((sizeof(T) << 3) - 1)); mask; mask >>= 1)
47  if(val & mask)
48  E_Notifyc('1', lvl);
49  else
50  E_Notifyc('0', lvl);
51 //#endif
52 }
53 
54 template <class T>
55 void SerialPrintHex(T val) {
56 //#ifdef DEBUG_USB_HOST
57  int num_nibbles = sizeof(T) * 2;
58 
59  do {
60  char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
61  if(v > 57) v += 7;
62  USB_HOST_SERIAL.print(v);
63  } while(--num_nibbles);
64 //#endif
65 }
66 
67 template <class T>
68 void PrintHex2(Print *prn, T val) {
69 //#ifdef DEBUG_USB_HOST
70  T mask = (((T) 1) << (((sizeof(T) << 1) - 1) << 2));
71 
72  while(mask > 1) {
73  if(val < mask)
74  prn->print("0");
75 
76  mask >>= 4;
77  }
78  prn->print((T) val, HEX);
79 //#endif
80 }
81 
82 #endif // __PRINTHEX_H__