From 32ff235822b8bd4038bfbe273dad738a2acb40fb Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 5 Nov 2023 15:55:45 +0100 Subject: [PATCH 1/9] First implementations for Arduino --- include/sta/bus/i2c/i2c.hpp | 2 +- include/sta/config.hpp | 3 ++ include/sta/devices/arduino/bus/i2c.hpp | 12 ++--- include/sta/devices/arduino/bus/spi.hpp | 40 +++++++++++++++ include/sta/devices/arduino/debug_serial.hpp | 7 +++ include/sta/devices/arduino/delay.hpp | 2 + src/devices/arduino/bus/i2c.cpp | 49 ++++++++++--------- src/devices/arduino/bus/spi.cpp | 51 ++++++++++++++++++++ src/devices/raspi/bus/i2c.cpp | 1 - 9 files changed, 135 insertions(+), 32 deletions(-) create mode 100644 include/sta/config.hpp create mode 100644 include/sta/devices/arduino/debug_serial.hpp diff --git a/include/sta/bus/i2c/i2c.hpp b/include/sta/bus/i2c/i2c.hpp index 1af5571..ab53e7c 100644 --- a/include/sta/bus/i2c/i2c.hpp +++ b/include/sta/bus/i2c/i2c.hpp @@ -31,7 +31,7 @@ namespace sta void setSettings(uint16_t address, bool master, bool blocking); protected: - uint16_t address_; + uint8_t address_; bool master_; bool blocking_; }; diff --git a/include/sta/config.hpp b/include/sta/config.hpp new file mode 100644 index 0000000..1e53727 --- /dev/null +++ b/include/sta/config.hpp @@ -0,0 +1,3 @@ +#include + +#define STA_PRINTF_USE_STDLIB \ No newline at end of file diff --git a/include/sta/devices/arduino/bus/i2c.hpp b/include/sta/devices/arduino/bus/i2c.hpp index ff9a4e9..44ac845 100644 --- a/include/sta/devices/arduino/bus/i2c.hpp +++ b/include/sta/devices/arduino/bus/i2c.hpp @@ -12,22 +12,20 @@ namespace sta class ArduinoI2C : public I2C { public: - ArduinoI2C(Mutex * mutex=nullptr, uint16_t address); - + ArduinoI2C(Mutex * mutex = nullptr); + + void transfer(uint8_t value) override; void transfer16(uint16_t value) override; void transfer(const uint8_t * buffer, size_t size) override; - void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; void receive(uint8_t * buffer, size_t size) override; - void fill(uint8_t value, size_t count) override; - void acquire(); - void release(); + void fill(uint8_t value, size_t count) override; }; class ArduinoI2CDevice : public I2CDevice { public: - ArduinoI2CDevice(I2C * intf, int address, bool master=false, bool blocking=true); + ArduinoI2CDevice(I2C * intf, uint8_t address, bool master=true, bool blocking=true); }; } // namespace sta diff --git a/include/sta/devices/arduino/bus/spi.hpp b/include/sta/devices/arduino/bus/spi.hpp index e69de29..9353863 100644 --- a/include/sta/devices/arduino/bus/spi.hpp +++ b/include/sta/devices/arduino/bus/spi.hpp @@ -0,0 +1,40 @@ +#ifndef STA_CORE_ARDUINO_SPI_HPP +#define STA_CORE_ARDUINO_SPI_HPP + +#include +#ifdef STA_PLATFORM_ARDUINO + +#include +#include + +namespace sta +{ + class ArduinoSPI : public SPI + { + public: + ArduinoSPI(const SPISettings & settings, Mutex * mutex = nullptr); + + void transfer(uint8_t value) override; + void transfer16(uint16_t value) override; + void transfer(const uint8_t * buffer, size_t size) override; + void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; + void receive(uint8_t * buffer, size_t size) override; + + void fill(uint8_t value, size_t count) override; + + void acquire() override; + void release() override; + private: + + }; + + class ArduinoSPIDevice : public SPIDevice + { + public: + ArduinoSPIDevice(ArduinoSPI * intf); + }; +} // namespace sta + +#endif // STA_PLATFORM_ARDUINO + +#endif // STA_CORE_ARDUINO_SPI_HPP \ No newline at end of file diff --git a/include/sta/devices/arduino/debug_serial.hpp b/include/sta/devices/arduino/debug_serial.hpp new file mode 100644 index 0000000..cfb5bbf --- /dev/null +++ b/include/sta/devices/arduino/debug_serial.hpp @@ -0,0 +1,7 @@ +#ifndef STA_CORE_ARDUINO_DEBUG_SERIAL_HPP +#define STA_CORE_ARDUINO_DEBUG_SERIAL_HPP + + + + +#endif // STA_CORE_ARDUINO_DEBUG_SERIAL_HPP \ No newline at end of file diff --git a/include/sta/devices/arduino/delay.hpp b/include/sta/devices/arduino/delay.hpp index bcc6ebb..6bc3cc1 100644 --- a/include/sta/devices/arduino/delay.hpp +++ b/include/sta/devices/arduino/delay.hpp @@ -6,6 +6,8 @@ #if defined(STA_PLATFORM_ARDUINO) || defined(DOXYGEN) +#include + namespace sta { /** diff --git a/src/devices/arduino/bus/i2c.cpp b/src/devices/arduino/bus/i2c.cpp index c52503a..e9733c5 100644 --- a/src/devices/arduino/bus/i2c.cpp +++ b/src/devices/arduino/bus/i2c.cpp @@ -7,30 +7,45 @@ namespace sta { - ArduinoI2C::ArduinoI2C(Mutex * mutex, uint16_t address) + ArduinoI2C::ArduinoI2C(Mutex * mutex /* = nullptr */) : I2C{mutex} { - Wire.begin(address); + Wire.begin(); + } + + void ArduinoI2C::transfer(uint8_t value) + { + Wire.beginTransmission(address_); + Wire.write(value); + Wire.endTransmission(true); } void ArduinoI2C::transfer16(uint16_t value) { + Wire.beginTransmission(address_); Wire.write(value); + Wire.endTransmission(true); } void ArduinoI2C::transfer(const uint8_t * buffer, size_t size) { + Wire.beginTransmission(address_); Wire.write(buffer, size); - } - - void ArduinoI2C::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) - { - // NOT REALLY A THING HERE, IS IT? + Wire.endTransmission(); } void ArduinoI2C::receive(uint8_t * buffer, size_t size) { - + Wire.requestFrom(address_, size); + size_t index = 0; + + while (index < size) + { + while (Wire.available()) + { + buffer[index++] = Wire.read(); + } + } } void ArduinoI2C::fill(uint8_t value, size_t count) @@ -39,26 +54,14 @@ namespace sta uint8_t *buffer = new uint8_t[count]; memset(buffer, value, count); - Serial.write(buffer, count); - - delete [] buffer; - } - - void ArduinoI2C::acquire() - { - I2C::acquire(); - Wire.beginTransmission(address_); - } - - void ArduinoI2C::release() - { + Wire.write(buffer, count); Wire.endTransmission(); - I2C::release(); + delete[] buffer; } - ArduinoI2CDevice::ArduinoI2CDevice(I2C * intf, int address, bool master, bool blocking) + ArduinoI2CDevice::ArduinoI2CDevice(I2C * intf, uint8_t address, bool master /* = true */, bool blocking /* = true */) : I2CDevice{intf, address, master, blocking} { diff --git a/src/devices/arduino/bus/spi.cpp b/src/devices/arduino/bus/spi.cpp index e69de29..5b8d7a1 100644 --- a/src/devices/arduino/bus/spi.cpp +++ b/src/devices/arduino/bus/spi.cpp @@ -0,0 +1,51 @@ +#include + + +namespace sta +{ + ArduinoSPI::ArduinoSPI(const SPISettings & settings, Mutex * mutex /* = nullptr */) + : SPI{ settings, mutex } + { + + } + + void ArduinoSPI::transfer(uint8_t value) + { + + } + + void ArduinoSPI::transfer16(uint16_t value) + { + + } + + void ArduinoSPI::transfer(const uint8_t * buffer, size_t size) + { + + } + + void ArduinoSPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + + } + + void ArduinoSPI::receive(uint8_t * buffer, size_t size) + { + + } + + void ArduinoSPI::fill(uint8_t value, size_t count) + { + + } + + void ArduinoSPI::acquire() + { + + } + + void ArduinoSPI::release() + { + + } +} // namespace sta diff --git a/src/devices/raspi/bus/i2c.cpp b/src/devices/raspi/bus/i2c.cpp index 35850a3..6c505af 100644 --- a/src/devices/raspi/bus/i2c.cpp +++ b/src/devices/raspi/bus/i2c.cpp @@ -134,7 +134,6 @@ namespace sta } - } // namespace sta #endif // STA_PLATFORM_RASPI From 2a79b47d6ae8916a33add88ba877350f2c355fa5 Mon Sep 17 00:00:00 2001 From: dario Date: Tue, 21 Nov 2023 21:34:07 +0100 Subject: [PATCH 2/9] Added formatted printing --- include/sta/debug/debug.hpp | 17 +++++++++++++---- include/sta/debug/printing/printable.hpp | 6 ++++++ src/debug/printing/printable.cpp | 15 ++++++++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/sta/debug/debug.hpp b/include/sta/debug/debug.hpp index 0396ea1..7a09609 100644 --- a/include/sta/debug/debug.hpp +++ b/include/sta/debug/debug.hpp @@ -19,7 +19,7 @@ namespace sta * * @ingroup sta_core_debug */ -# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__) +# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__) /** * @brief Debug print message followed by new-line to UART. @@ -28,12 +28,21 @@ namespace sta * * @ingroup sta_core_debug */ -# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__) +# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__) + +/** + * @brief Formatted debug printing with new-line. + * + * @param fmt See @ref sta::PrintableUART::printf + * @param ... See @ref sta::PrintableUART::printf + */ +# define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__) #else // !STA_DEBUGGING_ENABLED -# define STA_DEBUG_PRINT(...) ((void)0) -# define STA_DEBUG_PRINTLN(...) ((void)0) +# define STA_DEBUG_PRINT(...) ((void)0) +# define STA_DEBUG_PRINTLN(...) ((void)0) +# define STA_DEBUG_PRINTF(fmt, ...) ((void)0) #endif // STA_DEBUGGING_ENABLED diff --git a/include/sta/debug/printing/printable.hpp b/include/sta/debug/printing/printable.hpp index 77117a3..e1030fd 100644 --- a/include/sta/debug/printing/printable.hpp +++ b/include/sta/debug/printing/printable.hpp @@ -26,6 +26,12 @@ namespace sta class Printable { public: + /** + * @brief fmt The string defining the desired format. + * @breif ... The parameters for the formatted string. + */ + void printf(const char * fmt, ...); + /** * @brief Print single character. * diff --git a/src/debug/printing/printable.cpp b/src/debug/printing/printable.cpp index df6dcba..0c3bebd 100644 --- a/src/debug/printing/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -3,12 +3,25 @@ #include #include #include +#include +#include #include #include namespace sta { + void Printable::printf(const char * fmt, ...) + { + va_list args; + va_start (args, fmt); + char str[2*strlen(fmt)]; + int n = vsnprintf(str, 2*strlen(fmt), fmt, args); + STA_ASSERT(n > 0); + + println(str); + } + void Printable::print(char c) { print(&c, 1); @@ -182,4 +195,4 @@ namespace sta print(buffer, digits); } -} // namespace sta \ No newline at end of file +} // namespace sta From fb17c11903d1e7c92a2de3ea08bbed8386a7f35a Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 22 Nov 2023 19:16:32 +0100 Subject: [PATCH 3/9] Fixed some mistakes in debug printf --- src/debug/printing/printable.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/debug/printing/printable.cpp b/src/debug/printing/printable.cpp index 0c3bebd..5498c5b 100644 --- a/src/debug/printing/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -15,8 +15,13 @@ namespace sta { va_list args; va_start (args, fmt); - char str[2*strlen(fmt)]; - int n = vsnprintf(str, 2*strlen(fmt), fmt, args); + + char temp[1]; + int n = vsnprintf(temp, 1, fmt, args); + + va_start (args, fmt); + char str[n]; + vsnprintf(str, n, fmt, args); STA_ASSERT(n > 0); println(str); From d785e4b5ec317010ce12c379ea111199c59ec953 Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 23 Nov 2023 08:53:39 +0100 Subject: [PATCH 4/9] First sta-Core rework to work for Arduino --- include/sta/bus/can/filter.hpp | 7 ++++++- include/sta/bus/can/headers.hpp | 7 ++++++- include/sta/bus/can/id.hpp | 7 ++++++- include/sta/bus/can/iter.hpp | 7 ++++++- include/sta/bus/i2c/i2c.hpp | 3 --- include/sta/bus/interface.hpp | 10 ++++++++-- include/sta/bus/spi/device.hpp | 3 --- include/sta/bus/spi/settings.hpp | 7 ++++++- include/sta/bus/spi/spi.hpp | 4 ---- include/sta/config.hpp | 3 ++- include/sta/debug/printing/printable.hpp | 11 ++++++++--- include/sta/debug/printing/printable_uart.hpp | 10 ++++++++-- include/sta/devices/arduino/gpio_pin.hpp | 3 ++- include/sta/printf.hpp | 6 +++++- src/bus/spi/spi.cpp | 8 +++++++- src/debug/printing/printable.cpp | 12 +++++++++--- src/debug/printing/printable_printf.cpp | 7 ++++++- src/debug/printing/printable_uart.cpp | 9 +++++++-- src/devices/stm32/bus/i2c.cpp | 3 ++- 19 files changed, 94 insertions(+), 33 deletions(-) diff --git a/include/sta/bus/can/filter.hpp b/include/sta/bus/can/filter.hpp index 1499eea..2ec0121 100644 --- a/include/sta/bus/can/filter.hpp +++ b/include/sta/bus/can/filter.hpp @@ -7,7 +7,12 @@ #include -#include +#include +#ifdef STA_PLATFORM_ARDUINO +# include +#else +# include +#endif // STA_PLATFORM_ARDUINO namespace sta diff --git a/include/sta/bus/can/headers.hpp b/include/sta/bus/can/headers.hpp index 0214434..3d9b111 100644 --- a/include/sta/bus/can/headers.hpp +++ b/include/sta/bus/can/headers.hpp @@ -7,7 +7,12 @@ #include -#include +#include +#ifdef STA_PLATFORM_ARDUINO +# include +#else +# include +#endif // STA_PLATFORM_ARDUINO namespace sta diff --git a/include/sta/bus/can/id.hpp b/include/sta/bus/can/id.hpp index a832998..712042d 100644 --- a/include/sta/bus/can/id.hpp +++ b/include/sta/bus/can/id.hpp @@ -5,7 +5,12 @@ #ifndef STA_CORE_CAN_ID_HPP #define STA_CORE_CAN_ID_HPP -#include +#include +#ifdef STA_PLATFORM_ARDUINO +# include +#else +# include +#endif // STA_PLATFORM_ARDUINO namespace sta diff --git a/include/sta/bus/can/iter.hpp b/include/sta/bus/can/iter.hpp index 95fd187..c781eb5 100644 --- a/include/sta/bus/can/iter.hpp +++ b/include/sta/bus/can/iter.hpp @@ -5,7 +5,12 @@ #ifndef STA_CORE_CAN_ITER_HPP #define STA_CORE_CAN_ITER_HPP -#include +#include +#ifdef STA_PLATFORM_ARDUINO +# include +#else +# include +#endif // STA_PLATFORM_ARDUINO namespace sta diff --git a/include/sta/bus/i2c/i2c.hpp b/include/sta/bus/i2c/i2c.hpp index ab53e7c..83ef86d 100644 --- a/include/sta/bus/i2c/i2c.hpp +++ b/include/sta/bus/i2c/i2c.hpp @@ -4,9 +4,6 @@ #include #include -#include -#include - namespace sta { /** diff --git a/include/sta/bus/interface.hpp b/include/sta/bus/interface.hpp index e4a0cd8..027cd12 100644 --- a/include/sta/bus/interface.hpp +++ b/include/sta/bus/interface.hpp @@ -3,8 +3,14 @@ #include -#include -#include +#include +#ifdef STA_PLATFORM_ARDUINO +# include +# include +#else +# include +# include +#endif // STA_PLATFORM_ARDUINO namespace sta { diff --git a/include/sta/bus/spi/device.hpp b/include/sta/bus/spi/device.hpp index adeed46..34f0c4f 100644 --- a/include/sta/bus/spi/device.hpp +++ b/include/sta/bus/spi/device.hpp @@ -9,9 +9,6 @@ #include #include -#include -#include - namespace sta { diff --git a/include/sta/bus/spi/settings.hpp b/include/sta/bus/spi/settings.hpp index dea4618..f9ef566 100644 --- a/include/sta/bus/spi/settings.hpp +++ b/include/sta/bus/spi/settings.hpp @@ -11,7 +11,12 @@ * @brief SPI interface. */ -#include +#include +#ifdef STA_PLATFORM_ARDUINO +# include +#else +# include +#endif // STA_PLATFORM_ARDUINO namespace sta { diff --git a/include/sta/bus/spi/spi.hpp b/include/sta/bus/spi/spi.hpp index 2d0dc14..1949117 100644 --- a/include/sta/bus/spi/spi.hpp +++ b/include/sta/bus/spi/spi.hpp @@ -9,10 +9,6 @@ #include #include -#include -#include - - namespace sta { /** diff --git a/include/sta/config.hpp b/include/sta/config.hpp index 1e53727..ee160b7 100644 --- a/include/sta/config.hpp +++ b/include/sta/config.hpp @@ -1,3 +1,4 @@ #include -#define STA_PRINTF_USE_STDLIB \ No newline at end of file +#define STA_PRINTF_USE_STDLIB +#define STA_STDLIB_DISABLE \ No newline at end of file diff --git a/include/sta/debug/printing/printable.hpp b/include/sta/debug/printing/printable.hpp index 77117a3..0b4ba3f 100644 --- a/include/sta/debug/printing/printable.hpp +++ b/include/sta/debug/printing/printable.hpp @@ -5,9 +5,14 @@ #ifndef STA_CORE_PRINTABLE_HPP #define STA_CORE_PRINTABLE_HPP -#include -#include - +#include +#ifdef STA_PLATFORM_ARDUINO +# include +# include +#else +# include +# include +#endif namespace sta { diff --git a/include/sta/debug/printing/printable_uart.hpp b/include/sta/debug/printing/printable_uart.hpp index 93f92c5..d6ea375 100644 --- a/include/sta/debug/printing/printable_uart.hpp +++ b/include/sta/debug/printing/printable_uart.hpp @@ -8,8 +8,14 @@ #include #include -#include -#include +#include +#ifdef STA_PLATFORM_ARDUINO +# include +# include +#else +# include +# include +#endif // STA_PLATFORM_ARDUINO namespace sta diff --git a/include/sta/devices/arduino/gpio_pin.hpp b/include/sta/devices/arduino/gpio_pin.hpp index dd03289..ee82771 100644 --- a/include/sta/devices/arduino/gpio_pin.hpp +++ b/include/sta/devices/arduino/gpio_pin.hpp @@ -11,7 +11,8 @@ #if defined(STA_PLATFORM_ARDUINO) || defined(DOXYGEN) #include -#include + +#include namespace sta { diff --git a/include/sta/printf.hpp b/include/sta/printf.hpp index c738424..520fd74 100644 --- a/include/sta/printf.hpp +++ b/include/sta/printf.hpp @@ -17,7 +17,11 @@ #ifdef STA_PRINTF_USE_STDLIB -# include +# ifdef STA_PLATFORM_ARDUINO +# include +# else +# include +# endif // STA_PLATFORM_ARDUINO #endif // STA_PRINTF_USE_STDLIB #ifdef STA_PRINTF_USE_MPALAND # include diff --git a/src/bus/spi/spi.cpp b/src/bus/spi/spi.cpp index 012bcf3..62d636b 100644 --- a/src/bus/spi/spi.cpp +++ b/src/bus/spi/spi.cpp @@ -1,6 +1,12 @@ #include -#include +#include +#ifdef STA_PLATFORM_ARDUINO +# include +#else +# include +#endif // STA_PLATFORM_ARDUINO + namespace sta { diff --git a/src/debug/printing/printable.cpp b/src/debug/printing/printable.cpp index df6dcba..10e6b99 100644 --- a/src/debug/printing/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -1,8 +1,14 @@ #include -#include -#include -#include +#ifdef STA_PLATFORM_ARDUINO +# include +# include +# include +#else +# include +# include +# include +#endif // STA_PLATFORM_ARDUINO #include #include diff --git a/src/debug/printing/printable_printf.cpp b/src/debug/printing/printable_printf.cpp index 07357f5..48408fd 100644 --- a/src/debug/printing/printable_printf.cpp +++ b/src/debug/printing/printable_printf.cpp @@ -1,7 +1,12 @@ #include #include -#include +#include +#ifdef STA_PLATFORM_ARDUINO +# include +#else +# include +#endif // STA_PLATFORM_ARDUINO namespace sta { diff --git a/src/debug/printing/printable_uart.cpp b/src/debug/printing/printable_uart.cpp index 76a63f8..14dcb4b 100644 --- a/src/debug/printing/printable_uart.cpp +++ b/src/debug/printing/printable_uart.cpp @@ -3,8 +3,13 @@ #include #include -#include -#include +#ifdef STA_PLATFORM_ARDUINO +# include +# include +#else +# include +# include +#endif // STA_PLATFORM_ARDUINO namespace sta diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp index 29ba02d..f96bbe5 100644 --- a/src/devices/stm32/bus/i2c.cpp +++ b/src/devices/stm32/bus/i2c.cpp @@ -1,10 +1,11 @@ #include #include -#include #ifdef STA_STM32_I2C_ENABLED +#include + namespace sta { STM32I2C::STM32I2C(I2C_HandleTypeDef * handle, Mutex * mutex) From 645cbe09f06c9b99d65398b57c13833953b0628f Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 23 Nov 2023 09:44:24 +0100 Subject: [PATCH 5/9] Renamed some variables because Arduino.h is terrible as always --- include/sta/config.hpp | 3 +- include/sta/debug/debug.hpp | 2 +- include/sta/debug/printing/printable.hpp | 24 +++++---- .../sta/debug/printing/printable_printf.hpp | 2 +- include/sta/debug/printing/printable_uart.hpp | 2 +- src/debug/printing/printable.cpp | 52 +++++++++---------- 6 files changed, 45 insertions(+), 40 deletions(-) diff --git a/include/sta/config.hpp b/include/sta/config.hpp index ee160b7..6864f86 100644 --- a/include/sta/config.hpp +++ b/include/sta/config.hpp @@ -1,4 +1,5 @@ #include #define STA_PRINTF_USE_STDLIB -#define STA_STDLIB_DISABLE \ No newline at end of file +#define STA_STDLIB_DISABLE +#define STA_DEBUGGING_ENABLED \ No newline at end of file diff --git a/include/sta/debug/debug.hpp b/include/sta/debug/debug.hpp index 7a09609..cf7cbd1 100644 --- a/include/sta/debug/debug.hpp +++ b/include/sta/debug/debug.hpp @@ -8,7 +8,7 @@ namespace sta { - extern Printable * Debug; + extern BasePrintable * Debug; } // namespace sta diff --git a/include/sta/debug/printing/printable.hpp b/include/sta/debug/printing/printable.hpp index c1b5bbc..4dc5a26 100644 --- a/include/sta/debug/printing/printable.hpp +++ b/include/sta/debug/printing/printable.hpp @@ -23,12 +23,16 @@ namespace sta */ enum class IntegerBase { - DEC, /**< Decimal */ - BIN, /**< Binary */ - HEX /**< Hexadecimal */ + BASE_DEC, /**< Decimal */ + BASE_BIN, /**< Binary */ + BASE_HEX /**< Hexadecimal */ }; - class Printable + /** + * @brief Base class for printable. Renamed to "BasePrintable" from "Printable" because the Arduino dev are dickheads. + * + */ + class BasePrintable { public: /** @@ -64,7 +68,7 @@ namespace sta * @param num 8-bit unsigned integer * @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. @@ -72,7 +76,7 @@ namespace sta * @param num 16-bit unsigned integer * @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. @@ -80,7 +84,7 @@ namespace sta * @param num 32-bit unsigned integer * @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. @@ -129,7 +133,7 @@ namespace sta * @param num 8-bit unsigned integer * @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. @@ -137,7 +141,7 @@ namespace sta * @param num 16-bit unsigned integer * @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. @@ -145,7 +149,7 @@ namespace sta * @param num 32-bit unsigned integer * @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. diff --git a/include/sta/debug/printing/printable_printf.hpp b/include/sta/debug/printing/printable_printf.hpp index 2ef9ed6..1c26b0a 100644 --- a/include/sta/debug/printing/printable_printf.hpp +++ b/include/sta/debug/printing/printable_printf.hpp @@ -5,7 +5,7 @@ namespace sta { - class PrintablePrintf : public Printable + class PrintablePrintf : public BasePrintable { public: /** diff --git a/include/sta/debug/printing/printable_uart.hpp b/include/sta/debug/printing/printable_uart.hpp index d6ea375..85eac01 100644 --- a/include/sta/debug/printing/printable_uart.hpp +++ b/include/sta/debug/printing/printable_uart.hpp @@ -25,7 +25,7 @@ namespace sta * * @ingroup sta_core */ - class PrintableUART : public Printable + class PrintableUART : public BasePrintable { public: /** diff --git a/src/debug/printing/printable.cpp b/src/debug/printing/printable.cpp index f70582c..ead1af9 100644 --- a/src/debug/printing/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -19,7 +19,7 @@ namespace sta { - void Printable::printf(const char * fmt, ...) + void BasePrintable::printf(const char * fmt, ...) { va_list args; va_start (args, fmt); @@ -28,116 +28,116 @@ namespace sta int n = vsnprintf(temp, 1, fmt, args); va_start (args, fmt); - char str[n]; - vsnprintf(str, n, fmt, args); + char str[n+1]; + vsnprintf(str, n+1, fmt, args); STA_ASSERT(n > 0); println(str); } - void Printable::print(char c) + void BasePrintable::print(char c) { print(&c, 1); } - void Printable::print(bool b) + void BasePrintable::print(bool b) { print(b ? "true" : "false"); } - void Printable::print(double d) + void BasePrintable::print(double d) { char buffer[64]; snprintf(buffer, sizeof(buffer), "%f", d); print(buffer); } - void Printable::print(uint8_t num, IntegerBase base) + void BasePrintable::print(uint8_t num, IntegerBase base) { 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)); } - void Printable::print(uint32_t num, IntegerBase base) + void BasePrintable::print(uint32_t num, IntegerBase base) { printBase(num, base, "%" PRIu32, sizeof(num)); } - void Printable::print(const char * str) + void BasePrintable::print(const char * str) { print(str, strlen(str)); } - void Printable::println() + void BasePrintable::println() { print("\r\n", 2); } - void Printable::println(char c) + void BasePrintable::println(char c) { print(&c, 1); println(); } - void Printable::println(bool b) + void BasePrintable::println(bool b) { print(b); println(); } - void Printable::println(double d) + void BasePrintable::println(double d) { print(d); println(); } - void Printable::println(uint8_t num, IntegerBase base) + void BasePrintable::println(uint8_t num, IntegerBase base) { print(num, base); println(); } - void Printable::println(uint16_t num, IntegerBase base) + void BasePrintable::println(uint16_t num, IntegerBase base) { print(num, base); println(); } - void Printable::println(uint32_t num, IntegerBase base) + void BasePrintable::println(uint32_t num, IntegerBase base) { print(num, base); println(); } - void Printable::println(const char * str) + void BasePrintable::println(const char * 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); 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) { - case IntegerBase::DEC: + case IntegerBase::BASE_DEC: printDec(num, fmt); break; - case IntegerBase::BIN: + case IntegerBase::BASE_BIN: // Digits in base 2 = size in bytes * 8 printBin(num, size * 8); break; - case IntegerBase::HEX: + case IntegerBase::BASE_HEX: // Digits in base 16 = size in bytes * 2 printHex(num, size * 2); 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]; snprintf(buffer, sizeof(buffer), fmt, static_cast(num)); 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 char buffer[sizeof(value) * 8]; @@ -179,7 +179,7 @@ namespace sta 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 char buffer[sizeof(value) * 2]; From 89d440ea20f2f97ecf8690c78541ec4726100b74 Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 26 Nov 2023 18:37:50 +0100 Subject: [PATCH 6/9] added debug printing for arduino --- .../sta/debug/printing/printable_serial.hpp | 29 +++++++++++++++++++ src/debug/printing/printable_serial.cpp | 21 ++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 include/sta/debug/printing/printable_serial.hpp create mode 100644 src/debug/printing/printable_serial.cpp diff --git a/include/sta/debug/printing/printable_serial.hpp b/include/sta/debug/printing/printable_serial.hpp new file mode 100644 index 0000000..af7966a --- /dev/null +++ b/include/sta/debug/printing/printable_serial.hpp @@ -0,0 +1,29 @@ +#ifndef STA_CORE_PRINTABLE_SERIAL_HPP +#define STA_CORE_PRINTABLE_SERIAL_HPP + +#include +#ifdef STA_PLATFORM_ARDUINO + +namespace sta +{ + class PrintableSerial: public sta::BasePrintable + { + public: + /** + * @brief Construct a new Printable Serial object + */ + PrintableSerial(unsigned long baud); + + /** + * @brief Print string. + * + * @param str String buffer + * @param length String length + */ + void print(const char * str, size_t length) override; + }; +} // namespace sta + +#endif // STA_PLATFORM_ARDUINO + +#endif // STA_CORE_PRINTABLE_SERIAL_HPP \ No newline at end of file diff --git a/src/debug/printing/printable_serial.cpp b/src/debug/printing/printable_serial.cpp new file mode 100644 index 0000000..db37e62 --- /dev/null +++ b/src/debug/printing/printable_serial.cpp @@ -0,0 +1,21 @@ +#include + +#ifdef STA_PLATFORM_ARDUINO + +#include + +namespace sta +{ + PrintableSerial::PrintableSerial(unsigned long baud) + : BasePrintable() + { + Serial.begin(baud); + } + + void PrintableSerial::print(const char * str, size_t length) + { + Serial.write(str); + } +} // namespace sta + +#endif // STA_PLATFORM_ARDUINO \ No newline at end of file From dbe25b0cc4df370b44492f39b64531729cdb7823 Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 26 Nov 2023 18:39:09 +0100 Subject: [PATCH 7/9] Renamed debug folder to debugging to avoid gitignore nonsense --- src/{debug => debugging}/assert.cpp | 0 src/{debug => debugging}/printing/printable.cpp | 0 src/{debug => debugging}/printing/printable_printf.cpp | 0 src/{debug => debugging}/printing/printable_serial.cpp | 0 src/{debug => debugging}/printing/printable_uart.cpp | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename src/{debug => debugging}/assert.cpp (100%) rename src/{debug => debugging}/printing/printable.cpp (100%) rename src/{debug => debugging}/printing/printable_printf.cpp (100%) rename src/{debug => debugging}/printing/printable_serial.cpp (100%) rename src/{debug => debugging}/printing/printable_uart.cpp (100%) diff --git a/src/debug/assert.cpp b/src/debugging/assert.cpp similarity index 100% rename from src/debug/assert.cpp rename to src/debugging/assert.cpp diff --git a/src/debug/printing/printable.cpp b/src/debugging/printing/printable.cpp similarity index 100% rename from src/debug/printing/printable.cpp rename to src/debugging/printing/printable.cpp diff --git a/src/debug/printing/printable_printf.cpp b/src/debugging/printing/printable_printf.cpp similarity index 100% rename from src/debug/printing/printable_printf.cpp rename to src/debugging/printing/printable_printf.cpp diff --git a/src/debug/printing/printable_serial.cpp b/src/debugging/printing/printable_serial.cpp similarity index 100% rename from src/debug/printing/printable_serial.cpp rename to src/debugging/printing/printable_serial.cpp diff --git a/src/debug/printing/printable_uart.cpp b/src/debugging/printing/printable_uart.cpp similarity index 100% rename from src/debug/printing/printable_uart.cpp rename to src/debugging/printing/printable_uart.cpp From a95fc2a0ab83e14d893b41ad1886747165202471 Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 26 Nov 2023 19:16:45 +0100 Subject: [PATCH 8/9] Renamed debugging folder back to debug --- .gitignore | 2 +- include/sta/config.hpp | 5 ----- src/{debugging => debug}/assert.cpp | 0 src/{debugging => debug}/printing/printable.cpp | 0 src/{debugging => debug}/printing/printable_printf.cpp | 0 src/{debugging => debug}/printing/printable_serial.cpp | 0 src/{debugging => debug}/printing/printable_uart.cpp | 0 7 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 include/sta/config.hpp rename src/{debugging => debug}/assert.cpp (100%) rename src/{debugging => debug}/printing/printable.cpp (100%) rename src/{debugging => debug}/printing/printable_printf.cpp (100%) rename src/{debugging => debug}/printing/printable_serial.cpp (100%) rename src/{debugging => debug}/printing/printable_uart.cpp (100%) diff --git a/.gitignore b/.gitignore index 4b5a695..53fb342 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ *.launch # Build artifacts -Debug/ +# Debug/ Release/ # Doxygen diff --git a/include/sta/config.hpp b/include/sta/config.hpp deleted file mode 100644 index 6864f86..0000000 --- a/include/sta/config.hpp +++ /dev/null @@ -1,5 +0,0 @@ -#include - -#define STA_PRINTF_USE_STDLIB -#define STA_STDLIB_DISABLE -#define STA_DEBUGGING_ENABLED \ No newline at end of file diff --git a/src/debugging/assert.cpp b/src/debug/assert.cpp similarity index 100% rename from src/debugging/assert.cpp rename to src/debug/assert.cpp diff --git a/src/debugging/printing/printable.cpp b/src/debug/printing/printable.cpp similarity index 100% rename from src/debugging/printing/printable.cpp rename to src/debug/printing/printable.cpp diff --git a/src/debugging/printing/printable_printf.cpp b/src/debug/printing/printable_printf.cpp similarity index 100% rename from src/debugging/printing/printable_printf.cpp rename to src/debug/printing/printable_printf.cpp diff --git a/src/debugging/printing/printable_serial.cpp b/src/debug/printing/printable_serial.cpp similarity index 100% rename from src/debugging/printing/printable_serial.cpp rename to src/debug/printing/printable_serial.cpp diff --git a/src/debugging/printing/printable_uart.cpp b/src/debug/printing/printable_uart.cpp similarity index 100% rename from src/debugging/printing/printable_uart.cpp rename to src/debug/printing/printable_uart.cpp From 4f1db202ee00496578e98b9804af9d7306f46590 Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 26 Nov 2023 19:18:35 +0100 Subject: [PATCH 9/9] Updated gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 53fb342..4b5a695 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ *.launch # Build artifacts -# Debug/ +Debug/ Release/ # Doxygen