-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialHexTools.cpp
116 lines (103 loc) · 2.8 KB
/
SerialHexTools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#if !defined(__AVR_ATtiny85__)
#include <Arduino.h>
#if defined(__AVR__)
#include <EEPROM.h>
#endif
#include "SerialHexTools.h"
/*--------------------------------------------------------------*/
void hexdumpResetPositionCount()
{
hexdumpPositionCount = 0;
}
/*--------------------------------------------------------------*/
// just print a byte to the serial console (with leading zero)
void printHexToSerial( uint8_t value, bool addComma )
{
static uint8_t count = 0;
Serial.print( F("0x") );
if ( value < 0x10 )
{
Serial.print( F("0") );
}
Serial.print( value, HEX );
if ( addComma )
{
Serial.print( F(", ") );
}
// increase count
hexdumpPositionCount++;
// maximum reached?
if ( hexdumpPositionCount >= hexdumpValuesPerLine )
{
// reset count
hexdumpPositionCount = 0;
// insert line break
Serial.println();
}
}
/*--------------------------------------------------------------*/
// simple hexdump from RAM
void hexdumpToSerial( uint8_t *pData, uint16_t byteCount, bool finalComma, bool finalLinebreak )
{
for ( uint16_t n = 0; n < byteCount; n++ )
{
printHexToSerial( pData[n], ( n < byteCount - 1 ) || finalComma );
}
// insert line break if necessary
if ( finalLinebreak )
{
Serial.println();
}
}
#if defined(__AVR__)
/*--------------------------------------------------------------*/
// simple hexdump from EEPROM
void EEPROM_hexdumpToSerial( uint16_t startAddress, uint16_t byteCount, bool finalComma, bool finalLinebreak )
{
for ( uint16_t n = 0; n < byteCount; n++ )
{
printHexToSerial( EEPROM.read( startAddress + n ), ( n < byteCount - 1 ) || finalComma );
}
// insert line break if necessary
if ( finalLinebreak )
{
Serial.println();
}
}
#endif
/*--------------------------------------------------------------*/
// simple hexdump from PROGMEM
void pgm_hexdumpToSerial( uint8_t *pData, uint16_t byteCount, bool finalComma, bool finalLinebreak )
{
for ( uint16_t n = 0; n < byteCount; n++ )
{
printHexToSerial( pgm_read_byte( pData + n ), ( n < byteCount - 1 ) || finalComma );
}
// insert line break if necessary
if ( finalLinebreak )
{
Serial.println();
}
}
/*--------------------------------------------------------------*/
// print rotated 1 bit image buffer to serial as an 8 bit hexdump
void printScreenBufferToSerial( const uint8_t *buffer, const uint8_t widthInPixels, const uint8_t heightInBytes )
{
for ( int x = 0; x < widthInPixels; x++ )
{
for ( int y = 0; y < heightInBytes; y++ )
{
uint8_t value = buffer[y * widthInPixels + x];
// now convert to 8bpp
for ( int n = 0; n < 8; n++ )
{
if ( value & ( 1 << n ) )
{ Serial.print( F("0xFF,") ); }
else
{ Serial.print( F("0x00,") ); }
}
}
Serial.println();
}
}
#endif