DisplayInfoOnLCD.py 27 KB

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