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