forked from DFRobot/uPyCraft_src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
threadDownloadFirmware.py
244 lines (202 loc) · 7.25 KB
/
threadDownloadFirmware.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import time
import threading
import uPyCraft_src.Esp
import urllib
from urllib import request
import socket
import shutil
import codecs
from PyQt5.QtCore import *
from uPyCraft_src.Esp import ESPTool, Burn, downOkReset
class threadUserFirmware(QThread):
firmwareAnyDown = pyqtSignal(int)
firmwareAnyUpdate = pyqtSignal(int)
firmwareAnyErase = pyqtSignal(int)
goMicrobitUpdate = pyqtSignal()
def __init__(self, board, savepath, com, iserase, size, addr, parent):
super(threadUserFirmware,self).__init__(parent)
self.board=board
self.savepath=savepath
self.com=com
self.iserase=iserase
self.size=size
self.erasePer=0
self.erasestart=False
self.erasetimer=None
self.burnaddr=0
if addr=="0x0":
self.burnaddr=0
else:
self.burnaddr=0x1000
print("burnaddr=====%d"%self.burnaddr)
def run(self):
esptool=ESPTool()
esptool.percentchange.connect(self.updateFirmwarePer)
esptool.eraseStartSig.connect(self.eraseStart)
if self.iserase=="yes":
self.erasetimer=threading.Timer(0.1,self.eraseTimer)
self.erasetimer.start()
try:
Burn(esptool,str(self.board),self.savepath,self.com,True)
time.sleep(1)
self.erasePer=100
self.firmwareAnyErase.emit(self.erasePer)
self.erasetimer.cancel()
except:
time.sleep(1)
self.erasePer=-1
self.firmwareAnyErase.emit(self.erasePer)
self.erasetimer.cancel()
self.erasestart=False
self.exit()
return
if self.iserase=="yes":
self.firmwareAnyErase.emit(100)
try:
if self.board=="esp32" or self.board=="esp8266" or self.board=="TPYBoardV202":
Burn(esptool,str(self.board),self.savepath,self.com,False,self.burnaddr)
else:#microbit
print("In threaddownloadfirmware:savepath=%s"%self.savepath)
self.firmwareAnyErase.emit(-2)
time.sleep(0.5)
self.goMicrobitUpdate.emit()
except:
self.firmwareAnyUpdate.emit(-1)
self.exit()
return
if self.board=="esp8266":
downOkReset()
self.exit()
def cbdownFramware(self,blocknum,blocksize,totalsize):
print(blocknum)
print(blocksize)
print(totalsize)
per=100.0*blocknum*blocksize/self.size
if per>=100:
per=100
self.firmwareAnyDown.emit(per)
return
self.firmwareAnyDown.emit(per)
def updateFirmwarePer(self,per):
print("updateFirmwarePer:%d"%per)
self.firmwareAnyUpdate.emit(per)
def eraseStart(self):
self.erasestart=True
def eraseTimer(self):
if self.erasestart==True:
self.erasePer+=0.5
if self.erasePer>=99:
self.erasePer=99
self.firmwareAnyErase.emit(self.erasePer)
self.erasestart=False
return
self.firmwareAnyErase.emit(self.erasePer)
self.erasetimer=threading.Timer(0.1,self.eraseTimer)
self.erasetimer.start()
class threadDownloadFirmware(QThread):
firmwareAnyDown = pyqtSignal(int)
firmwareAnyUpdate = pyqtSignal(int)
firmwareAnyErase = pyqtSignal(int)
goMicrobitUpdate = pyqtSignal()
def __init__(self, url, board, savepath, com, iserase, size, addr, parent):
super(threadDownloadFirmware,self).__init__(parent)
self.url=url
self.board=board
self.savepath=savepath
self.com=com
self.iserase=iserase
self.size=size
self.erasePer=0
self.reDownloadNum=0
self.downloadOk=False
self.erasestart=False
self.erasetimer=None
self.burnaddr=0
if addr=="0x0":
self.burnaddr=0
else:
self.burnaddr=0x1000
print("burnaddr2=====%d"%self.burnaddr)
def run(self):
self.reDownload()
if self.downloadOk==True:
esptool=ESPTool()
esptool.percentchange.connect(self.updateFirmwarePer)
esptool.eraseStartSig.connect(self.eraseStart)
if self.iserase=="yes":
self.erasetimer=threading.Timer(0.1,self.eraseTimer)
self.erasetimer.start()
try:
Burn(esptool,str(self.board),self.savepath,self.com,True)
time.sleep(1)
self.erasePer=100
self.firmwareAnyErase.emit(self.erasePer)
self.erasetimer.cancel()
except:
time.sleep(1)
self.erasePer=-1
self.firmwareAnyErase.emit(self.erasePer)
self.erasetimer.cancel()
self.erasestart=False
self.exit()
return
if self.iserase=="yes":
self.firmwareAnyErase.emit(100)
try:
if self.board=="esp32" or self.board=="esp8266" or self.board=="TPYBoardV202":
Burn(esptool,str(self.board),self.savepath,self.com,False,self.burnaddr)
else:#microbit
print("In threaddownloadfirmware:savepath=%s"%self.savepath)
self.firmwareAnyUpdate.emit(-2)
time.sleep(0.5)
self.goMicrobitUpdate.emit()
except:
self.firmwareAnyUpdate.emit(-1)
self.exit()
return
if self.board=="esp8266" or self.board=="TPYBoardV202":
downOkReset()
self.exit()
def reDownload(self):
if self.reDownloadNum==3:
self.downloadOk=False
self.firmwareAnyDown.emit(-1)
return
try:
socket.setdefaulttimeout(5)
request.urlretrieve(self.url,self.savepath,self.cbdownFramware)
self.downloadOk=True
return
except:
print("urllib err :%s"%self.url)
self.reDownloadNum+=1
self.reDownload()
def cbdownFramware(self,blocknum,blocksize,totalsize):
print(blocknum)
print(blocksize)
print(totalsize)
per=100.0*blocknum*blocksize/self.size
if per>=100:
per=100
self.firmwareAnyDown.emit(per)
return
self.firmwareAnyDown.emit(per)
def updateFirmwarePer(self,per):
print("updateFirmwarePer:%d"%per)
self.firmwareAnyUpdate.emit(per)
def eraseStart(self):
self.erasestart=True
def eraseTimer(self):
if self.erasestart==True:
self.erasePer+=0.5
if self.erasePer>=99:
self.erasePer=99
self.firmwareAnyErase.emit(self.erasePer)
self.erasestart=False
return
self.firmwareAnyErase.emit(self.erasePer)
self.erasetimer=threading.Timer(0.1,self.eraseTimer)
self.erasetimer.start()