Skip to content

Commit

Permalink
bitcraze#439: Improve bootloader GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
ataffanel committed Sep 8, 2020
1 parent acc9f26 commit 5da03fe
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 148 deletions.
57 changes: 35 additions & 22 deletions src/cfclient/ui/dialogs/bootloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""
from cflib.bootloader import Bootloader

import tempfile
import logging
import json
import os
Expand Down Expand Up @@ -92,6 +93,8 @@ def __init__(self, helper, *args):
self.coldBootButton.clicked.connect(self.initiateColdboot)
self.resetButton.clicked.connect(self.resetCopter)
self._cancel_bootloading.clicked.connect(self.close)
self.sourceTab.currentChanged.connect(
lambda _: self.updateChipSelectRadio())

# connecting other signals
self.clt.programmed.connect(self.programDone)
Expand All @@ -106,9 +109,6 @@ def __init__(self, helper, *args):
self.clt.disconnectedSignal.connect(
lambda: self.setUiState(UIState.DISCONNECTED))

# set radiobutton for flashing both mcu's (default)
self.radioBoth.setChecked(True)

self._releases = {}
self._release_firmwares_found.connect(self._populate_firmware_dropdown)
self._release_downloaded.connect(self.release_zip_downloaded)
Expand Down Expand Up @@ -198,13 +198,31 @@ def release_zip_downloaded(self, release_name, release_path):
self.downloadStatus.setText('Downloaded')
self.clt.program.emit(release_path, '')

def updateChipSelectRadio(self):
if self.sourceTab.currentWidget() == self.tabFromFile:
if self.imagePathLine.text().endswith(".zip"):
self.radioBoth.setEnabled(True)
self.radioBoth.setChecked(True)
elif self.imagePathLine.text().endswith(".bin"):
self.radioBoth.setEnabled(False)
self.radioStm32.setChecked(True)
else:
self.radioBoth.setEnabled(True)
self.radioBoth.setChecked(True)

@pyqtSlot()
def pathBrowse(self):
filename = ""
# Fix for crash in X on Ubuntu 14.04
filename, _ = QtWidgets.QFileDialog.getOpenFileName()
if filename != "":
if filename != "" and filename[-4:] in (".bin", ".zip"):
self.imagePathLine.setText(filename)
self.updateChipSelectRadio()
elif filename != "":
msgBox = QtWidgets.QMessageBox()
msgBox.setText("Wrong file extention. Must be .bin or .zip.")
msgBox.exec_()

pass

@pyqtSlot()
Expand All @@ -215,7 +233,16 @@ def programAction(self):
self.imagePathBrowseButton.setEnabled(False)

# call the flasher
if self.imagePathLine.text() != "":
if self.sourceTab.currentWidget() == self.tabFromFile:
if self.imagePathLine.text() == "":
msgBox = QtWidgets.QMessageBox()
msgBox.setText("Please choose an image file to program.")
msgBox.exec_()

self.resetButton.setEnabled(True)
self.programButton.setEnabled(True)
self.imagePathBrowseButton.setEnabled(True)
return

# by default, both of the mcu:s are flashed
mcu_to_flash = None
Expand All @@ -225,18 +252,13 @@ def programAction(self):
elif self.radioNrf51.isChecked():
mcu_to_flash = 'nrf51'
self.clt.program.emit(self.imagePathLine.text(), mcu_to_flash)
# If no file is supplied, we try to get a firmware from web-request
else:
requested_release = self.firmwareDropdown.currentText()
download_url = self._releases[requested_release]
self.downloadStatus.setText('Fetching...')
self.firmware_downloader.download_release(requested_release,
download_url)

# msgBox = QtWidgets.QMessageBox()
# msgBox.setText("Please choose an image file to program.")
# msgBox.exec_()

@pyqtSlot(bool)
def programDone(self, success):
if success:
Expand Down Expand Up @@ -348,12 +370,11 @@ def __init__(self, qtsignal_get_all_firmwares, qtsignal_get_release):
self._qtsignal_get_all_firmwares = qtsignal_get_all_firmwares
self._qtsignal_get_release = qtsignal_get_release

self._filepath = os.path.join(os.path.dirname(__file__),
self._tempDirectory = tempfile.TemporaryDirectory()
self._filepath = os.path.join(self._tempDirectory.name,
'tmp.zip')
self.moveToThread(self)

self.bootload_complete.connect(lambda: self._remove_zip_file())

def get_firmware_releases(self):
""" Wrapper-function """
threading.Thread(target=self._get_firmware_releases,
Expand All @@ -370,6 +391,7 @@ def _get_firmware_releases(self, signal):
and returns a list of format [rel-name, {release: download-link}].
Returns None if the request fails.
"""
response = {}
try:
with urlopen(RELEASE_URL) as resp:
response = json.load(resp)
Expand Down Expand Up @@ -421,12 +443,3 @@ def _download_release(self, signal, release_name, url):
except URLError:
logger.warning('Failed to make web request to get requested'
' firmware-release')

def _remove_zip_file(self):
try:
os.remove(self._filepath)
logger.info('Removed temporary firmware-release file at'
'%s' % self._filepath)
except FileNotFoundError:
logger.warning('Failed to delete temporary firmware-release file'
' at %s' % self._filepath)
Loading

0 comments on commit 5da03fe

Please sign in to comment.