Browse Source

Support printing 64 bit integers

teensy4-core
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

return write(p, sizeof(buf) - (p - buf)); 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) size_t Print::printFloat(double number, uint8_t digits)
{ {
uint8_t sign=0; uint8_t sign=0;

+ 9
- 0
teensy/Print.h View File

size_t print(unsigned int n) { return printNumber(n, 0, 10); } size_t print(unsigned int n) { return printNumber(n, 0, 10); }
size_t print(long n); size_t print(long n);
size_t print(unsigned long n) { return printNumber(n, 0, 10); } 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(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(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(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(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(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(double n, int digits = 2) { return printFloat(n, digits); }
size_t print(const Printable &obj) { return obj.printTo(*this); } size_t print(const Printable &obj) { return obj.printTo(*this); }
size_t println(unsigned int n) { return print(n) + println(); } size_t println(unsigned int n) { return print(n) + println(); }
size_t println(long 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(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(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(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(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(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(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(double n, int digits = 2) { return print(n, digits) + println(); }
size_t println(const Printable &obj) { return obj.printTo(*this) + println(); } size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
if (base == 2) return printNumberBin(n); if (base == 2) return printNumberBin(n);
return printNumberAny(n, base); return printNumberAny(n, base);
} }
size_t printNumber64(uint64_t n, uint8_t base, uint8_t sign);
size_t printFloat(double n, uint8_t digits); size_t printFloat(double n, uint8_t digits);
}; };



+ 32
- 0
teensy3/Print.cpp View File



#endif #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) size_t Print::printFloat(double number, uint8_t digits)
{ {
uint8_t sign=0; uint8_t sign=0;

+ 9
- 0
teensy3/Print.h View File

size_t print(unsigned int n) { return printNumber(n, 10, 0); } size_t print(unsigned int n) { return printNumber(n, 10, 0); }
size_t print(long n); size_t print(long n);
size_t print(unsigned long n) { return printNumber(n, 10, 0); } 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(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(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(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(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(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(double n, int digits = 2) { return printFloat(n, digits); }
size_t print(const Printable &obj) { return obj.printTo(*this); } size_t print(const Printable &obj) { return obj.printTo(*this); }
size_t println(unsigned int n) { return print(n) + println(); } size_t println(unsigned int n) { return print(n) + println(); }
size_t println(long 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(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(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(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(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(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(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(double n, int digits = 2) { return print(n, digits) + println(); }
size_t println(const Printable &obj) { return obj.printTo(*this) + println(); } size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
#else #else
size_t printNumber(unsigned long n, uint8_t base, uint8_t sign); size_t printNumber(unsigned long n, uint8_t base, uint8_t sign);
#endif #endif
size_t printNumber64(uint64_t n, uint8_t base, uint8_t sign);
}; };





+ 31
- 1
teensy4/Print.cpp View File

return count; return count;
} }



size_t Print::print(long n) size_t Print::print(long n)
{ {
uint8_t sign=0; uint8_t sign=0;
return printNumber(n, 10, sign); 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) size_t Print::println(void)
{ {
return write(buf + i, sizeof(buf) - i); 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) size_t Print::printFloat(double number, uint8_t digits)
{ {
uint8_t sign=0; uint8_t sign=0;

+ 9
- 0
teensy4/Print.h View File

size_t print(unsigned int n) { return printNumber(n, 10, 0); } size_t print(unsigned int n) { return printNumber(n, 10, 0); }
size_t print(long n); size_t print(long n);
size_t print(unsigned long n) { return printNumber(n, 10, 0); } 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(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(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(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(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(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(double n, int digits = 2) { return printFloat(n, digits); }
size_t print(const Printable &obj) { return obj.printTo(*this); } size_t print(const Printable &obj) { return obj.printTo(*this); }
size_t println(unsigned int n) { return print(n) + println(); } size_t println(unsigned int n) { return print(n) + println(); }
size_t println(long 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(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(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(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(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(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(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(double n, int digits = 2) { return print(n, digits) + println(); }
size_t println(const Printable &obj) { return obj.printTo(*this) + println(); } size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
char write_error; char write_error;
size_t printFloat(double n, uint8_t digits); size_t printFloat(double n, uint8_t digits);
size_t printNumber(unsigned long n, uint8_t base, uint8_t sign); 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