|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2019 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select the ESP chip type and the backup and |
|
8 restore parameters. |
|
9 """ |
|
10 |
|
11 import os |
|
12 |
|
13 from PyQt6.QtCore import pyqtSlot |
|
14 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
15 |
|
16 from EricWidgets.EricPathPicker import EricPathPickerModes |
|
17 |
|
18 from .Ui_EspBackupRestoreFirmwareDialog import ( |
|
19 Ui_EspBackupRestoreFirmwareDialog |
|
20 ) |
|
21 |
|
22 |
|
23 class EspBackupRestoreFirmwareDialog(QDialog, |
|
24 Ui_EspBackupRestoreFirmwareDialog): |
|
25 """ |
|
26 Class implementing a dialog to select the ESP chip type and the backup and |
|
27 restore parameters. |
|
28 """ |
|
29 Chips = ( |
|
30 ("", ""), |
|
31 ("ESP32", "esp32"), |
|
32 ("ESP32-C3", "esp32c3"), |
|
33 ("ESP32-S2", "esp32s2"), |
|
34 ("ESP32-S3", "esp32s3"), |
|
35 ("ESP8266", "esp8266"), |
|
36 ) |
|
37 |
|
38 FlashModes = [ |
|
39 ("", ""), |
|
40 ("Quad I/O", "qio"), |
|
41 ("Quad Output", "qout"), |
|
42 ("Dual I/O", "dio"), |
|
43 ("Dual Output", "dout"), |
|
44 ] |
|
45 |
|
46 FlashSizes = { |
|
47 "esp32": [ |
|
48 (" 1 MB", "0x100000"), |
|
49 (" 2 MB", "0x200000"), |
|
50 (" 4 MB", "0x400000"), |
|
51 (" 8 MB", "0x800000"), |
|
52 ("16 MB", "0x1000000"), |
|
53 ], |
|
54 "esp32c3": [ |
|
55 (" 1 MB", "0x100000"), |
|
56 (" 2 MB", "0x200000"), |
|
57 (" 4 MB", "0x400000"), |
|
58 (" 8 MB", "0x800000"), |
|
59 ("16 MB", "0x1000000"), |
|
60 ], |
|
61 "esp32s2": [ |
|
62 (" 1 MB", "0x100000"), |
|
63 (" 2 MB", "0x200000"), |
|
64 (" 4 MB", "0x400000"), |
|
65 (" 8 MB", "0x800000"), |
|
66 ("16 MB", "0x1000000"), |
|
67 ], |
|
68 "esp32s3": [ |
|
69 (" 1 MB", "0x100000"), |
|
70 (" 2 MB", "0x200000"), |
|
71 (" 4 MB", "0x400000"), |
|
72 (" 8 MB", "0x800000"), |
|
73 ("16 MB", "0x1000000"), |
|
74 ], |
|
75 "esp8266": [ |
|
76 ("256 KB", "0x40000"), |
|
77 ("512 KB", "0x80000"), |
|
78 (" 1 MB", "0x100000"), |
|
79 (" 2 MB", "0x200000"), |
|
80 (" 4 MB", "0x400000"), |
|
81 (" 8 MB", "0x800000"), |
|
82 ("16 MB", "0x1000000"), |
|
83 ], |
|
84 } |
|
85 |
|
86 def __init__(self, backupMode=True, parent=None): |
|
87 """ |
|
88 Constructor |
|
89 |
|
90 @param backupMode flag indicating parameters for a firmware backup are |
|
91 requested |
|
92 @type bool |
|
93 @param parent reference to the parent widget |
|
94 @type QWidget |
|
95 """ |
|
96 super().__init__(parent) |
|
97 self.setupUi(self) |
|
98 |
|
99 self.__isBackupMode = backupMode |
|
100 |
|
101 for text, chip in self.Chips: |
|
102 self.espComboBox.addItem(text, chip) |
|
103 |
|
104 self.baudRateComboBox.addItems([ |
|
105 "74.880", "115.200", "230.400", "460.800", "921.600", "1.500.000"]) |
|
106 self.baudRateComboBox.setCurrentIndex(3) |
|
107 |
|
108 self.firmwarePicker.setFilters( |
|
109 self.tr("Firmware Files (*.img);;All Files (*)")) |
|
110 if self.__isBackupMode: |
|
111 self.firmwarePicker.setMode( |
|
112 EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE) |
|
113 self.sizeInfoLabel.clear() |
|
114 self.modeComboBox.setEnabled(False) |
|
115 self.modeInfoLabel.setEnabled(False) |
|
116 self.setWindowTitle(self.tr("Backup Firmware")) |
|
117 else: |
|
118 self.firmwarePicker.setMode(EricPathPickerModes.OPEN_FILE_MODE) |
|
119 for text, mode in self.FlashModes: |
|
120 self.modeComboBox.addItem(text, mode) |
|
121 self.setWindowTitle(self.tr("Restore Firmware")) |
|
122 |
|
123 msh = self.minimumSizeHint() |
|
124 self.resize(max(self.width(), msh.width()), msh.height()) |
|
125 |
|
126 def __updateOkButton(self): |
|
127 """ |
|
128 Private method to update the state of the OK button. |
|
129 """ |
|
130 firmwareFile = self.firmwarePicker.text() |
|
131 enable = (bool(self.espComboBox.currentText()) and |
|
132 bool(firmwareFile)) |
|
133 if self.__isBackupMode: |
|
134 enable &= bool(self.sizeComboBox.currentText()) |
|
135 else: |
|
136 enable &= os.path.exists(firmwareFile) |
|
137 self.buttonBox.button( |
|
138 QDialogButtonBox.StandardButton.Ok).setEnabled(enable) |
|
139 |
|
140 @pyqtSlot(str) |
|
141 def on_espComboBox_currentTextChanged(self, chip): |
|
142 """ |
|
143 Private slot to handle the selection of a chip type. |
|
144 |
|
145 @param chip selected chip type |
|
146 @type str |
|
147 """ |
|
148 selectedSize = self.sizeComboBox.currentText() |
|
149 self.sizeComboBox.clear() |
|
150 chipType = self.espComboBox.currentData() |
|
151 if chipType and chipType in self.FlashSizes: |
|
152 self.sizeComboBox.addItem("") |
|
153 for text, data in self.FlashSizes[chipType]: |
|
154 self.sizeComboBox.addItem(text, data) |
|
155 |
|
156 self.sizeComboBox.setCurrentText(selectedSize) |
|
157 |
|
158 self.__updateOkButton() |
|
159 |
|
160 @pyqtSlot(str) |
|
161 def on_sizeComboBox_currentTextChanged(self, size): |
|
162 """ |
|
163 Private slot handling a change of the selected firmware size. |
|
164 |
|
165 @param size selected size text |
|
166 @type str |
|
167 """ |
|
168 self.__updateOkButton() |
|
169 |
|
170 @pyqtSlot(str) |
|
171 def on_firmwarePicker_textChanged(self, firmware): |
|
172 """ |
|
173 Private slot handling a change of the firmware path. |
|
174 |
|
175 @param firmware path to the firmware |
|
176 @type str |
|
177 """ |
|
178 self.__updateOkButton() |
|
179 |
|
180 def getData(self): |
|
181 """ |
|
182 Public method to get the entered data. |
|
183 |
|
184 @return tuple containing the selected chip type, the firmware size, |
|
185 the baud rate or flashing, the flash mode and the path of the |
|
186 firmware file |
|
187 @rtype tuple of (str, str, str, str, str) |
|
188 """ |
|
189 flashSize = ( |
|
190 self.sizeComboBox.currentData() |
|
191 if self.__isBackupMode else |
|
192 self.sizeComboBox.currentText().replace(" ", "") |
|
193 ) |
|
194 |
|
195 return ( |
|
196 self.espComboBox.currentData(), |
|
197 flashSize, |
|
198 self.baudRateComboBox.currentText().replace(".", ""), |
|
199 self.modeComboBox.currentData(), |
|
200 self.firmwarePicker.text(), |
|
201 ) |