Renamed some variables because Arduino.h is terrible as always

This commit is contained in:
dario 2023-11-23 09:44:24 +01:00
parent 99a097b012
commit 645cbe09f0
6 changed files with 45 additions and 40 deletions

View File

@ -2,3 +2,4 @@
#define STA_PRINTF_USE_STDLIB #define STA_PRINTF_USE_STDLIB
#define STA_STDLIB_DISABLE #define STA_STDLIB_DISABLE
#define STA_DEBUGGING_ENABLED

View File

@ -8,7 +8,7 @@
namespace sta namespace sta
{ {
extern Printable * Debug; extern BasePrintable * Debug;
} // namespace sta } // namespace sta

View File

@ -23,12 +23,16 @@ namespace sta
*/ */
enum class IntegerBase enum class IntegerBase
{ {
DEC, /**< Decimal */ BASE_DEC, /**< Decimal */
BIN, /**< Binary */ BASE_BIN, /**< Binary */
HEX /**< Hexadecimal */ BASE_HEX /**< Hexadecimal */
}; };
class Printable /**
* @brief Base class for printable. Renamed to "BasePrintable" from "Printable" because the Arduino dev are dickheads.
*
*/
class BasePrintable
{ {
public: public:
/** /**
@ -64,7 +68,7 @@ namespace sta
* @param num 8-bit unsigned integer * @param num 8-bit unsigned integer
* @param base Integer base * @param base Integer base
*/ */
void print(uint8_t num, IntegerBase base = IntegerBase::DEC); void print(uint8_t num, IntegerBase base = IntegerBase::BASE_DEC);
/** /**
* @brief Print integer in selected base. * @brief Print integer in selected base.
@ -72,7 +76,7 @@ namespace sta
* @param num 16-bit unsigned integer * @param num 16-bit unsigned integer
* @param base Integer base * @param base Integer base
*/ */
void print(uint16_t num, IntegerBase base = IntegerBase::DEC); void print(uint16_t num, IntegerBase base = IntegerBase::BASE_DEC);
/** /**
* @brief Print integer in selected base. * @brief Print integer in selected base.
@ -80,7 +84,7 @@ namespace sta
* @param num 32-bit unsigned integer * @param num 32-bit unsigned integer
* @param base Integer base * @param base Integer base
*/ */
void print(uint32_t num, IntegerBase base = IntegerBase::DEC); void print(uint32_t num, IntegerBase base = IntegerBase::BASE_DEC);
/** /**
* @brief Print c-string. * @brief Print c-string.
@ -129,7 +133,7 @@ namespace sta
* @param num 8-bit unsigned integer * @param num 8-bit unsigned integer
* @param base Integer base * @param base Integer base
*/ */
void println(uint8_t num, IntegerBase base = IntegerBase::DEC); void println(uint8_t num, IntegerBase base = IntegerBase::BASE_DEC);
/** /**
* @brief Print integer in selected base followed by a new-line. * @brief Print integer in selected base followed by a new-line.
@ -137,7 +141,7 @@ namespace sta
* @param num 16-bit unsigned integer * @param num 16-bit unsigned integer
* @param base Integer base * @param base Integer base
*/ */
void println(uint16_t num, IntegerBase base = IntegerBase::DEC); void println(uint16_t num, IntegerBase base = IntegerBase::BASE_DEC);
/** /**
* @brief Print integer in selected base followed by a new-line. * @brief Print integer in selected base followed by a new-line.
@ -145,7 +149,7 @@ namespace sta
* @param num 32-bit unsigned integer * @param num 32-bit unsigned integer
* @param base Integer base * @param base Integer base
*/ */
void println(uint32_t num, IntegerBase base = IntegerBase::DEC); void println(uint32_t num, IntegerBase base = IntegerBase::BASE_DEC);
/** /**
* @brief Print c-string followed by a new-line. * @brief Print c-string followed by a new-line.

View File

@ -5,7 +5,7 @@
namespace sta namespace sta
{ {
class PrintablePrintf : public Printable class PrintablePrintf : public BasePrintable
{ {
public: public:
/** /**

View File

@ -25,7 +25,7 @@ namespace sta
* *
* @ingroup sta_core * @ingroup sta_core
*/ */
class PrintableUART : public Printable class PrintableUART : public BasePrintable
{ {
public: public:
/** /**

View File

@ -19,7 +19,7 @@
namespace sta namespace sta
{ {
void Printable::printf(const char * fmt, ...) void BasePrintable::printf(const char * fmt, ...)
{ {
va_list args; va_list args;
va_start (args, fmt); va_start (args, fmt);
@ -28,116 +28,116 @@ namespace sta
int n = vsnprintf(temp, 1, fmt, args); int n = vsnprintf(temp, 1, fmt, args);
va_start (args, fmt); va_start (args, fmt);
char str[n]; char str[n+1];
vsnprintf(str, n, fmt, args); vsnprintf(str, n+1, fmt, args);
STA_ASSERT(n > 0); STA_ASSERT(n > 0);
println(str); println(str);
} }
void Printable::print(char c) void BasePrintable::print(char c)
{ {
print(&c, 1); print(&c, 1);
} }
void Printable::print(bool b) void BasePrintable::print(bool b)
{ {
print(b ? "true" : "false"); print(b ? "true" : "false");
} }
void Printable::print(double d) void BasePrintable::print(double d)
{ {
char buffer[64]; char buffer[64];
snprintf(buffer, sizeof(buffer), "%f", d); snprintf(buffer, sizeof(buffer), "%f", d);
print(buffer); print(buffer);
} }
void Printable::print(uint8_t num, IntegerBase base) void BasePrintable::print(uint8_t num, IntegerBase base)
{ {
printBase(num, base, "%" PRIu8, sizeof(num)); printBase(num, base, "%" PRIu8, sizeof(num));
} }
void Printable::print(uint16_t num, IntegerBase base) void BasePrintable::print(uint16_t num, IntegerBase base)
{ {
printBase(num, base, "%" PRIu16, sizeof(num)); printBase(num, base, "%" PRIu16, sizeof(num));
} }
void Printable::print(uint32_t num, IntegerBase base) void BasePrintable::print(uint32_t num, IntegerBase base)
{ {
printBase(num, base, "%" PRIu32, sizeof(num)); printBase(num, base, "%" PRIu32, sizeof(num));
} }
void Printable::print(const char * str) void BasePrintable::print(const char * str)
{ {
print(str, strlen(str)); print(str, strlen(str));
} }
void Printable::println() void BasePrintable::println()
{ {
print("\r\n", 2); print("\r\n", 2);
} }
void Printable::println(char c) void BasePrintable::println(char c)
{ {
print(&c, 1); print(&c, 1);
println(); println();
} }
void Printable::println(bool b) void BasePrintable::println(bool b)
{ {
print(b); print(b);
println(); println();
} }
void Printable::println(double d) void BasePrintable::println(double d)
{ {
print(d); print(d);
println(); println();
} }
void Printable::println(uint8_t num, IntegerBase base) void BasePrintable::println(uint8_t num, IntegerBase base)
{ {
print(num, base); print(num, base);
println(); println();
} }
void Printable::println(uint16_t num, IntegerBase base) void BasePrintable::println(uint16_t num, IntegerBase base)
{ {
print(num, base); print(num, base);
println(); println();
} }
void Printable::println(uint32_t num, IntegerBase base) void BasePrintable::println(uint32_t num, IntegerBase base)
{ {
print(num, base); print(num, base);
println(); println();
} }
void Printable::println(const char * str) void BasePrintable::println(const char * str)
{ {
println(str, strlen(str)); println(str, strlen(str));
} }
void Printable::println(const char * str, size_t length) void BasePrintable::println(const char * str, size_t length)
{ {
print(str, length); print(str, length);
println(); println();
} }
void Printable::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size) void BasePrintable::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size)
{ {
switch (base) switch (base)
{ {
case IntegerBase::DEC: case IntegerBase::BASE_DEC:
printDec(num, fmt); printDec(num, fmt);
break; break;
case IntegerBase::BIN: case IntegerBase::BASE_BIN:
// Digits in base 2 = size in bytes * 8 // Digits in base 2 = size in bytes * 8
printBin(num, size * 8); printBin(num, size * 8);
break; break;
case IntegerBase::HEX: case IntegerBase::BASE_HEX:
// Digits in base 16 = size in bytes * 2 // Digits in base 16 = size in bytes * 2
printHex(num, size * 2); printHex(num, size * 2);
break; break;
@ -147,14 +147,14 @@ namespace sta
} }
} }
void Printable::printDec(uintmax_t num, const char * fmt) void BasePrintable::printDec(uintmax_t num, const char * fmt)
{ {
char buffer[64]; char buffer[64];
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num)); snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
print(buffer); print(buffer);
} }
void Printable::printBin(uintmax_t value, size_t digits) void BasePrintable::printBin(uintmax_t value, size_t digits)
{ {
// Need 8 digits for every byte // Need 8 digits for every byte
char buffer[sizeof(value) * 8]; char buffer[sizeof(value) * 8];
@ -179,7 +179,7 @@ namespace sta
print(buffer, digits); print(buffer, digits);
} }
void Printable::printHex(uintmax_t value, size_t digits) void BasePrintable::printHex(uintmax_t value, size_t digits)
{ {
// Need 2 digits for every byte // Need 2 digits for every byte
char buffer[sizeof(value) * 2]; char buffer[sizeof(value) * 2];