|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2019 - 2023 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 QRegularExpression, pyqtSlot |
|
14 from PyQt6.QtGui import QRegularExpressionValidator |
|
15 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
16 |
|
17 from eric7.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 |
|
28 Chips = ( |
|
29 ("", ""), |
|
30 ("ESP32", "esp32"), |
|
31 ("ESP32-C3", "esp32c3"), |
|
32 ("ESP32-S2", "esp32s2"), |
|
33 ("ESP32-S3", "esp32s3"), |
|
34 ("ESP8266", "esp8266"), |
|
35 ) |
|
36 |
|
37 FlashModes = ( |
|
38 ("", ""), |
|
39 ("Quad I/O", "qio"), |
|
40 ("Quad Output", "qout"), |
|
41 ("Dual I/O", "dio"), |
|
42 ("Dual Output", "dout"), |
|
43 ) |
|
44 |
|
45 FlashAddresses = { |
|
46 "esp8266": "0x0000", |
|
47 "esp32": "0x1000", |
|
48 "esp32c3": "0x0000", |
|
49 "esp32s2": "0x1000", |
|
50 "esp32s3": "0x0000", |
|
51 } |
|
52 |
|
53 def __init__(self, addon=False, parent=None): |
|
54 """ |
|
55 Constructor |
|
56 |
|
57 @param addon flag indicating an addon firmware |
|
58 @type bool |
|
59 @param parent reference to the parent widget |
|
60 @type QWidget |
|
61 """ |
|
62 super().__init__(parent) |
|
63 self.setupUi(self) |
|
64 |
|
65 self.__addon = addon |
|
66 |
|
67 self.firmwarePicker.setMode(EricPathPickerModes.OPEN_FILE_MODE) |
|
68 self.firmwarePicker.setFilters(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 ) |
|
76 self.baudRateComboBox.setCurrentIndex(3) |
|
77 |
|
78 for text, mode in self.FlashModes: |
|
79 self.modeComboBox.addItem(text, mode) |
|
80 |
|
81 if addon: |
|
82 self.__validator = QRegularExpressionValidator( |
|
83 QRegularExpression(r"[0-9a-fA-F]{0,7}") |
|
84 ) |
|
85 self.addressEdit.setValidator(self.__validator) |
|
86 else: |
|
87 self.addressLabel.hide() |
|
88 self.addressEdit.hide() |
|
89 |
|
90 msh = self.minimumSizeHint() |
|
91 self.resize(max(self.width(), msh.width()), msh.height()) |
|
92 |
|
93 def __updateOkButton(self): |
|
94 """ |
|
95 Private method to update the state of the OK button. |
|
96 """ |
|
97 firmwareFile = self.firmwarePicker.text() |
|
98 enable = ( |
|
99 bool(self.espComboBox.currentText()) |
|
100 and bool(firmwareFile) |
|
101 and os.path.exists(firmwareFile) |
|
102 ) |
|
103 if self.__addon: |
|
104 enable &= bool(self.addressEdit.text()) |
|
105 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(enable) |
|
106 |
|
107 @pyqtSlot(str) |
|
108 def on_espComboBox_currentTextChanged(self, chip): |
|
109 """ |
|
110 Private slot to handle the selection of a chip type. |
|
111 |
|
112 @param chip selected chip type |
|
113 @type str |
|
114 """ |
|
115 self.__updateOkButton() |
|
116 |
|
117 self.cpyCheckBox.setEnabled(chip == "ESP32-S2") |
|
118 # possible address override needed for CircuitPython |
|
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 self.cpyCheckBox.setChecked("circuitpython" in firmware) |
|
131 # possible address override needed for CircuitPython |
|
132 |
|
133 @pyqtSlot(str) |
|
134 def on_addressEdit_textChanged(self, address): |
|
135 """ |
|
136 Private slot handling a change of the address. |
|
137 |
|
138 @param address entered address |
|
139 @type str |
|
140 """ |
|
141 self.__updateOkButton() |
|
142 |
|
143 def getData(self): |
|
144 """ |
|
145 Public method to get the entered data. |
|
146 |
|
147 @return tuple containing the selected chip type, the path of the |
|
148 firmware file, the baud rate, the flash mode and the flash |
|
149 address |
|
150 @rtype tuple of (str, str, str, str, str) |
|
151 """ |
|
152 chip = self.espComboBox.currentData() |
|
153 |
|
154 address = self.addressEdit.text() if self.__addon else self.FlashAddresses[chip] |
|
155 if not self.__addon and chip == "esp32s2" and self.cpyCheckBox.isChecked(): |
|
156 # override address |
|
157 address = "0x0000" |
|
158 |
|
159 return ( |
|
160 chip, |
|
161 self.firmwarePicker.text(), |
|
162 self.baudRateComboBox.currentText().replace(".", ""), |
|
163 self.modeComboBox.currentData(), |
|
164 address, |
|
165 ) |