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
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 #include <avr/pgmspace.h>
32 #define sendchar(c) do { if (print_sendchar_func) (print_sendchar_func)(c); } while (0)
35 static int8_t (*print_sendchar_func)(uint8_t) = 0;
37 void print_set_sendchar(int8_t (*sendchar_func)(uint8_t))
39 print_sendchar_func = sendchar_func;
42 /* print string stored in data memory(SRAM)
43 * print_P("hello world");
44 * This consumes precious SRAM memory space for string.
46 void print_S(const char *s)
52 if (c == '\n') sendchar('\r');
57 /* print string stored in program memory(FLASH)
58 * print_P(PSTR("hello world");
59 * This consumes relatively abundant FLASH memory area not SRAM.
61 void print_P(const char *s)
65 c = pgm_read_byte(s++);
67 if (c == '\n') sendchar('\r');
74 sendchar('\r'); sendchar('\n');
87 return (i < 10 ? '0' + i : 'A' + i - 10);
91 void print_int(uint16_t data, uint8_t base)
95 if ((base & SIGNED) && (data & 0x8000)) {
104 *(--p) = itoc(n - data*base);
106 if (buf[0]) *(--p) = buf[0];
110 void print_dec(uint16_t data)
112 print_int(data, DEC);
115 void print_decs(int16_t data)
117 print_int(data, DEC|SIGNED);
121 void print_hex4(uint8_t data)
123 sendchar(data + ((data < 10) ? '0' : 'A' - 10));
126 void print_hex8(uint8_t data)
129 print_hex4(data&0x0F);
132 void print_hex16(uint16_t data)
138 void print_hex32(uint32_t data)
140 print_hex16(data>>16);
144 void print_bin4(uint8_t data)
146 for (int i = 4; i >= 0; i--) {
147 sendchar((data & (1<<i)) ? '1' : '0');
151 void print_bin8(uint8_t data)
153 for (int i = 7; i >= 0; i--) {
154 sendchar((data & (1<<i)) ? '1' : '0');
158 void print_bin16(uint16_t data)
164 void print_bin32(uint32_t data)
166 print_bin8(data>>24);
167 print_bin8(data>>16);
172 void print_bin_reverse8(uint8_t data)
174 for (int i = 0; i < 8; i++) {
175 sendchar((data & (1<<i)) ? '1' : '0');
179 void print_bin_reverse16(uint16_t data)
181 print_bin_reverse8(data);
182 print_bin_reverse8(data>>8);
185 void print_bin_reverse32(uint32_t data)
187 print_bin_reverse8(data);
188 print_bin_reverse8(data>>8);
189 print_bin_reverse8(data>>16);
190 print_bin_reverse8(data>>24);