From 9826479b5dd7a33a4097577b8956d316b9085373 Mon Sep 17 00:00:00 2001 From: "contact@brokestudio.fr" Date: Mon, 6 Nov 2023 09:52:09 +0100 Subject: [PATCH] rainbow: fixed FILE_WRITE and FILE_APPEND commands --- src/boards/rainbow_esp.cpp | 25 +++++++++++++++++++++---- src/boards/rainbow_esp.h | 2 ++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/boards/rainbow_esp.cpp b/src/boards/rainbow_esp.cpp index c9a560c8..11f1e96f 100644 --- a/src/boards/rainbow_esp.cpp +++ b/src/boards/rainbow_esp.cpp @@ -920,7 +920,7 @@ void BrokeStudioFirmware::processBufferedMessage() break; case toesp_cmds_t::FILE_WRITE: UDBG("[Rainbow] ESP received command FILE_WRITE\n"); - if (message_size >= 3 && this->working_file.active) + if (message_size >= 2 && this->working_file.active) { this->writeFile(this->rx_buffer.begin() + 2, this->rx_buffer.begin() + message_size + 1); this->working_file.offset += message_size - 1; @@ -928,10 +928,9 @@ void BrokeStudioFirmware::processBufferedMessage() break; case toesp_cmds_t::FILE_APPEND: UDBG("[Rainbow] ESP received command FILE_APPEND\n"); - if (message_size >= 3 && this->working_file.active) + if (message_size >= 2 && this->working_file.active) { - UDBG("[Rainbow] ESP command FILE_APPEND not implemented\n"); - // this->appendFile(this->rx_buffer.begin() + 2, this->rx_buffer.begin() + message_size + 1); + this->appendFile(this->rx_buffer.begin() + 2, this->rx_buffer.begin() + message_size + 1); } break; case toesp_cmds_t::FILE_COUNT: @@ -1415,6 +1414,24 @@ void BrokeStudioFirmware::writeFile(I data_begin, I data_end) } } +template +void BrokeStudioFirmware::appendFile(I data_begin, I data_end) +{ + if (this->working_file.active == false) { + return; + } + + auto const data_size = data_end - data_begin; + size_t file_size = this->working_file.file->data.size(); + uint32_t const offset_end = file_size + data_size; + this->working_file.file->data.resize(offset_end, 0); + + for (vector::size_type i = file_size; i < offset_end; ++i) { + this->working_file.file->data[i] = *data_begin; + ++data_begin; + } +} + void BrokeStudioFirmware::saveFiles() { #ifdef _WIN32 diff --git a/src/boards/rainbow_esp.h b/src/boards/rainbow_esp.h index 95264dc2..e5340a4f 100644 --- a/src/boards/rainbow_esp.h +++ b/src/boards/rainbow_esp.h @@ -284,6 +284,8 @@ private: void readFile(uint8_t n); template void writeFile(I data_begin, I data_end); + template + void appendFile(I data_begin, I data_end); void saveFiles(); void saveFile(uint8_t drive, char const* filename); void loadFiles();