|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
-
-
-
-
-
-
-
-
- #include <Arduino.h>
-
-
- size_t Print::write(const uint8_t *buffer, size_t size)
- {
- if (buffer == nullptr) return 0;
- size_t count = 0;
- while (size--) count += write(*buffer++);
- return count;
- }
-
-
- size_t Print::print(const String &s)
- {
- uint8_t buffer[33];
- size_t count = 0;
- unsigned int index = 0;
- unsigned int len = s.length();
- while (len > 0) {
- s.getBytes(buffer, sizeof(buffer), index);
- unsigned int nbytes = len;
- if (nbytes > sizeof(buffer)-1) nbytes = sizeof(buffer)-1;
- index += nbytes;
- len -= nbytes;
- count += write(buffer, nbytes);
- }
- return count;
- }
-
-
- size_t Print::print(long n)
- {
- uint8_t sign=0;
-
- if (n < 0) {
- sign = '-';
- n = -n;
- }
- return printNumber(n, 10, sign);
- }
-
-
- size_t Print::println(void)
- {
- uint8_t buf[2]={'\r', '\n'};
- return write(buf, 2);
- }
-
- extern "C" {
- __attribute__((weak))
- int _write(int file, char *ptr, int len)
- {
- ((class Print *)file)->write((uint8_t *)ptr, len);
- return len;
- }
- }
-
- int Print::printf(const char *format, ...)
- {
- va_list ap;
- va_start(ap, format);
- #ifdef __STRICT_ANSI__
- return 0;
- #else
- return vdprintf((int)this, format, ap);
- #endif
- }
-
- int Print::printf(const __FlashStringHelper *format, ...)
- {
- va_list ap;
- va_start(ap, format);
- #ifdef __STRICT_ANSI__
- return 0;
- #else
- return vdprintf((int)this, (const char *)format, ap);
- #endif
- }
-
- #ifdef __MKL26Z64__
-
-
-
-
-
-
-
-
-
-
- #if 1
-
- void inline divmod10_v2(uint32_t n,uint32_t *div,uint32_t *mod) {
- uint32_t p,q;
-
-
- q = (n&0xFFFF) + 1;
- p = (n>>16);
-
- q = 13107*q;
- p = 13107*p;
-
- q = q + (q>>16) + (p&0xFFFF);
- p = p + (p>>16) + (q>>16);
-
- p = p>>1;
- *div = p;
- *mod = n-10*p;
- }
- #define divmod10_asm(div, mod, tmp1, tmp2, const3333) \
- divmod10_v2(div, &div, &mod);
-
- #else
- #define divmod10_asm(_div, _mod, _tmp1, _tmp2, _const3333) \
- ({ _tmp1 = _div; _div = _div / 10; _mod = _tmp1 - _div * 10; })
-
- #endif
-
-
- size_t Print::printNumberDec(unsigned long n, uint8_t sign)
- {
- uint8_t buf[11], *p;
- uint32_t digit;
-
-
- p = buf + (sizeof(buf));
- do {
- uint32_t div;
- divmod10_v2(n, &div, &digit);
- n = div;
-
- *--p = digit + '0';
- } while (n);
- if (sign) *--p = '-';
- return write(p, sizeof(buf) - (p - buf));
- }
-
- size_t Print::printNumberHex(unsigned long n)
- {
- uint8_t digit, buf[8], *p;
-
- p = buf + (sizeof(buf));
- do {
- digit = n & 15;
- *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
- n >>= 4;
- } while (n);
- return write(p, sizeof(buf) - (p - buf));
- }
-
- size_t Print::printNumberBin(unsigned long n)
- {
- uint8_t buf[32], *p;
-
- p = buf + (sizeof(buf));
- do {
- *--p = '0' + ((uint8_t)n & 1);
- n >>= 1;
- } while (n);
- return write(p, sizeof(buf) - (p - buf));
- }
-
- size_t Print::printNumberAny(unsigned long n, uint8_t base)
- {
- uint8_t digit, buf[21], *p;
- uint32_t tmp;
-
- p = buf + sizeof(buf);
- do {
- tmp = n;
- n = n / base;
- digit = tmp - n * base;
- *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10;
- } while (n);
- return write(p, sizeof(buf) - (p - buf));
- }
-
- #else
-
- size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign)
- {
- uint8_t buf[34];
- uint8_t digit, i;
-
-
-
-
- if (base == 0) {
- return write((uint8_t)n);
- } else if (base == 1) {
- base = 10;
- }
-
-
- 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);
- }
-
- #endif
-
- size_t Print::printFloat(double number, uint8_t digits)
- {
- uint8_t sign=0;
- size_t count=0;
-
- if (isnan(number)) return print("nan");
- if (isinf(number)) return print("inf");
- if (number > 4294967040.0f) return print("ovf");
- if (number <-4294967040.0f) return print("ovf");
-
-
- if (number < 0.0) {
- sign = 1;
- number = -number;
- }
-
-
- double rounding = 0.5;
- for (uint8_t i=0; i<digits; ++i) {
- rounding *= 0.1;
- }
- number += rounding;
-
-
- unsigned long int_part = (unsigned long)number;
- double remainder = number - (double)int_part;
- count += printNumber(int_part, 10, sign);
-
-
- if (digits > 0) {
- uint8_t n, buf[16], count=1;
- buf[0] = '.';
-
-
- if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1;
-
- while (digits-- > 0) {
- remainder *= 10.0;
- n = (uint8_t)(remainder);
- buf[count++] = '0' + n;
- remainder -= n;
- }
- count += write(buf, count);
- }
- return count;
- }
-
-
|