print.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright 2012 Jun Wako <wakojun@gmail.com> */
  2. /* Very basic print functions, intended to be used with usb_debug_only.c
  3. * http://www.pjrc.com/teensy/
  4. * Copyright (c) 2008 PJRC.COM, LLC
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #pragma once
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include "util.h"
  28. #include "sendchar.h"
  29. #include "progmem.h"
  30. /**
  31. * @brief Configure the destination for routing all log data to.
  32. */
  33. void print_set_sendchar(sendchar_func_t func);
  34. /**
  35. * @def print_printf(fmt, ...)
  36. * @brief Low-level logging entrypoint - should not be called directly
  37. */
  38. #ifndef NO_PRINT
  39. # if __has_include_next("_print.h")
  40. # include_next "_print.h" // Include the platforms print.h - must export print_printf
  41. # else
  42. # include "printf.h" // Fall back to lib/printf/printf.h
  43. # define print_printf printf
  44. # endif
  45. #else
  46. # define print_printf(fmt, ...) // Remove ALL print defines
  47. #endif
  48. /**
  49. * @brief This macro suppress format warnings for the function that is passed
  50. * in. The main use-case is that `b` format specifier for printing binary
  51. * numbers is not in the official C standard. Inclusion is planned for the
  52. * upcoming C2X C standard, but until then GCC will always output a warning for
  53. * a unknown format specifier.
  54. */
  55. #define IGNORE_FORMAT_WARNING(func) \
  56. do { \
  57. _Pragma("GCC diagnostic push"); \
  58. _Pragma("GCC diagnostic ignored \"-Wformat\""); \
  59. _Pragma("GCC diagnostic ignored \"-Wformat-extra-args\""); \
  60. func; \
  61. _Pragma("GCC diagnostic pop"); \
  62. } while (0)
  63. #ifdef USER_PRINT
  64. # define xprintf(fmt, ...) // Remove normal print defines
  65. #else
  66. # define xprintf print_printf
  67. #endif
  68. #define print(s) xprintf(s)
  69. #define println(s) xprintf(s "\r\n")
  70. #define print_dec(i) xprintf("%u", i)
  71. #define print_decs(i) xprintf("%d", i)
  72. /* hex */
  73. #define print_hex4(i) xprintf("%X", i)
  74. #define print_hex8(i) xprintf("%02X", i)
  75. #define print_hex16(i) xprintf("%04X", i)
  76. #define print_hex32(i) xprintf("%08lX", i)
  77. /* binary */
  78. #define print_bin4(i) IGNORE_FORMAT_WARNING(xprintf("%04b", i))
  79. #define print_bin8(i) IGNORE_FORMAT_WARNING(xprintf("%08b", i))
  80. #define print_bin16(i) IGNORE_FORMAT_WARNING(xprintf("%016b", i))
  81. #define print_bin32(i) IGNORE_FORMAT_WARNING(xprintf("%032lb", i))
  82. #define print_bin_reverse8(i) IGNORE_FORMAT_WARNING(xprintf("%08b", bitrev(i)))
  83. #define print_bin_reverse16(i) IGNORE_FORMAT_WARNING(xprintf("%016b", bitrev16(i)))
  84. #define print_bin_reverse32(i) IGNORE_FORMAT_WARNING(xprintf("%032lb", bitrev32(i)))
  85. /* print value utility */
  86. #define print_val_dec(v) xprintf(#v ": %u\n", v)
  87. #define print_val_decs(v) xprintf(#v ": %d\n", v)
  88. #define print_val_hex8(v) xprintf(#v ": %X\n", v)
  89. #define print_val_hex16(v) xprintf(#v ": %02X\n", v)
  90. #define print_val_hex32(v) xprintf(#v ": %04lX\n", v)
  91. #define print_val_bin8(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %08b\n", v))
  92. #define print_val_bin16(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %016b\n", v))
  93. #define print_val_bin32(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %032lb\n", v))
  94. #define print_val_bin_reverse8(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %08b\n", bitrev(v)))
  95. #define print_val_bin_reverse16(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %016b\n", bitrev16(v)))
  96. #define print_val_bin_reverse32(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %032lb\n", bitrev32(v)))
  97. // User print disables the normal print messages in the body of QMK/TMK code and
  98. // is meant as a lightweight alternative to NOPRINT. Use it when you only want to do
  99. // a spot of debugging but lack flash resources for allowing all of the codebase to
  100. // print (and store their wasteful strings).
  101. //
  102. // !!! DO NOT USE USER PRINT CALLS IN THE BODY OF QMK/TMK !!!
  103. #define uprintf print_printf
  104. #define uprint(s) uprintf(s)
  105. #define uprintln(s) uprintf(s "\r\n")
  106. /* decimal */
  107. #define uprint_dec(i) uprintf("%u", i)
  108. #define uprint_decs(i) uprintf("%d", i)
  109. /* hex */
  110. #define uprint_hex4(i) uprintf("%X", i)
  111. #define uprint_hex8(i) uprintf("%02X", i)
  112. #define uprint_hex16(i) uprintf("%04X", i)
  113. #define uprint_hex32(i) uprintf("%08lX", i)
  114. /* binary */
  115. #define uprint_bin4(i) IGNORE_FORMAT_WARNING(uprintf("%04b", i))
  116. #define uprint_bin8(i) IGNORE_FORMAT_WARNING(uprintf("%08b", i))
  117. #define uprint_bin16(i) IGNORE_FORMAT_WARNING(uprintf("%016b", i))
  118. #define uprint_bin32(i) IGNORE_FORMAT_WARNING(uprintf("%032lb", i))
  119. #define uprint_bin_reverse8(i) IGNORE_FORMAT_WARNING(uprintf("%08b", bitrev(i)))
  120. #define uprint_bin_reverse16(i) IGNORE_FORMAT_WARNING(uprintf("%016b", bitrev16(i)))
  121. #define uprint_bin_reverse32(i) IGNORE_FORMAT_WARNING(uprintf("%032lb", bitrev32(i)))
  122. /* print value utility */
  123. #define uprint_val_dec(v) uprintf(#v ": %u\n", v)
  124. #define uprint_val_decs(v) uprintf(#v ": %d\n", v)
  125. #define uprint_val_hex8(v) uprintf(#v ": %X\n", v)
  126. #define uprint_val_hex16(v) uprintf(#v ": %02X\n", v)
  127. #define uprint_val_hex32(v) uprintf(#v ": %04lX\n", v)
  128. #define uprint_val_bin8(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %08b\n", v))
  129. #define uprint_val_bin16(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %016b\n", v))
  130. #define uprint_val_bin32(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %032lb\n", v))
  131. #define uprint_val_bin_reverse8(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %08b\n", bitrev(v)))
  132. #define uprint_val_bin_reverse16(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %016b\n", bitrev16(v)))
  133. #define uprint_val_bin_reverse32(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %032lb\n", bitrev32(v)))