]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/print.c
Merge branch 'keymap2'
[tmk_firmware.git] / common / print.c
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 #include <avr/io.h>
26 #include <avr/pgmspace.h>
27 #include "print.h"
28
29
30 #define sendchar(c)    do { if (print_enable && print_sendchar_func) (print_sendchar_func)(c); } while (0)
31
32
33 int8_t (*print_sendchar_func)(uint8_t) = 0;
34 bool print_enable = true;
35
36
37 /* print string stored in data memory(SRAM)
38  *     print_P("hello world");
39  * This consumes precious SRAM memory space for string.
40  */
41 void print_S(const char *s)
42 {
43     uint8_t c;
44     while (1) {
45         c = *s++;
46         if (!c) break;
47         if (c == '\n') sendchar('\r');
48         sendchar(c);
49     }
50 }
51
52 /* print string stored in program memory(FLASH)
53  *     print_P(PSTR("hello world");
54  * This consumes relatively abundant FLASH memory area not SRAM.
55  */
56 void print_P(const char *s)
57 {
58     uint8_t c;
59     while (1) {
60         c = pgm_read_byte(s++);
61         if (!c) break;
62         if (c == '\n') sendchar('\r');
63         sendchar(c);
64     }
65 }
66
67 void print_CRLF(void)
68 {
69     sendchar('\r'); sendchar('\n');
70 }
71
72
73 #define SIGNED  0x80
74 #define BIN     2
75 #define OCT     8
76 #define DEC     10
77 #define HEX     16
78
79 static inline
80 char itoc(uint8_t i)
81 {
82     return (i < 10 ? '0' + i : 'A' + i - 10);
83 }
84
85 static inline
86 void print_int(uint16_t data, uint8_t base)
87 {
88     char buf[7] = {'\0'};
89     char *p = &buf[6];
90     if ((base & SIGNED) && (data & 0x8000)) {
91         data = -data;
92         buf[0] = '-';
93     }
94     base &= ~SIGNED;
95     uint16_t n;
96     do {
97         n = data;
98         data /= base;
99         *(--p) = itoc(n - data*base);
100     } while (data);
101     if (buf[0]) *(--p) = buf[0];
102     print_S(p);
103 }
104
105 void print_dec(uint16_t data)
106 {
107     print_int(data, DEC);
108 }
109
110 void print_decs(int16_t data)
111 {
112     print_int(data, DEC|SIGNED);
113 }
114
115
116 void print_hex4(uint8_t data)
117 {
118     sendchar(data + ((data < 10) ? '0' : 'A' - 10));
119 }
120
121 void print_hex8(uint8_t data)
122 {
123     print_hex4(data>>4);
124     print_hex4(data&0x0F);
125 }
126
127 void print_hex16(uint16_t data)
128 {
129     print_hex8(data>>8);
130     print_hex8(data);
131 }
132
133 void print_hex32(uint32_t data)
134 {
135     print_hex16(data>>16);
136     print_hex16(data);
137 }
138
139 void print_bin4(uint8_t data)
140 {
141     for (int i = 4; i >= 0; i--) {
142         sendchar((data & (1<<i)) ? '1' : '0');
143     }
144 }
145
146 void print_bin8(uint8_t data)
147 {
148     for (int i = 7; i >= 0; i--) {
149         sendchar((data & (1<<i)) ? '1' : '0');
150     }
151 }
152
153 void print_bin16(uint16_t data)
154 {
155     print_bin8(data>>8);
156     print_bin8(data);
157 }
158
159 void print_bin32(uint32_t data)
160 {
161     print_bin8(data>>24);
162     print_bin8(data>>16);
163     print_bin8(data>>8);
164     print_bin8(data);
165 }
166
167 void print_bin_reverse8(uint8_t data)
168 {
169     for (int i = 0; i < 8; i++) {
170         sendchar((data & (1<<i)) ? '1' : '0');
171     }
172 }
173
174 void print_bin_reverse16(uint16_t data)
175 {
176     print_bin_reverse8(data);
177     print_bin_reverse8(data>>8);
178 }
179
180 void print_bin_reverse32(uint32_t data)
181 {
182     print_bin_reverse8(data);
183     print_bin_reverse8(data>>8);
184     print_bin_reverse8(data>>16);
185     print_bin_reverse8(data>>24);
186 }