File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed
Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 33
44#include < Arduino.h>
55#include < MD5Builder.h>
6+ #include < functional>
67#include " esp_partition.h"
78
89#define UPDATE_ERROR_OK (0 )
2728
2829class 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;
Original file line number Diff line number Diff 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+
7783void 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}
You can’t perform that action at this time.
0 commit comments