Tool to dump contents of a data buffer in traditional terminal format. Some VT100 color commands used.
PrintBuffer.cpp@3:4a6123f2a4c9, 2015-05-14 (annotated)
- Committer:
- loopsva
- Date:
- Thu May 14 16:40:04 2015 +0000
- Revision:
- 3:4a6123f2a4c9
- Parent:
- 2:e7f3ba216a14
- Child:
- 4:c3c8b072f80f
Correct end-of-line issue. Was: \n now: \r\n
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
loopsva | 0:94d1683e78b7 | 1 | #include "mbed.h" |
loopsva | 0:94d1683e78b7 | 2 | #include "PrintBuffer.h" |
loopsva | 0:94d1683e78b7 | 3 | |
loopsva | 1:8073086de8b7 | 4 | extern RawSerial pc; |
loopsva | 2:e7f3ba216a14 | 5 | //const char ESC = 0x1b; //ascii escape |
loopsva | 2:e7f3ba216a14 | 6 | |
loopsva | 2:e7f3ba216a14 | 7 | #define DOBLACK "\033[0;30;2m" |
loopsva | 2:e7f3ba216a14 | 8 | #define DORED "\033[0;31;2m" |
loopsva | 2:e7f3ba216a14 | 9 | #define DOGREEN "\033[0;32;2m" |
loopsva | 2:e7f3ba216a14 | 10 | #define DOYELLOW "\033[0;33;2m" |
loopsva | 2:e7f3ba216a14 | 11 | #define DOBLUE "\033[0;34;2m" |
loopsva | 2:e7f3ba216a14 | 12 | #define DOMAGENTA "\033[0;35;2m" |
loopsva | 2:e7f3ba216a14 | 13 | #define DOCYAN "\033[0;36;2m" |
loopsva | 2:e7f3ba216a14 | 14 | #define DOWHITE "\033[0;37;2m" |
loopsva | 2:e7f3ba216a14 | 15 | #define DODEFAULT "\033[0;39;2m" |
loopsva | 2:e7f3ba216a14 | 16 | #define DONONE "\033[0m" |
loopsva | 0:94d1683e78b7 | 17 | |
loopsva | 0:94d1683e78b7 | 18 | // Constructor |
loopsva | 0:94d1683e78b7 | 19 | PrintBuffer::PrintBuffer(const char* name) { |
loopsva | 0:94d1683e78b7 | 20 | } |
loopsva | 0:94d1683e78b7 | 21 | |
loopsva | 0:94d1683e78b7 | 22 | //--------------------------------------------------------------------------------------------------------------------------------------// |
loopsva | 0:94d1683e78b7 | 23 | // Used for doing a hex and ascii dump of the buffer. BufferLines tells PrintBuffer how |
loopsva | 0:94d1683e78b7 | 24 | // many 16 byte lines to print. |
loopsva | 0:94d1683e78b7 | 25 | |
loopsva | 0:94d1683e78b7 | 26 | int PrintBuffer::dump(const char* title, int BufferLines, int BufferOffset, const char buffer[]) { |
loopsva | 3:4a6123f2a4c9 | 27 | pc.printf("\n%s%s - lines: %d starting at: 0x%04x%s\r\n", DOMAGENTA, title, BufferLines, BufferOffset, DONONE); |
loopsva | 0:94d1683e78b7 | 28 | for(int i = BufferOffset; i < BufferLines * 16 + BufferOffset; i = i + 16) { |
loopsva | 0:94d1683e78b7 | 29 | pc.printf("%04x ", i); |
loopsva | 0:94d1683e78b7 | 30 | for(int s = 0; s < 8; s++) { |
loopsva | 0:94d1683e78b7 | 31 | pc.printf("%02x ", buffer [s + i]); |
loopsva | 0:94d1683e78b7 | 32 | } |
loopsva | 0:94d1683e78b7 | 33 | pc.printf(" "); |
loopsva | 0:94d1683e78b7 | 34 | for(int s = 0; s < 8; s++) { |
loopsva | 0:94d1683e78b7 | 35 | pc.printf("%02x ", buffer[s + i + 8]); |
loopsva | 0:94d1683e78b7 | 36 | } |
loopsva | 0:94d1683e78b7 | 37 | pc.printf(" "); |
loopsva | 0:94d1683e78b7 | 38 | for(int s = 0; s < 8; s++) { |
loopsva | 0:94d1683e78b7 | 39 | if((buffer[s + i] < 0x20) || (buffer[s + i] > 0x7f)) { |
loopsva | 2:e7f3ba216a14 | 40 | pc.printf("%s.%s", DOCYAN, DONONE); |
loopsva | 0:94d1683e78b7 | 41 | } else { |
loopsva | 0:94d1683e78b7 | 42 | pc.printf("%c", buffer[s + i]); |
loopsva | 0:94d1683e78b7 | 43 | } |
loopsva | 0:94d1683e78b7 | 44 | } |
loopsva | 0:94d1683e78b7 | 45 | pc.printf(" "); |
loopsva | 0:94d1683e78b7 | 46 | for(int s = 0; s < 8; s++) { |
loopsva | 0:94d1683e78b7 | 47 | if((buffer[s + i + 8] < 0x20) || (buffer[s + i + 8] > 0x7f)) { |
loopsva | 2:e7f3ba216a14 | 48 | pc.printf("%s.%s", DOCYAN, DONONE); |
loopsva | 0:94d1683e78b7 | 49 | } else { |
loopsva | 0:94d1683e78b7 | 50 | pc.printf("%c", buffer[s + i + 8]); |
loopsva | 0:94d1683e78b7 | 51 | } |
loopsva | 0:94d1683e78b7 | 52 | } |
loopsva | 3:4a6123f2a4c9 | 53 | pc.printf("\r\n"); |
loopsva | 0:94d1683e78b7 | 54 | } |
loopsva | 3:4a6123f2a4c9 | 55 | pc.printf("\r\n"); |
loopsva | 0:94d1683e78b7 | 56 | return(0); |
loopsva | 0:94d1683e78b7 | 57 | } |
loopsva | 0:94d1683e78b7 | 58 | |
loopsva | 0:94d1683e78b7 | 59 | //--------------------------------------------------------------------------------------------------------------------------------------// |
loopsva | 0:94d1683e78b7 | 60 | // Used for doing a hex and ascii dump of the buffer. BufferLines tells PrintBuffer how |
loopsva | 0:94d1683e78b7 | 61 | // many 16 byte lines to print. |
loopsva | 0:94d1683e78b7 | 62 | |
loopsva | 0:94d1683e78b7 | 63 | int PrintBuffer::dump_t(const char* title, int BufferLines, int BufferOffset, const uint8_t buffer[]) { |
loopsva | 3:4a6123f2a4c9 | 64 | pc.printf("\n%s%s - lines: %d starting at: 0x%04x%s\r\n", DOMAGENTA, title, BufferLines, BufferOffset, DONONE); |
loopsva | 0:94d1683e78b7 | 65 | for(int i = BufferOffset; i < BufferLines * 16 + BufferOffset; i = i + 16) { |
loopsva | 0:94d1683e78b7 | 66 | pc.printf("%04x ", i); |
loopsva | 0:94d1683e78b7 | 67 | for(int s = 0; s < 8; s++) { |
loopsva | 0:94d1683e78b7 | 68 | pc.printf("%02x ", buffer [s + i]); |
loopsva | 0:94d1683e78b7 | 69 | } |
loopsva | 0:94d1683e78b7 | 70 | pc.printf(" "); |
loopsva | 0:94d1683e78b7 | 71 | for(int s = 0; s < 8; s++) { |
loopsva | 0:94d1683e78b7 | 72 | pc.printf("%02x ", buffer[s + i + 8]); |
loopsva | 0:94d1683e78b7 | 73 | } |
loopsva | 0:94d1683e78b7 | 74 | pc.printf(" "); |
loopsva | 0:94d1683e78b7 | 75 | for(int s = 0; s < 8; s++) { |
loopsva | 0:94d1683e78b7 | 76 | if((buffer[s + i] < 0x20) || (buffer[s + i] > 0x7f)) { |
loopsva | 2:e7f3ba216a14 | 77 | pc.printf("%s.%s", DOCYAN, DONONE); |
loopsva | 0:94d1683e78b7 | 78 | } else { |
loopsva | 0:94d1683e78b7 | 79 | pc.printf("%c", buffer[s + i]); |
loopsva | 0:94d1683e78b7 | 80 | } |
loopsva | 0:94d1683e78b7 | 81 | } |
loopsva | 0:94d1683e78b7 | 82 | pc.printf(" "); |
loopsva | 0:94d1683e78b7 | 83 | for(int s = 0; s < 8; s++) { |
loopsva | 0:94d1683e78b7 | 84 | if((buffer[s + i + 8] < 0x20) || (buffer[s + i + 8] > 0x7f)) { |
loopsva | 2:e7f3ba216a14 | 85 | pc.printf("%s.%s", DOCYAN, DONONE); |
loopsva | 2:e7f3ba216a14 | 86 | } else { |
loopsva | 2:e7f3ba216a14 | 87 | pc.printf("%c", buffer[s + i + 8]); |
loopsva | 2:e7f3ba216a14 | 88 | } |
loopsva | 2:e7f3ba216a14 | 89 | } |
loopsva | 3:4a6123f2a4c9 | 90 | pc.printf("\r\n"); |
loopsva | 2:e7f3ba216a14 | 91 | } |
loopsva | 3:4a6123f2a4c9 | 92 | pc.printf("\r\n"); |
loopsva | 2:e7f3ba216a14 | 93 | return(0); |
loopsva | 2:e7f3ba216a14 | 94 | } |
loopsva | 2:e7f3ba216a14 | 95 | |
loopsva | 2:e7f3ba216a14 | 96 | //--------------------------------------------------------------------------------------------------------------------------------------// |
loopsva | 2:e7f3ba216a14 | 97 | // Used for doing a hex and ascii dump of the buffer. BufferLines tells PrintBuffer how |
loopsva | 2:e7f3ba216a14 | 98 | // many 16 byte lines to print. |
loopsva | 2:e7f3ba216a14 | 99 | |
loopsva | 2:e7f3ba216a14 | 100 | int PrintBuffer::dump_uc(const char* title, int BufferLines, int BufferOffset, const unsigned char buffer[]) { |
loopsva | 3:4a6123f2a4c9 | 101 | pc.printf("\n%s%s - lines: %d starting at: 0x%04x%s\r\n", DOMAGENTA, title, BufferLines, BufferOffset, DONONE); |
loopsva | 2:e7f3ba216a14 | 102 | for(int i = BufferOffset; i < BufferLines * 16 + BufferOffset; i = i + 16) { |
loopsva | 2:e7f3ba216a14 | 103 | pc.printf("%04x ", i); |
loopsva | 2:e7f3ba216a14 | 104 | for(int s = 0; s < 8; s++) { |
loopsva | 2:e7f3ba216a14 | 105 | pc.printf("%02x ", buffer [s + i]); |
loopsva | 2:e7f3ba216a14 | 106 | } |
loopsva | 2:e7f3ba216a14 | 107 | pc.printf(" "); |
loopsva | 2:e7f3ba216a14 | 108 | for(int s = 0; s < 8; s++) { |
loopsva | 2:e7f3ba216a14 | 109 | pc.printf("%02x ", buffer[s + i + 8]); |
loopsva | 2:e7f3ba216a14 | 110 | } |
loopsva | 2:e7f3ba216a14 | 111 | pc.printf(" "); |
loopsva | 2:e7f3ba216a14 | 112 | for(int s = 0; s < 8; s++) { |
loopsva | 2:e7f3ba216a14 | 113 | if((buffer[s + i] < 0x20) || (buffer[s + i] > 0x7f)) { |
loopsva | 2:e7f3ba216a14 | 114 | pc.printf("%s.%s", DOCYAN, DONONE); |
loopsva | 2:e7f3ba216a14 | 115 | } else { |
loopsva | 2:e7f3ba216a14 | 116 | pc.printf("%c", buffer[s + i]); |
loopsva | 2:e7f3ba216a14 | 117 | } |
loopsva | 2:e7f3ba216a14 | 118 | } |
loopsva | 2:e7f3ba216a14 | 119 | pc.printf(" "); |
loopsva | 2:e7f3ba216a14 | 120 | for(int s = 0; s < 8; s++) { |
loopsva | 2:e7f3ba216a14 | 121 | if((buffer[s + i + 8] < 0x20) || (buffer[s + i + 8] > 0x7f)) { |
loopsva | 2:e7f3ba216a14 | 122 | pc.printf("%s.%s", DOCYAN, DONONE); |
loopsva | 0:94d1683e78b7 | 123 | } else { |
loopsva | 0:94d1683e78b7 | 124 | pc.printf("%c", buffer[s + i + 8]); |
loopsva | 0:94d1683e78b7 | 125 | } |
loopsva | 0:94d1683e78b7 | 126 | } |
loopsva | 3:4a6123f2a4c9 | 127 | pc.printf("\r\n"); |
loopsva | 0:94d1683e78b7 | 128 | } |
loopsva | 3:4a6123f2a4c9 | 129 | pc.printf("\r\n"); |
loopsva | 0:94d1683e78b7 | 130 | return(0); |
loopsva | 0:94d1683e78b7 | 131 | } |
loopsva | 0:94d1683e78b7 | 132 | |
loopsva | 1:8073086de8b7 | 133 |