Skip to content

Commit

Permalink
Add back PrintBasic code
Browse files Browse the repository at this point in the history
  • Loading branch information
morio committed Oct 3, 2024
1 parent 1c6a440 commit a4573a4
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/common/PrintBasic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright (c) 2011-2020 Bill Greiman
* This file is part of the SdFat library for SD memory cards.
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "PrintBasic.h"
#if ENABLE_ARDUINO_FEATURES == 0
#include <math.h>

size_t PrintBasic::print(long n, uint8_t base) {
if (n < 0 && base == 10) {
return print('-') + printNum(-n, base);
}
return printNum(n, base);
}
size_t PrintBasic::printNum(unsigned long n, uint8_t base) {
const uint8_t DIM = 8*sizeof(long);
char buf[DIM];
char *str = &buf[DIM];

if (base < 2) return 0;

do {
char c = n%base;
n /= base;
*--str = c + (c < 10 ? '0' : 'A' - 10);
} while (n);
return write(str, &buf[DIM] - str);
}

size_t PrintBasic::printDouble(double n, uint8_t prec) {
// Max printable 32-bit floating point number. AVR uses 32-bit double.
const double maxfp = static_cast<double>(0XFFFFFF00UL);
size_t rtn = 0;

if (isnan(n)) {
return write("NaN");
}
if (n < 0) {
n = -n;
rtn += print('-');
}
if (isinf(n)) {
return rtn + write("Inf");
}
if (n > maxfp) {
return rtn + write("Ovf");
}

double round = 0.5;
for (uint8_t i = 0; i < prec; ++i) {
round *= 0.1;
}

n += round;

uint32_t whole = (uint32_t)n;
rtn += print(whole);

if (prec) {
rtn += print('.');
double fraction = n - static_cast<double>(whole);
for (uint8_t i = 0; i < prec; i++) {
fraction *= 10.0;
uint8_t digit = fraction;
rtn += print(digit);
fraction -= digit;
}
}
return rtn;
}
#endif // ENABLE_ARDUINO_FEATURES == 0
170 changes: 170 additions & 0 deletions src/common/PrintBasic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* Copyright (c) 2011-2020 Bill Greiman
* This file is part of the SdFat library for SD memory cards.
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef PrintBasic_h
#define PrintBasic_h
/**
* \file
* \brief Stream/Print like replacement for non-Arduino systems.
*/
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "../SdFatConfig.h"

#ifndef F
#if defined(__AVR__)
#include <avr/pgmspace.h>
class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
#else // defined(__AVR__)
#define F(str) (str)
struct __FlashStringHelper {};
#endif // defined(__AVR__)
#endif // F

#ifdef BIN
#undef BIN
#endif // BIN
#define BIN 2
#define OCT 8
#define DEC 10
#define HEX 16

class PrintBasic {
public:
PrintBasic() : m_error(0) {}

void clearWriteError() {
setWriteError(0);
}
int getWriteError() {
return m_error;
}
size_t print(char c) {
return write(c);
}
size_t print(const char* str) {
return write(str);
}
size_t print(const __FlashStringHelper *str) {
#ifdef __AVR__
PGM_P p = reinterpret_cast<PGM_P>(str);
size_t n = 0;
for (uint8_t c; (c = pgm_read_byte(p + n)) && write(c); n++) {}
return n;
#else // __AVR__
return print(reinterpret_cast<const char *>(str));
#endif // __AVR__
}
size_t println(const __FlashStringHelper *str) {
#ifdef __AVR__
return print(str) + println();
#else // __AVR__
return println(reinterpret_cast<const char *>(str));
#endif // __AVR__
}
size_t print(double n, uint8_t prec = 2) {
return printDouble(n, prec);
}
size_t print(signed char n, uint8_t base = 10) {
return print((long)n, base);
}
size_t print(unsigned char n, uint8_t base = 10) {
return print((unsigned long)n, base);
}
size_t print(int n, uint8_t base = 10) {
return print((long)n, base);
}
size_t print(unsigned int n, uint8_t base = 10) {
return print((unsigned long)n, base);
}
size_t print(long n, uint8_t base = 10);
size_t print(unsigned long n, uint8_t base = 10) {
return printNum(n, base);
}
size_t println() {
return write("\r\n");
}
size_t println(char c) {
return write(c) + println();
}
size_t println(const char* str) {
return print(str) + println();
}
size_t println(double n, uint8_t prec = 2) {
return print(n, prec) + println();
}
size_t println(signed char n, uint8_t base = 10) {
return print(n, base) + println();
}
size_t println(unsigned char n, uint8_t base = 10) {
return print(n, base) + println();
}
size_t println(int n, uint8_t base = 10) {
return print(n, base) + println();
}
size_t println(unsigned int n, uint8_t base = 10) {
return print(n, base) + println();
}
size_t println(long n, uint8_t base = 10) {
return print(n, base) + println();
}
size_t println(unsigned long n, uint8_t base = 10) {
return print(n, base) + println();
}
size_t write(const char *str) {
return write(str, strlen(str));
}
virtual size_t write(uint8_t b) = 0;

virtual size_t write(const uint8_t* buffer, size_t size) {
size_t i;
for (i = 0; i < size; i++) {
if (!write(buffer[i])) break;
}
return i;
}
size_t write(const char *buffer, size_t size) {
return write((const uint8_t*)buffer, size);
}

protected:
void setWriteError(int err = 1) {
m_error = err;
}

private:
size_t printDouble(double n, uint8_t prec);
size_t printNum(unsigned long n, uint8_t base);
int m_error;
};
//------------------------------------------------------------------------------
class StreamBasic : public PrintBasic {
public:
virtual int available() = 0;
virtual int peek() = 0;
virtual int read() = 0;
};
#endif // PrintBasic_h

0 comments on commit a4573a4

Please sign in to comment.