aboutsummaryrefslogtreecommitdiff
path: root/common/print.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/print.c')
-rw-r--r--common/print.c160
1 files changed, 120 insertions, 40 deletions
diff --git a/common/print.c b/common/print.c
index 4e36d3935..6a8a725bc 100644
--- a/common/print.c
+++ b/common/print.c
@@ -1,3 +1,4 @@
1/* Copyright 2012 Jun Wako <wakojun@gmail.com> */
1/* Very basic print functions, intended to be used with usb_debug_only.c 2/* Very basic print functions, intended to be used with usb_debug_only.c
2 * http://www.pjrc.com/teensy/ 3 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2008 PJRC.COM, LLC 4 * Copyright (c) 2008 PJRC.COM, LLC
@@ -24,78 +25,157 @@
24#include <avr/io.h> 25#include <avr/io.h>
25#include <avr/pgmspace.h> 26#include <avr/pgmspace.h>
26#include "print.h" 27#include "print.h"
27#include "sendchar.h"
28 28
29 29
30#define sendchar(c) do { if (print_enable && print_sendchar_func) (print_sendchar_func)(c); } while (0)
31
32
33int8_t (*print_sendchar_func)(uint8_t) = 0;
30bool print_enable = false; 34bool print_enable = false;
31 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 */
32void print_S(const char *s) 41void print_S(const char *s)
33{ 42{
34 if (!print_enable) return; 43 uint8_t c;
35 char c; 44 while (1) {
36 45 c = *s++;
37 while (1) { 46 if (!c) break;
38 c = *s++; 47 if (c == '\n') sendchar('\r');
39 if (!c) break; 48 sendchar(c);
40 if (c == '\n') sendchar('\r'); 49 }
41 sendchar(c);
42 }
43} 50}
44 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 */
45void print_P(const char *s) 56void print_P(const char *s)
46{ 57{
47 if (!print_enable) return; 58 uint8_t c;
48 char 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
67void 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
79static inline
80char itoc(uint8_t i)
81{
82 return (i < 10 ? '0' + i : 'A' + i - 10);
83}
84
85static inline
86void 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
105void print_dec(uint16_t data)
106{
107 print_int(data, DEC);
108}
49 109
50 while (1) { 110void print_decs(int16_t data)
51 c = pgm_read_byte(s++); 111{
52 if (!c) break; 112 print_int(data, DEC|SIGNED);
53 if (c == '\n') sendchar('\r');
54 sendchar(c);
55 }
56} 113}
57 114
58void phex1(unsigned char c) 115
116static inline
117void print_hex4(uint8_t data)
59{ 118{
60 if (!print_enable) return; 119 sendchar(data + ((data < 10) ? '0' : 'A' - 10));
61 sendchar(c + ((c < 10) ? '0' : 'A' - 10));
62} 120}
63 121
64void phex(unsigned char c) 122void print_hex8(uint8_t data)
65{ 123{
66 if (!print_enable) return; 124 print_hex4(data>>4);
67 phex1(c >> 4); 125 print_hex4(data&0x0F);
68 phex1(c & 15);
69} 126}
70 127
71void phex16(unsigned int i) 128void print_hex16(uint16_t data)
72{ 129{
73 if (!print_enable) return; 130 print_hex8(data>>8);
74 phex(i >> 8); 131 print_hex8(data);
75 phex(i);
76} 132}
77 133
78void pdec(uint8_t i) 134void print_hex32(uint32_t data)
79{ 135{
80 if (!print_enable) return; 136 print_hex16(data>>16);
81 if (i/100) sendchar('0' + (i/100)); 137 print_hex16(data);
82 if (i/100 || i%100/10) sendchar('0' + (i%100/10));
83 sendchar('0' + (i%10));
84} 138}
85 139
86 140
87void pbin(unsigned char c) 141void print_bin(uint8_t data)
88{ 142{
89 if (!print_enable) return;
90 for (int i = 7; i >= 0; i--) { 143 for (int i = 7; i >= 0; i--) {
91 sendchar((c & (1<<i)) ? '1' : '0'); 144 sendchar((data & (1<<i)) ? '1' : '0');
92 } 145 }
93} 146}
94 147
95void pbin_reverse(unsigned char c) 148void print_bin16(uint16_t data)
149{
150 print_bin8(data>>8);
151 print_bin8(data);
152}
153
154void print_bin32(uint32_t data)
155{
156 print_bin8(data>>24);
157 print_bin8(data>>16);
158 print_bin8(data>>8);
159 print_bin8(data);
160}
161
162void print_bin_reverse8(uint8_t data)
96{ 163{
97 if (!print_enable) return;
98 for (int i = 0; i < 8; i++) { 164 for (int i = 0; i < 8; i++) {
99 sendchar((c & (1<<i)) ? '1' : '0'); 165 sendchar((data & (1<<i)) ? '1' : '0');
100 } 166 }
101} 167}
168
169void print_bin_reverse16(uint16_t data)
170{
171 print_bin_reverse8(data);
172 print_bin_reverse8(data>>8);
173}
174
175void print_bin_reverse32(uint32_t data)
176{
177 print_bin_reverse8(data);
178 print_bin_reverse8(data>>8);
179 print_bin_reverse8(data>>16);
180 print_bin_reverse8(data>>24);
181}