|
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 firmware to |
|
8 be flashed. |
|
9 """ |
|
10 |
|
11 import os |
|
12 |
|
13 from PyQt5.QtCore import pyqtSlot, QRegularExpression |
|
14 from PyQt5.QtGui import QRegularExpressionValidator |
|
15 from PyQt5.QtWidgets import QDialog, QDialogButtonBox |
|
16 |
|
17 from E5Gui.E5PathPicker import E5PathPickerModes |
|
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 FlashModes = ( |
|
28 ("", ""), |
|
29 ("Quad I/O", "qio"), |
|
30 ("Quad Output", "qout"), |
|
31 ("Dual I/O", "dio"), |
|
32 ("Dual Output", "dout"), |
|
33 ) |
|
34 |
|
35 def __init__(self, addon=False, parent=None): |
|
36 """ |
|
37 Constructor |
|
38 |
|
39 @param addon flag indicating an addon firmware |
|
40 @type bool |
|
41 @param parent reference to the parent widget |
|
42 @type QWidget |
|
43 """ |
|
44 super().__init__(parent) |
|
45 self.setupUi(self) |
|
46 |
|
47 self.__addon = addon |
|
48 |
|
49 self.firmwarePicker.setMode(E5PathPickerModes.OpenFileMode) |
|
50 self.firmwarePicker.setFilters( |
|
51 self.tr("Firmware Files (*.bin);;All Files (*)")) |
|
52 |
|
53 self.espComboBox.addItems(["", "ESP32", "ESP8266"]) |
|
54 |
|
55 self.baudRateComboBox.addItems([ |
|
56 "74.880", "115.200", "230.400", "460.800", "921.600", "1.500.000"]) |
|
57 self.baudRateComboBox.setCurrentIndex(1) |
|
58 |
|
59 for text, mode in self.FlashModes: |
|
60 self.modeComboBox.addItem(text, mode) |
|
61 |
|
62 if addon: |
|
63 self.__validator = QRegularExpressionValidator( |
|
64 QRegularExpression(r"[0-9a-fA-F]{0,4}") |
|
65 ) |
|
66 self.addressEdit.setValidator(self.__validator) |
|
67 else: |
|
68 self.addressLabel.hide() |
|
69 self.addressEdit.hide() |
|
70 |
|
71 msh = self.minimumSizeHint() |
|
72 self.resize(max(self.width(), msh.width()), msh.height()) |
|
73 |
|
74 def __updateOkButton(self): |
|
75 """ |
|
76 Private method to update the state of the OK button. |
|
77 """ |
|
78 firmwareFile = self.firmwarePicker.text() |
|
79 enable = (bool(self.espComboBox.currentText()) and |
|
80 bool(firmwareFile) and os.path.exists(firmwareFile)) |
|
81 if self.__addon: |
|
82 enable &= bool(self.addressEdit.text()) |
|
83 self.buttonBox.button( |
|
84 QDialogButtonBox.StandardButton.Ok).setEnabled(enable) |
|
85 |
|
86 @pyqtSlot(str) |
|
87 def on_espComboBox_currentTextChanged(self, chip): |
|
88 """ |
|
89 Private slot to handle the selection of a chip type. |
|
90 |
|
91 @param chip selected chip type |
|
92 @type str |
|
93 """ |
|
94 self.__updateOkButton() |
|
95 |
|
96 @pyqtSlot(str) |
|
97 def on_firmwarePicker_textChanged(self, firmware): |
|
98 """ |
|
99 Private slot handling a change of the firmware path. |
|
100 |
|
101 @param firmware path to the firmware |
|
102 @type str |
|
103 """ |
|
104 self.__updateOkButton() |
|
105 |
|
106 def getData(self): |
|
107 """ |
|
108 Public method to get the entered data. |
|
109 |
|
110 @return tuple containing the selected chip type, the path of the |
|
111 firmware file, the baud rate, the flash mode and the flash |
|
112 address |
|
113 @rtype tuple of (str, str, str, str, str) |
|
114 """ |
|
115 address = self.addressEdit.text() if self.__addon else "" |
|
116 |
|
117 return ( |
|
118 self.espComboBox.currentText().lower(), |
|
119 self.firmwarePicker.text(), |
|
120 self.baudRateComboBox.currentText().replace(".", ""), |
|
121 self.modeComboBox.currentData(), |
|
122 address, |
|
123 ) |