class PDDEmulationKnittingPlugin(BaseKnittingPlugin): __PLUGIN_NAME__ = u"PDD" def __init__(self, callbacks_dict=None, interactive_callbacks=None): super(PDDEmulationKnittingPlugin, self).__init__(callbacks_dict, interactive_callbacks) logging.debug("Loaded PDD emulator version: {}".format(PDDemulate.version)) self.__port = "" self.__conf = None self.__main_temp_dir = tempfile.gettempdir() self.__folder = os.path.join(self.__main_temp_dir, "PDD_tmp_dir") self.__emu = None # TODO: init emulator def register_interactive_callbacks(self, callbacks=None): super(PDDEmulationKnittingPlugin, self).register_interactive_callbacks(callbacks) def set_port(self, portname): self.__port = portname def onconfigure(self, e): if hasattr(e, "conf") and e.conf is not None: self.__conf = e.conf else: raise knitlib.exceptions.InputException("Conf dict is missing") conf = self.__conf if self.__port is "": self.__port = conf.get("port", "/dev/ttyUSB0") try: os.mkdir(self.__folder) except OSError: # Exception thrown meaning the folder already exists logging.warning("PDD temp folder already exists") # Copying the image file to temp folder. try: shutil.copy(conf.get("file_url"), self.__folder) except IOError: logging.error("Error when copying file url") self.__emu = PDDemulator(self.__folder) def onknit(self, e): self.__emu.open(cport=self.__port) logging.info("PDD Emulator Ready") self.__emu.handleRequests() def validate_configuration(self, conf): # TODO validate formally return True def onfinish(self, e): # TODO: remove and cleanup dir at self.__folder self.__emu.close() @staticmethod def supported_config_features(): return {"$schema": "http://json-schema.org/schema#", "type": "object"}
return True def onfinish(self, e): # TODO: remove and cleanup dir at self.__folder self.__emu.close() @staticmethod def supported_config_features(): return {"$schema": "http://json-schema.org/schema#", "type": "object"} if __name__ == "__main__": if len(sys.argv) < 3: print 'Usage: %s basedir serialdevice' % sys.argv[0] sys.exit() print 'Preparing . . . Please Wait' emu = PDDemulator(sys.argv[1]) # TODO: fix usb port hardcoding emu.open(cport=sys.argv[2]) print 'Emulator Ready!' try: while 1: emu.handleRequests() except (KeyboardInterrupt): pass emu.close()
class PDDEmulationKnittingPlugin(BaseKnittingPlugin): __PLUGIN_NAME__ = u"PDD" def __init__(self, callbacks_dict=None, interactive_callbacks=None): super(PDDEmulationKnittingPlugin, self).__init__(callbacks_dict, interactive_callbacks) logging.debug("Loaded PDD emulator version: {}".format( PDDemulate.version)) self.__port = "" self.__conf = None self.__main_temp_dir = tempfile.gettempdir() self.__folder = os.path.join(self.__main_temp_dir, "PDD_tmp_dir") self.__emu = None # TODO: init emulator def register_interactive_callbacks(self, callbacks=None): super(PDDEmulationKnittingPlugin, self).register_interactive_callbacks(callbacks) def set_port(self, portname): self.__port = portname def onconfigure(self, e): if hasattr(e, "conf") and e.conf is not None: self.__conf = e.conf else: raise knitlib.exceptions.InputException("Conf dict is missing") conf = self.__conf if self.__port is "": self.__port = conf.get("port", "/dev/ttyUSB0") try: os.mkdir(self.__folder) except OSError: # Exception thrown meaning the folder already exists logging.warning("PDD temp folder already exists") # Copying the image file to temp folder. try: shutil.copy(conf.get("file_url"), self.__folder) except IOError: logging.error("Error when copying file url") self.__emu = PDDemulator(self.__folder) def onknit(self, e): self.__emu.open(cport=self.__port) logging.info("PDD Emulator Ready") self.__emu.handleRequests() def validate_configuration(self, conf): # TODO validate formally return True def onfinish(self, e): # TODO: remove and cleanup dir at self.__folder self.__emu.close() @staticmethod def supported_config_features(): return {"$schema": "http://json-schema.org/schema#", "type": "object"}
class KnittingApp(Tkinter.Tk): def __init__(self,parent=None): Tkinter.Tk.__init__(self,parent) self.parent = parent self.initialize() #self.startEmulator() def initialize(self): self.msg = Messages(self) self.patterns = [] self.pattern = None self.currentDatFile = None self.initConfig() self.initializeUtilities() self.gui = Gui() self.gui.initializeMainWindow(self) self.updatePatternCanvasLastSize() self.patternListBox.bind('<<ListboxSelect>>', self.patternSelected) self.after_idle(self.canvasConfigured) self.deviceEntry.entryText.set(self.getConfig().device) self.datFileEntry.entryText.set(self.getConfig().datFile) self.initEmulator() self.after_idle(self.reloadPatternFile) def initializeUtilities(self): self.patternDumper = PatternDumper() self.patternDumper.printInfoCallback = self.msg.showInfo self.patternInserter = PatternInserter() self.patternInserter.printInfoCallback = self.msg.showInfo self.patternInserter.printErrorCallback = self.msg.showError def initEmulator(self): self.emu = PDDemulator(self.getConfig().imgdir) self.emu.listeners.append(PDDListener(self)) #self.emu = lambda: 1 self.setEmulatorStarted(False) def emuButtonClicked(self): self.getConfig().device = self.deviceEntry.entryText.get() if self.emu.started: self.stopEmulator() else: self.startEmulator() def startEmulator(self): self.msg.showInfo('Preparing emulator. . . Please Wait') if self.getConfig().simulateEmulator: self.msg.showInfo('Simulating emulator, emulator is not started...') self.setEmulatorStarted(True) else: try: port = self.getConfig().device self.emu.open(cport=port) self.msg.showInfo('PDDemulate Version 1.1 Ready!') self.setEmulatorStarted(True) self.after_idle(self.emulatorLoop) except Exception, e: self.msg.showError('Ensure that TFDI cable is connected to port ' + port + '\n\nError: ' + str(e)) self.setEmulatorStarted(False)