File tree 2 files changed +26
-1
lines changed
2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 3
3
4
4
#include < Arduino.h>
5
5
#include < MD5Builder.h>
6
+ #include < functional>
6
7
#include " esp_partition.h"
7
8
8
9
#define UPDATE_ERROR_OK (0 )
27
28
28
29
class UpdateClass {
29
30
public:
31
+ typedef std::function<void (size_t , size_t )> THandlerFunction_Progress;
32
+
30
33
UpdateClass ();
34
+
35
+ /*
36
+ This callback will be called when Update is receiving data
37
+ */
38
+ UpdateClass& onProgress (THandlerFunction_Progress fn);
39
+
31
40
/*
32
41
Call this to check the space needed for the update
33
42
Will return false if there is not enough space
@@ -153,6 +162,8 @@ class UpdateClass {
153
162
bool _verifyHeader (uint8_t data);
154
163
bool _verifyEnd ();
155
164
165
+ THandlerFunction_Progress _progress_callback;
166
+
156
167
uint8_t _error;
157
168
uint8_t *_buffer;
158
169
size_t _bufferLen;
Original file line number Diff line number Diff line change @@ -71,9 +71,15 @@ UpdateClass::UpdateClass()
71
71
, _progress(0 )
72
72
, _command(U_FLASH)
73
73
, _partition(NULL )
74
+ , _progress_callback(NULL )
74
75
{
75
76
}
76
77
78
+ UpdateClass& UpdateClass::onProgress (THandlerFunction_Progress fn) {
79
+ _progress_callback = fn;
80
+ return *this ;
81
+ }
82
+
77
83
void UpdateClass::_reset () {
78
84
if (_buffer)
79
85
delete[] _buffer;
@@ -306,7 +312,9 @@ size_t UpdateClass::writeStream(Stream &data) {
306
312
_reset ();
307
313
return 0 ;
308
314
}
309
-
315
+ if (_progress_callback) {
316
+ _progress_callback (0 , _size);
317
+ }
310
318
while (remaining ()) {
311
319
toRead = data.readBytes (_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen));
312
320
if (toRead == 0 ) { // Timeout
@@ -321,6 +329,12 @@ size_t UpdateClass::writeStream(Stream &data) {
321
329
if ((_bufferLen == remaining () || _bufferLen == SPI_FLASH_SEC_SIZE) && !_writeBuffer ())
322
330
return written;
323
331
written += toRead;
332
+ if (_progress_callback) {
333
+ _progress_callback (_progress, _size);
334
+ }
335
+ }
336
+ if (_progress_callback) {
337
+ _progress_callback (_size, _size);
324
338
}
325
339
return written;
326
340
}
You can’t perform that action at this time.
0 commit comments