diff options
Diffstat (limited to 'common/print.c')
| -rw-r--r-- | common/print.c | 127 |
1 files changed, 86 insertions, 41 deletions
diff --git a/common/print.c b/common/print.c index 4e36d3935..dd73ff59d 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 |
| @@ -21,81 +22,125 @@ | |||
| 21 | * THE SOFTWARE. | 22 | * THE SOFTWARE. |
| 22 | */ | 23 | */ |
| 23 | 24 | ||
| 25 | #include <stdio.h> | ||
| 24 | #include <avr/io.h> | 26 | #include <avr/io.h> |
| 25 | #include <avr/pgmspace.h> | 27 | #include <avr/pgmspace.h> |
| 26 | #include "print.h" | 28 | #include "print.h" |
| 27 | #include "sendchar.h" | 29 | #define sendchar(c) do { if (print_enable && print_sendchar_func) (print_sendchar_func)(c); } while (0) |
| 28 | 30 | ||
| 29 | 31 | ||
| 32 | int8_t (*print_sendchar_func)(uint8_t) = NULL; | ||
| 30 | bool print_enable = false; | 33 | bool print_enable = false; |
| 31 | 34 | ||
| 35 | /* print string stored in data memory(SRAM) | ||
| 36 | * print_P("hello world"); | ||
| 37 | * This consumes precious SRAM memory space for string. | ||
| 38 | */ | ||
| 32 | void print_S(const char *s) | 39 | void print_S(const char *s) |
| 33 | { | 40 | { |
| 34 | if (!print_enable) return; | 41 | uint8_t c; |
| 35 | char c; | 42 | while (1) { |
| 36 | 43 | c = *s++; | |
| 37 | while (1) { | 44 | if (!c) break; |
| 38 | c = *s++; | 45 | if (c == '\n') sendchar('\r'); |
| 39 | if (!c) break; | 46 | sendchar(c); |
| 40 | if (c == '\n') sendchar('\r'); | 47 | } |
| 41 | sendchar(c); | ||
| 42 | } | ||
| 43 | } | 48 | } |
| 44 | 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 | */ | ||
| 45 | void print_P(const char *s) | 54 | void print_P(const char *s) |
| 46 | { | 55 | { |
| 47 | if (!print_enable) return; | 56 | uint8_t c; |
| 48 | char c; | 57 | while (1) { |
| 49 | 58 | c = pgm_read_byte(s++); | |
| 50 | while (1) { | 59 | if (!c) break; |
| 51 | c = pgm_read_byte(s++); | 60 | if (c == '\n') sendchar('\r'); |
| 52 | if (!c) break; | 61 | sendchar(c); |
| 53 | if (c == '\n') sendchar('\r'); | 62 | } |
| 54 | sendchar(c); | 63 | } |
| 55 | } | 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); | ||
| 56 | } | 75 | } |
| 57 | 76 | ||
| 58 | void phex1(unsigned char c) | 77 | void print_hex16(uint16_t data) |
| 59 | { | 78 | { |
| 60 | if (!print_enable) return; | 79 | print_hex8(data>>8); |
| 61 | sendchar(c + ((c < 10) ? '0' : 'A' - 10)); | 80 | print_hex8(data); |
| 62 | } | 81 | } |
| 63 | 82 | ||
| 64 | void phex(unsigned char c) | 83 | void print_hex32(uint32_t data) |
| 65 | { | 84 | { |
| 66 | if (!print_enable) return; | 85 | print_hex16(data>>16); |
| 67 | phex1(c >> 4); | 86 | print_hex16(data); |
| 68 | phex1(c & 15); | ||
| 69 | } | 87 | } |
| 70 | 88 | ||
| 71 | void phex16(unsigned int i) | 89 | void print_dec8(uint8_t data) |
| 72 | { | 90 | { |
| 73 | if (!print_enable) return; | 91 | if (data/100) sendchar('0' + (data/100)); |
| 74 | phex(i >> 8); | 92 | if (data/100 || data%100/10) sendchar('0' + (data%100/10)); |
| 75 | phex(i); | 93 | sendchar('0' + (data%10)); |
| 76 | } | 94 | } |
| 77 | 95 | ||
| 78 | void pdec(uint8_t i) | 96 | void print_dec16(uint16_t data) |
| 79 | { | 97 | { |
| 80 | if (!print_enable) return; | 98 | // TODO |
| 81 | if (i/100) sendchar('0' + (i/100)); | ||
| 82 | if (i/100 || i%100/10) sendchar('0' + (i%100/10)); | ||
| 83 | sendchar('0' + (i%10)); | ||
| 84 | } | 99 | } |
| 85 | 100 | ||
| 101 | void print_dec32(uint32_t data) | ||
| 102 | { | ||
| 103 | // TODO | ||
| 104 | } | ||
| 86 | 105 | ||
| 87 | void pbin(unsigned char c) | 106 | void print_bin(uint8_t data) |
| 88 | { | 107 | { |
| 89 | if (!print_enable) return; | ||
| 90 | for (int i = 7; i >= 0; i--) { | 108 | for (int i = 7; i >= 0; i--) { |
| 91 | sendchar((c & (1<<i)) ? '1' : '0'); | 109 | sendchar((data & (1<<i)) ? '1' : '0'); |
| 92 | } | 110 | } |
| 93 | } | 111 | } |
| 94 | 112 | ||
| 95 | void pbin_reverse(unsigned char c) | 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) | ||
| 96 | { | 128 | { |
| 97 | if (!print_enable) return; | ||
| 98 | for (int i = 0; i < 8; i++) { | 129 | for (int i = 0; i < 8; i++) { |
| 99 | sendchar((c & (1<<i)) ? '1' : '0'); | 130 | sendchar((data & (1<<i)) ? '1' : '0'); |
| 100 | } | 131 | } |
| 101 | } | 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 | } | ||
