Skip to content
Snippets Groups Projects
Commit f1b6a19f authored by Tommaso Lamon's avatar Tommaso Lamon Committed by Tommaso Lamon
Browse files

[USART] Created new function writeFile

This function, given a filename sends the content of
the file through the USART interface in 1 KB chunks
parent ba079bd7
No related branches found
No related tags found
1 merge request!345[USART] Introduce a way to send a file as binary stream
Pipeline #10897 passed
......@@ -27,6 +27,7 @@
#include <utils/Debug.h>
#include <utils/Numeric.h>
#include <fstream>
#include <string>
#include "arch/common/drivers/serial.h"
......@@ -592,6 +593,26 @@ void USART::writeString(const char* buffer)
};
}
bool USART::writeFile(const std::string& fileName)
{
std::ifstream file(fileName, std::ifstream::binary);
std::vector<uint8_t> buffer(1024, 0); // 1024 Bytes buffer.
if (!file.is_open())
{
LOG_ERR(logger, "Failed to open file {}", fileName);
return false;
}
while (file.read(reinterpret_cast<char*>(buffer.data()), buffer.size()))
{
std::streamsize s = file.gcount();
if (s > 0)
write(buffer.data(), static_cast<size_t>(s));
}
return true;
}
void USART::clearQueue()
{
char buf[INTERNAL_QUEUE_LENGTH];
......
......@@ -267,6 +267,14 @@ public:
*/
void writeString(const char* buffer);
/**
* @brief Given a filename, uses the USART interface to stream the file in
* 1KB chunks
* @param fileName String containing the name of the file to stream.
* @return A boolean value (true if stream is complete, false otherwise).
*/
bool writeFile(const std::string& fileName);
/**
* @brief Set the length of the word to 8 or to 9.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment