Browse Source

Support printing 64 bit integers

main
PaulStoffregen 4 years ago
parent
commit
e772699044
6 changed files with 123 additions and 1 deletions
  1. +33
    -0
      teensy/Print.cpp
  2. +9
    -0
      teensy/Print.h
  3. +32
    -0
      teensy3/Print.cpp
  4. +9
    -0
      teensy3/Print.h
  5. +31
    -1
      teensy4/Print.cpp
  6. +9
    -0
      teensy4/Print.h

+ 33
- 0
teensy/Print.cpp View File

@@ -353,6 +353,39 @@ size_t Print::printNumberAny(unsigned long n, uint8_t base)
return write(p, sizeof(buf) - (p - buf));
}

size_t Print::print(int64_t n)
{
if (n < 0) return printNumber64(-n, 10, 1);
return printNumber64(n, 10, 0);
}

size_t Print::printNumber64(uint64_t n, uint8_t base, uint8_t sign)
{
uint8_t buf[66];
uint8_t digit, i;

if (base < 2) return 0;
if (n == 0) {
buf[sizeof(buf) - 1] = '0';
i = sizeof(buf) - 1;
} else {
i = sizeof(buf) - 1;
while (1) {
digit = n % base;
buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
n /= base;
if (n == 0) break;
i--;
}
}
if (sign) {
i--;
buf[i] = '-';
}
return write(buf + i, sizeof(buf) - i);
}


size_t Print::printFloat(double number, uint8_t digits)
{
uint8_t sign=0;

+ 9
- 0
teensy/Print.h View File

@@ -60,12 +60,16 @@ class Print
size_t print(unsigned int n) { return printNumber(n, 0, 10); }
size_t print(long n);
size_t print(unsigned long n) { return printNumber(n, 0, 10); }
size_t print(int64_t n);
size_t print(uint64_t n) { return printNumber64(n, 10, 0); }

size_t print(unsigned char n, int base) { return printNumber(n, 0, base); }
size_t print(int n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
size_t print(unsigned int n, int base) { return printNumber(n, 0, base); }
size_t print(long n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
size_t print(unsigned long n, int base) { return printNumber(n, 0, base); }
size_t print(int64_t n, int base) { return (base == 10) ? print(n) : printNumber64(n, base, 0); }
size_t print(uint64_t n, int base) { return printNumber64(n, base, 0); }

size_t print(double n, int digits = 2) { return printFloat(n, digits); }
size_t print(const Printable &obj) { return obj.printTo(*this); }
@@ -80,12 +84,16 @@ class Print
size_t println(unsigned int n) { return print(n) + println(); }
size_t println(long n) { return print(n) + println(); }
size_t println(unsigned long n) { return print(n) + println(); }
size_t println(int64_t n) { return print(n) + println(); }
size_t println(uint64_t n) { return print(n) + println(); }

size_t println(unsigned char n, int base) { return print(n, base) + println(); }
size_t println(int n, int base) { return print(n, base) + println(); }
size_t println(unsigned int n, int base) { return print(n, base) + println(); }
size_t println(long n, int base) { return print(n, base) + println(); }
size_t println(unsigned long n, int base) { return print(n, base) + println(); }
size_t println(int64_t n, int base) { return print(n, base) + println(); }
size_t println(uint64_t n, int base) { return print(n, base) + println(); }

size_t println(double n, int digits = 2) { return print(n, digits) + println(); }
size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
@@ -111,6 +119,7 @@ class Print
if (base == 2) return printNumberBin(n);
return printNumberAny(n, base);
}
size_t printNumber64(uint64_t n, uint8_t base, uint8_t sign);
size_t printFloat(double n, uint8_t digits);
};


+ 32
- 0
teensy3/Print.cpp View File

@@ -287,6 +287,38 @@ size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)

#endif

size_t Print::print(int64_t n)
{
if (n < 0) return printNumber64(-n, 10, 1);
return printNumber64(n, 10, 0);
}

size_t Print::printNumber64(uint64_t n, uint8_t base, uint8_t sign)
{
uint8_t buf[66];
uint8_t digit, i;

if (base < 2) return 0;
if (n == 0) {
buf[sizeof(buf) - 1] = '0';
i = sizeof(buf) - 1;
} else {
i = sizeof(buf) - 1;
while (1) {
digit = n % base;
buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
n /= base;
if (n == 0) break;
i--;
}
}
if (sign) {
i--;
buf[i] = '-';
}
return write(buf + i, sizeof(buf) - i);
}

size_t Print::printFloat(double number, uint8_t digits)
{
uint8_t sign=0;

+ 9
- 0
teensy3/Print.h View File

@@ -71,12 +71,16 @@ class Print
size_t print(unsigned int n) { return printNumber(n, 10, 0); }
size_t print(long n);
size_t print(unsigned long n) { return printNumber(n, 10, 0); }
size_t print(int64_t n);
size_t print(uint64_t n) { return printNumber64(n, 10, 0); }

size_t print(unsigned char n, int base) { return printNumber(n, base, 0); }
size_t print(int n, int base) { return (base == 10) ? print(n) : printNumber(n, base, 0); }
size_t print(unsigned int n, int base) { return printNumber(n, base, 0); }
size_t print(long n, int base) { return (base == 10) ? print(n) : printNumber(n, base, 0); }
size_t print(unsigned long n, int base) { return printNumber(n, base, 0); }
size_t print(int64_t n, int base) { return (base == 10) ? print(n) : printNumber64(n, base, 0); }
size_t print(uint64_t n, int base) { return printNumber64(n, base, 0); }

size_t print(double n, int digits = 2) { return printFloat(n, digits); }
size_t print(const Printable &obj) { return obj.printTo(*this); }
@@ -91,12 +95,16 @@ class Print
size_t println(unsigned int n) { return print(n) + println(); }
size_t println(long n) { return print(n) + println(); }
size_t println(unsigned long n) { return print(n) + println(); }
size_t println(int64_t n) { return print(n) + println(); }
size_t println(uint64_t n) { return print(n) + println(); }

size_t println(unsigned char n, int base) { return print(n, base) + println(); }
size_t println(int n, int base) { return print(n, base) + println(); }
size_t println(unsigned int n, int base) { return print(n, base) + println(); }
size_t println(long n, int base) { return print(n, base) + println(); }
size_t println(unsigned long n, int base) { return print(n, base) + println(); }
size_t println(int64_t n, int base) { return print(n, base) + println(); }
size_t println(uint64_t n, int base) { return print(n, base) + println(); }

size_t println(double n, int digits = 2) { return print(n, digits) + println(); }
size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
@@ -128,6 +136,7 @@ class Print
#else
size_t printNumber(unsigned long n, uint8_t base, uint8_t sign);
#endif
size_t printNumber64(uint64_t n, uint8_t base, uint8_t sign);
};



+ 31
- 1
teensy4/Print.cpp View File

@@ -65,7 +65,6 @@ size_t Print::print(const String &s)
return count;
}


size_t Print::print(long n)
{
uint8_t sign=0;
@@ -77,6 +76,11 @@ size_t Print::print(long n)
return printNumber(n, 10, sign);
}

size_t Print::print(int64_t n)
{
if (n < 0) return printNumber64(-n, 10, 1);
return printNumber64(n, 10, 0);
}

size_t Print::println(void)
{
@@ -150,6 +154,32 @@ size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
return write(buf + i, sizeof(buf) - i);
}

size_t Print::printNumber64(uint64_t n, uint8_t base, uint8_t sign)
{
uint8_t buf[66];
uint8_t digit, i;

if (base < 2) return 0;
if (n == 0) {
buf[sizeof(buf) - 1] = '0';
i = sizeof(buf) - 1;
} else {
i = sizeof(buf) - 1;
while (1) {
digit = n % base;
buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
n /= base;
if (n == 0) break;
i--;
}
}
if (sign) {
i--;
buf[i] = '-';
}
return write(buf + i, sizeof(buf) - i);
}

size_t Print::printFloat(double number, uint8_t digits)
{
uint8_t sign=0;

+ 9
- 0
teensy4/Print.h View File

@@ -71,12 +71,16 @@ class Print
size_t print(unsigned int n) { return printNumber(n, 10, 0); }
size_t print(long n);
size_t print(unsigned long n) { return printNumber(n, 10, 0); }
size_t print(int64_t n);
size_t print(uint64_t n) { return printNumber64(n, 10, 0); }

size_t print(unsigned char n, int base) { return printNumber(n, base, 0); }
size_t print(int n, int base) { return (base == 10) ? print(n) : printNumber(n, base, 0); }
size_t print(unsigned int n, int base) { return printNumber(n, base, 0); }
size_t print(long n, int base) { return (base == 10) ? print(n) : printNumber(n, base, 0); }
size_t print(unsigned long n, int base) { return printNumber(n, base, 0); }
size_t print(int64_t n, int base) { return (base == 10) ? print(n) : printNumber64(n, base, 0); }
size_t print(uint64_t n, int base) { return printNumber64(n, base, 0); }

size_t print(double n, int digits = 2) { return printFloat(n, digits); }
size_t print(const Printable &obj) { return obj.printTo(*this); }
@@ -91,12 +95,16 @@ class Print
size_t println(unsigned int n) { return print(n) + println(); }
size_t println(long n) { return print(n) + println(); }
size_t println(unsigned long n) { return print(n) + println(); }
size_t println(int64_t n) { return print(n) + println(); }
size_t println(uint64_t n) { return print(n) + println(); }

size_t println(unsigned char n, int base) { return print(n, base) + println(); }
size_t println(int n, int base) { return print(n, base) + println(); }
size_t println(unsigned int n, int base) { return print(n, base) + println(); }
size_t println(long n, int base) { return print(n, base) + println(); }
size_t println(unsigned long n, int base) { return print(n, base) + println(); }
size_t println(int64_t n, int base) { return print(n, base) + println(); }
size_t println(uint64_t n, int base) { return print(n, base) + println(); }

size_t println(double n, int digits = 2) { return print(n, digits) + println(); }
size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
@@ -112,6 +120,7 @@ class Print
char write_error;
size_t printFloat(double n, uint8_t digits);
size_t printNumber(unsigned long n, uint8_t base, uint8_t sign);
size_t printNumber64(uint64_t n, uint8_t base, uint8_t sign);
};



Loading…
Cancel
Save