diff options
| author | tmk <nobody@nowhere> | 2013-05-14 16:16:57 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2013-05-14 16:16:57 +0900 |
| commit | d9c06db60006e2191d8b86e34f651644a54426b4 (patch) | |
| tree | 14b79372b1d0f0d9623b21e6dbffa73ce2036b7f /common/print.c | |
| parent | b9f558b3d89fc434d6dab348698d5100ff82d16b (diff) | |
| download | qmk_firmware-d9c06db60006e2191d8b86e34f651644a54426b4.tar.gz qmk_firmware-d9c06db60006e2191d8b86e34f651644a54426b4.zip | |
Add xprintf(xitoa) from elm-chan.org
Diffstat (limited to 'common/print.c')
| -rw-r--r-- | common/print.c | 147 |
1 files changed, 8 insertions, 139 deletions
diff --git a/common/print.c b/common/print.c index 329f83512..783bb4e9b 100644 --- a/common/print.c +++ b/common/print.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* Copyright 2012 Jun Wako <wakojun@gmail.com> */ | 1 | /* Copyright 2012,2013 Jun Wako <wakojun@gmail.com> */ |
| 2 | /* 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 |
| 3 | * http://www.pjrc.com/teensy/ | 3 | * http://www.pjrc.com/teensy/ |
| 4 | * Copyright (c) 2008 PJRC.COM, LLC | 4 | * Copyright (c) 2008 PJRC.COM, LLC |
| @@ -29,20 +29,14 @@ | |||
| 29 | 29 | ||
| 30 | #ifndef NO_PRINT | 30 | #ifndef NO_PRINT |
| 31 | 31 | ||
| 32 | #define sendchar(c) do { if (print_sendchar_func) (print_sendchar_func)(c); } while (0) | 32 | #define sendchar(c) xputc(c) |
| 33 | 33 | ||
| 34 | 34 | ||
| 35 | static int8_t (*print_sendchar_func)(uint8_t) = 0; | ||
| 36 | |||
| 37 | void print_set_sendchar(int8_t (*sendchar_func)(uint8_t)) | 35 | void print_set_sendchar(int8_t (*sendchar_func)(uint8_t)) |
| 38 | { | 36 | { |
| 39 | print_sendchar_func = sendchar_func; | 37 | xdev_out(sendchar_func); |
| 40 | } | 38 | } |
| 41 | 39 | ||
| 42 | /* print string stored in data memory(SRAM) | ||
| 43 | * print_P("hello world"); | ||
| 44 | * This consumes precious SRAM memory space for string. | ||
| 45 | */ | ||
| 46 | void print_S(const char *s) | 40 | void print_S(const char *s) |
| 47 | { | 41 | { |
| 48 | uint8_t c; | 42 | uint8_t c; |
| @@ -54,140 +48,15 @@ void print_S(const char *s) | |||
| 54 | } | 48 | } |
| 55 | } | 49 | } |
| 56 | 50 | ||
| 57 | /* print string stored in program memory(FLASH) | 51 | void print_lf(void) |
| 58 | * print_P(PSTR("hello world"); | ||
| 59 | * This consumes relatively abundant FLASH memory area not SRAM. | ||
| 60 | */ | ||
| 61 | void print_P(const char *s) | ||
| 62 | { | ||
| 63 | uint8_t c; | ||
| 64 | while (1) { | ||
| 65 | c = pgm_read_byte(s++); | ||
| 66 | if (!c) break; | ||
| 67 | if (c == '\n') sendchar('\r'); | ||
| 68 | sendchar(c); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | void print_CRLF(void) | ||
| 73 | { | ||
| 74 | sendchar('\r'); sendchar('\n'); | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | #define SIGNED 0x80 | ||
| 79 | #define BIN 2 | ||
| 80 | #define OCT 8 | ||
| 81 | #define DEC 10 | ||
| 82 | #define HEX 16 | ||
| 83 | |||
| 84 | static inline | ||
| 85 | char itoc(uint8_t i) | ||
| 86 | { | ||
| 87 | return (i < 10 ? '0' + i : 'A' + i - 10); | ||
| 88 | } | ||
| 89 | |||
| 90 | static inline | ||
| 91 | void print_int(uint16_t data, uint8_t base) | ||
| 92 | { | ||
| 93 | char buf[7] = {'\0'}; | ||
| 94 | char *p = &buf[6]; | ||
| 95 | if ((base & SIGNED) && (data & 0x8000)) { | ||
| 96 | data = -data; | ||
| 97 | buf[0] = '-'; | ||
| 98 | } | ||
| 99 | base &= ~SIGNED; | ||
| 100 | uint16_t n; | ||
| 101 | do { | ||
| 102 | n = data; | ||
| 103 | data /= base; | ||
| 104 | *(--p) = itoc(n - data*base); | ||
| 105 | } while (data); | ||
| 106 | if (buf[0]) *(--p) = buf[0]; | ||
| 107 | print_S(p); | ||
| 108 | } | ||
| 109 | |||
| 110 | void print_dec(uint16_t data) | ||
| 111 | { | ||
| 112 | print_int(data, DEC); | ||
| 113 | } | ||
| 114 | |||
| 115 | void print_decs(int16_t data) | ||
| 116 | { | ||
| 117 | print_int(data, DEC|SIGNED); | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | void print_hex4(uint8_t data) | ||
| 122 | { | ||
| 123 | sendchar(data + ((data < 10) ? '0' : 'A' - 10)); | ||
| 124 | } | ||
| 125 | |||
| 126 | void print_hex8(uint8_t data) | ||
| 127 | { | ||
| 128 | print_hex4(data>>4); | ||
| 129 | print_hex4(data&0x0F); | ||
| 130 | } | ||
| 131 | |||
| 132 | void print_hex16(uint16_t data) | ||
| 133 | { | ||
| 134 | print_hex8(data>>8); | ||
| 135 | print_hex8(data); | ||
| 136 | } | ||
| 137 | |||
| 138 | void print_hex32(uint32_t data) | ||
| 139 | { | ||
| 140 | print_hex16(data>>16); | ||
| 141 | print_hex16(data); | ||
| 142 | } | ||
| 143 | |||
| 144 | void print_bin4(uint8_t data) | ||
| 145 | { | ||
| 146 | for (int i = 4; i >= 0; i--) { | ||
| 147 | sendchar((data & (1<<i)) ? '1' : '0'); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | void print_bin8(uint8_t data) | ||
| 152 | { | ||
| 153 | for (int i = 7; i >= 0; i--) { | ||
| 154 | sendchar((data & (1<<i)) ? '1' : '0'); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | void print_bin16(uint16_t data) | ||
| 159 | { | ||
| 160 | print_bin8(data>>8); | ||
| 161 | print_bin8(data); | ||
| 162 | } | ||
| 163 | |||
| 164 | void print_bin32(uint32_t data) | ||
| 165 | { | ||
| 166 | print_bin8(data>>24); | ||
| 167 | print_bin8(data>>16); | ||
| 168 | print_bin8(data>>8); | ||
| 169 | print_bin8(data); | ||
| 170 | } | ||
| 171 | |||
| 172 | void print_bin_reverse8(uint8_t data) | ||
| 173 | { | ||
| 174 | for (int i = 0; i < 8; i++) { | ||
| 175 | sendchar((data & (1<<i)) ? '1' : '0'); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | void print_bin_reverse16(uint16_t data) | ||
| 180 | { | 52 | { |
| 181 | print_bin_reverse8(data); | 53 | sendchar('\n'); |
| 182 | print_bin_reverse8(data>>8); | ||
| 183 | } | 54 | } |
| 184 | 55 | ||
| 185 | void print_bin_reverse32(uint32_t data) | 56 | void print_crlf(void) |
| 186 | { | 57 | { |
| 187 | print_bin_reverse8(data); | 58 | sendchar('\r'); |
| 188 | print_bin_reverse8(data>>8); | 59 | sendchar('\n'); |
| 189 | print_bin_reverse8(data>>16); | ||
| 190 | print_bin_reverse8(data>>24); | ||
| 191 | } | 60 | } |
| 192 | 61 | ||
| 193 | #endif | 62 | #endif |
