DisplayInfoOnLCD.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. # Display Filename and Layer on the LCD by Amanda de Castilho on August 28, 2018
  2. # Modified: Joshua Pope-Lewis on November 16, 2018
  3. # Display Progress on LCD by Mathias Lyngklip Kjeldgaard, Alexander Gee, Kimmo Toivanen, Inigo Martinez on July 31, 2019
  4. # Show Progress was adapted from Display Progress by Louis Wooters on January 6, 2020. His changes are included here.
  5. #---------------------------------------------------------------
  6. # DisplayNameOrProgressOnLCD.py
  7. # Cura Post-Process plugin
  8. # Combines 'Display Filename and Layer on the LCD' with 'Display Progress'
  9. # Combined and with additions by: GregValiant (Greg Foresi)
  10. # Date: September 8, 2023
  11. # NOTE: This combined post processor will make 'Display Filename and Layer on the LCD' and 'Display Progress' obsolete
  12. # Description: Display Filename and Layer options:
  13. # Status messages sent to the printer...
  14. # - Scrolling (SCROLL_LONG_FILENAMES) if enabled in Marlin and you aren't printing a small item select this option.
  15. # - Name: By default it will use the name generated by Cura (EG: TT_Test_Cube) - You may enter a custom name here
  16. # - Start Num: Choose which number you prefer for the initial layer, 0 or 1
  17. # - Max Layer: Enabling this will show how many layers are in the entire print (EG: Layer 1 of 265!)
  18. # - Add prefix 'Printing': Enabling this will add the prefix 'Printing'
  19. # - Example Line on LCD: Printing Layer 0 of 395 3DBenchy
  20. # Display Progress options:
  21. # - Display Total Layer Count
  22. # - Disply Time Remaining for the print
  23. # - Time Fudge Factor % - Divide the Actual Print Time by the Cura Estimate. Enter as a percentage and the displayed time will be adjusted. This allows you to bring the displayed time closer to reality (Ex: Entering 87.5 would indicate an adjustment to 87.5% of the Cura estimate).
  24. # - Example line on LCD: 1/479 | ET 2h13m
  25. # - Time to Pauses changes the M117/M118 lines to countdown to the next pause as 1/479 | TP 2h36m
  26. # - 'Add M118 Line' is available with either option. M118 will bounce the message back to a remote print server through the USB connection.
  27. # - 'Add M73 Line' is used by 'Display Progress' only. There are options to incluse M73 P(percent) and M73 R(time remaining)
  28. # - Enable 'Finish-Time' Message - when enabled, takes the Print Time and calculates when the print will end. It takes into account the Time Fudge Factor. The user may enter a print start time. This is also available for Display Filename.
  29. from ..Script import Script
  30. from UM.Application import Application
  31. from UM.Qt.Duration import DurationFormat
  32. import time
  33. import datetime
  34. import math
  35. from UM.Message import Message
  36. class DisplayInfoOnLCD(Script):
  37. def getSettingDataString(self):
  38. return """{
  39. "name": "Display Info on LCD",
  40. "key": "DisplayInfoOnLCD",
  41. "metadata": {},
  42. "version": 2,
  43. "settings":
  44. {
  45. "display_option":
  46. {
  47. "label": "LCD display option...",
  48. "description": "Display Filename and Layer was formerly 'Display Filename and Layer on LCD' post-processor. The message format on the LCD is 'Printing Layer 0 of 15 3D Benchy'. Display Progress is similar to the former 'Display Progress on LCD' post-processor. The display format is '1/16 | ET 2hr28m'. Display Progress includes a fudge factor for the print time estimate.",
  49. "type": "enum",
  50. "options": {
  51. "display_progress": "Display Progress",
  52. "filename_layer": "Filename and Layer"
  53. },
  54. "default_value": "display_progress"
  55. },
  56. "format_option":
  57. {
  58. "label": "Scroll enabled/Small layers?",
  59. "description": "If SCROLL_LONG_FILENAMES is enabled in your firmware select this setting.",
  60. "type": "bool",
  61. "default_value": false,
  62. "enabled": "display_option == 'filename_layer'"
  63. },
  64. "file_name":
  65. {
  66. "label": "Text to display:",
  67. "description": "By default the current filename will be displayed on the LCD. Enter text here to override the filename and display something else.",
  68. "type": "str",
  69. "default_value": "",
  70. "enabled": "display_option == 'filename_layer'"
  71. },
  72. "startNum":
  73. {
  74. "label": "Initial layer number:",
  75. "description": "Choose which number you prefer for the initial layer, 0 or 1",
  76. "type": "int",
  77. "default_value": 0,
  78. "minimum_value": 0,
  79. "maximum_value": 1,
  80. "enabled": "display_option == 'filename_layer'"
  81. },
  82. "maxlayer":
  83. {
  84. "label": "Display max layer?:",
  85. "description": "Display how many layers are in the entire print on status bar?",
  86. "type": "bool",
  87. "default_value": true,
  88. "enabled": "display_option == 'filename_layer'"
  89. },
  90. "addPrefixPrinting":
  91. {
  92. "label": "Add prefix 'Printing'?",
  93. "description": "This will add the prefix 'Printing'",
  94. "type": "bool",
  95. "default_value": true,
  96. "enabled": "display_option == 'filename_layer'"
  97. },
  98. "display_total_layers":
  99. {
  100. "label": "Display total layers",
  101. "description": "This setting adds the 'Total Layers' to the LCD message as '17/234'.",
  102. "type": "bool",
  103. "default_value": true,
  104. "enabled": "display_option == 'display_progress'"
  105. },
  106. "display_remaining_time":
  107. {
  108. "label": "Display remaining time",
  109. "description": "This will add the remaining printing time to the LCD message.",
  110. "type": "bool",
  111. "default_value": true,
  112. "enabled": "display_option == 'display_progress'"
  113. },
  114. "add_m118_line":
  115. {
  116. "label": "Add M118 Line",
  117. "description": "Adds M118 in addition to the M117. It will bounce the message back through the USB port to a computer print server (if a printer server like Octoprint or Pronterface is in use).",
  118. "type": "bool",
  119. "default_value": false
  120. },
  121. "add_m73_line":
  122. {
  123. "label": "Add M73 Line(s)",
  124. "description": "Adds M73 in addition to the M117. For some firmware this will set the printers time and or percentage.",
  125. "type": "bool",
  126. "default_value": false,
  127. "enabled": "display_option == 'display_progress'"
  128. },
  129. "add_m73_percent":
  130. {
  131. "label": " Add M73 Percentage",
  132. "description": "Adds M73 with the P parameter. For some firmware this will set the printers 'percentage' of layers completed and it will count upward.",
  133. "type": "bool",
  134. "default_value": false,
  135. "enabled": "add_m73_line and display_option == 'display_progress'"
  136. },
  137. "add_m73_time":
  138. {
  139. "label": " Add M73 Time",
  140. "description": "Adds M73 with the R parameter. For some firmware this will set the printers 'print time' and it will count downward.",
  141. "type": "bool",
  142. "default_value": false,
  143. "enabled": "add_m73_line and display_option == 'display_progress'"
  144. },
  145. "speed_factor":
  146. {
  147. "label": "Time Fudge Factor %",
  148. "description": "When using 'Display Progress' tweak this value to get better estimates. ([Actual Print Time]/[Cura Estimate]) x 100 = Time Fudge Factor. If Cura estimated 9hr and the print actually took 10hr30min then enter 117 here to adjust any estimate closer to reality. This Fudge Factor is also used to calculate the print finish time.",
  149. "type": "float",
  150. "unit": "%",
  151. "default_value": 100,
  152. "enabled": "enable_end_message or display_option == 'display_progress'"
  153. },
  154. "countdown_to_pause":
  155. {
  156. "label": "Countdown to Pauses",
  157. "description": "Instead of the remaining print time the LCD will show the estimated time to pause (TP).",
  158. "type": "bool",
  159. "default_value": false,
  160. "enabled": "display_option == 'display_progress'"
  161. },
  162. "enable_end_message":
  163. {
  164. "label": "Enable 'Finish-Time' Message",
  165. "description": "Get a message when you save a fresh slice. It will show the estimated date and time that the print would finish.",
  166. "type": "bool",
  167. "default_value": true,
  168. "enabled": true
  169. },
  170. "print_start_time":
  171. {
  172. "label": "Print Start Time (Ex 16:45)",
  173. "description": "Use 'Military' time. 16:45 would be 4:45PM. 09:30 would be 9:30AM. If you leave this blank it will be assumed that the print will start Now. If you enter a guesstimate of your printer start time and that time is before 'Now' the guesstimate will consider that the print will start tomorrow at the entered time. ",
  174. "type": "str",
  175. "default_value": "",
  176. "unit": "hrs ",
  177. "enabled": "enable_end_message"
  178. }
  179. }
  180. }"""
  181. def execute(self, data):
  182. display_option = self.getSettingValueByKey("display_option")
  183. add_m118_line = self.getSettingValueByKey("add_m118_line")
  184. add_m73_line = self.getSettingValueByKey("add_m73_line")
  185. add_m73_time = self.getSettingValueByKey("add_m73_time")
  186. add_m73_percent = self.getSettingValueByKey("add_m73_percent")
  187. # This is Display Filename and Layer on LCD---------------------------------------------------------
  188. if display_option == "filename_layer":
  189. max_layer = 0
  190. lcd_text = "M117 "
  191. if self.getSettingValueByKey("file_name") != "":
  192. file_name = self.getSettingValueByKey("file_name")
  193. else:
  194. file_name = Application.getInstance().getPrintInformation().jobName
  195. if self.getSettingValueByKey("addPrefixPrinting"):
  196. lcd_text += "Printing "
  197. if not self.getSettingValueByKey("scroll"):
  198. lcd_text += "Layer "
  199. else:
  200. lcd_text += file_name + " - Layer "
  201. i = self.getSettingValueByKey("startNum")
  202. for layer in data:
  203. display_text = lcd_text + str(i)
  204. layer_index = data.index(layer)
  205. lines = layer.split("\n")
  206. for line in lines:
  207. if line.startswith(";LAYER_COUNT:"):
  208. max_layer = line
  209. max_layer = max_layer.split(":")[1]
  210. if self.getSettingValueByKey("startNum") == 0:
  211. max_layer = str(int(max_layer) - 1)
  212. if line.startswith(";LAYER:"):
  213. if self.getSettingValueByKey("maxlayer"):
  214. display_text = display_text + " of " + max_layer
  215. if not self.getSettingValueByKey("scroll"):
  216. display_text = display_text + " " + file_name
  217. else:
  218. if not self.getSettingValueByKey("scroll"):
  219. display_text = display_text + " " + file_name + "!"
  220. else:
  221. display_text = display_text + "!"
  222. line_index = lines.index(line)
  223. lines.insert(line_index + 1, display_text)
  224. if add_m118_line:
  225. lines.insert(line_index + 2, str(display_text.replace("M117", "M118", 1)))
  226. i += 1
  227. final_lines = "\n".join(lines)
  228. data[layer_index] = final_lines
  229. if bool(self.getSettingValueByKey("enable_end_message")):
  230. message_str = self.message_to_user(self.getSettingValueByKey("speed_factor") / 100)
  231. Message(title = "Display Info on LCD - Estimated Finish Time", text = message_str[0] + "\n\n" + message_str[1] + "\n" + message_str[2] + "\n" + message_str[3]).show()
  232. return data
  233. # Display Progress (from 'Show Progress' and 'Display Progress on LCD')---------------------------------------
  234. elif display_option == "display_progress":
  235. # get settings
  236. display_total_layers = self.getSettingValueByKey("display_total_layers")
  237. display_remaining_time = self.getSettingValueByKey("display_remaining_time")
  238. speed_factor = self.getSettingValueByKey("speed_factor") / 100
  239. m73_time = False
  240. m73_percent = False
  241. if add_m73_line and add_m73_time:
  242. m73_time = True
  243. if add_m73_line and add_m73_percent:
  244. m73_percent = True
  245. # initialize global variables
  246. first_layer_index = 0
  247. time_total = 0
  248. number_of_layers = 0
  249. time_elapsed = 0
  250. # if at least one of the settings is disabled, there is enough room on the display to display "layer"
  251. first_section = data[0]
  252. lines = first_section.split("\n")
  253. for line in lines:
  254. if line.startswith(";TIME:"):
  255. tindex = lines.index(line)
  256. cura_time = int(line.split(":")[1])
  257. print_time = cura_time * speed_factor
  258. hhh = print_time/3600
  259. hr = round(hhh // 1)
  260. mmm = round((hhh % 1) * 60)
  261. orig_hhh = cura_time/3600
  262. orig_hr = round(orig_hhh // 1)
  263. orig_mmm = math.floor((orig_hhh % 1) * 60)
  264. orig_sec = round((((orig_hhh % 1) * 60) % 1) * 60)
  265. if add_m118_line: lines.insert(tindex + 3,"M118 Adjusted Print Time " + str(hr) + "hr " + str(mmm) + "min")
  266. lines.insert(tindex + 3,"M117 ET " + str(hr) + "hr " + str(mmm) + "min")
  267. # add M73 line at beginning
  268. mins = int(60 * hr + mmm)
  269. if m73_time:
  270. lines.insert(tindex + 3, "M73 R{}".format(mins))
  271. if m73_percent:
  272. lines.insert(tindex + 3, "M73 P0")
  273. # If Countdonw to pause is enabled then count the pauses
  274. pause_str = ""
  275. if bool(self.getSettingValueByKey("countdown_to_pause")):
  276. pause_count = 0
  277. for num in range(2,len(data) - 1, 1):
  278. if "PauseAtHeight.py" in data[num]:
  279. pause_count += 1
  280. pause_str = f" with {pause_count} pause(s)"
  281. # This line goes in to convert seconds to hours and minutes
  282. lines.insert(tindex + 3, f";Cura Time Estimate: {cura_time}sec = {orig_hr}hr {orig_mmm}min {orig_sec}sec {pause_str}")
  283. data[0] = "\n".join(lines)
  284. data[len(data)-1] += "M117 Orig Cura Est " + str(orig_hr) + "hr " + str(orig_mmm) + "min\n"
  285. if add_m118_line: data[len(data)-1] += "M118 Est w/FudgeFactor " + str(speed_factor * 100) + "% was " + str(hr) + "hr " + str(mmm) + "min\n"
  286. if not display_total_layers or not display_remaining_time:
  287. base_display_text = "layer "
  288. else:
  289. base_display_text = ""
  290. layer = data[len(data)-1]
  291. data[len(data)-1] = layer.replace(";End of Gcode" + "\n", "")
  292. data[len(data)-1] += ";End of Gcode" + "\n"
  293. # Search for the number of layers and the total time from the start code
  294. for index in range(len(data)):
  295. data_section = data[index]
  296. # We have everything we need, save the index of the first layer and exit the loop
  297. if ";LAYER:" in data_section:
  298. first_layer_index = index
  299. break
  300. else:
  301. for line in data_section.split("\n"):
  302. if line.startswith(";LAYER_COUNT:"):
  303. number_of_layers = int(line.split(":")[1])
  304. elif line.startswith(";TIME:"):
  305. time_total = int(line.split(":")[1])
  306. # for all layers...
  307. current_layer = 0
  308. for layer_counter in range(len(data)-2):
  309. current_layer += 1
  310. layer_index = first_layer_index + layer_counter
  311. display_text = base_display_text
  312. display_text += str(current_layer)
  313. # create a list where each element is a single line of code within the layer
  314. lines = data[layer_index].split("\n")
  315. if not ";LAYER:" in data[layer_index]:
  316. current_layer -= 1
  317. continue
  318. # add the total number of layers if this option is checked
  319. if display_total_layers:
  320. display_text += "/" + str(number_of_layers)
  321. # if display_remaining_time is checked, it is calculated in this loop
  322. if display_remaining_time:
  323. time_remaining_display = " | ET " # initialize the time display
  324. m = (time_total - time_elapsed) // 60 # estimated time in minutes
  325. m *= speed_factor # correct for printing time
  326. m = int(m)
  327. h, m = divmod(m, 60) # convert to hours and minutes
  328. # add the time remaining to the display_text
  329. if h > 0: # if it's more than 1 hour left, display format = xhxxm
  330. time_remaining_display += str(h) + "h"
  331. if m < 10: # add trailing zero if necessary
  332. time_remaining_display += "0"
  333. time_remaining_display += str(m) + "m"
  334. else:
  335. time_remaining_display += str(m) + "m"
  336. display_text += time_remaining_display
  337. # find time_elapsed at the end of the layer (used to calculate the remaining time of the next layer)
  338. if not current_layer == number_of_layers:
  339. for line_index in range(len(lines) - 1, -1, -1):
  340. line = lines[line_index]
  341. if line.startswith(";TIME_ELAPSED:"):
  342. # update time_elapsed for the NEXT layer and exit the loop
  343. time_elapsed = int(float(line.split(":")[1]))
  344. break
  345. # insert the text AFTER the first line of the layer (in case other scripts use ";LAYER:")
  346. for l_index, line in enumerate(lines):
  347. if line.startswith(";LAYER:"):
  348. lines[l_index] += "\nM117 " + display_text
  349. # add M73 line
  350. mins = int(60 * h + m)
  351. if m73_time:
  352. lines[l_index] += "\nM73 R{}".format(mins)
  353. if m73_percent:
  354. lines[l_index] += "\nM73 P" + str(round(int(current_layer) / int(number_of_layers) * 100))
  355. if add_m118_line:
  356. lines[l_index] += "\nM118 " + display_text
  357. break
  358. # overwrite the layer with the modified layer
  359. data[layer_index] = "\n".join(lines)
  360. # If enabled then change the ET to TP for 'Time To Pause'
  361. if bool(self.getSettingValueByKey("countdown_to_pause")):
  362. time_list = []
  363. time_list.append("0")
  364. time_list.append("0")
  365. this_time = 0
  366. pause_index = 1
  367. # Get the layer times
  368. for num in range(2,len(data) - 1):
  369. layer = data[num]
  370. lines = layer.split("\n")
  371. for line in lines:
  372. if line.startswith(";TIME_ELAPSED:"):
  373. this_time = (float(line.split(":")[1]))*speed_factor
  374. time_list.append(str(this_time))
  375. if "PauseAtHeight.py" in layer:
  376. for qnum in range(num - 1, pause_index, -1):
  377. time_list[qnum] = str(float(this_time) - float(time_list[qnum])) + "P"
  378. pause_index = num-1
  379. # Make the adjustments to the M117 (and M118) lines that are prior to a pause
  380. for num in range (2, len(data) - 1,1):
  381. layer = data[num]
  382. lines = layer.split("\n")
  383. for line in lines:
  384. if line.startswith("M117") and "|" in line and "P" in time_list[num]:
  385. M117_line = line.split("|")[0] + "| TP "
  386. alt_time = time_list[num][:-1]
  387. hhh = int(float(alt_time) / 3600)
  388. if hhh > 0:
  389. hhr = str(hhh) + "h"
  390. else:
  391. hhr = ""
  392. mmm = ((float(alt_time) / 3600) - (int(float(alt_time) / 3600))) * 60
  393. sss = int((mmm - int(mmm)) * 60)
  394. mmm = str(round(mmm)) + "m"
  395. time_to_go = str(hhr) + str(mmm)
  396. if hhr == "": time_to_go = time_to_go + str(sss) + "s"
  397. M117_line = M117_line + time_to_go
  398. layer = layer.replace(line, M117_line)
  399. if line.startswith("M118") and "|" in line and "P" in time_list[num]:
  400. M118_line = line.split("|")[0] + "| TP " + time_to_go
  401. layer = layer.replace(line, M118_line)
  402. data[num] = layer
  403. setting_data = ""
  404. if bool(self.getSettingValueByKey("enable_end_message")):
  405. message_str = self.message_to_user(speed_factor)
  406. Message(title = "[Display Info on LCD] - Estimated Finish Time", text = message_str[0] + "\n\n" + message_str[1] + "\n" + message_str[2] + "\n" + message_str[3]).show()
  407. return data
  408. def message_to_user(self, speed_factor: float):
  409. # Message the user of the projected finish time of the print
  410. print_time = Application.getInstance().getPrintInformation().currentPrintTime.getDisplayString(DurationFormat.Format.ISO8601)
  411. print_start_time = self.getSettingValueByKey("print_start_time")
  412. # If the user entered a print start time make sure it is in the correct format or ignore it.
  413. if print_start_time == "" or print_start_time == "0" or len(print_start_time) != 5 or not ":" in print_start_time:
  414. print_start_time = ""
  415. # Change the print start time to proper time format, or, use the current time
  416. if print_start_time != "":
  417. hr = int(print_start_time.split(":")[0])
  418. min = int(print_start_time.split(":")[1])
  419. sec = 0
  420. else:
  421. hr = int(time.strftime("%H"))
  422. min = int(time.strftime("%M"))
  423. sec = int(time.strftime("%S"))
  424. #Get the current data/time info
  425. yr = int(time.strftime("%Y"))
  426. day = int(time.strftime("%d"))
  427. mo = int(time.strftime("%m"))
  428. date_and_time = datetime.datetime(yr, mo, day, hr, min, sec)
  429. #Split the Cura print time
  430. pr_hr = int(print_time.split(":")[0])
  431. pr_min = int(print_time.split(":")[1])
  432. pr_sec = int(print_time.split(":")[2])
  433. #Adjust the print time if none was entered
  434. print_seconds = pr_hr*3600 + pr_min*60 + pr_sec
  435. #Adjust the total seconds by the Fudge Factor
  436. adjusted_print_time = print_seconds * speed_factor
  437. #Break down the adjusted seconds back into hh:mm:ss
  438. adj_hr = int(adjusted_print_time/3600)
  439. print_seconds = adjusted_print_time - (adj_hr * 3600)
  440. adj_min = int(print_seconds) / 60
  441. adj_sec = int(print_seconds - (adj_min * 60))
  442. #Get the print time to add to the start time
  443. time_change = datetime.timedelta(hours=adj_hr, minutes=adj_min, seconds=adj_sec)
  444. new_time = date_and_time + time_change
  445. #Get the day of the week that the print will end on
  446. week_day = str(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][int(new_time.strftime("%w"))])
  447. #Get the month that the print will end in
  448. mo_str = str(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][int(new_time.strftime("%m"))-1])
  449. #Make adjustments from 24hr time to 12hr time
  450. if int(new_time.strftime("%H")) > 12:
  451. show_hr = str(int(new_time.strftime("%H")) - 12) + ":"
  452. show_ampm = " PM"
  453. elif int(new_time.strftime("%H")) == 0:
  454. show_hr = "12:"
  455. show_ampm = " AM"
  456. else:
  457. show_hr = str(new_time.strftime("%H")) + ":"
  458. show_ampm = " AM"
  459. if print_start_time == "":
  460. start_str = "Now"
  461. else:
  462. start_str = "and your entered 'print start time' of " + print_start_time + "hrs."
  463. if print_start_time != "":
  464. print_start_str = "Print Start Time................." + str(print_start_time) + "hrs"
  465. else:
  466. print_start_str = "Print Start Time.................Now."
  467. estimate_str = "Cura Time Estimate.........." + str(print_time)
  468. adjusted_str = "Adjusted Time Estimate..." + str(time_change)
  469. finish_str = week_day + " " + str(mo_str) + " " + str(new_time.strftime("%d")) + ", " + str(new_time.strftime("%Y")) + " at " + str(show_hr) + str(new_time.strftime("%M")) + str(show_ampm)
  470. return finish_str, estimate_str, adjusted_str, print_start_str