Prusa MINI Firmware overview
printhex.h
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Contact information
19  * -------------------
20  *
21  * Circuits At Home, LTD
22  * Web : http://www.circuitsathome.com
23  * e-mail : support@circuitsathome.com
24  */
25 #pragma once
26 
27 #ifndef _usb_h_
28  #error "Never include printhex.h directly; include Usb.h instead"
29 #endif
30 
31 void E_Notifyc(char c, int lvl);
32 
33 template <class T>
34 void PrintHex(T val, int lvl) {
35  int num_nibbles = sizeof (T) * 2;
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  E_Notifyc(val & mask ? '1' : '0', lvl);
47 }
48 
49 template <class T>
50 void SerialPrintHex(T val) {
51  int num_nibbles = sizeof (T) * 2;
52  do {
53  char v = 48 + (((val >> (num_nibbles - 1) * 4)) & 0x0f);
54  if (v > 57) v += 7;
55  USB_HOST_SERIAL.print(v);
56  } while (--num_nibbles);
57 }
58 
59 template <class T>
60 void PrintHex2(Print *prn, T val) {
61  T mask = (((T)1) << (((sizeof (T) << 1) - 1) << 2));
62  while (mask > 1) {
63  if (val < mask) prn->print("0");
64  mask >>= 4;
65  }
66  prn->print((T)val, HEX);
67 }
68 
69 template <class T> void D_PrintHex(T val __attribute__((unused)), int lvl __attribute__((unused))) {
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 }
HEX
#define HEX
Definition: Print.h:30
PrintHex2
void PrintHex2(Print *prn, T val)
Definition: printhex.h:60
USB_HOST_SERIAL
#define USB_HOST_SERIAL
Definition: settings.h:73
Print::print
size_t print(const __FlashStringHelper *)
Definition: Print.cpp:46
SerialPrintHex
void SerialPrintHex(T val)
Definition: printhex.h:50
__attribute__
bool boolean __attribute__((deprecated))
Definition: wiring_constants.h:110
PrintBin
void PrintBin(T val, int lvl)
Definition: printhex.h:44
E_Notifyc
void E_Notifyc(char c, int lvl)
PrintHex
void PrintHex(T val, int lvl)
Definition: printhex.h:34
Print
Definition: Print.h:37
D_PrintBin
void D_PrintBin(T val, int lvl)
Definition: printhex.h:76
unused
unsigned unused
Definition: masstorage.h:51
D_PrintHex
void D_PrintHex(T val __attribute__((unused)), int lvl __attribute__((unused)))
Definition: printhex.h:69