Skip to content

Commit

Permalink
Merge pull request #117 from irarykim/feature-enhance-printf()
Browse files Browse the repository at this point in the history
add %x, %X to printf() (second PR)
  • Loading branch information
jamesadevine authored Mar 12, 2020
2 parents ccf409c + 67cf54c commit 5357bc5
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion source/driver-models/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ void Serial::printf(const char* format, ...)
char* str = (char *)((void *)val);
char* buffPtr = buff;
char c = 0;
bool firstDigitFound = false;
bool lowerCase = false;
switch (*end++)
{

Expand All @@ -467,8 +469,29 @@ void Serial::printf(const char* format, ...)
break;

case 'x':
case 'p':
lowerCase = true;
// fall through
case 'X':
for (uint8_t i = 8; i > 0; i--)
{
uint8_t digit = ((uint8_t) (val >> ((i - 1) * 4)) & 0x0f) + (uint8_t) '0';
if (digit > '9')
{
if (lowerCase)
digit += 39;
else
digit += 7;
}
if (digit != '0')
{
putc((char)digit);
firstDigitFound = true;
}
else if (firstDigitFound || i == 1)
putc((char)digit);
}
break;
case 'p':
default:
putc('?');
putc('?');
Expand Down

0 comments on commit 5357bc5

Please sign in to comment.