Arduino Print library for MBED

Dependents:   LiquidCrystal_I2C

Summary

A quick port of the Arduino Print library.
Required to make a class that derives from the predefined Print class.

Committer:
DVSProductions
Date:
Mon Jul 29 03:49:50 2019 +0000
Revision:
0:4f62090ee72f
First Commit

Who changed what in which revision?

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