eric7/MicroPython/MicrobitDevices.py

branch
eric7
changeset 8356
68ec9c3d4de5
parent 8318
962bce857696
child 8358
144a6b854f70
equal deleted inserted replaced
8355:8a7677a63c8d 8356:68ec9c3d4de5
15 from PyQt6.QtWidgets import QInputDialog, QLineEdit 15 from PyQt6.QtWidgets import QInputDialog, QLineEdit
16 16
17 from .MicroPythonDevices import MicroPythonDevice 17 from .MicroPythonDevices import MicroPythonDevice
18 from .MicroPythonWidget import HAS_QTCHART 18 from .MicroPythonWidget import HAS_QTCHART
19 19
20 from E5Gui import E5MessageBox, E5FileDialog 20 from E5Gui import EricMessageBox, EricFileDialog
21 from E5Gui.E5Application import e5App 21 from E5Gui.EricApplication import ericApp
22 22
23 import Utilities 23 import Utilities
24 import Preferences 24 import Preferences
25 25
26 26
204 findAll=True) 204 findAll=True)
205 if len(deviceDirectories) == 0: 205 if len(deviceDirectories) == 0:
206 if self.getDeviceType() == "bbc_microbit": 206 if self.getDeviceType() == "bbc_microbit":
207 # BBC micro:bit is not ready or not mounted 207 # BBC micro:bit is not ready or not mounted
208 if firmware: 208 if firmware:
209 E5MessageBox.critical( 209 EricMessageBox.critical(
210 self.microPython, 210 self.microPython,
211 self.tr("Flash MicroPython/Firmware"), 211 self.tr("Flash MicroPython/Firmware"),
212 self.tr( 212 self.tr(
213 '<p>The BBC micro:bit is not ready for flashing' 213 '<p>The BBC micro:bit is not ready for flashing'
214 ' the DAPLink firmware. Follow these' 214 ' the DAPLink firmware. Follow these'
224 '<a href="https://microbit.org/guide/firmware/">' 224 '<a href="https://microbit.org/guide/firmware/">'
225 'micro:bit web site</a> for details.</p>' 225 'micro:bit web site</a> for details.</p>'
226 ) 226 )
227 ) 227 )
228 else: 228 else:
229 E5MessageBox.critical( 229 EricMessageBox.critical(
230 self.microPython, 230 self.microPython,
231 self.tr("Flash MicroPython/Firmware"), 231 self.tr("Flash MicroPython/Firmware"),
232 self.tr( 232 self.tr(
233 '<p>The BBC micro:bit is not ready for flashing' 233 '<p>The BBC micro:bit is not ready for flashing'
234 ' the MicroPython firmware. Please make sure,' 234 ' the MicroPython firmware. Please make sure,'
237 ) 237 )
238 ) 238 )
239 else: 239 else:
240 # Calliope mini is not ready or not mounted 240 # Calliope mini is not ready or not mounted
241 if firmware: 241 if firmware:
242 E5MessageBox.critical( 242 EricMessageBox.critical(
243 self.microPython, 243 self.microPython,
244 self.tr("Flash MicroPython/Firmware"), 244 self.tr("Flash MicroPython/Firmware"),
245 self.tr( 245 self.tr(
246 '<p>The "Calliope mini" is not ready for flashing' 246 '<p>The "Calliope mini" is not ready for flashing'
247 ' the DAPLink firmware. Follow these' 247 ' the DAPLink firmware. Follow these'
254 ' available</li>' 254 ' available</li>'
255 '</ul>' 255 '</ul>'
256 ) 256 )
257 ) 257 )
258 else: 258 else:
259 E5MessageBox.critical( 259 EricMessageBox.critical(
260 self.microPython, 260 self.microPython,
261 self.tr("Flash MicroPython/Firmware"), 261 self.tr("Flash MicroPython/Firmware"),
262 self.tr( 262 self.tr(
263 '<p>The "Calliope mini" is not ready for flashing' 263 '<p>The "Calliope mini" is not ready for flashing'
264 ' the MicroPython firmware. Please make sure,' 264 ' the MicroPython firmware. Please make sure,'
267 ) 267 )
268 ) 268 )
269 elif len(deviceDirectories) == 1: 269 elif len(deviceDirectories) == 1:
270 downloadsPath = QStandardPaths.standardLocations( 270 downloadsPath = QStandardPaths.standardLocations(
271 QStandardPaths.StandardLocation.DownloadLocation)[0] 271 QStandardPaths.StandardLocation.DownloadLocation)[0]
272 firmware = E5FileDialog.getOpenFileName( 272 firmware = EricFileDialog.getOpenFileName(
273 self.microPython, 273 self.microPython,
274 self.tr("Flash MicroPython/Firmware"), 274 self.tr("Flash MicroPython/Firmware"),
275 downloadsPath, 275 downloadsPath,
276 self.tr("MicroPython/Firmware Files (*.hex *.bin);;" 276 self.tr("MicroPython/Firmware Files (*.hex *.bin);;"
277 "All Files (*)")) 277 "All Files (*)"))
278 if firmware and os.path.exists(firmware): 278 if firmware and os.path.exists(firmware):
279 shutil.copy2(firmware, deviceDirectories[0]) 279 shutil.copy2(firmware, deviceDirectories[0])
280 else: 280 else:
281 E5MessageBox.warning( 281 EricMessageBox.warning(
282 self, 282 self,
283 self.tr("Flash MicroPython/Firmware"), 283 self.tr("Flash MicroPython/Firmware"),
284 self.tr("There are multiple devices ready for flashing." 284 self.tr("There are multiple devices ready for flashing."
285 " Please make sure, that only one device is prepared.") 285 " Please make sure, that only one device is prepared.")
286 ) 286 )
300 device. 300 device.
301 301
302 @param scriptName name of the file on the device 302 @param scriptName name of the file on the device
303 @type str 303 @type str
304 """ 304 """
305 aw = e5App().getObject("ViewManager").activeWindow() 305 aw = ericApp().getObject("ViewManager").activeWindow()
306 if not aw: 306 if not aw:
307 return 307 return
308 308
309 title = ( 309 title = (
310 self.tr("Save Script as '{0}'").format(scriptName) 310 self.tr("Save Script as '{0}'").format(scriptName)
311 if scriptName else 311 if scriptName else
312 self.tr("Save Script") 312 self.tr("Save Script")
313 ) 313 )
314 314
315 if not (aw.isPyFile() or aw.isMicroPythonFile()): 315 if not (aw.isPyFile() or aw.isMicroPythonFile()):
316 yes = E5MessageBox.yesNo( 316 yes = EricMessageBox.yesNo(
317 self.microPython, 317 self.microPython,
318 title, 318 title,
319 self.tr("""The current editor does not contain a Python""" 319 self.tr("""The current editor does not contain a Python"""
320 """ script. Write it anyway?""")) 320 """ script. Write it anyway?"""))
321 if not yes: 321 if not yes:
322 return 322 return
323 323
324 script = aw.text().strip() 324 script = aw.text().strip()
325 if not script: 325 if not script:
326 E5MessageBox.warning( 326 EricMessageBox.warning(
327 self.microPython, 327 self.microPython,
328 title, 328 title,
329 self.tr("""The script is empty. Aborting.""")) 329 self.tr("""The script is empty. Aborting."""))
330 return 330 return
331 331
349 for line in script.splitlines(): 349 for line in script.splitlines():
350 commands.append("f(" + repr(line + "\n") + ")") 350 commands.append("f(" + repr(line + "\n") + ")")
351 commands.append("fd.close()") 351 commands.append("fd.close()")
352 out, err = self.microPython.commandsInterface().execute(commands) 352 out, err = self.microPython.commandsInterface().execute(commands)
353 if err: 353 if err:
354 E5MessageBox.critical( 354 EricMessageBox.critical(
355 self.microPython, 355 self.microPython,
356 title, 356 title,
357 self.tr("""<p>The script could not be saved to the""" 357 self.tr("""<p>The script could not be saved to the"""
358 """ device.</p><p>Reason: {0}</p>""") 358 """ device.</p><p>Reason: {0}</p>""")
359 .format(err.decode("utf-8"))) 359 .format(err.decode("utf-8")))

eric ide

mercurial