|
| 1 | +/** |
| 2 | + * Copyright (c) 2011-2025 Bill Greiman |
| 3 | + * This file is part of the SdFat library for SD memory cards. |
| 4 | + * |
| 5 | + * MIT License |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | + * copy of this software and associated documentation files (the "Software"), |
| 9 | + * to deal in the Software without restriction, including without limitation |
| 10 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | + * Software is furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included |
| 15 | + * in all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | + * DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | +#pragma once |
| 26 | +/** |
| 27 | + * \file |
| 28 | + * \brief Fast buffered print. |
| 29 | + */ |
| 30 | +#ifdef __AVR__ |
| 31 | +#include <avr/pgmspace.h> |
| 32 | +#endif // __AVR__ |
| 33 | +#include "common/FmtNumber.h" |
| 34 | +/** |
| 35 | + * \class BufferedPrint |
| 36 | + * \brief Fast buffered print template. |
| 37 | + */ |
| 38 | +template <typename WriteClass, uint8_t BUF_DIM> |
| 39 | +class BufferedPrint { |
| 40 | + public: |
| 41 | + BufferedPrint() : m_wr(nullptr), m_in(0) {} |
| 42 | + /** BufferedPrint constructor. |
| 43 | + * \param[in] wr Print destination. |
| 44 | + */ |
| 45 | + explicit BufferedPrint(WriteClass* wr) : m_wr(wr), m_in(0) {} |
| 46 | + /** Initialize the BuffedPrint class. |
| 47 | + * \param[in] wr Print destination. |
| 48 | + */ |
| 49 | + void begin(WriteClass* wr) { |
| 50 | + m_wr = wr; |
| 51 | + m_in = 0; |
| 52 | + } |
| 53 | + /** Flush the buffer - same as sync() with no status return. */ |
| 54 | + void flush() { sync(); } |
| 55 | + /** Print a character followed by a field terminator. |
| 56 | + * \param[in] c character to print. |
| 57 | + * \param[in] term The field terminator. Use '\\n' for CR LF. |
| 58 | + * \return true for success or false if an error occurs. |
| 59 | + */ |
| 60 | + size_t printField(char c, char term) { |
| 61 | + char buf[3]; |
| 62 | + char* str = buf + sizeof(buf); |
| 63 | + if (term) { |
| 64 | + *--str = term; |
| 65 | + if (term == '\n') { |
| 66 | + *--str = '\r'; |
| 67 | + } |
| 68 | + } |
| 69 | + *--str = c; |
| 70 | + return write(str, buf + sizeof(buf) - str); |
| 71 | + } |
| 72 | + /** Print a string stored in AVR flash followed by a field terminator. |
| 73 | + * \param[in] fsh string to print. |
| 74 | + * \param[in] term The field terminator. Use '\\n' for CR LF. |
| 75 | + * \return true for success or false if an error occurs. |
| 76 | + */ |
| 77 | + size_t printField(const __FlashStringHelper* fsh, char term) { |
| 78 | +#ifdef __AVR__ |
| 79 | + size_t rtn = 0; |
| 80 | + PGM_P p = reinterpret_cast<PGM_P>(fsh); |
| 81 | + char c; |
| 82 | + while ((c = pgm_read_byte(p++))) { |
| 83 | + if (!write(&c, 1)) { |
| 84 | + return 0; |
| 85 | + } |
| 86 | + rtn++; |
| 87 | + } |
| 88 | + if (term) { |
| 89 | + char buf[2]; |
| 90 | + char* str = buf + sizeof(buf); |
| 91 | + *--str = term; |
| 92 | + if (term == '\n') { |
| 93 | + *--str = '\r'; |
| 94 | + } |
| 95 | + rtn += write(str, buf + sizeof(buf) - str); |
| 96 | + } |
| 97 | + return rtn; |
| 98 | +#else // __AVR__ |
| 99 | + return printField(reinterpret_cast<const char*>(fsh), term); |
| 100 | +#endif // __AVR__ |
| 101 | + } |
| 102 | + /** Print a string followed by a field terminator. |
| 103 | + * \param[in] str string to print. |
| 104 | + * \param[in] term The field terminator. Use '\\n' for CR LF. |
| 105 | + * \return true for success or false if an error occurs. |
| 106 | + */ |
| 107 | + size_t printField(const char* str, char term) { |
| 108 | + size_t rtn = write(str, strlen(str)); |
| 109 | + if (term) { |
| 110 | + char buf[2]; |
| 111 | + char* ptr = buf + sizeof(buf); |
| 112 | + *--ptr = term; |
| 113 | + if (term == '\n') { |
| 114 | + *--ptr = '\r'; |
| 115 | + } |
| 116 | + rtn += write(ptr, buf + sizeof(buf) - ptr); |
| 117 | + } |
| 118 | + return rtn; |
| 119 | + } |
| 120 | + /** Print a double followed by a field terminator. |
| 121 | + * \param[in] d The number to be printed. |
| 122 | + * \param[in] term The field terminator. Use '\\n' for CR LF. |
| 123 | + * \param[in] prec Number of digits after decimal point. |
| 124 | + * \return true for success or false if an error occurs. |
| 125 | + */ |
| 126 | + size_t printField(double d, char term, uint8_t prec = 2) { |
| 127 | + char buf[24]; |
| 128 | + char* str = buf + sizeof(buf); |
| 129 | + if (term) { |
| 130 | + *--str = term; |
| 131 | + if (term == '\n') { |
| 132 | + *--str = '\r'; |
| 133 | + } |
| 134 | + } |
| 135 | + str = fmtDouble(str, d, prec, false); |
| 136 | + return write(str, buf + sizeof(buf) - str); |
| 137 | + } |
| 138 | + /** Print a float followed by a field terminator. |
| 139 | + * \param[in] f The number to be printed. |
| 140 | + * \param[in] term The field terminator. Use '\\n' for CR LF. |
| 141 | + * \param[in] prec Number of digits after decimal point. |
| 142 | + * \return true for success or false if an error occurs. |
| 143 | + */ |
| 144 | + size_t printField(float f, char term, uint8_t prec = 2) { |
| 145 | + return printField(static_cast<double>(f), term, prec); |
| 146 | + } |
| 147 | + /** Print an integer value for 8, 16, and 32 bit signed and unsigned types. |
| 148 | + * \param[in] n The value to print. |
| 149 | + * \param[in] term The field terminator. Use '\\n' for CR LF. |
| 150 | + * \return true for success or false if an error occurs. |
| 151 | + */ |
| 152 | + template <typename Type> |
| 153 | + size_t printField(Type n, char term) { |
| 154 | + const uint8_t DIM = sizeof(Type) <= 2 ? 8 : 13; |
| 155 | + char buf[DIM]; |
| 156 | + char* str = buf + sizeof(buf); |
| 157 | + |
| 158 | + if (term) { |
| 159 | + *--str = term; |
| 160 | + if (term == '\n') { |
| 161 | + *--str = '\r'; |
| 162 | + } |
| 163 | + } |
| 164 | + Type p = n < 0 ? -n : n; |
| 165 | + if (sizeof(Type) <= 2) { |
| 166 | + str = fmtBase10(str, static_cast<uint16_t>(p)); |
| 167 | + } else { |
| 168 | + str = fmtBase10(str, static_cast<uint32_t>(p)); |
| 169 | + } |
| 170 | + if (n < 0) { |
| 171 | + *--str = '-'; |
| 172 | + } |
| 173 | + return write(str, buf + sizeof(buf) - str); |
| 174 | + } |
| 175 | + /** Print CR LF. |
| 176 | + * \return true for success or false if an error occurs. |
| 177 | + */ |
| 178 | + size_t println() { |
| 179 | + char buf[2]; |
| 180 | + buf[0] = '\r'; |
| 181 | + buf[1] = '\n'; |
| 182 | + return write(buf, 2); |
| 183 | + } |
| 184 | + /** Print a double. |
| 185 | + * \param[in] d The number to be printed. |
| 186 | + * \param[in] prec Number of digits after decimal point. |
| 187 | + * \return true for success or false if an error occurs. |
| 188 | + */ |
| 189 | + size_t print(double d, uint8_t prec = 2) { return printField(d, 0, prec); } |
| 190 | + /** Print a double followed by CR LF. |
| 191 | + * \param[in] d The number to be printed. |
| 192 | + * \param[in] prec Number of digits after decimal point. |
| 193 | + * \return true for success or false if an error occurs. |
| 194 | + */ |
| 195 | + size_t println(double d, uint8_t prec = 2) { |
| 196 | + return printField(d, '\n', prec); |
| 197 | + } |
| 198 | + /** Print a float. |
| 199 | + * \param[in] f The number to be printed. |
| 200 | + * \param[in] prec Number of digits after decimal point. |
| 201 | + * \return true for success or false if an error occurs. |
| 202 | + */ |
| 203 | + size_t print(float f, uint8_t prec = 2) { |
| 204 | + return printField(static_cast<double>(f), 0, prec); |
| 205 | + } |
| 206 | + /** Print a float followed by CR LF. |
| 207 | + * \param[in] f The number to be printed. |
| 208 | + * \param[in] prec Number of digits after decimal point. |
| 209 | + * \return true for success or false if an error occurs. |
| 210 | + */ |
| 211 | + size_t println(float f, uint8_t prec) { |
| 212 | + return printField(static_cast<double>(f), '\n', prec); |
| 213 | + } |
| 214 | + /** Print character, string, or number. |
| 215 | + * \param[in] v item to print. |
| 216 | + * \return true for success or false if an error occurs. |
| 217 | + */ |
| 218 | + template <typename Type> |
| 219 | + size_t print(Type v) { |
| 220 | + return printField(v, 0); |
| 221 | + } |
| 222 | + /** Print character, string, or number followed by CR LF. |
| 223 | + * \param[in] v item to print. |
| 224 | + * \return true for success or false if an error occurs. |
| 225 | + */ |
| 226 | + template <typename Type> |
| 227 | + size_t println(Type v) { |
| 228 | + return printField(v, '\n'); |
| 229 | + } |
| 230 | + |
| 231 | + /** Flush the buffer. |
| 232 | + * \return true for success or false if an error occurs. |
| 233 | + */ |
| 234 | + bool sync() { |
| 235 | + if (!m_wr || m_wr->write(m_buf, m_in) != m_in) { |
| 236 | + return false; |
| 237 | + } |
| 238 | + m_in = 0; |
| 239 | + return true; |
| 240 | + } |
| 241 | + /** Write data to an open file. |
| 242 | + * \param[in] src Pointer to the location of the data to be written. |
| 243 | + * |
| 244 | + * \param[in] n Number of bytes to write. |
| 245 | + * |
| 246 | + * \return For success write() returns the number of bytes written, always |
| 247 | + * \a n. |
| 248 | + */ |
| 249 | + size_t write(const void* src, size_t n) { |
| 250 | + if ((m_in + n) > sizeof(m_buf)) { |
| 251 | + if (!sync()) { |
| 252 | + return 0; |
| 253 | + } |
| 254 | + if (n >= sizeof(m_buf)) { |
| 255 | + return n == m_wr->write((const uint8_t*)src, n) ? n : 0; |
| 256 | + } |
| 257 | + } |
| 258 | + memcpy(m_buf + m_in, src, n); |
| 259 | + m_in += n; |
| 260 | + return n; |
| 261 | + } |
| 262 | + |
| 263 | + private: |
| 264 | + WriteClass* m_wr; |
| 265 | + uint8_t m_in; |
| 266 | + // Insure room for double. |
| 267 | + uint8_t m_buf[BUF_DIM < 24 ? 24 : BUF_DIM]; // NOLINT |
| 268 | +}; |
0 commit comments