forked from D3R-ST3FAN/LaCrosseGatewayMQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialPortFlasher.cpp
More file actions
122 lines (97 loc) · 2.9 KB
/
Copy pathSerialPortFlasher.cpp
File metadata and controls
122 lines (97 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "SerialPortFlasher.h"
SerialPortFlasher::SerialPortFlasher() {
////m_isUploading = false;
m_state = State::Finished;
}
void SerialPortFlasher::Begin() {
m_receivedSize = 0;
m_lastReception = millis();
////m_isUploading = true;
m_sizeBuffer = "";
m_state = State::GetSize;
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
Update.begin(maxSketchSpace);
}
////void SerialPortFlasher::End() {
//// m_isUploading = false;
////}
bool SerialPortFlasher::IsUploading() {
return m_state != State::Finished;
}
/*
Protocol
-------------------------------------------------------
-> 418320S file size
<- 1
-> b b: Byte = nbr. of data bytes
-> bbbbb ... b data bytes
<- C C: byte with checksum
-> b b: Byte = nbr. of data bytes
-> bbbbb ... b data bytes
<- b b: byte with checksum
...
-> b b: Byte = nbr. of data bytes
-> bbbbb ... b data bytes
<- b b: byte with checksum
<- Finish Message
*/
void SerialPortFlasher::Add(byte b) {
m_lastReception = millis();
if(m_state == State::GetSize) {
if((char)b == 'S') {
m_expectedSize = m_sizeBuffer.toInt();
m_state = State::GetBlockSize;
Serial.print(1);
}
else {
m_sizeBuffer += (char)b;
}
}
else if(m_state == State::GetBlockSize) {
m_blockSize = b;
m_state = State::GetBytes;
m_dataBufferPointer = 0;
m_checksum = 0;
}
else if(m_state == State::GetBytes) {
m_dataBuffer[m_dataBufferPointer++] = b;
m_checksum += b;
m_receivedSize++;
if(m_dataBufferPointer == m_blockSize) {
Update.write(m_dataBuffer, m_blockSize);
m_state = State::GetBlockSize;
Serial.print(m_checksum);
if(m_receivedSize == m_expectedSize) {
m_state = State::Finished;
Serial.print("Finished");
Serial.println("Update.end: " + String(Update.end(true)));
Serial.print("Error: ");
Update.printError(Serial);
ESP.restart();
}
}
}
//// m_receivedSize++;
////if (Update.write(&b, 1) != 1) {
//// Serial.println(Update.getError());
////}
////Serial.println(b, HEX);
////if (m_receivedSize % 5000 == 0) {
//// Serial.println(String(m_receivedSize));
////}
////Serial.print(b);
////Serial.print(".");
}
void SerialPortFlasher::Handle() {
if (IsUploading() && millis() > m_lastReception + 2000) {
Serial.println("ERROR: timeout");
m_state = State::Finished;
////Serial.println("Finished: " + String(m_receivedSize));
////delay(100);
////Serial.println("Update.end: " + String(Update.end(true)));
////Serial.print("Error: ");
////Update.printError(Serial);
////Serial.println("restart");
////ESP.restart();
}
}