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 27KB

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