]> git.gir.st - tmk_keyboard.git/blob - common/print.h
a828328b6e15c645d36dbe933d1e04c1dab53a52
[tmk_keyboard.git] / common / print.h
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 #ifndef PRINT_H__
26 #define PRINT_H__ 1
27
28 #include <stdint.h>
29 #include <stdbool.h>
30 #include <avr/pgmspace.h>
31 #include "xprintf.h"
32 #include "util.h"
33
34
35 // this macro allows you to write print("some text") and
36 // the string is automatically placed into flash memory :)
37 // TODO: avoid collision with arduino/Print.h
38 #ifndef __cplusplus
39 #define print(s) print_P(PSTR(s))
40 #endif
41 #define println(s) print_P(PSTR(s "\n"))
42
43 #ifndef AVR_LIBC_PRINTF
44 #define printf(f, ...) xprintf(f, ##__VA_ARGS__)
45 #endif
46
47 /* for old name */
48 #define pdec(data) print_dec(data)
49 #define pdec16(data) print_dec(data)
50 #define phex(data) print_hex8(data)
51 #define phex16(data) print_hex16(data)
52 #define pbin(data) print_bin8(data)
53 #define pbin16(data) print_bin16(data)
54 #define pbin_reverse(data) print_bin_reverse8(data)
55 #define pbin_reverse16(data) print_bin_reverse16(data)
56
57 /* print value utility */
58 #define print_val_dec(v) xprintf(#v ": %u\n", v)
59 #define print_val_decs(v) xprintf(#v ": %d\n", v)
60 #define print_val_hex8(v) xprintf(#v ": %X\n", v)
61 #define print_val_hex16(v) xprintf(#v ": %02X\n", v)
62 #define print_val_hex32(v) xprintf(#v ": %04lX\n", v)
63 #define print_val_bin8(v) xprintf(#v ": %08b\n", v)
64 #define print_val_bin16(v) xprintf(#v ": %016b\n", v)
65 #define print_val_bin32(v) xprintf(#v ": %032lb\n", v)
66 #define print_val_bin_reverse8(v) xprintf(#v ": %08b\n", bitrev(v))
67 #define print_val_bin_reverse16(v) xprintf(#v ": %016b\n", bitrev16(v))
68 #define print_val_bin_reverse32(v) xprintf(#v ": %032lb\n", bitrev32(v))
69
70
71
72 #ifndef NO_PRINT
73
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77
78 /* function pointer of sendchar to be used by print utility */
79 void print_set_sendchar(int8_t (*print_sendchar_func)(uint8_t));
80
81 /* print string stored in data memory(SRAM)
82 * print_S("hello world");
83 * This consumes precious SRAM memory space for string.
84 */
85 void print_S(const char *s);
86
87 void print_lf(void);
88 void print_crlf(void);
89
90
91 /* print string stored in program memory(FLASH)
92 * print_P(PSTR("hello world");
93 * This consumes relatively abundant FLASH memory area not SRAM.
94 */
95 #define print_P(s) xputs(s)
96
97 /* decimal */
98 #define print_dec(i) xprintf("%u", i)
99 #define print_decs(i) xprintf("%d", i)
100
101 /* hex */
102 #define print_hex4(i) xprintf("%X", i)
103 #define print_hex8(i) xprintf("%02X", i)
104 #define print_hex16(i) xprintf("%04X", i)
105 #define print_hex32(i) xprintf("%08lX", i)
106
107 /* binary */
108 #define print_bin4(i) xprintf("%04b", i)
109 #define print_bin8(i) xprintf("%08b", i)
110 #define print_bin16(i) xprintf("%016b", i)
111 #define print_bin32(i) xprintf("%032lb", i)
112
113 #define print_bin_reverse8(i) xprintf("%08b", bitrev(i))
114 #define print_bin_reverse16(i) xprintf("%016b", bitrev16(i))
115 #define print_bin_reverse32(i) xprintf("%032lb", bitrev32(i))
116
117 #ifdef __cplusplus
118 }
119 #endif
120
121 #else
122
123 #define print_set_sendchar(func)
124 #define print_S(s)
125 #define print_P(s)
126 #define print_dec(data)
127 #define print_decs(data)
128 #define print_hex4(data)
129 #define print_hex8(data)
130 #define print_hex16(data)
131 #define print_hex32(data)
132 #define print_bin4(data)
133 #define print_bin8(data)
134 #define print_bin16(data)
135 #define print_bin32(data)
136 #define print_bin_reverse8(data)
137 #define print_bin_reverse16(data)
138 #define print_bin_reverse32(data)
139
140 #endif
141
142
143 #endif
Imprint / Impressum