|
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 firmware to |
|
8 be flashed. |
|
9 """ |
|
10 |
|
11 import os |
|
12 |
|
13 from PyQt6.QtCore import pyqtSlot, QRegularExpression |
|
14 from PyQt6.QtGui import QRegularExpressionValidator |
|
15 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
16 |
|
17 from EricWidgets.EricPathPicker import EricPathPickerModes |
|
18 |
|
19 from .Ui_EspFirmwareSelectionDialog import Ui_EspFirmwareSelectionDialog |
|
20 |
|
21 |
|
22 class EspFirmwareSelectionDialog(QDialog, Ui_EspFirmwareSelectionDialog): |
|
23 """ |
|
24 Class implementing a dialog to select the ESP chip type and the firmware to |
|
25 be flashed. |
|
26 """ |
|
27 Chips = ( |
|
28 ("", ""), |
|
29 ("ESP32", "esp32"), |
|
30 ("ESP32-C3", "esp32c3"), |
|
31 ("ESP32-S2", "esp32s2"), |
|
32 ("ESP32-S3", "esp32s3"), |
|
33 ("ESP8266", "esp8266"), |
|
34 ) |
|
35 |
|
36 FlashModes = ( |
|
37 ("", ""), |
|
38 ("Quad I/O", "qio"), |
|
39 ("Quad Output", "qout"), |
|
40 ("Dual I/O", "dio"), |
|
41 ("Dual Output", "dout"), |
|
42 ) |
|
43 |
|
44 FlashAddresses = { |
|
45 "esp8266": "0x0000", |
|
46 "esp32": "0x1000", |
|
47 "esp32c3": "0x0000", |
|
48 "esp32s2": "0x1000", |
|
49 "esp32s3": "0x0000", |
|
50 } |
|
51 |
|
52 def __init__(self, addon=False, parent=None): |
|
53 """ |
|
54 Constructor |
|
55 |
|
56 @param addon flag indicating an addon firmware |
|
57 @type bool |
|
58 @param parent reference to the parent widget |
|
59 @type QWidget |
|
60 """ |
|
61 super().__init__(parent) |
|
62 self.setupUi(self) |
|
63 |
|
64 self.__addon = addon |
|
65 |
|
66 self.firmwarePicker.setMode(EricPathPickerModes.OPEN_FILE_MODE) |
|
67 self.firmwarePicker.setFilters( |
|
68 self.tr("Firmware Files (*.bin);;All Files (*)")) |
|
69 |
|
70 for text, chip in self.Chips: |
|
71 self.espComboBox.addItem(text, chip) |
|
72 |
|
73 self.baudRateComboBox.addItems([ |
|
74 "74.880", "115.200", "230.400", "460.800", "921.600", "1.500.000"]) |
|
75 self.baudRateComboBox.setCurrentIndex(3) |
|
76 |
|
77 for text, mode in self.FlashModes: |
|
78 self.modeComboBox.addItem(text, mode) |
|
79 |
|
80 if addon: |
|
81 self.__validator = QRegularExpressionValidator( |
|
82 QRegularExpression(r"[0-9a-fA-F]{0,7}") |
|
83 ) |
|
84 self.addressEdit.setValidator(self.__validator) |
|
85 else: |
|
86 self.addressLabel.hide() |
|
87 self.addressEdit.hide() |
|
88 |
|
89 msh = self.minimumSizeHint() |
|
90 self.resize(max(self.width(), msh.width()), msh.height()) |
|
91 |
|
92 def __updateOkButton(self): |
|
93 """ |
|
94 Private method to update the state of the OK button. |
|
95 """ |
|
96 firmwareFile = self.firmwarePicker.text() |
|
97 enable = (bool(self.espComboBox.currentText()) and |
|
98 bool(firmwareFile) and os.path.exists(firmwareFile)) |
|
99 if self.__addon: |
|
100 enable &= bool(self.addressEdit.text()) |
|
101 self.buttonBox.button( |
|
102 QDialogButtonBox.StandardButton.Ok).setEnabled(enable) |
|
103 |
|
104 @pyqtSlot(str) |
|
105 def on_espComboBox_currentTextChanged(self, chip): |
|
106 """ |
|
107 Private slot to handle the selection of a chip type. |
|
108 |
|
109 @param chip selected chip type |
|
110 @type str |
|
111 """ |
|
112 self.__updateOkButton() |
|
113 |
|
114 @pyqtSlot(str) |
|
115 def on_firmwarePicker_textChanged(self, firmware): |
|
116 """ |
|
117 Private slot handling a change of the firmware path. |
|
118 |
|
119 @param firmware path to the firmware |
|
120 @type str |
|
121 """ |
|
122 self.__updateOkButton() |
|
123 |
|
124 @pyqtSlot(str) |
|
125 def on_addressEdit_textChanged(self, address): |
|
126 """ |
|
127 Private slot handling a change of the address. |
|
128 |
|
129 @param address entered address |
|
130 @type str |
|
131 """ |
|
132 self.__updateOkButton() |
|
133 |
|
134 def getData(self): |
|
135 """ |
|
136 Public method to get the entered data. |
|
137 |
|
138 @return tuple containing the selected chip type, the path of the |
|
139 firmware file, the baud rate, the flash mode and the flash |
|
140 address |
|
141 @rtype tuple of (str, str, str, str, str) |
|
142 """ |
|
143 chip = self.espComboBox.currentData() |
|
144 address = ( |
|
145 self.addressEdit.text() |
|
146 if self.__addon else |
|
147 self.FlashAddresses[chip]) |
|
148 |
|
149 return ( |
|
150 chip, |
|
151 self.firmwarePicker.text(), |
|
152 self.baudRateComboBox.currentText().replace(".", ""), |
|
153 self.modeComboBox.currentData(), |
|
154 address, |
|
155 ) |