def toggle_flash(self, event): """ Display the folder to regist script. Return a dictionary of the settings that may have been changed by the admin dialog. """ regist_box = RegisterWindow(self.view) result = regist_box.exec() if (result == 0): return # Display sending message self.editor.show_status_message(_("Updating...")) # Display sending message reg_info = regist_box.get_register_info() reg_num = reg_info[0] tab = self.view.current_tab usr_file = os.path.join(HOME_DIRECTORY, WORKSPACE_NAME, 'studuinobit', 'usr' + reg_num + '.py') save_and_encode(tab.text(), usr_file, tab.newline) # Send script device_port, serial_number = self.find_device() serial = None exp_flag = False try: serial = Serial(device_port, 115200, timeout=1, parity='N') filename = os.path.basename(usr_file) microfs.put(usr_file, 'usr/' + filename, serial) microfs.execute([ 'import machine', 'machine.nvs_setint("lastSelected", {0})'.format(reg_num), ], serial) time.sleep(0.1) serial.write(b'\x04') except Exception as e: logger.error(e) exp_flag = True finally: if serial is not None: serial.dtr = True serial.close() self.toggle_repl(None) self.toggle_repl(None) # dlg_msg.close() if not exp_flag: self.editor.show_status_message( _("Finished transfer. \ Press the reset button on the Studuino:bit")) else: self.editor.show_status_message(_("Can't transfer."))
def copy_main(self): """ If the attribute self.python_script contains any code, copy it onto the connected micro:bit as main.py, then restart the board (CTRL-D). """ if self.python_script.strip(): script = self.python_script logger.info('Copying main.py onto device') commands = [ "fd = open('main.py', 'wb')", "f = fd.write", ] while script: line = script[:64] commands.append('f(' + repr(line) + ')') script = script[64:] commands.append('fd.close()') logger.info(commands) serial = microfs.get_serial() out, err = microfs.execute(commands, serial) logger.info((out, err)) if err: raise IOError(microfs.clean_error(err)) # Reset the device. serial.write(b'import microbit\r\n') serial.write(b'microbit.reset()\r\n') self.editor.show_status_message(_('Copied code onto micro:bit.')) self.python_script = ''
def copy_main(self): """ If the attribute self.python_script contains any code, copy it onto the connected micro:bit as main.py, then restart the board (CTRL-D). """ if self.python_script.strip(): script = self.python_script logger.info('Copying main.py onto device') commands = [ "fd = open('main.py', 'wb')", "f = fd.write", ] while script: line = script[:64] commands.append('f(' + repr(line) + ')') script = script[64:] commands.append('fd.close()') logger.info(commands) serial = microfs.get_serial() out, err = microfs.execute(commands, serial) logger.info((out, err)) if err: raise IOError(microfs.clean_error(err)) # Send CTRL-D for soft restart. serial.write(b'\x04') self.python_script = ''
def run(self): """ Takes the currently active tab, compiles the Python script therein into a hex file and flashes it all onto the connected device. """ if not self.repl: device_port, serial_number = self.find_device() try: serial = Serial(device_port, 115200, timeout=1, parity='N') except Exception as e: logger.error(e) return try: microfs.execute([ 'import machine', 'machine.nvs_setint("lastSelected", 99)', ], serial) except IOError as e: self.editor.\ show_status_message(_('Please REST Button')) logger.error(e) serial.close() return serial.close() logger.info('Running script.') # Grab the Python script. tab = self.view.current_tab if tab is None: # There is no active text editor. message = _("Cannot run anything without any active editor tabs.") information = _("Running transfers the content of the current tab" " onto the device. It seems like you don't have " " any tabs open.") self.view.show_message(message, information) return python_script = tab.text().split('\n') if not self.repl: self.toggle_repl(None) if self.repl: self.view.repl_pane.send_commands(python_script)
def dropEvent(self, event): source = event.source() item = source.currentItem() if not isinstance(source, LocalFileTree): return if not item.is_file: msg = "Not successfuly, current version just support copy file." logger.info(msg) self.set_message.emit(msg) return source.need_update_tree = False name = item.name path = os.path.join(item.dir, name) if not os.path.exists(path): self.set_message.emit("Sorry, " + name + " not exist in current folder, " + "place reopen file panel.") return if (self.findItems(name, Qt.MatchExactly) and not self.show_confirm_overwrite_dialog()): return try: msg = execute( ["import os", "print(os.statvfs('/'), end='')"], ArdupyDeviceFileList.serial, ) msg = str(msg[0], "utf-8") print(msg) except Exception as ex: print(ex) msg = "Fail! serial error." self.set_message.emit(msg) return val = msg.split(", ") avaliable_byte = int(val[1]) * int(val[4]) file_size = os.path.getsize(path) if avaliable_byte > file_size: msg = "Copying '%s' to seeed board." % name self.disable.emit() self.set_message.emit(msg) self.put.emit(path) else: msg = "Fail! target device doesn't have enough space." self.set_message.emit(msg) logger.info(msg)
def copy_main(self, script): """ If script argument contains any code, copy it onto the connected micro:bit as main.py, then restart the board (CTRL-D). """ if script.strip(): logger.info("Copying main.py onto device") commands = ["fd = open('main.py', 'wb')", "f = fd.write"] while script: line = script[:64] commands.append("f(" + repr(line) + ")") script = script[64:] commands.append("fd.close()") logger.info(commands) serial = microfs.get_serial() out, err = microfs.execute(commands, serial) logger.info((out, err)) if err: raise IOError(microfs.clean_error(err)) # Reset the device. serial.write(b"import microbit\r\n") serial.write(b"microbit.reset()\r\n") self.editor.show_status_message(_("Copied code onto micro:bit."))