Improved printable implementation; data reading with macro

This commit is contained in:
dario 2024-06-26 17:41:56 +02:00 committed by dario
parent b9ccb2d65f
commit ebb14896b8
7 changed files with 56 additions and 16 deletions

View File

@ -35,16 +35,17 @@ namespace sta
* *
* @param fmt See @ref sta::Printable::printf * @param fmt See @ref sta::Printable::printf
* @param ... See @ref sta::Printable::printf * @param ... See @ref sta::Printable::printf
*
* @ingroup sta_core_debug
*/ */
# define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__) # define STA_DEBUG_PRINTF(fmt, ...) sta::Debug->printf(fmt, __VA_ARGS__)
/** /**
* @brief Read string via printable. * @brief Read data via printable.
* *
* @param ... See @ref sta::Printable::read * @param ... See @ref sta::Printable::read
* *
* @ingroup sta_core_debug * @ingroup sta_core_debug
*
*/ */
# define STA_DEBUG_READ(buffer, length) sta::Debug->read(buffer, length) # define STA_DEBUG_READ(buffer, length) sta::Debug->read(buffer, length)

View File

@ -170,13 +170,37 @@ namespace sta
*/ */
void println(const char * str, size_t length); void println(const char * str, size_t length);
public: public:
/**
* @brief Read bytes.
*
* @param buffer Byte buffer
* @param length Buffer length
*/
virtual void read(uint8_t * buffer, size_t length) = 0;
/** /**
* @brief Read string. * @brief Read string.
* *
* @param str String buffer * @param str String buffer
* @param length String length * @param length String length
*/ */
virtual void read(char* str, size_t length) = 0; void read(char * str, size_t length);
/**
* @brief Read floats.
*
* @param buffer Float buffer
* @param length Buffer length
*/
void read(float * buffer, size_t length);
/**
* @brief Read doubles.
*
* @param buffer Double buffer
* @param length Buffer length
*/
void read(double * buffer, size_t length);
private: private:
/** /**

View File

@ -28,12 +28,12 @@ namespace sta
void print(const char * str, size_t length, bool newline = false) override; void print(const char * str, size_t length, bool newline = false) override;
/** /**
* @brief Print string. * @brief Read bytes.
* *
* @param str String buffer * @param buffer Byte buffer
* @param length String length * @param length Buffer length
*/ */
void read(char * str, size_t length) override; void read(uint8_t * buffer, size_t length) override;
}; };
} // namespace sta } // namespace sta

View File

@ -37,12 +37,12 @@ namespace sta
void print(const char * str, size_t length, bool newline = false) override; void print(const char * str, size_t length, bool newline = false) override;
/** /**
* @brief Read string. * @brief Read bytes.
* *
* @param str String buffer * @param buffer Byte buffer
* @param length String length * @param length Buffer length
*/ */
void read(char * str, size_t length) override; void read(uint8_t * str, size_t length) override;
private: private:
UART * intf_; UART * intf_;

View File

@ -193,4 +193,19 @@ namespace sta
print(buffer, digits, newline); print(buffer, digits, newline);
} }
void Printable::read(char * str, size_t length)
{
read(reinterpret_cast<uint8_t*>(str), length * sizeof(char));
}
void Printable::read(float * buffer, size_t length)
{
read(reinterpret_cast<uint8_t*>(buffer), length * sizeof(float));
}
void Printable::read(double * buffer, size_t length)
{
read(reinterpret_cast<uint8_t*>(buffer), length * sizeof(double));
}
} // namespace sta } // namespace sta

View File

@ -20,7 +20,7 @@ namespace sta
printf("\r\n"); printf("\r\n");
} }
void PrintablePrintf::read(char * str, size_t length) void PrintablePrintf::read(uint8_t * str, size_t length)
{ {
STA_ASSERT(str != nullptr); STA_ASSERT(str != nullptr);
STA_ASSERT(length > 0); STA_ASSERT(length > 0);

View File

@ -27,10 +27,10 @@ namespace sta
intf_->release(); intf_->release();
} }
void PrintableUART::read(char * str, size_t length) void PrintableUART::read(uint8_t * buffer, size_t length)
{ {
intf_->acquire(); intf_->acquire();
intf_->receive(reinterpret_cast<uint8_t *>(str), length); intf_->receive(buffer, length);
intf_->release(); intf_->release();
} }