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.

ui_api.cpp 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**************
  23. * ui_api.cpp *
  24. **************/
  25. /****************************************************************************
  26. * Written By Marcio Teixeira 2018 - Aleph Objects, Inc. *
  27. * *
  28. * This program is free software: you can redistribute it and/or modify *
  29. * it under the terms of the GNU General Public License as published by *
  30. * the Free Software Foundation, either version 3 of the License, or *
  31. * (at your option) any later version. *
  32. * *
  33. * This program is distributed in the hope that it will be useful, *
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  36. * GNU General Public License for more details. *
  37. * *
  38. * To view a copy of the GNU General Public License, go to the following *
  39. * location: <http://www.gnu.org/licenses/>. *
  40. ****************************************************************************/
  41. #include "../../inc/MarlinConfigPre.h"
  42. #if ENABLED(EXTENSIBLE_UI)
  43. #include "../ultralcd.h"
  44. #include "../../gcode/queue.h"
  45. #include "../../module/motion.h"
  46. #include "../../module/planner.h"
  47. #include "../../module/probe.h"
  48. #include "../../module/temperature.h"
  49. #include "../../module/printcounter.h"
  50. #include "../../libs/duration_t.h"
  51. #include "../../HAL/shared/Delay.h"
  52. #if ENABLED(PRINTCOUNTER)
  53. #include "../../core/utility.h"
  54. #endif
  55. #if DO_SWITCH_EXTRUDER || EITHER(SWITCHING_NOZZLE, PARKING_EXTRUDER)
  56. #include "../../module/tool_change.h"
  57. #endif
  58. #if ENABLED(EMERGENCY_PARSER)
  59. #include "../../feature/emergency_parser.h"
  60. #endif
  61. #if ENABLED(SDSUPPORT)
  62. #include "../../sd/cardreader.h"
  63. #define IFSD(A,B) (A)
  64. #else
  65. #define IFSD(A,B) (B)
  66. #endif
  67. #if HAS_TRINAMIC
  68. #include "../../feature/tmc_util.h"
  69. #include "../../module/stepper_indirection.h"
  70. #endif
  71. #include "ui_api.h"
  72. #if ENABLED(BACKLASH_GCODE)
  73. extern float backlash_distance_mm[XYZ], backlash_correction;
  74. #ifdef BACKLASH_SMOOTHING_MM
  75. extern float backlash_smoothing_mm;
  76. #endif
  77. #endif
  78. #if HAS_LEVELING
  79. #include "../../feature/bedlevel.h"
  80. #endif
  81. #if HAS_FILAMENT_SENSOR
  82. #include "../../feature/runout.h"
  83. #endif
  84. inline float clamp(const float value, const float minimum, const float maximum) {
  85. return MAX(MIN(value, maximum), minimum);
  86. }
  87. static struct {
  88. uint8_t printer_killed : 1;
  89. uint8_t manual_motion : 1;
  90. } flags;
  91. namespace ExtUI {
  92. #ifdef __SAM3X8E__
  93. /**
  94. * Implement a special millis() to allow time measurement
  95. * within an ISR (such as when the printer is killed).
  96. *
  97. * To keep proper time, must be called at least every 1s.
  98. */
  99. uint32_t safe_millis() {
  100. // Not killed? Just call millis()
  101. if (!flags.printer_killed) return millis();
  102. static uint32_t currTimeHI = 0; /* Current time */
  103. // Machine was killed, reinit SysTick so we are able to compute time without ISRs
  104. if (currTimeHI == 0) {
  105. // Get the last time the Arduino time computed (from CMSIS) and convert it to SysTick
  106. currTimeHI = (uint32_t)((GetTickCount() * (uint64_t)(F_CPU / 8000)) >> 24);
  107. // Reinit the SysTick timer to maximize its period
  108. SysTick->LOAD = SysTick_LOAD_RELOAD_Msk; // get the full range for the systick timer
  109. SysTick->VAL = 0; // Load the SysTick Counter Value
  110. SysTick->CTRL = // MCLK/8 as source
  111. // No interrupts
  112. SysTick_CTRL_ENABLE_Msk; // Enable SysTick Timer
  113. }
  114. // Check if there was a timer overflow from the last read
  115. if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) {
  116. // There was. This means (SysTick_LOAD_RELOAD_Msk * 1000 * 8)/F_CPU ms has elapsed
  117. currTimeHI++;
  118. }
  119. // Calculate current time in milliseconds
  120. uint32_t currTimeLO = SysTick_LOAD_RELOAD_Msk - SysTick->VAL; // (in MCLK/8)
  121. uint64_t currTime = ((uint64_t)currTimeLO) | (((uint64_t)currTimeHI) << 24);
  122. // The ms count is
  123. return (uint32_t)(currTime / (F_CPU / 8000));
  124. }
  125. #endif // __SAM3X8E__
  126. void delay_us(unsigned long us) {
  127. DELAY_US(us);
  128. }
  129. void delay_ms(unsigned long ms) {
  130. if (flags.printer_killed)
  131. DELAY_US(ms * 1000);
  132. else
  133. safe_delay(ms);
  134. }
  135. void yield() {
  136. if (!flags.printer_killed)
  137. thermalManager.manage_heater();
  138. }
  139. float getActualTemp_celsius(const heater_t heater) {
  140. return heater == BED ?
  141. #if HAS_HEATED_BED
  142. thermalManager.degBed()
  143. #else
  144. 0
  145. #endif
  146. : thermalManager.degHotend(heater - H0);
  147. }
  148. float getActualTemp_celsius(const extruder_t extruder) {
  149. return thermalManager.degHotend(extruder - E0);
  150. }
  151. float getTargetTemp_celsius(const heater_t heater) {
  152. return heater == BED ?
  153. #if HAS_HEATED_BED
  154. thermalManager.degTargetBed()
  155. #else
  156. 0
  157. #endif
  158. : thermalManager.degTargetHotend(heater - H0);
  159. }
  160. float getTargetTemp_celsius(const extruder_t extruder) {
  161. return thermalManager.degTargetHotend(extruder - E0);
  162. }
  163. float getTargetFan_percent(const fan_t fan) {
  164. return thermalManager.fanPercent(thermalManager.fan_speed[fan - FAN0]);
  165. }
  166. float getActualFan_percent(const fan_t fan) {
  167. return thermalManager.fanPercent((thermalManager.fan_speed[fan - FAN0] * uint16_t(thermalManager.fan_speed_scaler[fan - FAN0])) >> 7);
  168. }
  169. float getAxisPosition_mm(const axis_t axis) {
  170. return flags.manual_motion ? destination[axis] : current_position[axis];
  171. }
  172. float getAxisPosition_mm(const extruder_t extruder) {
  173. return flags.manual_motion ? destination[E_AXIS] : current_position[E_AXIS];
  174. }
  175. void setAxisPosition_mm(const float position, const axis_t axis) {
  176. // Start with no limits to movement
  177. float min = current_position[axis] - 1000,
  178. max = current_position[axis] + 1000;
  179. // Limit to software endstops, if enabled
  180. #if HAS_SOFTWARE_ENDSTOPS
  181. if (soft_endstops_enabled) switch (axis) {
  182. case X_AXIS:
  183. #if ENABLED(MIN_SOFTWARE_ENDSTOP_X)
  184. min = soft_endstop[X_AXIS].min;
  185. #endif
  186. #if ENABLED(MAX_SOFTWARE_ENDSTOP_X)
  187. max = soft_endstop[X_AXIS].max;
  188. #endif
  189. break;
  190. case Y_AXIS:
  191. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
  192. min = soft_endstop[Y_AXIS].min;
  193. #endif
  194. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
  195. max = soft_endstop[Y_AXIS].max;
  196. #endif
  197. break;
  198. case Z_AXIS:
  199. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
  200. min = soft_endstop[Z_AXIS].min;
  201. #endif
  202. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
  203. max = soft_endstop[Z_AXIS].max;
  204. #endif
  205. default: break;
  206. }
  207. #endif // HAS_SOFTWARE_ENDSTOPS
  208. // Delta limits XY based on the current offset from center
  209. // This assumes the center is 0,0
  210. #if ENABLED(DELTA)
  211. if (axis != Z_AXIS) {
  212. max = SQRT(sq((float)(DELTA_PRINTABLE_RADIUS)) - sq(current_position[Y_AXIS - axis])); // (Y_AXIS - axis) == the other axis
  213. min = -max;
  214. }
  215. #endif
  216. if (!flags.manual_motion)
  217. set_destination_from_current();
  218. destination[axis] = clamp(position, min, max);
  219. flags.manual_motion = true;
  220. }
  221. void setAxisPosition_mm(const float position, const extruder_t extruder) {
  222. setActiveTool(extruder, true);
  223. if (!flags.manual_motion)
  224. set_destination_from_current();
  225. destination[E_AXIS] = position;
  226. flags.manual_motion = true;
  227. }
  228. void _processManualMoveToDestination() {
  229. // Lower max_response_lag makes controls more responsive, but makes CPU work harder
  230. constexpr float max_response_lag = 0.1; // seconds
  231. constexpr uint8_t segments_to_buffer = 4; // keep planner filled with this many segments
  232. if (flags.manual_motion && planner.movesplanned() < segments_to_buffer) {
  233. float saved_destination[XYZ];
  234. COPY(saved_destination, destination);
  235. // Compute direction vector from current_position towards destination.
  236. destination[X_AXIS] -= current_position[X_AXIS];
  237. destination[Y_AXIS] -= current_position[Y_AXIS];
  238. destination[Z_AXIS] -= current_position[Z_AXIS];
  239. const float inv_length = RSQRT(sq(destination[X_AXIS]) + sq(destination[Y_AXIS]) + sq(destination[Z_AXIS]));
  240. // Find move segment length so that all segments can execute in less time than max_response_lag
  241. const float scale = inv_length * feedrate_mm_s * max_response_lag / segments_to_buffer;
  242. if (scale < 1) {
  243. // Move a small bit towards the destination.
  244. destination[X_AXIS] = scale * destination[X_AXIS] + current_position[X_AXIS];
  245. destination[Y_AXIS] = scale * destination[Y_AXIS] + current_position[Y_AXIS];
  246. destination[Z_AXIS] = scale * destination[Z_AXIS] + current_position[Z_AXIS];
  247. prepare_move_to_destination();
  248. COPY(destination, saved_destination);
  249. }
  250. else {
  251. // We are close enough to finish off the move.
  252. COPY(destination, saved_destination);
  253. prepare_move_to_destination();
  254. flags.manual_motion = false;
  255. }
  256. }
  257. }
  258. void setActiveTool(const extruder_t extruder, bool no_move) {
  259. #if EXTRUDERS > 1
  260. const uint8_t e = extruder - E0;
  261. #if DO_SWITCH_EXTRUDER || EITHER(SWITCHING_NOZZLE, PARKING_EXTRUDER)
  262. if (e != active_extruder)
  263. tool_change(e, 0, no_move);
  264. #endif
  265. active_extruder = e;
  266. #endif
  267. }
  268. extruder_t getActiveTool() {
  269. switch (active_extruder) {
  270. case 5: return E5;
  271. case 4: return E4;
  272. case 3: return E3;
  273. case 2: return E2;
  274. case 1: return E1;
  275. default: return E0;
  276. }
  277. }
  278. bool isMoving() { return planner.has_blocks_queued(); }
  279. bool canMove(const axis_t axis) {
  280. switch (axis) {
  281. #if IS_KINEMATIC || ENABLED(NO_MOTION_BEFORE_HOMING)
  282. case X: return TEST(axis_homed, X_AXIS);
  283. case Y: return TEST(axis_homed, Y_AXIS);
  284. case Z: return TEST(axis_homed, Z_AXIS);
  285. #else
  286. case X: case Y: case Z: return true;
  287. #endif
  288. default: return false;
  289. }
  290. }
  291. bool canMove(const extruder_t extruder) {
  292. return !thermalManager.tooColdToExtrude(extruder - E0);
  293. }
  294. #if HAS_SOFTWARE_ENDSTOPS
  295. bool getSoftEndstopState() {
  296. return soft_endstops_enabled;
  297. }
  298. void setSoftEndstopState(const bool value) {
  299. soft_endstops_enabled = value;
  300. }
  301. #endif
  302. #if HAS_TRINAMIC
  303. float getAxisCurrent_mA(const axis_t axis) {
  304. switch (axis) {
  305. #if AXIS_IS_TMC(X)
  306. case X: return stepperX.getMilliamps();
  307. #endif
  308. #if AXIS_IS_TMC(Y)
  309. case Y: return stepperY.getMilliamps();
  310. #endif
  311. #if AXIS_IS_TMC(Z)
  312. case Z: return stepperZ.getMilliamps();
  313. #endif
  314. default: return NAN;
  315. };
  316. }
  317. float getAxisCurrent_mA(const extruder_t extruder) {
  318. switch (extruder) {
  319. #if AXIS_IS_TMC(E0)
  320. case E0: return stepperE0.getMilliamps();
  321. #endif
  322. #if AXIS_IS_TMC(E1)
  323. case E1: return stepperE1.getMilliamps();
  324. #endif
  325. #if AXIS_IS_TMC(E2)
  326. case E2: return stepperE2.getMilliamps();
  327. #endif
  328. #if AXIS_IS_TMC(E3)
  329. case E3: return stepperE3.getMilliamps();
  330. #endif
  331. #if AXIS_IS_TMC(E4)
  332. case E4: return stepperE4.getMilliamps();
  333. #endif
  334. #if AXIS_IS_TMC(E5)
  335. case E5: return stepperE5.getMilliamps();
  336. #endif
  337. default: return NAN;
  338. };
  339. }
  340. void setAxisCurrent_mA(const float mA, const axis_t axis) {
  341. switch (axis) {
  342. #if AXIS_IS_TMC(X)
  343. case X: stepperX.rms_current(clamp(mA, 500, 1500)); break;
  344. #endif
  345. #if AXIS_IS_TMC(Y)
  346. case Y: stepperY.rms_current(clamp(mA, 500, 1500)); break;
  347. #endif
  348. #if AXIS_IS_TMC(Z)
  349. case Z: stepperZ.rms_current(clamp(mA, 500, 1500)); break;
  350. #endif
  351. };
  352. }
  353. void setAxisCurrent_mA(const float mA, const extruder_t extruder) {
  354. switch (extruder) {
  355. #if AXIS_IS_TMC(E0)
  356. case E0: stepperE0.rms_current(clamp(mA, 500, 1500)); break;
  357. #endif
  358. #if AXIS_IS_TMC(E1)
  359. case E1: stepperE1.rms_current(clamp(mA, 500, 1500)); break;
  360. #endif
  361. #if AXIS_IS_TMC(E2)
  362. case E2: stepperE2.rms_current(clamp(mA, 500, 1500)); break;
  363. #endif
  364. #if AXIS_IS_TMC(E3)
  365. case E3: stepperE3.rms_current(clamp(mA, 500, 1500)); break;
  366. #endif
  367. #if AXIS_IS_TMC(E4)
  368. case E4: stepperE4.rms_current(clamp(mA, 500, 1500)); break;
  369. #endif
  370. #if AXIS_IS_TMC(E5)
  371. case E5: stepperE5.rms_current(clamp(mA, 500, 1500)); break;
  372. #endif
  373. };
  374. }
  375. int getTMCBumpSensitivity(const axis_t axis) {
  376. switch (axis) {
  377. #if X_SENSORLESS && AXIS_HAS_STALLGUARD(X)
  378. case X: return stepperX.sgt();
  379. #endif
  380. #if Y_SENSORLESS && AXIS_HAS_STALLGUARD(Y)
  381. case Y: return stepperY.sgt();
  382. #endif
  383. #if Z_SENSORLESS && AXIS_HAS_STALLGUARD(Z)
  384. case Z: return stepperZ.sgt();
  385. #endif
  386. }
  387. }
  388. void setTMCBumpSensitivity(const float value, const axis_t axis) {
  389. switch (axis) {
  390. #if X_SENSORLESS && AXIS_HAS_STALLGUARD(X)
  391. case X: stepperX.sgt(clamp(value, -64, 63)); break;
  392. #endif
  393. #if Y_SENSORLESS && AXIS_HAS_STALLGUARD(Y)
  394. case Y: stepperY.sgt(clamp(value, -64, 63)); break;
  395. #endif
  396. #if Z_SENSORLESS && AXIS_HAS_STALLGUARD(Z)
  397. case Z: stepperZ.sgt(clamp(value, -64, 63)); break;
  398. #endif
  399. }
  400. }
  401. #endif
  402. float getAxisSteps_per_mm(const axis_t axis) {
  403. return planner.settings.axis_steps_per_mm[axis];
  404. }
  405. float getAxisSteps_per_mm(const extruder_t extruder) {
  406. return planner.settings.axis_steps_per_mm[E_AXIS_N(extruder - E0)];
  407. }
  408. void setAxisSteps_per_mm(const float value, const axis_t axis) {
  409. planner.settings.axis_steps_per_mm[axis] = value;
  410. }
  411. void setAxisSteps_per_mm(const float value, const extruder_t extruder) {
  412. planner.settings.axis_steps_per_mm[E_AXIS_N(axis - E0)] = value;
  413. }
  414. float getAxisMaxFeedrate_mm_s(const axis_t axis) {
  415. return planner.settings.max_feedrate_mm_s[axis];
  416. }
  417. float getAxisMaxFeedrate_mm_s(const extruder_t extruder) {
  418. return planner.settings.max_feedrate_mm_s[E_AXIS_N(axis - E0)];
  419. }
  420. void setAxisMaxFeedrate_mm_s(const float value, const axis_t axis) {
  421. planner.settings.max_feedrate_mm_s[axis] = value;
  422. }
  423. void setAxisMaxFeedrate_mm_s(const float value, const extruder_t extruder) {
  424. planner.settings.max_feedrate_mm_s[E_AXIS_N(axis - E0)] = value;
  425. }
  426. float getAxisMaxAcceleration_mm_s2(const axis_t axis) {
  427. return planner.settings.max_acceleration_mm_per_s2[axis];
  428. }
  429. float getAxisMaxAcceleration_mm_s2(const extruder_t extruder) {
  430. return planner.settings.max_acceleration_mm_per_s2[E_AXIS_N(extruder - E0)];
  431. }
  432. void setAxisMaxAcceleration_mm_s2(const float value, const axis_t axis) {
  433. planner.settings.max_acceleration_mm_per_s2[axis] = value;
  434. }
  435. void setAxisMaxAcceleration_mm_s2(const float value, const extruder_t extruder) {
  436. planner.settings.max_acceleration_mm_per_s2[E_AXIS_N(extruder - E0)] = value;
  437. }
  438. #if HAS_FILAMENT_SENSOR
  439. bool getFilamentRunoutEnabled() { return runout.enabled; }
  440. void setFilamentRunoutEnabled(const bool value) { runout.enabled = value; }
  441. #if FILAMENT_RUNOUT_DISTANCE_MM > 0
  442. float getFilamentRunoutDistance_mm() {
  443. return RunoutResponseDelayed::runout_distance_mm;
  444. }
  445. void setFilamentRunoutDistance_mm(const float value) {
  446. RunoutResponseDelayed::runout_distance_mm = clamp(value, 0, 999);
  447. }
  448. #endif
  449. #endif
  450. #if ENABLED(LIN_ADVANCE)
  451. float getLinearAdvance_mm_mm_s(const extruder_t extruder) {
  452. return (extruder < EXTRUDERS) ? planner.extruder_advance_K[extruder - E0] : 0;
  453. }
  454. void setLinearAdvance_mm_mm_s(const float value, const extruder_t extruder) {
  455. if (extruder < EXTRUDERS)
  456. planner.extruder_advance_K[extruder - E0] = clamp(value, 0, 999);
  457. }
  458. #endif
  459. #if ENABLED(JUNCTION_DEVIATION)
  460. float getJunctionDeviation_mm() {
  461. return planner.junction_deviation_mm;
  462. }
  463. void setJunctionDeviation_mm(const float value) {
  464. planner.junction_deviation_mm = clamp(value, 0.01, 0.3);
  465. #if ENABLED(LIN_ADVANCE)
  466. planner.recalculate_max_e_jerk();
  467. #endif
  468. }
  469. #else
  470. float getAxisMaxJerk_mm_s(const axis_t axis) {
  471. return planner.max_jerk[axis];
  472. }
  473. float getAxisMaxJerk_mm_s(const extruder_t extruder) {
  474. return planner.max_jerk[E_AXIS];
  475. }
  476. void setAxisMaxJerk_mm_s(const float value, const axis_t axis) {
  477. planner.max_jerk[axis] = value;
  478. }
  479. void setAxisMaxJerk_mm_s(const float value, const extruder_t extruder) {
  480. planner.max_jerk[E_AXIS] = value;
  481. }
  482. #endif
  483. float getFeedrate_mm_s() { return feedrate_mm_s; }
  484. float getMinFeedrate_mm_s() { return planner.settings.min_feedrate_mm_s; }
  485. float getMinTravelFeedrate_mm_s() { return planner.settings.min_travel_feedrate_mm_s; }
  486. float getPrintingAcceleration_mm_s2() { return planner.settings.acceleration; }
  487. float getRetractAcceleration_mm_s2() { return planner.settings.retract_acceleration; }
  488. float getTravelAcceleration_mm_s2() { return planner.settings.travel_acceleration; }
  489. void setFeedrate_mm_s(const float fr) { feedrate_mm_s = fr; }
  490. void setMinFeedrate_mm_s(const float fr) { planner.settings.min_feedrate_mm_s = fr; }
  491. void setMinTravelFeedrate_mm_s(const float fr) { planner.settings.min_travel_feedrate_mm_s = fr; }
  492. void setPrintingAcceleration_mm_s2(const float acc) { planner.settings.acceleration = acc; }
  493. void setRetractAcceleration_mm_s2(const float acc) { planner.settings.retract_acceleration = acc; }
  494. void setTravelAcceleration_mm_s2(const float acc) { planner.settings.travel_acceleration = acc; }
  495. #if ENABLED(BABYSTEPPING)
  496. bool babystepAxis_steps(const int16_t steps, const axis_t axis) {
  497. switch (axis) {
  498. #if ENABLED(BABYSTEP_XY)
  499. case X: thermalManager.babystep_axis(X_AXIS, steps); break;
  500. case Y: thermalManager.babystep_axis(Y_AXIS, steps); break;
  501. #endif
  502. case Z: thermalManager.babystep_axis(Z_AXIS, steps); break;
  503. default: return false;
  504. };
  505. return true;
  506. }
  507. /**
  508. * This function adjusts an axis during a print.
  509. *
  510. * When linked_nozzles is false, each nozzle in a multi-nozzle
  511. * printer can be babystepped independently of the others. This
  512. * lets the user to fine tune the Z-offset and Nozzle Offsets
  513. * while observing the first layer of a print, regardless of
  514. * what nozzle is printing.
  515. */
  516. void smartAdjustAxis_steps(const int16_t steps, const axis_t axis, bool linked_nozzles) {
  517. const float mm = steps * planner.steps_to_mm[axis];
  518. if (!babystepAxis_steps(steps, axis)) return;
  519. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  520. // Make it so babystepping in Z adjusts the Z probe offset.
  521. if (axis == Z
  522. #if EXTRUDERS > 1
  523. && (linked_nozzles || active_extruder == 0)
  524. #endif
  525. ) zprobe_zoffset += mm;
  526. #endif
  527. #if EXTRUDERS > 1
  528. /**
  529. * When linked_nozzles is false, as an axis is babystepped
  530. * adjust the hotend offsets so that the other nozzles are
  531. * unaffected by the babystepping of the active nozzle.
  532. */
  533. if (!linked_nozzles) {
  534. HOTEND_LOOP()
  535. if (e != active_extruder)
  536. hotend_offset[axis][e] += mm;
  537. normalizeNozzleOffset(X);
  538. normalizeNozzleOffset(Y);
  539. normalizeNozzleOffset(Z);
  540. }
  541. #else
  542. UNUSED(linked_nozzles);
  543. #endif
  544. }
  545. /**
  546. * Converts a mm displacement to a number of whole number of
  547. * steps that is at least mm long.
  548. */
  549. int16_t mmToWholeSteps(const float mm, const axis_t axis) {
  550. const float steps = mm / planner.steps_to_mm[axis];
  551. return steps > 0 ? ceil(steps) : floor(steps);
  552. }
  553. #endif
  554. #if HAS_BED_PROBE
  555. float getZOffset_mm() {
  556. return zprobe_zoffset;
  557. }
  558. void setZOffset_mm(const float value) {
  559. if (WITHIN(value, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX)) {
  560. zprobe_zoffset = value;
  561. }
  562. }
  563. #endif // HAS_BED_PROBE
  564. #if HAS_HOTEND_OFFSET
  565. float getNozzleOffset_mm(const axis_t axis, const extruder_t extruder) {
  566. if (extruder - E0 >= HOTENDS) return 0;
  567. return hotend_offset[axis][extruder - E0];
  568. }
  569. void setNozzleOffset_mm(const float value, const axis_t axis, const extruder_t extruder) {
  570. if (extruder - E0 >= HOTENDS) return;
  571. hotend_offset[axis][extruder - E0] = value;
  572. }
  573. /**
  574. * The UI should call this if needs to guarantee the first
  575. * nozzle offset is zero (such as when it doesn't allow the
  576. * user to edit the offset the first nozzle).
  577. */
  578. void normalizeNozzleOffset(const axis_t axis) {
  579. const float offs = hotend_offset[axis][0];
  580. HOTEND_LOOP() hotend_offset[axis][e] -= offs;
  581. }
  582. #endif // HAS_HOTEND_OFFSET
  583. #if ENABLED(BACKLASH_GCODE)
  584. float getAxisBacklash_mm(const axis_t axis) { return backlash_distance_mm[axis]; }
  585. void setAxisBacklash_mm(const float value, const axis_t axis)
  586. { backlash_distance_mm[axis] = clamp(value,0,5); }
  587. float getBacklashCorrection_percent() { return backlash_correction * 100; }
  588. void setBacklashCorrection_percent(const float value) { backlash_correction = clamp(value, 0, 100) / 100.0f; }
  589. #ifdef BACKLASH_SMOOTHING_MM
  590. float getBacklashSmoothing_mm() { return backlash_smoothing_mm; }
  591. void setBacklashSmoothing_mm(const float value) { backlash_smoothing_mm = clamp(value, 0, 999); }
  592. #endif
  593. #endif
  594. uint8_t getProgress_percent() {
  595. return ui.get_progress();
  596. }
  597. uint32_t getProgress_seconds_elapsed() {
  598. const duration_t elapsed = print_job_timer.duration();
  599. return elapsed.value;
  600. }
  601. #if HAS_LEVELING
  602. bool getLevelingActive() { return planner.leveling_active; }
  603. void setLevelingActive(const bool state) { set_bed_leveling_enabled(state); }
  604. #if HAS_MESH
  605. bool getMeshValid() { return leveling_is_valid(); }
  606. bed_mesh_t getMeshArray() { return Z_VALUES_ARR; }
  607. void setMeshPoint(const uint8_t xpos, const uint8_t ypos, const float zoff) {
  608. if (WITHIN(xpos, 0, GRID_MAX_POINTS_X) && WITHIN(ypos, 0, GRID_MAX_POINTS_Y)) {
  609. Z_VALUES(xpos, ypos) = zoff;
  610. #if ENABLED(ABL_BILINEAR_SUBDIVISION)
  611. bed_level_virt_interpolate();
  612. #endif
  613. }
  614. }
  615. #endif
  616. #endif
  617. #if ENABLED(HOST_PROMPT_SUPPORT)
  618. void setHostResponse(const uint8_t response) { host_response_handler(response); }
  619. #endif
  620. #if ENABLED(PRINTCOUNTER)
  621. char* getTotalPrints_str(char buffer[21]) { strcpy(buffer,i16tostr3left(print_job_timer.getStats().totalPrints)); return buffer; }
  622. char* getFinishedPrints_str(char buffer[21]) { strcpy(buffer,i16tostr3left(print_job_timer.getStats().finishedPrints)); return buffer; }
  623. char* getTotalPrintTime_str(char buffer[21]) { duration_t(print_job_timer.getStats().printTime).toString(buffer); return buffer; }
  624. char* getLongestPrint_str(char buffer[21]) { duration_t(print_job_timer.getStats().printTime).toString(buffer); return buffer; }
  625. char* getFilamentUsed_str(char buffer[21]) {
  626. printStatistics stats = print_job_timer.getStats();
  627. sprintf_P(buffer, PSTR("%ld.%im"), long(stats.filamentUsed / 1000), int16_t(stats.filamentUsed / 100) % 10);
  628. return buffer;
  629. }
  630. #endif
  631. float getFeedrate_percent() { return feedrate_percentage; }
  632. void enqueueCommands_P(PGM_P const gcode) {
  633. enqueue_and_echo_commands_P(gcode);
  634. }
  635. bool isAxisPositionKnown(const axis_t axis) {
  636. return TEST(axis_known_position, axis);
  637. }
  638. PGM_P getFirmwareName_str() {
  639. static const char firmware_name[] PROGMEM = "Marlin " SHORT_BUILD_VERSION;
  640. return firmware_name;
  641. }
  642. void setTargetTemp_celsius(float value, const heater_t heater) {
  643. constexpr int16_t heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP);
  644. const int16_t e = heater - H0;
  645. #if HAS_HEATED_BED
  646. if (heater == BED)
  647. thermalManager.setTargetBed(clamp(value, 0, BED_MAXTEMP - 10));
  648. else
  649. #endif
  650. thermalManager.setTargetHotend(clamp(value, 0, heater_maxtemp[e] - 15), e);
  651. }
  652. void setTargetTemp_celsius(float value, const extruder_t extruder) {
  653. constexpr int16_t heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP);
  654. const int16_t e = extruder - E0;
  655. thermalManager.setTargetHotend(clamp(value, 0, heater_maxtemp[e] - 15), e);
  656. }
  657. void setTargetFan_percent(const float value, const fan_t fan) {
  658. if (fan < FAN_COUNT)
  659. thermalManager.set_fan_speed(fan - FAN0, map(clamp(value, 0, 100), 0, 100, 0, 255));
  660. }
  661. void setFeedrate_percent(const float value) {
  662. feedrate_percentage = clamp(value, 10, 500);
  663. }
  664. void setUserConfirmed(void) {
  665. #if HAS_RESUME_CONTINUE
  666. wait_for_user = false;
  667. #endif
  668. }
  669. void printFile(const char *filename) {
  670. IFSD(card.openAndPrintFile(filename), NOOP);
  671. }
  672. bool isPrintingFromMediaPaused() {
  673. return IFSD(isPrintingFromMedia() && !IS_SD_PRINTING(), false);
  674. }
  675. bool isPrintingFromMedia() {
  676. return IFSD(card.isFileOpen(), false);
  677. }
  678. bool isPrinting() {
  679. return (planner.movesplanned() || isPrintingFromMedia() || IFSD(IS_SD_PRINTING(), false));
  680. }
  681. bool isMediaInserted() {
  682. return IFSD(IS_SD_INSERTED() && card.isDetected(), false);
  683. }
  684. void pausePrint() {
  685. #if ENABLED(SDSUPPORT)
  686. card.pauseSDPrint();
  687. print_job_timer.pause();
  688. #if ENABLED(PARK_HEAD_ON_PAUSE)
  689. enqueue_and_echo_commands_P(PSTR("M125"));
  690. #endif
  691. ui.set_status_P(PSTR(MSG_PRINT_PAUSED));
  692. #endif
  693. }
  694. void resumePrint() {
  695. #if ENABLED(SDSUPPORT)
  696. ui.set_status_P(PSTR(MSG_FILAMENT_CHANGE_RESUME_1));
  697. #if ENABLED(PARK_HEAD_ON_PAUSE)
  698. wait_for_heatup = wait_for_user = false;
  699. enqueue_and_echo_commands_P(PSTR("M24"));
  700. #else
  701. card.startFileprint();
  702. print_job_timer.start();
  703. #endif
  704. #endif
  705. }
  706. void stopPrint() {
  707. #if ENABLED(SDSUPPORT)
  708. wait_for_heatup = wait_for_user = false;
  709. card.flag.abort_sd_printing = true;
  710. ui.set_status_P(PSTR(MSG_PRINT_ABORTED));
  711. #endif
  712. }
  713. FileList::FileList() { refresh(); }
  714. void FileList::refresh() { num_files = 0xFFFF; }
  715. bool FileList::seek(const uint16_t pos, const bool skip_range_check) {
  716. #if ENABLED(SDSUPPORT)
  717. if (!skip_range_check && (pos + 1) > count()) return false;
  718. const uint16_t nr =
  719. #if ENABLED(SDCARD_RATHERRECENTFIRST) && DISABLED(SDCARD_SORT_ALPHA)
  720. count() - 1 -
  721. #endif
  722. pos;
  723. card.getfilename_sorted(nr);
  724. return card.filename[0] != '\0';
  725. #else
  726. return false;
  727. #endif
  728. }
  729. const char* FileList::filename() {
  730. return IFSD(card.longFilename[0] ? card.longFilename : card.filename, "");
  731. }
  732. const char* FileList::shortFilename() {
  733. return IFSD(card.filename, "");
  734. }
  735. const char* FileList::longFilename() {
  736. return IFSD(card.longFilename, "");
  737. }
  738. bool FileList::isDir() {
  739. return IFSD(card.flag.filenameIsDir, false);
  740. }
  741. uint16_t FileList::count() {
  742. return IFSD((num_files = (num_files == 0xFFFF ? card.get_num_Files() : num_files)), 0);
  743. }
  744. bool FileList::isAtRootDir() {
  745. #if ENABLED(SDSUPPORT)
  746. card.getWorkDirName();
  747. return card.filename[0] == '/';
  748. #else
  749. return true;
  750. #endif
  751. }
  752. void FileList::upDir() {
  753. #if ENABLED(SDSUPPORT)
  754. card.updir();
  755. num_files = 0xFFFF;
  756. #endif
  757. }
  758. void FileList::changeDir(const char * const dirname) {
  759. #if ENABLED(SDSUPPORT)
  760. card.chdir(dirname);
  761. num_files = 0xFFFF;
  762. #endif
  763. }
  764. } // namespace ExtUI
  765. // At the moment, we piggy-back off the ultralcd calls, but this could be cleaned up in the future
  766. void MarlinUI::init() {
  767. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  768. SET_INPUT_PULLUP(SD_DETECT_PIN);
  769. #endif
  770. ExtUI::onStartup();
  771. }
  772. void MarlinUI::update() {
  773. #if ENABLED(SDSUPPORT)
  774. static bool last_sd_status;
  775. const bool sd_status = IS_SD_INSERTED();
  776. if (sd_status != last_sd_status) {
  777. last_sd_status = sd_status;
  778. if (sd_status) {
  779. card.initsd();
  780. if (card.isDetected())
  781. ExtUI::onMediaInserted();
  782. else
  783. ExtUI::onMediaError();
  784. }
  785. else {
  786. const bool ok = card.isDetected();
  787. card.release();
  788. if (ok) ExtUI::onMediaRemoved();
  789. }
  790. }
  791. #endif // SDSUPPORT
  792. ExtUI::_processManualMoveToDestination();
  793. ExtUI::onIdle();
  794. }
  795. void MarlinUI::kill_screen(PGM_P const msg) {
  796. if (!flags.printer_killed) {
  797. flags.printer_killed = true;
  798. ExtUI::onPrinterKilled(msg);
  799. }
  800. }
  801. #endif // EXTENSIBLE_UI