print.h (6455B)
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 25 #pragma once 26 27 #include <stdint.h> 28 #include <stdbool.h> 29 #include "util.h" 30 #include "sendchar.h" 31 #include "progmem.h" 32 33 void print_set_sendchar(sendchar_func_t func); 34 35 /** 36 * @brief This macro suppress format warnings for the function that is passed 37 * in. The main use-case is that `b` format specifier for printing binary 38 * numbers is not in the official C standard. Inclusion is planned for the 39 * upcoming C2X C standard, but until then GCC will always output a warning for 40 * a unknown format specifier. 41 */ 42 #define IGNORE_FORMAT_WARNING(func) \ 43 do { \ 44 _Pragma("GCC diagnostic push"); \ 45 _Pragma("GCC diagnostic ignored \"-Wformat\""); \ 46 _Pragma("GCC diagnostic ignored \"-Wformat-extra-args\""); \ 47 func; \ 48 _Pragma("GCC diagnostic pop"); \ 49 } while (0) 50 51 #ifndef NO_PRINT 52 # if __has_include_next("_print.h") 53 # include_next "_print.h" /* Include the platforms print.h */ 54 # else 55 # include "printf.h" // // Fall back to lib/printf/printf.h 56 # define xprintf printf 57 # endif 58 #else 59 // Remove print defines 60 # undef xprintf 61 # define xprintf(fmt, ...) 62 #endif 63 64 // Resolve before USER_PRINT can remove 65 #define uprintf xprintf 66 67 #ifdef USER_PRINT 68 // Remove normal print defines 69 # undef xprintf 70 # define xprintf(fmt, ...) 71 #endif 72 73 #define print(s) xprintf(s) 74 #define println(s) xprintf(s "\r\n") 75 76 #define print_dec(i) xprintf("%u", i) 77 #define print_decs(i) xprintf("%d", i) 78 /* hex */ 79 #define print_hex4(i) xprintf("%X", i) 80 #define print_hex8(i) xprintf("%02X", i) 81 #define print_hex16(i) xprintf("%04X", i) 82 #define print_hex32(i) xprintf("%08lX", i) 83 /* binary */ 84 #define print_bin4(i) IGNORE_FORMAT_WARNING(xprintf("%04b", i)) 85 #define print_bin8(i) IGNORE_FORMAT_WARNING(xprintf("%08b", i)) 86 #define print_bin16(i) IGNORE_FORMAT_WARNING(xprintf("%016b", i)) 87 #define print_bin32(i) IGNORE_FORMAT_WARNING(xprintf("%032lb", i)) 88 #define print_bin_reverse8(i) IGNORE_FORMAT_WARNING(xprintf("%08b", bitrev(i))) 89 #define print_bin_reverse16(i) IGNORE_FORMAT_WARNING(xprintf("%016b", bitrev16(i))) 90 #define print_bin_reverse32(i) IGNORE_FORMAT_WARNING(xprintf("%032lb", bitrev32(i))) 91 /* print value utility */ 92 #define print_val_dec(v) xprintf(#v ": %u\n", v) 93 #define print_val_decs(v) xprintf(#v ": %d\n", v) 94 #define print_val_hex8(v) xprintf(#v ": %X\n", v) 95 #define print_val_hex16(v) xprintf(#v ": %02X\n", v) 96 #define print_val_hex32(v) xprintf(#v ": %04lX\n", v) 97 #define print_val_bin8(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %08b\n", v)) 98 #define print_val_bin16(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %016b\n", v)) 99 #define print_val_bin32(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %032lb\n", v)) 100 #define print_val_bin_reverse8(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %08b\n", bitrev(v))) 101 #define print_val_bin_reverse16(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %016b\n", bitrev16(v))) 102 #define print_val_bin_reverse32(v) IGNORE_FORMAT_WARNING(xprintf(#v ": %032lb\n", bitrev32(v))) 103 104 // User print disables the normal print messages in the body of QMK/TMK code and 105 // is meant as a lightweight alternative to NOPRINT. Use it when you only want to do 106 // a spot of debugging but lack flash resources for allowing all of the codebase to 107 // print (and store their wasteful strings). 108 // 109 // !!! DO NOT USE USER PRINT CALLS IN THE BODY OF QMK/TMK !!! 110 111 #define uprint(s) uprintf(s) 112 #define uprintln(s) uprintf(s "\r\n") 113 114 /* decimal */ 115 #define uprint_dec(i) uprintf("%u", i) 116 #define uprint_decs(i) uprintf("%d", i) 117 /* hex */ 118 #define uprint_hex4(i) uprintf("%X", i) 119 #define uprint_hex8(i) uprintf("%02X", i) 120 #define uprint_hex16(i) uprintf("%04X", i) 121 #define uprint_hex32(i) uprintf("%08lX", i) 122 /* binary */ 123 #define uprint_bin4(i) IGNORE_FORMAT_WARNING(uprintf("%04b", i)) 124 #define uprint_bin8(i) IGNORE_FORMAT_WARNING(uprintf("%08b", i)) 125 #define uprint_bin16(i) IGNORE_FORMAT_WARNING(uprintf("%016b", i)) 126 #define uprint_bin32(i) IGNORE_FORMAT_WARNING(uprintf("%032lb", i)) 127 #define uprint_bin_reverse8(i) IGNORE_FORMAT_WARNING(uprintf("%08b", bitrev(i))) 128 #define uprint_bin_reverse16(i) IGNORE_FORMAT_WARNING(uprintf("%016b", bitrev16(i))) 129 #define uprint_bin_reverse32(i) IGNORE_FORMAT_WARNING(uprintf("%032lb", bitrev32(i))) 130 /* print value utility */ 131 #define uprint_val_dec(v) uprintf(#v ": %u\n", v) 132 #define uprint_val_decs(v) uprintf(#v ": %d\n", v) 133 #define uprint_val_hex8(v) uprintf(#v ": %X\n", v) 134 #define uprint_val_hex16(v) uprintf(#v ": %02X\n", v) 135 #define uprint_val_hex32(v) uprintf(#v ": %04lX\n", v) 136 #define uprint_val_bin8(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %08b\n", v)) 137 #define uprint_val_bin16(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %016b\n", v)) 138 #define uprint_val_bin32(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %032lb\n", v)) 139 #define uprint_val_bin_reverse8(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %08b\n", bitrev(v))) 140 #define uprint_val_bin_reverse16(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %016b\n", bitrev16(v))) 141 #define uprint_val_bin_reverse32(v) IGNORE_FORMAT_WARNING(uprintf(#v ": %032lb\n", bitrev32(v)))