My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

powerloss.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * feature/powerloss.h - Resume an SD print after power-loss
  25. */
  26. #include "../sd/cardreader.h"
  27. #include "../gcode/gcode.h"
  28. #include "../inc/MarlinConfig.h"
  29. #if ENABLED(GCODE_REPEAT_MARKERS)
  30. #include "../feature/repeat.h"
  31. #endif
  32. #if ENABLED(MIXING_EXTRUDER)
  33. #include "../feature/mixing.h"
  34. #endif
  35. #if !defined(POWER_LOSS_STATE) && PIN_EXISTS(POWER_LOSS)
  36. #define POWER_LOSS_STATE HIGH
  37. #endif
  38. //#define DEBUG_POWER_LOSS_RECOVERY
  39. //#define SAVE_EACH_CMD_MODE
  40. //#define SAVE_INFO_INTERVAL_MS 0
  41. typedef struct {
  42. uint8_t valid_head;
  43. // Machine state
  44. xyze_pos_t current_position;
  45. uint16_t feedrate;
  46. float zraise;
  47. // Repeat information
  48. TERN_(GCODE_REPEAT_MARKERS, Repeat stored_repeat);
  49. TERN_(HAS_HOME_OFFSET, xyz_pos_t home_offset);
  50. TERN_(HAS_POSITION_SHIFT, xyz_pos_t position_shift);
  51. TERN_(HAS_MULTI_EXTRUDER, uint8_t active_extruder);
  52. #if DISABLED(NO_VOLUMETRICS)
  53. bool volumetric_enabled;
  54. float filament_size[EXTRUDERS];
  55. #endif
  56. TERN_(HAS_HOTEND, int16_t target_temperature[HOTENDS]);
  57. TERN_(HAS_HEATED_BED, int16_t target_temperature_bed);
  58. TERN_(HAS_FAN, uint8_t fan_speed[FAN_COUNT]);
  59. TERN_(HAS_LEVELING, float fade);
  60. #if ENABLED(FWRETRACT)
  61. float retract[EXTRUDERS], retract_hop;
  62. #endif
  63. // Mixing extruder and gradient
  64. #if ENABLED(MIXING_EXTRUDER)
  65. //uint_fast8_t selected_vtool;
  66. //mixer_comp_t color[NR_MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
  67. TERN_(GRADIENT_MIX, gradient_t gradient);
  68. #endif
  69. // SD Filename and position
  70. char sd_filename[MAXPATHNAMELENGTH];
  71. volatile uint32_t sdpos;
  72. // Job elapsed time
  73. millis_t print_job_elapsed;
  74. // Relative axis modes
  75. uint8_t axis_relative;
  76. // Misc. Marlin flags
  77. struct {
  78. bool dryrun:1; // M111 S8
  79. bool allow_cold_extrusion:1; // M302 P1
  80. TERN_(HAS_LEVELING, bool leveling:1);
  81. } flag;
  82. uint8_t valid_foot;
  83. bool valid() { return valid_head && valid_head == valid_foot; }
  84. } job_recovery_info_t;
  85. class PrintJobRecovery {
  86. public:
  87. static const char filename[5];
  88. static SdFile file;
  89. static job_recovery_info_t info;
  90. static uint8_t queue_index_r; //!< Queue index of the active command
  91. static uint32_t cmd_sdpos, //!< SD position of the next command
  92. sdpos[BUFSIZE]; //!< SD positions of queued commands
  93. #if ENABLED(DWIN_CREALITY_LCD)
  94. static bool dwin_flag;
  95. #endif
  96. static void init();
  97. static void prepare();
  98. static inline void setup() {
  99. #if PIN_EXISTS(POWER_LOSS)
  100. #if ENABLED(POWER_LOSS_PULLUP)
  101. SET_INPUT_PULLUP(POWER_LOSS_PIN);
  102. #elif ENABLED(POWER_LOSS_PULLDOWN)
  103. SET_INPUT_PULLDOWN(POWER_LOSS_PIN);
  104. #else
  105. SET_INPUT(POWER_LOSS_PIN);
  106. #endif
  107. #endif
  108. }
  109. // Track each command's file offsets
  110. static inline uint32_t command_sdpos() { return sdpos[queue_index_r]; }
  111. static inline void commit_sdpos(const uint8_t index_w) { sdpos[index_w] = cmd_sdpos; }
  112. static bool enabled;
  113. static void enable(const bool onoff);
  114. static void changed();
  115. static inline bool exists() { return card.jobRecoverFileExists(); }
  116. static inline void open(const bool read) { card.openJobRecoveryFile(read); }
  117. static inline void close() { file.close(); }
  118. static void check();
  119. static void resume();
  120. static void purge();
  121. static inline void cancel() { purge(); card.autostart_index = 0; }
  122. static void load();
  123. static void save(const bool force=ENABLED(SAVE_EACH_CMD_MODE), const float zraise=0);
  124. #if PIN_EXISTS(POWER_LOSS)
  125. static inline void outage() {
  126. if (enabled && READ(POWER_LOSS_PIN) == POWER_LOSS_STATE)
  127. _outage();
  128. }
  129. #endif
  130. // The referenced file exists
  131. static inline bool interrupted_file_exists() { return card.fileExists(info.sd_filename); }
  132. static inline bool valid() { return info.valid() && interrupted_file_exists(); }
  133. #if ENABLED(DEBUG_POWER_LOSS_RECOVERY)
  134. static void debug(PGM_P const prefix);
  135. #else
  136. static inline void debug(PGM_P const) {}
  137. #endif
  138. private:
  139. static void write();
  140. #if ENABLED(BACKUP_POWER_SUPPLY)
  141. static void retract_and_lift(const float &zraise);
  142. #endif
  143. #if PIN_EXISTS(POWER_LOSS)
  144. friend class GcodeSuite;
  145. static void _outage();
  146. #endif
  147. };
  148. extern PrintJobRecovery recovery;