diff --git a/PrinterConnection.py b/PrinterConnection.py index ff5e97584d..57aedd55a4 100644 --- a/PrinterConnection.py +++ b/PrinterConnection.py @@ -38,6 +38,9 @@ class PrinterConnection(SignalEmitter): self._listen_thread = threading.Thread(target=self._listen) self._listen_thread.daemon = True + self._update_firmware_thread = threading.Thread(target= self._updateFirmware) + self._update_firmware_thread.demon = True + self._heatup_wait_start_time = time.time() ## Queue for commands that need to be send. Used when command is sent when a print is active. @@ -79,6 +82,8 @@ class PrinterConnection(SignalEmitter): self._updating_firmware = False + self._firmware_file_name = None + #TODO: Might need to add check that extruders can not be changed when it started printing or loading these settings from settings object def setNumExtuders(self, num): self._extruder_count = num @@ -116,20 +121,20 @@ class PrinterConnection(SignalEmitter): if not self._updating_firmware and not self._connect_thread.isAlive(): self._connect_thread.start() - def updateFirmware(self, file_name): + def _updateFirmware(self): if self._is_connecting or self._is_connected: self.close() - hex_file = intelHex.readHex(file_name) + hex_file = intelHex.readHex(self._firmware_file_name) if len(hex_file) == 0: Logger.log('e', "Unable to read provided hex file. Could not update firmware") - return False + return programmer = stk500v2.Stk500v2() programmer.progressCallback = self.setProgress programmer.connect(self._serial_port) time.sleep(1) #Give programmer some time to connect if not programmer.isConnected(): Logger.log('e', "Unable to connect with serial. Could not update firmware") - return False + return self._updating_firmware = True try: programmer.programChip(hex_file) @@ -137,9 +142,14 @@ class PrinterConnection(SignalEmitter): except Exception as e: Logger.log("e", "Exception while trying to update firmware %s" %e) self._updating_firmware = False - return False + return programmer.close() - return True + return + + def updateFirmware(self, file_name): + self._firmware_file_name = file_name + self._update_firmware_thread.start() + ## Private connect function run by thread. Can be started by calling connect. def _connect(self): diff --git a/avr_isp/ispBase.py b/avr_isp/ispBase.py index 8d65f03f71..bfed45f0ac 100644 --- a/avr_isp/ispBase.py +++ b/avr_isp/ispBase.py @@ -26,6 +26,7 @@ class IspBase(): self.writeFlash(flashData) print("Verifying %i bytes" % len(flashData)) self.verifyFlash(flashData) + print("Completed") def getSignature(self): """ diff --git a/avr_isp/stk500v2.py b/avr_isp/stk500v2.py index f4c352f977..1af5416a40 100644 --- a/avr_isp/stk500v2.py +++ b/avr_isp/stk500v2.py @@ -80,19 +80,19 @@ class Stk500v2(ispBase.IspBase): #Set load addr to 0, in case we have more then 64k flash we need to enable the address extension page_size = self.chip['pageSize'] * 2 flashSize = page_size * self.chip['pageCount'] + print("Writing flash") if flashSize > 0xFFFF: self.sendMessage([0x06, 0x80, 0x00, 0x00, 0x00]) else: self.sendMessage([0x06, 0x00, 0x00, 0x00, 0x00]) - - loadCount = (len(flash_data) + page_size - 1) / page_size - for i in range(0, loadCount): + load_count = (len(flash_data) + page_size - 1) / page_size + for i in range(0, int(load_count)): recv = self.sendMessage([0x13, page_size >> 8, page_size & 0xFF, 0xc1, 0x0a, 0x40, 0x4c, 0x20, 0x00, 0x00] + flash_data[(i * page_size):(i * page_size + page_size)]) if self.progressCallback is not None: if self._has_checksum: - self.progressCallback(i + 1, loadCount) + self.progressCallback(i + 1, load_count) else: - self.progressCallback(i + 1, loadCount*2) + self.progressCallback(i + 1, load_count*2) def verifyFlash(self, flashData): if self._has_checksum: @@ -114,7 +114,7 @@ class Stk500v2(ispBase.IspBase): self.sendMessage([0x06, 0x00, 0x00, 0x00, 0x00]) loadCount = (len(flashData) + 0xFF) / 0x100 - for i in range(0, loadCount): + for i in range(0, int(loadCount)): recv = self.sendMessage([0x14, 0x01, 0x00, 0x20])[2:0x102] if self.progressCallback is not None: self.progressCallback(loadCount + i + 1, loadCount*2)