Sat, 12 Feb 2022 13:02:35 +0100
MicroPython
- enhanced support for ESP32 devices
7352 | 1 | # -*- coding: utf-8 -*- |
2 | ||
8881
54e42bc2437a
Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
3 | # Copyright (c) 2019 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
7352 | 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 | ||
8318
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
13 | from PyQt6.QtCore import pyqtSlot |
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
14 | from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
7352 | 15 | |
8358
144a6b854f70
Sorted the eric specific extensions into packages named like the corresponding PyQt packages (i.e. EricCore,EricGui and EricWidgets).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8356
diff
changeset
|
16 | from EricWidgets.EricPathPicker import EricPathPickerModes |
7352 | 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 | """ | |
8924 | 29 | Chips = ( |
30 | ("", ""), | |
31 | ("ESP32", "esp32"), | |
32 | ("ESP32-C3", "esp32c3"), | |
33 | ("ESP32-S2", "esp32s2"), | |
34 | ("ESP32-S3", "esp32s3"), | |
35 | ("ESP8266", "esp8266"), | |
36 | ) | |
37 | ||
7595
5db6bfeff23e
MicroPython: added code to allow the user to select the flash mod for flashing the MicroPython firmware for ESP32/ESP8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
38 | FlashModes = [ |
8945 | 39 | ("", ""), |
7595
5db6bfeff23e
MicroPython: added code to allow the user to select the flash mod for flashing the MicroPython firmware for ESP32/ESP8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
40 | ("Quad I/O", "qio"), |
5db6bfeff23e
MicroPython: added code to allow the user to select the flash mod for flashing the MicroPython firmware for ESP32/ESP8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
41 | ("Quad Output", "qout"), |
5db6bfeff23e
MicroPython: added code to allow the user to select the flash mod for flashing the MicroPython firmware for ESP32/ESP8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
42 | ("Dual I/O", "dio"), |
5db6bfeff23e
MicroPython: added code to allow the user to select the flash mod for flashing the MicroPython firmware for ESP32/ESP8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
43 | ("Dual Output", "dout"), |
5db6bfeff23e
MicroPython: added code to allow the user to select the flash mod for flashing the MicroPython firmware for ESP32/ESP8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
44 | ] |
8924 | 45 | |
7352 | 46 | FlashSizes = { |
8924 | 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": [ | |
7352 | 55 | (" 1 MB", "0x100000"), |
56 | (" 2 MB", "0x200000"), | |
57 | (" 4 MB", "0x400000"), | |
58 | (" 8 MB", "0x800000"), | |
59 | ("16 MB", "0x1000000"), | |
60 | ], | |
8924 | 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": [ | |
7352 | 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 | """ | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8143
diff
changeset
|
96 | super().__init__(parent) |
7352 | 97 | self.setupUi(self) |
98 | ||
99 | self.__isBackupMode = backupMode | |
100 | ||
8924 | 101 | for text, chip in self.Chips: |
102 | self.espComboBox.addItem(text, chip) | |
7352 | 103 | |
8945 | 104 | self.baudRateComboBox.addItems([ |
105 | "74.880", "115.200", "230.400", "460.800", "921.600", "1.500.000"]) | |
106 | self.baudRateComboBox.setCurrentIndex(3) | |
107 | ||
7352 | 108 | self.firmwarePicker.setFilters( |
109 | self.tr("Firmware Files (*.img);;All Files (*)")) | |
110 | if self.__isBackupMode: | |
111 | self.firmwarePicker.setMode( | |
8356
68ec9c3d4de5
Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8327
diff
changeset
|
112 | EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE) |
8945 | 113 | self.sizeInfoLabel.clear() |
7352 | 114 | self.modeComboBox.setEnabled(False) |
8945 | 115 | self.modeInfoLabel.setEnabled(False) |
7352 | 116 | self.setWindowTitle(self.tr("Backup Firmware")) |
117 | else: | |
8356
68ec9c3d4de5
Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8327
diff
changeset
|
118 | self.firmwarePicker.setMode(EricPathPickerModes.OPEN_FILE_MODE) |
7595
5db6bfeff23e
MicroPython: added code to allow the user to select the flash mod for flashing the MicroPython firmware for ESP32/ESP8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
119 | for text, mode in self.FlashModes: |
5db6bfeff23e
MicroPython: added code to allow the user to select the flash mod for flashing the MicroPython firmware for ESP32/ESP8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
120 | self.modeComboBox.addItem(text, mode) |
7352 | 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) | |
8143
2c730d5fd177
Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
137 | self.buttonBox.button( |
2c730d5fd177
Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
138 | QDialogButtonBox.StandardButton.Ok).setEnabled(enable) |
7352 | 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() | |
8924 | 150 | chipType = self.espComboBox.currentData() |
151 | if chipType and chipType in self.FlashSizes: | |
7352 | 152 | self.sizeComboBox.addItem("") |
8924 | 153 | for text, data in self.FlashSizes[chipType]: |
7352 | 154 | self.sizeComboBox.addItem(text, data) |
155 | ||
156 | self.sizeComboBox.setCurrentText(selectedSize) | |
157 | ||
158 | self.__updateOkButton() | |
159 | ||
160 | @pyqtSlot(str) | |
8945 | 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) | |
7352 | 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, | |
8945 | 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) | |
7352 | 188 | """ |
8945 | 189 | if self.__isBackupMode: |
190 | flashSize = self.sizeComboBox.currentData() | |
191 | else: | |
192 | flashSize = self.sizeComboBox.currentText().replace(" ", "") | |
193 | ||
7352 | 194 | return ( |
8924 | 195 | self.espComboBox.currentData(), |
8945 | 196 | flashSize, |
197 | self.baudRateComboBox.currentText().replace(".", ""), | |
7595
5db6bfeff23e
MicroPython: added code to allow the user to select the flash mod for flashing the MicroPython firmware for ESP32/ESP8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
198 | self.modeComboBox.currentData(), |
7352 | 199 | self.firmwarePicker.text(), |
200 | ) |