Tcodes.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "Tcodes.h"
  2. #include "Marlin.h"
  3. #include "mmu2.h"
  4. #include "stepper.h"
  5. #include <avr/pgmspace.h>
  6. #include <ctype.h>
  7. #include <stdint.h>
  8. #include "language.h"
  9. #include "messages.h"
  10. #include "ultralcd.h"
  11. #include <stdio.h>
  12. static const char duplicate_Tcode_ignored[] PROGMEM = "Duplicate T-code ignored.";
  13. inline bool IsInvalidTCode(char *const s, uint8_t i) {
  14. return ((s[i] < '0' || s[i] > '4') && s[i] != '?' && s[i] != 'x' && s[i] != 'c');
  15. }
  16. inline void TCodeInvalid() {
  17. SERIAL_ECHOLNPGM("Invalid T code.");
  18. }
  19. // load to bondtech gears; if mmu is not present do nothing
  20. void TCodeX() {
  21. if (MMU2::mmu2.Enabled()) {
  22. uint8_t selectedSlot = choose_menu_P(_T(MSG_CHOOSE_FILAMENT), _T(MSG_FILAMENT));
  23. if ((selectedSlot == MMU2::mmu2.get_current_tool()) /*&& mmu_fil_loaded @@TODO */){
  24. // dont execute the same T-code twice in a row
  25. puts_P(duplicate_Tcode_ignored);
  26. } else {
  27. st_synchronize();
  28. MMU2::mmu2.tool_change(selectedSlot);
  29. }
  30. }
  31. }
  32. // load to from bondtech gears to nozzle (nozzle should be preheated)
  33. void TCodeC() {
  34. if (MMU2::mmu2.Enabled()) {
  35. st_synchronize();
  36. // @@TODO mmu_continue_loading(usb_timer.running() || (lcd_commands_type == LcdCommands::Layer1Cal));
  37. // mmu_extruder = selectedSlot; // filament change is finished
  38. // MMU2::mmu2.load_filament_to_nozzle();
  39. }
  40. }
  41. struct SChooseFromMenu {
  42. uint8_t slot:7;
  43. uint8_t loadToNozzle:1;
  44. inline constexpr SChooseFromMenu(uint8_t slot, bool loadToNozzle):slot(slot), loadToNozzle(loadToNozzle){}
  45. inline constexpr SChooseFromMenu():slot(0), loadToNozzle(false) { }
  46. };
  47. SChooseFromMenu TCodeChooseFromMenu() {
  48. if (MMU2::mmu2.Enabled()) {
  49. return SChooseFromMenu( choose_menu_P(_T(MSG_CHOOSE_FILAMENT), _T(MSG_FILAMENT)), true );
  50. } else {
  51. return SChooseFromMenu( choose_menu_P(_T(MSG_CHOOSE_EXTRUDER), _T(MSG_EXTRUDER)), false );
  52. }
  53. }
  54. void TCodes(char *const strchr_pointer, uint8_t codeValue) {
  55. uint8_t index;
  56. for (index = 1; strchr_pointer[index] == ' ' || strchr_pointer[index] == '\t'; index++)
  57. ;
  58. strchr_pointer[index] = tolower(strchr_pointer[index]);
  59. if (IsInvalidTCode(strchr_pointer, index))
  60. TCodeInvalid();
  61. else if (strchr_pointer[index] == 'x')
  62. TCodeX();
  63. else if (strchr_pointer[index] == 'c')
  64. TCodeC();
  65. else {
  66. SChooseFromMenu selectedSlot;
  67. if (strchr_pointer[index] == '?')
  68. selectedSlot = TCodeChooseFromMenu();
  69. else {
  70. selectedSlot.slot = codeValue;
  71. if (MMU2::mmu2.Enabled() && lcd_autoDepleteEnabled()) {
  72. // @@TODO selectedSlot.slot = ad_getAlternative(selectedSlot);
  73. }
  74. }
  75. st_synchronize();
  76. if (MMU2::mmu2.Enabled()) {
  77. if ((selectedSlot.slot == MMU2::mmu2.get_current_tool()) /*&& mmu_fil_loaded @@TODO*/){
  78. // don't execute the same T-code twice in a row
  79. puts_P(duplicate_Tcode_ignored);
  80. } else {
  81. #if defined(MMU_HAS_CUTTER) && defined(MMU_ALWAYS_CUT)
  82. if (EEPROM_MMU_CUTTER_ENABLED_always == eeprom_read_byte((uint8_t *)EEPROM_MMU_CUTTER_ENABLED)) {
  83. mmu_command(MmuCmd::K0 + selectedSlot);
  84. manage_response(true, true, MMU_UNLOAD_MOVE);
  85. }
  86. #endif // defined(MMU_HAS_CUTTER) && defined(MMU_ALWAYS_CUT)
  87. MMU2::mmu2.tool_change(selectedSlot.slot);
  88. // @@TODO mmu_continue_loading(usb_timer.running() || (lcd_commands_type == LcdCommands::Layer1Cal));
  89. if (selectedSlot.loadToNozzle){ // for single material usage with mmu
  90. MMU2::mmu2.load_filament_to_nozzle(selectedSlot.slot);
  91. }
  92. }
  93. } else {
  94. if (selectedSlot.slot >= EXTRUDERS) {
  95. SERIAL_ECHO_START;
  96. SERIAL_ECHO('T');
  97. SERIAL_ECHOLN(selectedSlot.slot + '0');
  98. SERIAL_ECHOLNRPGM(_n("Invalid extruder")); ////MSG_INVALID_EXTRUDER
  99. } else {
  100. // @@TODO if (code_seen('F')) {
  101. // next_feedrate = code_value();
  102. // if (next_feedrate > 0.0) {
  103. // feedrate = next_feedrate;
  104. // }
  105. // }
  106. SERIAL_ECHO_START;
  107. SERIAL_ECHORPGM(_n("Active Extruder: ")); ////MSG_ACTIVE_EXTRUDER
  108. SERIAL_ECHOLN(active_extruder + '0'); // this is not changed in our FW at all, can be optimized away
  109. }
  110. }
  111. }
  112. }