From df0265d4c669ea66e094784d04c41f471a326950 Mon Sep 17 00:00:00 2001 From: Chris Olah Date: Wed, 9 Jan 2013 18:59:39 -0500 Subject: [PATCH 1/3] Make prompt dynamically generated. Prompt is now generated by a promptf() call for every cmdloop iteration. --- pronsole.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pronsole.py b/pronsole.py index 982665b..c994502 100755 --- a/pronsole.py +++ b/pronsole.py @@ -192,6 +192,7 @@ class pronsole(cmd.Cmd): self.p.recvcb = self.recvcb self.recvlisteners = [] self.prompt = "PC>" + self.in_macro = False self.p.onlinecb = self.online self.f = None self.listing = 0 @@ -231,6 +232,19 @@ class pronsole(cmd.Cmd): self.web_config = None self.web_auth_config = None + def promptf(self): + """A function to generate prompts so that we can do dynamic prompts. """ + if self.in_macro: + return "..>" + else: + return "PC>" + + def postcmd(self, stop, line): + """ A hook we override to generate prompts after + each command is executed, for the next prompt.""" + self.prompt = self.promptf() + return stop + def set_temp_preset(self, key, value): if not key.startswith("bed"): self.temps["pla"] = str(self.settings.temperature_pla) @@ -258,7 +272,7 @@ class pronsole(cmd.Cmd): def online(self): print "Printer is now online" - sys.stdout.write(self.prompt) + sys.stdout.write(self.promptf()) sys.stdout.flush() def help_help(self, l): @@ -290,7 +304,8 @@ class pronsole(cmd.Cmd): def end_macro(self): if self.__dict__.has_key("onecmd"): del self.onecmd # remove override - self.prompt = "PC>" + self.in_macro = False + self.prompt = self.promptf() if self.cur_macro_def!="": self.macros[self.cur_macro_name] = self.cur_macro_def macro = self.compile_macro(self.cur_macro_name, self.cur_macro_def) @@ -342,7 +357,8 @@ class pronsole(cmd.Cmd): self.cur_macro_name = macro_name self.cur_macro_def = "" self.onecmd = self.hook_macro # override onecmd temporarily - self.prompt = "..>" + self.in_macro = False + self.prompt = self.promptf() def delete_macro(self, macro_name): if macro_name in self.macros.keys(): @@ -816,7 +832,7 @@ class pronsole(cmd.Cmd): tstring = l.rstrip() if(tstring!="ok" and not tstring.startswith("ok T") and not tstring.startswith("T:") and not self.listing and not self.monitoring): print tstring - sys.stdout.write(self.prompt) + sys.stdout.write(self.promptf()) sys.stdout.flush() for i in self.recvlisteners: i(l) From a224a10ce01c8065b969ee4e7304587739344f1a Mon Sep 17 00:00:00 2001 From: Christopher Olah Date: Fri, 11 Jan 2013 19:02:14 -0500 Subject: [PATCH 2/3] We make the prompt aware of extruder temperature. In order to do this, we consolidate handeling of printer status with a Status class. The status class is updated by recvcb. This has the side effect of simplifying the implementation of gettemp. We also detect whether there is a heated build platform or not, and don't display info about it if there isn't. --- pronsole.py | 59 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/pronsole.py b/pronsole.py index c994502..1e014db 100755 --- a/pronsole.py +++ b/pronsole.py @@ -183,15 +183,44 @@ class Settings: def _all_settings(self): return dict([(k, getattr(self, k)) for k in self.__dict__.keys() if not k.startswith("_")]) +class Status: + + def __init__(self): + self.extruder_temp = 0 + self.extruder_temp_target = 0 + self.bed_temp = 0 + self.bed_temp_target = 0 + self.print_job = None + self.print_job_progress = 1.0 + + def update_tempreading(self, tempstr): + r = tempstr.split() + # eg. r = ["ok", "T:20.5", "/0.0", "B:0.0", "/0.0", "@:0"] + if len(r) == 6: + self.extruder_temp = float(r[1][2:]) + self.extruder_temp_target = float(r[2][1:]) + self.bed_temp = float(r[3][2:]) + self.bed_temp_target = float(r[4][1:]) + + @property + def bed_enabled(self): + return self.bed_temp != 0 + + @property + def extruder_enabled(self): + return self.extruder_temp != 0 + + + class pronsole(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) if not READLINE: self.completekey = None + self.status = Status() self.p = printcore.printcore() self.p.recvcb = self.recvcb self.recvlisteners = [] - self.prompt = "PC>" self.in_macro = False self.p.onlinecb = self.online self.f = None @@ -236,12 +265,23 @@ class pronsole(cmd.Cmd): """A function to generate prompts so that we can do dynamic prompts. """ if self.in_macro: return "..>" + elif not self.p.online: + return "uninitialized>" + elif self.status.extruder_enabled:# and not self.status.bed_enabled: + if self.status.extruder_temp_target == 0: + return "T:%s>" % self.status.extruder_temp + else: + return "T:%s/%s>" % (self.status.extruder_temp, self.status.extruder_temp_target) else: - return "PC>" + return "printer>" def postcmd(self, stop, line): """ A hook we override to generate prompts after - each command is executed, for the next prompt.""" + each command is executed, for the next prompt. + We also use it to send M105 commands so that + temp info gets updated for the prompt.""" + if self.p.online: + self.p.send_now("M105") self.prompt = self.promptf() return stop @@ -536,6 +576,7 @@ class pronsole(cmd.Cmd): def preloop(self): print "Welcome to the printer console! Type \"help\" for a list of available commands." + self.prompt = self.promptf() cmd.Cmd.preloop(self) def do_connect(self, l): @@ -829,6 +870,7 @@ class pronsole(cmd.Cmd): def recvcb(self, l): if "T:" in l: self.tempreadings = l + self.status.update_tempreading(l) tstring = l.rstrip() if(tstring!="ok" and not tstring.startswith("ok T") and not tstring.startswith("T:") and not self.listing and not self.monitoring): print tstring @@ -864,16 +906,15 @@ class pronsole(cmd.Cmd): def help_help(self): self.do_help("") - def tempcb(self, l): - if "T:" in l: - print l.replace("\r", "").replace("T", "Hotend").replace("B", "Bed").replace("\n", "").replace("ok ", "") - def do_gettemp(self, l): if self.p.online: - self.recvlisteners+=[self.tempcb] self.p.send_now("M105") time.sleep(0.75) - self.recvlisteners.remove(self.tempcb) + if not self.status.bed_enabled: + print "Hotend: %s/%s" % (self.status.extruder_temp, self.status.extruder_temp_target) + else: + print "Hotend: %s/%s" % (self.status.extruder_temp, self.status.extruder_temp_target) + print "Bed: %s/%s" % (self.status.bed_temp, self.status.bed_temp_target) def help_gettemp(self): print "Read the extruder and bed temperature." From a377b85c8f3163f051c17486993c198d88864b10 Mon Sep 17 00:00:00 2001 From: Chris Olah Date: Sat, 12 Jan 2013 14:14:10 -0500 Subject: [PATCH 3/3] Make the dynamic prompt temperature stuff configurable. --- pronsole.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pronsole.py b/pronsole.py index 1e014db..b983b9b 100755 --- a/pronsole.py +++ b/pronsole.py @@ -218,6 +218,7 @@ class pronsole(cmd.Cmd): if not READLINE: self.completekey = None self.status = Status() + self.dynamic_temp = False self.p = printcore.printcore() self.p.recvcb = self.recvcb self.recvlisteners = [] @@ -267,7 +268,7 @@ class pronsole(cmd.Cmd): return "..>" elif not self.p.online: return "uninitialized>" - elif self.status.extruder_enabled:# and not self.status.bed_enabled: + elif self.status.extruder_enabled and self.dynamic_temp: if self.status.extruder_temp_target == 0: return "T:%s>" % self.status.extruder_temp else: @@ -280,7 +281,7 @@ class pronsole(cmd.Cmd): each command is executed, for the next prompt. We also use it to send M105 commands so that temp info gets updated for the prompt.""" - if self.p.online: + if self.p.online and self.dynamic_temp: self.p.send_now("M105") self.prompt = self.promptf() return stop @@ -907,6 +908,8 @@ class pronsole(cmd.Cmd): self.do_help("") def do_gettemp(self, l): + if "dynamic" in l: + self.dynamic_temp = True if self.p.online: self.p.send_now("M105") time.sleep(0.75)