|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2019 - 2021 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 PyQt5.QtCore import pyqtSlot |
|
14 from PyQt5.QtWidgets import QDialog, QDialogButtonBox |
|
15 |
|
16 from E5Gui.E5PathPicker import E5PathPickerModes |
|
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 FlashModes = [ |
|
30 ("Quad I/O", "qio"), |
|
31 ("Quad Output", "qout"), |
|
32 ("Dual I/O", "dio"), |
|
33 ("Dual Output", "dout"), |
|
34 ] |
|
35 FlashSizes = { |
|
36 "ESP32": [ |
|
37 (" 1 MB", "0x100000"), |
|
38 (" 2 MB", "0x200000"), |
|
39 (" 4 MB", "0x400000"), |
|
40 (" 8 MB", "0x800000"), |
|
41 ("16 MB", "0x1000000"), |
|
42 ], |
|
43 "ESP8266": [ |
|
44 ("256 KB", "0x40000"), |
|
45 ("512 KB", "0x80000"), |
|
46 (" 1 MB", "0x100000"), |
|
47 (" 2 MB", "0x200000"), |
|
48 (" 4 MB", "0x400000"), |
|
49 (" 8 MB", "0x800000"), |
|
50 ("16 MB", "0x1000000"), |
|
51 ], |
|
52 } |
|
53 |
|
54 def __init__(self, backupMode=True, parent=None): |
|
55 """ |
|
56 Constructor |
|
57 |
|
58 @param backupMode flag indicating parameters for a firmware backup are |
|
59 requested |
|
60 @type bool |
|
61 @param parent reference to the parent widget |
|
62 @type QWidget |
|
63 """ |
|
64 super().__init__(parent) |
|
65 self.setupUi(self) |
|
66 |
|
67 self.__isBackupMode = backupMode |
|
68 |
|
69 self.espComboBox.addItems(["", "ESP32", "ESP8266"]) |
|
70 |
|
71 self.firmwarePicker.setFilters( |
|
72 self.tr("Firmware Files (*.img);;All Files (*)")) |
|
73 if self.__isBackupMode: |
|
74 self.firmwarePicker.setMode( |
|
75 E5PathPickerModes.SaveFileEnsureExtensionMode) |
|
76 self.modeComboBox.setEnabled(False) |
|
77 self.setWindowTitle(self.tr("Backup Firmware")) |
|
78 else: |
|
79 self.firmwarePicker.setMode(E5PathPickerModes.OpenFileMode) |
|
80 for text, mode in self.FlashModes: |
|
81 self.modeComboBox.addItem(text, mode) |
|
82 self.setWindowTitle(self.tr("Restore Firmware")) |
|
83 |
|
84 msh = self.minimumSizeHint() |
|
85 self.resize(max(self.width(), msh.width()), msh.height()) |
|
86 |
|
87 def __updateOkButton(self): |
|
88 """ |
|
89 Private method to update the state of the OK button. |
|
90 """ |
|
91 firmwareFile = self.firmwarePicker.text() |
|
92 enable = (bool(self.espComboBox.currentText()) and |
|
93 bool(firmwareFile)) |
|
94 if self.__isBackupMode: |
|
95 enable &= bool(self.sizeComboBox.currentText()) |
|
96 else: |
|
97 enable &= os.path.exists(firmwareFile) |
|
98 self.buttonBox.button( |
|
99 QDialogButtonBox.StandardButton.Ok).setEnabled(enable) |
|
100 |
|
101 @pyqtSlot(str) |
|
102 def on_espComboBox_currentTextChanged(self, chip): |
|
103 """ |
|
104 Private slot to handle the selection of a chip type. |
|
105 |
|
106 @param chip selected chip type |
|
107 @type str |
|
108 """ |
|
109 selectedSize = self.sizeComboBox.currentText() |
|
110 self.sizeComboBox.clear() |
|
111 if chip and chip in self.FlashSizes: |
|
112 self.sizeComboBox.addItem("") |
|
113 for text, data in self.FlashSizes[chip]: |
|
114 self.sizeComboBox.addItem(text, data) |
|
115 |
|
116 self.sizeComboBox.setCurrentText(selectedSize) |
|
117 |
|
118 self.__updateOkButton() |
|
119 |
|
120 @pyqtSlot(str) |
|
121 def on_firmwarePicker_textChanged(self, firmware): |
|
122 """ |
|
123 Private slot handling a change of the firmware path. |
|
124 |
|
125 @param firmware path to the firmware |
|
126 @type str |
|
127 """ |
|
128 self.__updateOkButton() |
|
129 |
|
130 def getData(self): |
|
131 """ |
|
132 Public method to get the entered data. |
|
133 |
|
134 @return tuple containing the selected chip type, the firmware size, |
|
135 the flash mode and the path of the firmware file |
|
136 @rtype tuple of (str, str, str, str) |
|
137 """ |
|
138 return ( |
|
139 self.espComboBox.currentText().lower(), |
|
140 self.sizeComboBox.currentData(), |
|
141 self.modeComboBox.currentData(), |
|
142 self.firmwarePicker.text(), |
|
143 ) |