Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Commit

Permalink
[fix][io] Fixed uint64_t in logstream.
Browse files Browse the repository at this point in the history
Before, only the lower 32Bit of an uint64_t were shown in logstream.
  • Loading branch information
Marten Junga authored and strongly-typed committed Dec 18, 2017
1 parent b5540e5 commit 2c898ae
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/xpcc/io/iostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class IOStream

// The 64-bit types on the AVR are extremely slow and are
// therefore excluded here
#if !defined(XPCC__CPU_AVR)
#if not defined(XPCC__CPU_AVR)
xpcc_always_inline IOStream&
operator << (const uint64_t& v)
{
Expand Down Expand Up @@ -502,13 +502,18 @@ class IOStream
void
writeFloat(const float& value);

#if !defined(XPCC__CPU_AVR)
#if not defined(XPCC__CPU_AVR)
void
writeDouble(const double& value);
#endif

void
writeUnsignedInteger(unsigned long unsignedValue, uint_fast8_t base, size_t width, char fill, bool isNegative);
void
writeUnsignedInteger(unsigned long unsignedValue, uint_fast8_t base, size_t width, char fill, bool isNegative);
#if not defined(XPCC__CPU_AVR)
void
writeUnsignedLongLong(unsigned long long unsignedValue, uint_fast8_t base, size_t width, char fill, bool isNegative);
#endif


private:
enum class
Expand Down
63 changes: 63 additions & 0 deletions src/xpcc/io/iostream_printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ xpcc::IOStream::vprintf(const char *fmt, va_list ap)
// Print fractional part
writeUnsignedInteger((unsigned int)float_value, base, width_frac, '0', false);
}
#if not defined(XPCC__CPU_AVR)
else if (isLongLong)
{
long long signedValue = va_arg(ap, long long);
if (isSigned)
{
if (signedValue < 0)
{
isNegative = true;
signedValue = -signedValue; // make it positive
}
}
writeUnsignedLongLong((unsigned long long) signedValue, base, width, fill, isNegative);
}
#endif
else
{
unsigned long unsignedValue;
Expand Down Expand Up @@ -247,3 +262,51 @@ xpcc::IOStream::writeUnsignedInteger(
this->device->write(ch);
}
}

#if not defined(XPCC__CPU_AVR)
void
xpcc::IOStream::writeUnsignedLongLong(
unsigned long long unsignedValue, uint_fast8_t base,
size_t width, char fill, bool isNegative)
{
char scratch[26];

char *ptr = scratch + sizeof(scratch);
*--ptr = 0;
do
{
char ch = (unsignedValue % base) + '0';

if (ch > '9') {
ch += 'A' - '9' - 1;
}

*--ptr = ch;
unsignedValue /= base;

if (width) {
--width;
}
} while (unsignedValue);

// Insert minus sign if needed
if (isNegative)
{
*--ptr = '-';
if (width) {
--width;
}
}

// insert padding chars
while (width--) {
*--ptr = fill;
}

// output result
char ch;
while ((ch = *ptr++)) {
this->device->write(ch);
}
}
#endif
17 changes: 17 additions & 0 deletions src/xpcc/io/test/io_stream_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,23 @@ IoStreamTest::testPrintf2()
#endif
}

void
IoStreamTest::testPrintf3()
{
#if not defined(XPCC__CPU_AVR)
// Test for 64 bit uints and ints on printf
unsigned long long unsignedlonglong = 0xFEDCBA9876543210;
(*stream).printf("%llx", unsignedlonglong);
TEST_ASSERT_EQUALS_ARRAY("FEDCBA9876543210", device.buffer, 16);
(*stream).flush();

long long longlong = -9223372036854775806;
(*stream).printf("%lld", longlong);
TEST_ASSERT_EQUALS_ARRAY("-9223372036854775806", device.buffer, 20);
(*stream).flush();
#endif
}

int myFunc1(void) { return -1; };
int myFunc2(void) { return -1; };

Expand Down
3 changes: 3 additions & 0 deletions src/xpcc/io/test/io_stream_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ class IoStreamTest : public unittest::TestSuite
void
testPrintf2();

void
testPrintf3();

void
testFp();

Expand Down

0 comments on commit 2c898ae

Please sign in to comment.