|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the device interface class for RP2040 based boards |
|
8 (e.g. Raspberry Pi Pico). |
|
9 """ |
|
10 |
|
11 from PyQt5.QtCore import pyqtSlot |
|
12 |
|
13 from .MicroPythonDevices import MicroPythonDevice |
|
14 from .MicroPythonWidget import HAS_QTCHART |
|
15 |
|
16 import Preferences |
|
17 |
|
18 |
|
19 class RP2040Device(MicroPythonDevice): |
|
20 """ |
|
21 Class implementing the device for RP2040 based boards. |
|
22 """ |
|
23 def __init__(self, microPythonWidget, deviceType, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param microPythonWidget reference to the main MicroPython widget |
|
28 @type MicroPythonWidget |
|
29 @param deviceType device type assigned to this device interface |
|
30 @type str |
|
31 @param parent reference to the parent object |
|
32 @type QObject |
|
33 """ |
|
34 super(RP2040Device, self).__init__( |
|
35 microPythonWidget, deviceType, parent) |
|
36 |
|
37 def setButtons(self): |
|
38 """ |
|
39 Public method to enable the supported action buttons. |
|
40 """ |
|
41 super(RP2040Device, self).setButtons() |
|
42 self.microPython.setActionButtons( |
|
43 run=True, repl=True, files=True, chart=HAS_QTCHART) |
|
44 |
|
45 def forceInterrupt(self): |
|
46 """ |
|
47 Public method to determine the need for an interrupt when opening the |
|
48 serial connection. |
|
49 |
|
50 @return flag indicating an interrupt is needed |
|
51 @rtype bool |
|
52 """ |
|
53 return False |
|
54 |
|
55 def deviceName(self): |
|
56 """ |
|
57 Public method to get the name of the device. |
|
58 |
|
59 @return name of the device |
|
60 @rtype str |
|
61 """ |
|
62 return self.tr("RP2040") |
|
63 |
|
64 def canStartRepl(self): |
|
65 """ |
|
66 Public method to determine, if a REPL can be started. |
|
67 |
|
68 @return tuple containing a flag indicating it is safe to start a REPL |
|
69 and a reason why it cannot. |
|
70 @rtype tuple of (bool, str) |
|
71 """ |
|
72 return True, "" |
|
73 |
|
74 def canStartPlotter(self): |
|
75 """ |
|
76 Public method to determine, if a Plotter can be started. |
|
77 |
|
78 @return tuple containing a flag indicating it is safe to start a |
|
79 Plotter and a reason why it cannot. |
|
80 @rtype tuple of (bool, str) |
|
81 """ |
|
82 return True, "" |
|
83 |
|
84 def canRunScript(self): |
|
85 """ |
|
86 Public method to determine, if a script can be executed. |
|
87 |
|
88 @return tuple containing a flag indicating it is safe to start a |
|
89 Plotter and a reason why it cannot. |
|
90 @rtype tuple of (bool, str) |
|
91 """ |
|
92 return True, "" |
|
93 |
|
94 def runScript(self, script): |
|
95 """ |
|
96 Public method to run the given Python script. |
|
97 |
|
98 @param script script to be executed |
|
99 @type str |
|
100 """ |
|
101 pythonScript = script.split("\n") |
|
102 self.sendCommands(pythonScript) |
|
103 |
|
104 def canStartFileManager(self): |
|
105 """ |
|
106 Public method to determine, if a File Manager can be started. |
|
107 |
|
108 @return tuple containing a flag indicating it is safe to start a |
|
109 File Manager and a reason why it cannot. |
|
110 @rtype tuple of (bool, str) |
|
111 """ |
|
112 return True, "" |
|
113 |
|
114 def addDeviceMenuEntries(self, menu): |
|
115 """ |
|
116 Public method to add device specific entries to the given menu. |
|
117 |
|
118 @param menu reference to the context menu |
|
119 @type QMenu |
|
120 """ |
|
121 connected = self.microPython.isConnected() |
|
122 |
|
123 act = menu.addAction(self.tr("Activate Bootloader"), |
|
124 self.__activateBootloader) |
|
125 act.setEnabled(connected) |
|
126 act = menu.addAction(self.tr("Flash Firmware"), self.__flashPython) |
|
127 act.setEnabled(not connected) |
|
128 |
|
129 def hasFlashMenuEntry(self): |
|
130 """ |
|
131 Public method to check, if the device has its own flash menu entry. |
|
132 |
|
133 @return flag indicating a specific flash menu entry |
|
134 @rtype bool |
|
135 """ |
|
136 return True |
|
137 |
|
138 @pyqtSlot() |
|
139 def __flashPython(self): |
|
140 """ |
|
141 Private slot to flash a MicroPython firmware to the device. |
|
142 """ |
|
143 from .UF2FlashDialog import UF2FlashDialog |
|
144 dlg = UF2FlashDialog(boardType="rp2040") |
|
145 dlg.exec() |
|
146 |
|
147 def __activateBootloader(self): |
|
148 """ |
|
149 Private method to switch the board into 'bootloader' mode. |
|
150 """ |
|
151 if self.microPython.isConnected(): |
|
152 self.microPython.commandsInterface().execute([ |
|
153 "import machine", |
|
154 "machine.bootloader()", |
|
155 ]) |
|
156 # simulate pressing the disconnect button |
|
157 self.microPython.on_connectButton_clicked() |
|
158 |
|
159 def getDocumentationUrl(self): |
|
160 """ |
|
161 Public method to get the device documentation URL. |
|
162 |
|
163 @return documentation URL of the device |
|
164 @rtype str |
|
165 """ |
|
166 return Preferences.getMicroPython("MicroPythonDocuUrl") |
|
167 |
|
168 def getDownloadMenuEntries(self): |
|
169 """ |
|
170 Public method to retrieve the entries for the downloads menu. |
|
171 |
|
172 @return list of tuples with menu text and URL to be opened for each |
|
173 entry |
|
174 @rtype list of tuple of (str, str) |
|
175 """ |
|
176 return [ |
|
177 (self.tr("MicroPython Firmware"), |
|
178 Preferences.getMicroPython("MicroPythonFirmwareUrl")), |
|
179 ("<separator>", ""), |
|
180 (self.tr("CircuitPython Firmware"), |
|
181 Preferences.getMicroPython("CircuitPythonFirmwareUrl")), |
|
182 (self.tr("CircuitPython Libraries"), |
|
183 Preferences.getMicroPython("CircuitPythonLibrariesUrl")) |
|
184 ] |