|
| 1 | +// NAME: Debug.h |
| 2 | +// |
| 3 | +// DESC: Helper methods for debugging |
| 4 | +// |
| 5 | +// Copyright (c) 2018 by Andreas Trappmann. All rights reserved. |
| 6 | +// |
| 7 | +// This file is part of the PN5180 library for the Arduino environment. |
| 8 | +// |
| 9 | +// This library is free software; you can redistribute it and/or |
| 10 | +// modify it under the terms of the GNU Lesser General Public |
| 11 | +// License as published by the Free Software Foundation; either |
| 12 | +// version 2.1 of the License, or (at your option) any later version. |
| 13 | +// |
| 14 | +// This library is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | +// Lesser General Public License for more details. |
| 18 | +// |
| 19 | +#ifndef DEBUG_H |
| 20 | +#define DEBUG_H |
| 21 | + |
| 22 | +#ifdef DEBUG |
| 23 | +extern uint8_t _pn5180_debugIndent; |
| 24 | +extern uint8_t _pn5180_debugIndentN; |
| 25 | +extern bool _pn5180_debugNL; |
| 26 | +extern uint8_t _pn5180_debugSilent; |
| 27 | + |
| 28 | +// These macros are helper macros to make the see the debug macros like function calls so they do not alter any current code block structures when the macro contains if/else/break, etc. |
| 29 | +#define __DEBUG_BEGIN__ do{if(DEBUG){ |
| 30 | +#define __DEBUG_END__ }}while(0) |
| 31 | + |
| 32 | +// DEBUG print with indention macros: |
| 33 | +// |
| 34 | +// USAGE: |
| 35 | +// 1. At the top of any function use PN5180DEBUG_PRINTLN(...) or PN5180DEBUG_PRINTF(...)or PN5180DEBUG_PRINT(...) to output that you've entered the function. |
| 36 | +// 2. If you didn't use PN5180DEBUG_PRINTLN, then follow it with PN5180DEBUG_PRINTLN() without parameters. |
| 37 | +// 3. Use PN5180DEBUG_ENTER, which will increment the indent level. |
| 38 | +// 4. Before every return call in the method, use PN5180DEBUG_EXIT to decrement the indent level. |
| 39 | +// 5. Use PN5180DEBUG_OFF and PN5180DEBUG_ON to block debug output for a call tree. |
| 40 | +// You can see this used in the PN5180::setRF_on() which blcks the thousands repeated debug messages to readRegister() and tranceiveCommand() functions while allowing those functions to continue to issue debug messages when called from other functions. |
| 41 | +// |
| 42 | +// NOTES: |
| 43 | +// 1. Always make sure you end any secuence of _PRINT* macros with a _PRINTNL. See example below for putputting the register value. |
| 44 | +// 2. If you directly use PN5180DEBUG_INDENT, it will break the indention macros. |
| 45 | +// 3. NOTE: The older PN5180DEBUG macro is kept for backward compatibility. |
| 46 | +// 4. Code that uses Serial.print() function calls outside these macros will not have a prepended "| " at the beginning of each line, which helps you identify the code being debugged from other code. |
| 47 | +// |
| 48 | +// EXAMPLE: (from PN5180.cpp) |
| 49 | +// bool PN5180::readRegister(uint8_t reg, uint32_t *value) { |
| 50 | +// PN5180DEBUG_PRINTF(F("PN5180::readRegister(reg=0x%s, *value=%d"), formatHex(reg), (uint8_t*)value); |
| 51 | +// PN5180DEBUG_PRINTLN(); |
| 52 | +// PN5180DEBUG_ENTER; |
| 53 | +// |
| 54 | +// uint8_t cmd[] = { PN5180_READ_REGISTER, reg }; |
| 55 | +// |
| 56 | +// transceiveCommand(cmd, sizeof(cmd), (uint8_t*)value, 4); |
| 57 | +// |
| 58 | +// PN5180DEBUG(F("Register value=0x")); |
| 59 | +// PN5180DEBUG(formatHex(*value)); |
| 60 | +// PN5180DEBUG_PRINTLN(); |
| 61 | +// |
| 62 | +// PN5180DEBUG_EXIT; |
| 63 | +// return true; |
| 64 | +// } |
| 65 | +// |
| 66 | +// SAMPLE OUTPUT using PN5180 library |
| 67 | +// ================================== |
| 68 | +// Uploaded: Oct 28 2024 04:22:04 |
| 69 | +// PN5180 ISO14443 Demo Sketch |
| 70 | +// | PN5180::begin(sck=19, miso=20, mosi=18, ss=-1) |
| 71 | +// | Custom SPI pinout: SS=21, MOSI=18, MISO=20, SCK=19 |
| 72 | +// ---------------------------------- |
| 73 | +// PN5180 Hard-Reset... |
| 74 | +// | PN5180::reset() |
| 75 | +// | PN5180::getIRQStatus() |
| 76 | +// | Read IRQ-Status register... |
| 77 | +// | PN5180::readRegister(reg=0x02, *value=1082650700 |
| 78 | +// | PN5180::transceiveCommand(*sendBuffer, sendBufferLen=2, *recvBuffer, recvBufferLen=4) |
| 79 | +// | Sending SPI frame: '04 02' |
| 80 | +// | Receiving SPI frame... |
| 81 | +// | Received: '04 00 00 00' |
| 82 | +// | Register value=0x00000004 |
| 83 | +// | IRQ-Status=0x00000004 |
| 84 | +// ---------------------------------- |
| 85 | + |
| 86 | +#define PN5180DEBUG_OFF __DEBUG_BEGIN__ ++_pn5180_debugSilent; __DEBUG_END__ |
| 87 | +#define PN5180DEBUG_ON __DEBUG_BEGIN__ _pn5180_debugSilent-=((_pn5180_debugSilent>0)?1:0); __DEBUG_END__ |
| 88 | +#define PN5180DEBUG_ENTER __DEBUG_BEGIN__ ++_pn5180_debugIndent; __DEBUG_END__ |
| 89 | +#define PN5180DEBUG_EXIT __DEBUG_BEGIN__ _pn5180_debugIndent-=((_pn5180_debugIndent>0)?1:0); __DEBUG_END__ |
| 90 | +#define PN5180DEBUG_INDENT __DEBUG_BEGIN__ Serial.print("| "); for (int _pn5180_debugIndentN=0; _pn5180_debugIndentN<_pn5180_debugIndent; _pn5180_debugIndentN++) Serial.print(" "); _pn5180_debugNL=false; __DEBUG_END__ |
| 91 | +#define PN5180DEBUG_PRINTLN(...) __DEBUG_BEGIN__ if (!_pn5180_debugSilent) { if (_pn5180_debugNL) PN5180DEBUG_INDENT; Serial.println(__VA_ARGS__); _pn5180_debugNL=true; }; __DEBUG_END__ |
| 92 | +#define PN5180DEBUG_PRINTF(...) __DEBUG_BEGIN__ if (!_pn5180_debugSilent) { if (_pn5180_debugNL) PN5180DEBUG_INDENT; Serial.printf(__VA_ARGS__); }; __DEBUG_END__ |
| 93 | +#define PN5180DEBUG_PRINT(...) __DEBUG_BEGIN__ if (!_pn5180_debugSilent) { if (_pn5180_debugNL) PN5180DEBUG_INDENT; Serial.print(__VA_ARGS__); }; __DEBUG_END__ |
| 94 | +#define PN5180DEBUG(msg) PN5180DEBUG_PRINT(msg) |
| 95 | +#else |
| 96 | +#define PN5180DEBUG_OFF |
| 97 | +#define PN5180DEBUG_ON |
| 98 | +#define PN5180DEBUG_ENTER |
| 99 | +#define PN5180DEBUG_EXIT |
| 100 | +#define PN5180DEBUG_INDENT |
| 101 | +#define PN5180DEBUG_PRINTLN(...) |
| 102 | +#define PN5180DEBUG_PRINTF(...) |
| 103 | +#define PN5180DEBUG_PRINT(...) |
| 104 | +#define PN5180DEBUG(msg) |
| 105 | +#endif |
| 106 | + |
| 107 | +#ifdef DEBUG |
| 108 | +extern char * formatHex(const uint8_t val); |
| 109 | +extern char * formatHex(const uint16_t val); |
| 110 | +extern char * formatHex(const uint32_t val); |
| 111 | +#endif |
| 112 | + |
| 113 | +#endif /* DEBUG_H */ |
0 commit comments