Skip to content

Add progress callback to Update::writeStream(). #948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libraries/Update/src/Update.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <Arduino.h>
#include <MD5Builder.h>
#include <functional>
#include "esp_partition.h"

#define UPDATE_ERROR_OK (0)
Expand All @@ -27,7 +28,15 @@

class UpdateClass {
public:
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;

UpdateClass();

/*
This callback will be called when Update is receiving data
*/
UpdateClass& onProgress(THandlerFunction_Progress fn);

/*
Call this to check the space needed for the update
Will return false if there is not enough space
Expand Down Expand Up @@ -153,6 +162,8 @@ class UpdateClass {
bool _verifyHeader(uint8_t data);
bool _verifyEnd();

THandlerFunction_Progress _progress_callback;

uint8_t _error;
uint8_t *_buffer;
size_t _bufferLen;
Expand Down
16 changes: 15 additions & 1 deletion libraries/Update/src/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ UpdateClass::UpdateClass()
, _progress(0)
, _command(U_FLASH)
, _partition(NULL)
, _progress_callback(NULL)
{
}

UpdateClass& UpdateClass::onProgress(THandlerFunction_Progress fn) {
_progress_callback = fn;
return *this;
}

void UpdateClass::_reset() {
if (_buffer)
delete[] _buffer;
Expand Down Expand Up @@ -306,7 +312,9 @@ size_t UpdateClass::writeStream(Stream &data) {
_reset();
return 0;
}

if (_progress_callback) {
_progress_callback(0, _size);
}
while(remaining()) {
toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen));
if(toRead == 0) { //Timeout
Expand All @@ -321,6 +329,12 @@ size_t UpdateClass::writeStream(Stream &data) {
if((_bufferLen == remaining() || _bufferLen == SPI_FLASH_SEC_SIZE) && !_writeBuffer())
return written;
written += toRead;
if(_progress_callback) {
_progress_callback(_progress, _size);
}
}
if(_progress_callback) {
_progress_callback(_size, _size);
}
return written;
}
Expand Down