Skip to content

Commit 2633fc3

Browse files
everslickme-no-dev
authored andcommitted
Add progress callback to Update::writeStream(). (espressif#948)
1 parent 78acedd commit 2633fc3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

libraries/Update/src/Update.h

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <Arduino.h>
55
#include <MD5Builder.h>
6+
#include <functional>
67
#include "esp_partition.h"
78

89
#define UPDATE_ERROR_OK (0)
@@ -27,7 +28,15 @@
2728

2829
class UpdateClass {
2930
public:
31+
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;
32+
3033
UpdateClass();
34+
35+
/*
36+
This callback will be called when Update is receiving data
37+
*/
38+
UpdateClass& onProgress(THandlerFunction_Progress fn);
39+
3140
/*
3241
Call this to check the space needed for the update
3342
Will return false if there is not enough space
@@ -153,6 +162,8 @@ class UpdateClass {
153162
bool _verifyHeader(uint8_t data);
154163
bool _verifyEnd();
155164

165+
THandlerFunction_Progress _progress_callback;
166+
156167
uint8_t _error;
157168
uint8_t *_buffer;
158169
size_t _bufferLen;

libraries/Update/src/Updater.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@ UpdateClass::UpdateClass()
7171
, _progress(0)
7272
, _command(U_FLASH)
7373
, _partition(NULL)
74+
, _progress_callback(NULL)
7475
{
7576
}
7677

78+
UpdateClass& UpdateClass::onProgress(THandlerFunction_Progress fn) {
79+
_progress_callback = fn;
80+
return *this;
81+
}
82+
7783
void UpdateClass::_reset() {
7884
if (_buffer)
7985
delete[] _buffer;
@@ -306,7 +312,9 @@ size_t UpdateClass::writeStream(Stream &data) {
306312
_reset();
307313
return 0;
308314
}
309-
315+
if (_progress_callback) {
316+
_progress_callback(0, _size);
317+
}
310318
while(remaining()) {
311319
toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen));
312320
if(toRead == 0) { //Timeout
@@ -321,6 +329,12 @@ size_t UpdateClass::writeStream(Stream &data) {
321329
if((_bufferLen == remaining() || _bufferLen == SPI_FLASH_SEC_SIZE) && !_writeBuffer())
322330
return written;
323331
written += toRead;
332+
if(_progress_callback) {
333+
_progress_callback(_progress, _size);
334+
}
335+
}
336+
if(_progress_callback) {
337+
_progress_callback(_size, _size);
324338
}
325339
return written;
326340
}

0 commit comments

Comments
 (0)