work.

Dependencies:   Blynk mbed

Committer:
lixianyu
Date:
Thu Jun 16 08:12:33 2016 +0000
Revision:
4:e5018e5ba340
Parent:
0:d8f4c441e032
ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:d8f4c441e032 1 /*
lixianyu 0:d8f4c441e032 2 Print.cpp - Base class that provides print() and println()
lixianyu 0:d8f4c441e032 3 Copyright (c) 2008 David A. Mellis. All right reserved.
lixianyu 0:d8f4c441e032 4
lixianyu 0:d8f4c441e032 5 This library is free software; you can redistribute it and/or
lixianyu 0:d8f4c441e032 6 modify it under the terms of the GNU Lesser General Public
lixianyu 0:d8f4c441e032 7 License as published by the Free Software Foundation; either
lixianyu 0:d8f4c441e032 8 version 2.1 of the License, or (at your option) any later version.
lixianyu 0:d8f4c441e032 9
lixianyu 0:d8f4c441e032 10 This library is distributed in the hope that it will be useful,
lixianyu 0:d8f4c441e032 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
lixianyu 0:d8f4c441e032 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lixianyu 0:d8f4c441e032 13 Lesser General Public License for more details.
lixianyu 0:d8f4c441e032 14
lixianyu 0:d8f4c441e032 15 You should have received a copy of the GNU Lesser General Public
lixianyu 0:d8f4c441e032 16 License along with this library; if not, write to the Free Software
lixianyu 0:d8f4c441e032 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lixianyu 0:d8f4c441e032 18
lixianyu 0:d8f4c441e032 19 Modified 23 November 2006 by David A. Mellis
lixianyu 0:d8f4c441e032 20 Modified 03 August 2015 by Chuck Todd
lixianyu 0:d8f4c441e032 21 */
lixianyu 0:d8f4c441e032 22
lixianyu 0:d8f4c441e032 23 //#include <stdlib.h>
lixianyu 0:d8f4c441e032 24 //#include <stdio.h>
lixianyu 0:d8f4c441e032 25 //#include <string.h>
lixianyu 0:d8f4c441e032 26 #include <math.h>
lixianyu 0:d8f4c441e032 27 //#include "Arduino.h"
lixianyu 0:d8f4c441e032 28
lixianyu 0:d8f4c441e032 29 #include "Printit.h"
lixianyu 0:d8f4c441e032 30
lixianyu 0:d8f4c441e032 31 // Public Methods //////////////////////////////////////////////////////////////
lixianyu 0:d8f4c441e032 32
lixianyu 0:d8f4c441e032 33 /* default implementation: may be overridden */
lixianyu 0:d8f4c441e032 34 size_t Print::write(const uint8_t *buffer, size_t size)
lixianyu 0:d8f4c441e032 35 {
lixianyu 0:d8f4c441e032 36 size_t n = 0;
lixianyu 0:d8f4c441e032 37 while (size--) {
lixianyu 0:d8f4c441e032 38 if (write(*buffer++)) n++;
lixianyu 0:d8f4c441e032 39 else break;
lixianyu 0:d8f4c441e032 40 }
lixianyu 0:d8f4c441e032 41 return n;
lixianyu 0:d8f4c441e032 42 }
lixianyu 0:d8f4c441e032 43
lixianyu 0:d8f4c441e032 44 #if 0
lixianyu 0:d8f4c441e032 45 size_t Print::print(const __FlashStringHelper *ifsh)
lixianyu 0:d8f4c441e032 46 {
lixianyu 0:d8f4c441e032 47 PGM_P p = reinterpret_cast<PGM_P>(ifsh);
lixianyu 0:d8f4c441e032 48 size_t n = 0;
lixianyu 0:d8f4c441e032 49 while (1) {
lixianyu 0:d8f4c441e032 50 unsigned char c = pgm_read_byte(p++);
lixianyu 0:d8f4c441e032 51 if (c == 0) break;
lixianyu 0:d8f4c441e032 52 if (write(c)) n++;
lixianyu 0:d8f4c441e032 53 else break;
lixianyu 0:d8f4c441e032 54 }
lixianyu 0:d8f4c441e032 55 return n;
lixianyu 0:d8f4c441e032 56 }
lixianyu 0:d8f4c441e032 57
lixianyu 0:d8f4c441e032 58 size_t Print::print(const String &s)
lixianyu 0:d8f4c441e032 59 {
lixianyu 0:d8f4c441e032 60 return write(s.c_str(), s.length());
lixianyu 0:d8f4c441e032 61 }
lixianyu 0:d8f4c441e032 62 #endif
lixianyu 0:d8f4c441e032 63
lixianyu 0:d8f4c441e032 64 size_t Print::print(char *str)
lixianyu 0:d8f4c441e032 65 {
lixianyu 0:d8f4c441e032 66 return write(str);
lixianyu 0:d8f4c441e032 67 }
lixianyu 0:d8f4c441e032 68
lixianyu 0:d8f4c441e032 69 size_t Print::print(char c)
lixianyu 0:d8f4c441e032 70 {
lixianyu 0:d8f4c441e032 71 return write(c);
lixianyu 0:d8f4c441e032 72 }
lixianyu 0:d8f4c441e032 73
lixianyu 0:d8f4c441e032 74 size_t Print::print(unsigned char b, int base)
lixianyu 0:d8f4c441e032 75 {
lixianyu 0:d8f4c441e032 76 return print((unsigned long) b, base);
lixianyu 0:d8f4c441e032 77 }
lixianyu 0:d8f4c441e032 78
lixianyu 0:d8f4c441e032 79 size_t Print::print(int n, int base)
lixianyu 0:d8f4c441e032 80 {
lixianyu 0:d8f4c441e032 81 return print((long) n, base);
lixianyu 0:d8f4c441e032 82 }
lixianyu 0:d8f4c441e032 83
lixianyu 0:d8f4c441e032 84 size_t Print::print(unsigned int n, int base)
lixianyu 0:d8f4c441e032 85 {
lixianyu 0:d8f4c441e032 86 return print((unsigned long) n, base);
lixianyu 0:d8f4c441e032 87 }
lixianyu 0:d8f4c441e032 88
lixianyu 0:d8f4c441e032 89 size_t Print::print(long n, int base)
lixianyu 0:d8f4c441e032 90 {
lixianyu 0:d8f4c441e032 91 if (base == 0) {
lixianyu 0:d8f4c441e032 92 return write(n);
lixianyu 0:d8f4c441e032 93 } else if (base == 10) {
lixianyu 0:d8f4c441e032 94 if (n < 0) {
lixianyu 0:d8f4c441e032 95 int t = print('-');
lixianyu 0:d8f4c441e032 96 n = -n;
lixianyu 0:d8f4c441e032 97 return printNumber(n, 10) + t;
lixianyu 0:d8f4c441e032 98 }
lixianyu 0:d8f4c441e032 99 return printNumber(n, 10);
lixianyu 0:d8f4c441e032 100 } else {
lixianyu 0:d8f4c441e032 101 return printNumber(n, base);
lixianyu 0:d8f4c441e032 102 }
lixianyu 0:d8f4c441e032 103 }
lixianyu 0:d8f4c441e032 104
lixianyu 0:d8f4c441e032 105 size_t Print::print(unsigned long n, int base)
lixianyu 0:d8f4c441e032 106 {
lixianyu 0:d8f4c441e032 107 if (base == 0) return write(n);
lixianyu 0:d8f4c441e032 108 else return printNumber(n, base);
lixianyu 0:d8f4c441e032 109 }
lixianyu 0:d8f4c441e032 110
lixianyu 0:d8f4c441e032 111 size_t Print::print(double n, int digits)
lixianyu 0:d8f4c441e032 112 {
lixianyu 0:d8f4c441e032 113 return printFloat(n, digits);
lixianyu 0:d8f4c441e032 114 }
lixianyu 0:d8f4c441e032 115
lixianyu 0:d8f4c441e032 116 #if 0
lixianyu 0:d8f4c441e032 117 size_t Print::println(const __FlashStringHelper *ifsh)
lixianyu 0:d8f4c441e032 118 {
lixianyu 0:d8f4c441e032 119 size_t n = print(ifsh);
lixianyu 0:d8f4c441e032 120 n += println();
lixianyu 0:d8f4c441e032 121 return n;
lixianyu 0:d8f4c441e032 122 }
lixianyu 0:d8f4c441e032 123 #endif
lixianyu 0:d8f4c441e032 124
lixianyu 0:d8f4c441e032 125 size_t Print::print(const Printable& x)
lixianyu 0:d8f4c441e032 126 {
lixianyu 0:d8f4c441e032 127 return x.printTo(*this);
lixianyu 0:d8f4c441e032 128 }
lixianyu 0:d8f4c441e032 129
lixianyu 0:d8f4c441e032 130 size_t Print::println(void)
lixianyu 0:d8f4c441e032 131 {
lixianyu 0:d8f4c441e032 132 return write("\r\n");
lixianyu 0:d8f4c441e032 133 }
lixianyu 0:d8f4c441e032 134
lixianyu 0:d8f4c441e032 135 #if 0
lixianyu 0:d8f4c441e032 136 size_t Print::println(const String &s)
lixianyu 0:d8f4c441e032 137 {
lixianyu 0:d8f4c441e032 138 size_t n = print(s);
lixianyu 0:d8f4c441e032 139 n += println();
lixianyu 0:d8f4c441e032 140 return n;
lixianyu 0:d8f4c441e032 141 }
lixianyu 0:d8f4c441e032 142 #endif
lixianyu 0:d8f4c441e032 143
lixianyu 0:d8f4c441e032 144 size_t Print::println(char *c)
lixianyu 0:d8f4c441e032 145 {
lixianyu 0:d8f4c441e032 146 size_t n = print(c);
lixianyu 0:d8f4c441e032 147 n += println();
lixianyu 0:d8f4c441e032 148 return n;
lixianyu 0:d8f4c441e032 149 }
lixianyu 0:d8f4c441e032 150
lixianyu 0:d8f4c441e032 151 size_t Print::println(char c)
lixianyu 0:d8f4c441e032 152 {
lixianyu 0:d8f4c441e032 153 size_t n = print(c);
lixianyu 0:d8f4c441e032 154 n += println();
lixianyu 0:d8f4c441e032 155 return n;
lixianyu 0:d8f4c441e032 156 }
lixianyu 0:d8f4c441e032 157
lixianyu 0:d8f4c441e032 158 size_t Print::println(unsigned char b, int base)
lixianyu 0:d8f4c441e032 159 {
lixianyu 0:d8f4c441e032 160 size_t n = print(b, base);
lixianyu 0:d8f4c441e032 161 n += println();
lixianyu 0:d8f4c441e032 162 return n;
lixianyu 0:d8f4c441e032 163 }
lixianyu 0:d8f4c441e032 164
lixianyu 0:d8f4c441e032 165 size_t Print::println(int num, int base)
lixianyu 0:d8f4c441e032 166 {
lixianyu 0:d8f4c441e032 167 size_t n = print(num, base);
lixianyu 0:d8f4c441e032 168 n += println();
lixianyu 0:d8f4c441e032 169 return n;
lixianyu 0:d8f4c441e032 170 }
lixianyu 0:d8f4c441e032 171
lixianyu 0:d8f4c441e032 172 size_t Print::println(unsigned int num, int base)
lixianyu 0:d8f4c441e032 173 {
lixianyu 0:d8f4c441e032 174 size_t n = print(num, base);
lixianyu 0:d8f4c441e032 175 n += println();
lixianyu 0:d8f4c441e032 176 return n;
lixianyu 0:d8f4c441e032 177 }
lixianyu 0:d8f4c441e032 178
lixianyu 0:d8f4c441e032 179 size_t Print::println(long num, int base)
lixianyu 0:d8f4c441e032 180 {
lixianyu 0:d8f4c441e032 181 size_t n = print(num, base);
lixianyu 0:d8f4c441e032 182 n += println();
lixianyu 0:d8f4c441e032 183 return n;
lixianyu 0:d8f4c441e032 184 }
lixianyu 0:d8f4c441e032 185
lixianyu 0:d8f4c441e032 186 size_t Print::println(unsigned long num, int base)
lixianyu 0:d8f4c441e032 187 {
lixianyu 0:d8f4c441e032 188 size_t n = print(num, base);
lixianyu 0:d8f4c441e032 189 n += println();
lixianyu 0:d8f4c441e032 190 return n;
lixianyu 0:d8f4c441e032 191 }
lixianyu 0:d8f4c441e032 192
lixianyu 0:d8f4c441e032 193 size_t Print::println(double num, int digits)
lixianyu 0:d8f4c441e032 194 {
lixianyu 0:d8f4c441e032 195 size_t n = print(num, digits);
lixianyu 0:d8f4c441e032 196 n += println();
lixianyu 0:d8f4c441e032 197 return n;
lixianyu 0:d8f4c441e032 198 }
lixianyu 0:d8f4c441e032 199
lixianyu 0:d8f4c441e032 200 size_t Print::println(const Printable& x)
lixianyu 0:d8f4c441e032 201 {
lixianyu 0:d8f4c441e032 202 size_t n = print(x);
lixianyu 0:d8f4c441e032 203 n += println();
lixianyu 0:d8f4c441e032 204 return n;
lixianyu 0:d8f4c441e032 205 }
lixianyu 0:d8f4c441e032 206
lixianyu 0:d8f4c441e032 207 // Private Methods /////////////////////////////////////////////////////////////
lixianyu 0:d8f4c441e032 208
lixianyu 0:d8f4c441e032 209 size_t Print::printNumber(unsigned long n, uint8_t base)
lixianyu 0:d8f4c441e032 210 {
lixianyu 0:d8f4c441e032 211 char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
lixianyu 0:d8f4c441e032 212 char *str = &buf[sizeof(buf) - 1];
lixianyu 0:d8f4c441e032 213
lixianyu 0:d8f4c441e032 214 *str = '\0';
lixianyu 0:d8f4c441e032 215
lixianyu 0:d8f4c441e032 216 // prevent crash if called with base == 1
lixianyu 0:d8f4c441e032 217 if (base < 2) base = 10;
lixianyu 0:d8f4c441e032 218
lixianyu 0:d8f4c441e032 219 do {
lixianyu 0:d8f4c441e032 220 char c = n % base;
lixianyu 0:d8f4c441e032 221 n /= base;
lixianyu 0:d8f4c441e032 222
lixianyu 0:d8f4c441e032 223 *--str = c < 10 ? c + '0' : c + 'A' - 10;
lixianyu 0:d8f4c441e032 224 } while(n);
lixianyu 0:d8f4c441e032 225
lixianyu 0:d8f4c441e032 226 return write(str);
lixianyu 0:d8f4c441e032 227 }
lixianyu 0:d8f4c441e032 228
lixianyu 0:d8f4c441e032 229 size_t Print::printFloat(double number, uint8_t digits)
lixianyu 0:d8f4c441e032 230 {
lixianyu 0:d8f4c441e032 231 size_t n = 0;
lixianyu 0:d8f4c441e032 232
lixianyu 0:d8f4c441e032 233 if (isnan(number)) return print("nan");
lixianyu 0:d8f4c441e032 234 if (isinf(number)) return print("inf");
lixianyu 0:d8f4c441e032 235 if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
lixianyu 0:d8f4c441e032 236 if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
lixianyu 0:d8f4c441e032 237
lixianyu 0:d8f4c441e032 238 // Handle negative numbers
lixianyu 0:d8f4c441e032 239 if (number < 0.0) {
lixianyu 0:d8f4c441e032 240 n += print('-');
lixianyu 0:d8f4c441e032 241 number = -number;
lixianyu 0:d8f4c441e032 242 }
lixianyu 0:d8f4c441e032 243
lixianyu 0:d8f4c441e032 244 // Round correctly so that print(1.999, 2) prints as "2.00"
lixianyu 0:d8f4c441e032 245 double rounding = 0.5;
lixianyu 0:d8f4c441e032 246 for (uint8_t i=0; i<digits; ++i)
lixianyu 0:d8f4c441e032 247 rounding /= 10.0;
lixianyu 0:d8f4c441e032 248
lixianyu 0:d8f4c441e032 249 number += rounding;
lixianyu 0:d8f4c441e032 250
lixianyu 0:d8f4c441e032 251 // Extract the integer part of the number and print it
lixianyu 0:d8f4c441e032 252 unsigned long int_part = (unsigned long)number;
lixianyu 0:d8f4c441e032 253 double remainder = number - (double)int_part;
lixianyu 0:d8f4c441e032 254 n += print(int_part);
lixianyu 0:d8f4c441e032 255
lixianyu 0:d8f4c441e032 256 // Print the decimal point, but only if there are digits beyond
lixianyu 0:d8f4c441e032 257 if (digits > 0) {
lixianyu 0:d8f4c441e032 258 n += print(".");
lixianyu 0:d8f4c441e032 259 }
lixianyu 0:d8f4c441e032 260
lixianyu 0:d8f4c441e032 261 // Extract digits from the remainder one at a time
lixianyu 0:d8f4c441e032 262 while (digits-- > 0) {
lixianyu 0:d8f4c441e032 263 remainder *= 10.0;
lixianyu 0:d8f4c441e032 264 int toPrint = int(remainder);
lixianyu 0:d8f4c441e032 265 n += print(toPrint);
lixianyu 0:d8f4c441e032 266 remainder -= toPrint;
lixianyu 0:d8f4c441e032 267 }
lixianyu 0:d8f4c441e032 268
lixianyu 0:d8f4c441e032 269 return n;
lixianyu 0:d8f4c441e032 270 }
lixianyu 0:d8f4c441e032 271