DisplayRemainingTimeOnLCD.py 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Cura PostProcessingPlugin
  2. # Author: Mathias Lyngklip Kjeldgaard
  3. # Date: July 31, 2019
  4. # Modified: November 26, 2019
  5. # Description: This plugin displayes the remaining time on the LCD of the printer
  6. # using the estimated print-time generated by Cura.
  7. from ..Script import Script
  8. import re
  9. import datetime
  10. class DisplayRemainingTimeOnLCD(Script):
  11. def __init__(self):
  12. super().__init__()
  13. def getSettingDataString(self):
  14. return """{
  15. "name":"Display Remaining Time on LCD",
  16. "key":"DisplayRemainingTimeOnLCD",
  17. "metadata": {},
  18. "version": 2,
  19. "settings":
  20. {
  21. "TurnOn":
  22. {
  23. "label": "Enable",
  24. "description": "When enabled, It will write Time Left: HHMMSS on the display. This is updated every layer.",
  25. "type": "bool",
  26. "default_value": false
  27. }
  28. }
  29. }"""
  30. def execute(self, data):
  31. if self.getSettingValueByKey("TurnOn"):
  32. total_time = 0
  33. total_time_string = ""
  34. for layer in data:
  35. layer_index = data.index(layer)
  36. lines = layer.split("\n")
  37. for line in lines:
  38. if line.startswith(";TIME:"):
  39. # At this point, we have found a line in the GCODE with ";TIME:"
  40. # which is the indication of total_time. Looks like: ";TIME:1337", where
  41. # 1337 is the total print time in seconds.
  42. line_index = lines.index(line) # We take a hold of that line
  43. split_string = re.split(":", line) # Then we split it, so we can get the number
  44. string_with_numbers = "{}".format(split_string[1]) # Here we insert that number from the
  45. # list into a string.
  46. total_time = int(string_with_numbers) # Only to contert it to a int.
  47. m, s = divmod(total_time, 60) # Math to calculate
  48. h, m = divmod(m, 60) # hours, minutes and seconds.
  49. total_time_string = "{:d}h{:02d}m{:02d}s".format(h, m, s) # Now we put it into the string
  50. lines[line_index] = "M117 Time Left {}".format(total_time_string) # And print that string instead of the original one
  51. elif line.startswith(";TIME_ELAPSED:"):
  52. # As we didnt find the total time (";TIME:"), we have found a elapsed time mark
  53. # This time represents the time the printer have printed. So with some math;
  54. # totalTime - printTime = RemainingTime.
  55. line_index = lines.index(line) # We get a hold of the line
  56. list_split = re.split(":", line) # Again, we split at ":" so we can get the number
  57. string_with_numbers = "{}".format(list_split[1]) # Then we put that number from the list, into a string
  58. current_time = float(string_with_numbers) # This time we convert to a float, as the line looks something like:
  59. # ;TIME_ELAPSED:1234.6789
  60. # which is total time in seconds
  61. time_left = total_time - current_time # Here we calculate remaining time
  62. m1, s1 = divmod(time_left, 60) # And some math to get the total time in seconds into
  63. h1, m1 = divmod(m1, 60) # the right format. (HH,MM,SS)
  64. current_time_string = "{:d}h{:2d}m{:2d}s".format(int(h1), int(m1), int(s1)) # Here we create the string holding our time
  65. lines[line_index] = "M117 Time Left {}".format(current_time_string) # And now insert that into the GCODE
  66. # Here we are OUT of the second for-loop
  67. # Which means we have found and replaces all the occurences.
  68. # Which also means we are ready to join the lines for that section of the GCODE file.
  69. final_lines = "\n".join(lines)
  70. data[layer_index] = final_lines
  71. return data