def run(self): """ Flash the device. If we are sending a custom hex we need to manually read it and copy it into the micro:bit drive otherwise use uFlash. """ try: if self.path_to_runtime: if self.python_script: raise Exception( "Cannot flash a custom runtime with a Python script") with open(self.path_to_runtime, "rb") as f_input: rt_bytes = f_input.read() with open( os.path.join(self.path_to_microbit, "micropython.hex"), "wb", ) as f_output: f_output.write(rt_bytes) f_output.flush() os.fsync(f_output.fileno()) else: uflash.flash( paths_to_microbits=[self.path_to_microbit], python_script=self.python_script, ) # After flash ends DAPLink reboots the MSD, and serial might not # be immediately available, so this small delay helps. time.sleep(0.5) except Exception as ex: # Catch everything so Mu can recover from all of the wide variety # of possible exceptions that could happen at this point. logger.error(ex) self.on_flash_fail.emit(str(ex))
def run(self): """ Flash the device. """ try: uflash.flash(paths_to_microbits=self.paths_to_microbits, python_script=self.python_script, path_to_runtime=self.path_to_runtime) except Exception as ex: # Catch everything so Mu can recover from all of the wide variety # of possible exceptions that could happen at this point. self.on_flash_fail.emit(str(ex))
def flash(self): """ Takes the currently active tab, compiles the Python script therein into a hex file and flashes it all onto the connected device. """ logger.info('Flashing script.') # Grab the Python script. tab = self.view.current_tab if tab is None: # There is no active text editor. return python_script = tab.text().encode('utf-8') logger.debug('Python script:') logger.debug(python_script) if len(python_script) >= 8192: message = _('Unable to flash "{}"').format(tab.label) information = _("Your script is too long!") self.view.show_message(message, information, 'Warning') return # Determine the location of the BBC micro:bit. If it can't be found # fall back to asking the user to locate it. path_to_microbit = uflash.find_microbit() if path_to_microbit is None: # Has the path to the device already been specified? if self.user_defined_microbit_path: path_to_microbit = self.user_defined_microbit_path else: # Ask the user to locate the device. path_to_microbit = self.view.get_microbit_path(HOME_DIRECTORY) # Store the user's specification of the path for future use. self.user_defined_microbit_path = path_to_microbit logger.debug('User defined path to micro:bit: {}'.format( self.user_defined_microbit_path)) # Check the path and that it exists simply because the path maybe based # on stale data. logger.debug('Path to micro:bit: {}'.format(path_to_microbit)) if path_to_microbit and os.path.exists(path_to_microbit): logger.debug('Flashing to device.') # Flash the microbit rt_hex_path = self.get_hex_path() uflash.flash(paths_to_microbits=[path_to_microbit], python_script=python_script, path_to_runtime=rt_hex_path) message = _('Flashing "{}" onto the micro:bit.').format(tab.label) if (rt_hex_path is not None and os.path.exists(rt_hex_path)): message = message + _(" Runtime: {}").format(rt_hex_path) self.editor.show_status_message(message, 10) else: # Reset user defined path since it's incorrect. self.user_defined_microbit_path = None # Try to be helpful... essentially there is nothing Mu can do but # prompt for patience while the device is mounted and/or do the # classic "have you tried switching it off and on again?" trick. # This one's for James at the Raspberry Pi Foundation. ;-) message = _('Could not find an attached BBC micro:bit.') information = _("Please ensure you leave enough time for the BBC" " micro:bit to be attached and configured" " correctly by your computer. This may take" " several seconds." " Alternatively, try removing and re-attaching the" " device or saving your work and restarting Mu if" " the device remains unfound.") self.view.show_message(message, information)