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

Commit 2c898ae

Browse files
Marten Jungastrongly-typed
authored andcommitted
[fix][io] Fixed uint64_t in logstream.
Before, only the lower 32Bit of an uint64_t were shown in logstream.
1 parent b5540e5 commit 2c898ae

4 files changed

Lines changed: 92 additions & 4 deletions

File tree

src/xpcc/io/iostream.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class IOStream
300300

301301
// The 64-bit types on the AVR are extremely slow and are
302302
// therefore excluded here
303-
#if !defined(XPCC__CPU_AVR)
303+
#if not defined(XPCC__CPU_AVR)
304304
xpcc_always_inline IOStream&
305305
operator << (const uint64_t& v)
306306
{
@@ -502,13 +502,18 @@ class IOStream
502502
void
503503
writeFloat(const float& value);
504504

505-
#if !defined(XPCC__CPU_AVR)
505+
#if not defined(XPCC__CPU_AVR)
506506
void
507507
writeDouble(const double& value);
508508
#endif
509509

510-
void
511-
writeUnsignedInteger(unsigned long unsignedValue, uint_fast8_t base, size_t width, char fill, bool isNegative);
510+
void
511+
writeUnsignedInteger(unsigned long unsignedValue, uint_fast8_t base, size_t width, char fill, bool isNegative);
512+
#if not defined(XPCC__CPU_AVR)
513+
void
514+
writeUnsignedLongLong(unsigned long long unsignedValue, uint_fast8_t base, size_t width, char fill, bool isNegative);
515+
#endif
516+
512517

513518
private:
514519
enum class

src/xpcc/io/iostream_printf.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ xpcc::IOStream::vprintf(const char *fmt, va_list ap)
167167
// Print fractional part
168168
writeUnsignedInteger((unsigned int)float_value, base, width_frac, '0', false);
169169
}
170+
#if not defined(XPCC__CPU_AVR)
171+
else if (isLongLong)
172+
{
173+
long long signedValue = va_arg(ap, long long);
174+
if (isSigned)
175+
{
176+
if (signedValue < 0)
177+
{
178+
isNegative = true;
179+
signedValue = -signedValue; // make it positive
180+
}
181+
}
182+
writeUnsignedLongLong((unsigned long long) signedValue, base, width, fill, isNegative);
183+
}
184+
#endif
170185
else
171186
{
172187
unsigned long unsignedValue;
@@ -247,3 +262,51 @@ xpcc::IOStream::writeUnsignedInteger(
247262
this->device->write(ch);
248263
}
249264
}
265+
266+
#if not defined(XPCC__CPU_AVR)
267+
void
268+
xpcc::IOStream::writeUnsignedLongLong(
269+
unsigned long long unsignedValue, uint_fast8_t base,
270+
size_t width, char fill, bool isNegative)
271+
{
272+
char scratch[26];
273+
274+
char *ptr = scratch + sizeof(scratch);
275+
*--ptr = 0;
276+
do
277+
{
278+
char ch = (unsignedValue % base) + '0';
279+
280+
if (ch > '9') {
281+
ch += 'A' - '9' - 1;
282+
}
283+
284+
*--ptr = ch;
285+
unsignedValue /= base;
286+
287+
if (width) {
288+
--width;
289+
}
290+
} while (unsignedValue);
291+
292+
// Insert minus sign if needed
293+
if (isNegative)
294+
{
295+
*--ptr = '-';
296+
if (width) {
297+
--width;
298+
}
299+
}
300+
301+
// insert padding chars
302+
while (width--) {
303+
*--ptr = fill;
304+
}
305+
306+
// output result
307+
char ch;
308+
while ((ch = *ptr++)) {
309+
this->device->write(ch);
310+
}
311+
}
312+
#endif

src/xpcc/io/test/io_stream_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,23 @@ IoStreamTest::testPrintf2()
520520
#endif
521521
}
522522

523+
void
524+
IoStreamTest::testPrintf3()
525+
{
526+
#if not defined(XPCC__CPU_AVR)
527+
// Test for 64 bit uints and ints on printf
528+
unsigned long long unsignedlonglong = 0xFEDCBA9876543210;
529+
(*stream).printf("%llx", unsignedlonglong);
530+
TEST_ASSERT_EQUALS_ARRAY("FEDCBA9876543210", device.buffer, 16);
531+
(*stream).flush();
532+
533+
long long longlong = -9223372036854775806;
534+
(*stream).printf("%lld", longlong);
535+
TEST_ASSERT_EQUALS_ARRAY("-9223372036854775806", device.buffer, 20);
536+
(*stream).flush();
537+
#endif
538+
}
539+
523540
int myFunc1(void) { return -1; };
524541
int myFunc2(void) { return -1; };
525542

src/xpcc/io/test/io_stream_test.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ class IoStreamTest : public unittest::TestSuite
127127
void
128128
testPrintf2();
129129

130+
void
131+
testPrintf3();
132+
130133
void
131134
testFp();
132135

0 commit comments

Comments
 (0)