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. """ # 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') # Generate a hex file. python_hex = uflash.hexlify(python_script) micropython_hex = uflash.embed_hex(uflash._RUNTIME, python_hex) path_to_microbit = uflash.find_microbit() if path_to_microbit: hex_file = os.path.join(path_to_microbit, 'micropython.hex') uflash.save_hex(micropython_hex, hex_file) message = 'Flashing "{}" onto the micro:bit.'.format(tab.label) information = ("When the yellow LED stops flashing the device" " will restart and your script will run. If there" " is an error, you'll see a helpful message scroll" " across the device's display.") self._view.show_message(message, information, 'Information') else: message = 'Could not find an attached BBC micro:bit.' self._view.show_message(message)
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. """ # 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") # Generate a hex file python_hex = uflash.hexlify(python_script) micropython_hex = uflash.embed_hex(uflash._RUNTIME, python_hex) path_to_microbit = uflash.find_microbit() if path_to_microbit: hex_file = os.path.join(path_to_microbit, "micropython.hex") uflash.save_hex(micropython_hex, hex_file) message = 'Flashing "{}" onto the micro:bit.'.format(tab.label) information = ( "When the yellow LED stops flashing the device" " will restart and your script will run. If there" " is an error, you'll see a helpful message scroll" " across the device's display." ) self._view.show_message(message, information, "Information") else: message = "Could not find an attached BBC micro:bit." self._view.show_message(message)
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 # Generate a hex file. python_hex = uflash.hexlify(python_script) logger.debug('Python hex:') logger.debug(python_hex) micropython_hex = uflash.embed_hex(uflash._RUNTIME, python_hex) # 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.') hex_file = os.path.join(path_to_microbit, 'micropython.hex') uflash.save_hex(micropython_hex, hex_file) message = 'Flashing "{}" onto the micro:bit.'.format(tab.label) information = ("When the yellow LED stops flashing the device" " will restart and your script will run. If there" " is an error, you'll see a helpful message scroll" " across the device's display.") self._view.show_message(message, information, 'Information') 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)
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 # Generate a hex file. python_hex = uflash.hexlify(python_script) logger.debug('Python hex:') logger.debug(python_hex) micropython_hex = uflash.embed_hex(uflash._RUNTIME, python_hex) # 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.') hex_file = os.path.join(path_to_microbit, 'micropython.hex') uflash.save_hex(micropython_hex, hex_file) message = 'Flashing "{}" onto the micro:bit.'.format(tab.label) information = ("When the yellow LED stops flashing the device" " will restart and your script will run. If there" " is an error, you'll see a helpful message scroll" " across the device's display.") self._view.show_message(message, information, 'Information') 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)