]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/print.c
Add print utility
[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 <stdio.h>
26 #include <avr/io.h>
27 #include <avr/pgmspace.h>
28 #include "print.h"
29 #define sendchar(c)    do { if (print_enable && print_sendchar_func) (print_sendchar_func)(c); } while (0)
30
31
32 int8_t (*print_sendchar_func)(uint8_t) = NULL;
33 bool print_enable = false;
34
35 /* print string stored in data memory(SRAM)
36  *     print_P("hello world");
37  * This consumes precious SRAM memory space for string.
38  */
39 void print_S(const char *s)
40 {
41     uint8_t c;
42     while (1) {
43         c = *s++;
44         if (!c) break;
45         if (c == '\n') sendchar('\r');
46         sendchar(c);
47     }
48 }
49
50 /* print string stored in program memory(FLASH)
51  *     print_P(PSTR("hello world");
52  * This consumes relatively abundant FLASH memory area not SRAM.
53  */
54 void print_P(const char *s)
55 {
56     uint8_t c;
57     while (1) {
58         c = pgm_read_byte(s++);
59         if (!c) break;
60         if (c == '\n') sendchar('\r');
61         sendchar(c);
62     }
63 }
64
65 static inline
66 void print_hex4(uint8_t data)
67 {
68     sendchar(data + ((data < 10) ? '0' : 'A' - 10));
69 }
70
71 void print_hex8(uint8_t data)
72 {
73     print_hex4(data>>4);
74     print_hex4(data&0x0F);
75 }
76
77 void print_hex16(uint16_t data)
78 {
79     print_hex8(data>>8);
80     print_hex8(data);
81 }
82
83 void print_hex32(uint32_t data)
84 {
85     print_hex16(data>>16);
86     print_hex16(data);
87 }
88
89 void print_dec8(uint8_t data)
90 {
91     if (data/100) sendchar('0' + (data/100));
92     if (data/100 || data%100/10) sendchar('0' + (data%100/10));
93     sendchar('0' + (data%10));
94 }
95
96 void print_dec16(uint16_t data)
97 {
98     // TODO
99 }
100
101 void print_dec32(uint32_t data)
102 {
103     // TODO
104 }
105
106 void print_bin(uint8_t data)
107 {
108     for (int i = 7; i >= 0; i--) {
109         sendchar((data & (1<<i)) ? '1' : '0');
110     }
111 }
112
113 void print_bin16(uint16_t data)
114 {
115     print_bin8(data>>8);
116     print_bin8(data);
117 }
118
119 void print_bin32(uint32_t data)
120 {
121     print_bin8(data>>24);
122     print_bin8(data>>16);
123     print_bin8(data>>8);
124     print_bin8(data);
125 }
126
127 void print_bin_reverse8(uint8_t data)
128 {
129     for (int i = 0; i < 8; i++) {
130         sendchar((data & (1<<i)) ? '1' : '0');
131     }
132 }
133
134 void print_bin_reverse16(uint16_t data)
135 {
136     print_bin_reverse8(data);
137     print_bin_reverse8(data>>8);
138 }
139
140 void print_bin_reverse32(uint32_t data)
141 {
142     print_bin_reverse8(data);
143     print_bin_reverse8(data>>8);
144     print_bin_reverse8(data>>16);
145     print_bin_reverse8(data>>24);
146 }