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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. #include "../../libs/numtostr.h"
  55. #endif
  56. #if EXTRUDERS > 1
  57. #include "../../module/tool_change.h"
  58. #endif
  59. #if ENABLED(EMERGENCY_PARSER)
  60. #include "../../feature/emergency_parser.h"
  61. #endif
  62. #if ENABLED(SDSUPPORT)
  63. #include "../../sd/cardreader.h"
  64. #define IFSD(A,B) (A)
  65. #else
  66. #define IFSD(A,B) (B)
  67. #endif
  68. #if HAS_TRINAMIC
  69. #include "../../feature/tmc_util.h"
  70. #include "../../module/stepper/indirection.h"
  71. #endif
  72. #include "ui_api.h"
  73. #if ENABLED(BACKLASH_GCODE)
  74. #include "../../feature/backlash.h"
  75. #endif
  76. #if HAS_LEVELING
  77. #include "../../feature/bedlevel/bedlevel.h"
  78. #endif
  79. #if HAS_FILAMENT_SENSOR
  80. #include "../../feature/runout.h"
  81. #endif
  82. #if ENABLED(BABYSTEPPING)
  83. #include "../../feature/babystep.h"
  84. #endif
  85. #if ENABLED(HOST_PROMPT_SUPPORT)
  86. #include "../../feature/host_actions.h"
  87. #endif
  88. namespace ExtUI {
  89. static struct {
  90. uint8_t printer_killed : 1;
  91. #if ENABLED(JOYSTICK)
  92. uint8_t jogging : 1;
  93. #endif
  94. } flags;
  95. #ifdef __SAM3X8E__
  96. /**
  97. * Implement a special millis() to allow time measurement
  98. * within an ISR (such as when the printer is killed).
  99. *
  100. * To keep proper time, must be called at least every 1s.
  101. */
  102. uint32_t safe_millis() {
  103. // Not killed? Just call millis()
  104. if (!flags.printer_killed) return millis();
  105. static uint32_t currTimeHI = 0; /* Current time */
  106. // Machine was killed, reinit SysTick so we are able to compute time without ISRs
  107. if (currTimeHI == 0) {
  108. // Get the last time the Arduino time computed (from CMSIS) and convert it to SysTick
  109. currTimeHI = (uint32_t)((GetTickCount() * (uint64_t)(F_CPU / 8000)) >> 24);
  110. // Reinit the SysTick timer to maximize its period
  111. SysTick->LOAD = SysTick_LOAD_RELOAD_Msk; // get the full range for the systick timer
  112. SysTick->VAL = 0; // Load the SysTick Counter Value
  113. SysTick->CTRL = // MCLK/8 as source
  114. // No interrupts
  115. SysTick_CTRL_ENABLE_Msk; // Enable SysTick Timer
  116. }
  117. // Check if there was a timer overflow from the last read
  118. if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) {
  119. // There was. This means (SysTick_LOAD_RELOAD_Msk * 1000 * 8)/F_CPU ms has elapsed
  120. currTimeHI++;
  121. }
  122. // Calculate current time in milliseconds
  123. uint32_t currTimeLO = SysTick_LOAD_RELOAD_Msk - SysTick->VAL; // (in MCLK/8)
  124. uint64_t currTime = ((uint64_t)currTimeLO) | (((uint64_t)currTimeHI) << 24);
  125. // The ms count is
  126. return (uint32_t)(currTime / (F_CPU / 8000));
  127. }
  128. #endif // __SAM3X8E__
  129. void delay_us(unsigned long us) { DELAY_US(us); }
  130. void delay_ms(unsigned long ms) {
  131. if (flags.printer_killed)
  132. DELAY_US(ms * 1000);
  133. else
  134. safe_delay(ms);
  135. }
  136. void yield() {
  137. if (!flags.printer_killed)
  138. thermalManager.manage_heater();
  139. }
  140. void enableHeater(const extruder_t extruder) {
  141. #if HOTENDS && HEATER_IDLE_HANDLER
  142. thermalManager.reset_heater_idle_timer(extruder - E0);
  143. #else
  144. UNUSED(extruder);
  145. #endif
  146. }
  147. void enableHeater(const heater_t heater) {
  148. #if HEATER_IDLE_HANDLER
  149. switch (heater) {
  150. #if HAS_HEATED_BED
  151. case BED:
  152. thermalManager.reset_bed_idle_timer();
  153. return;
  154. #endif
  155. #if HAS_HEATED_CHAMBER
  156. case CHAMBER: return; // Chamber has no idle timer
  157. #endif
  158. default:
  159. #if HOTENDS
  160. thermalManager.reset_heater_idle_timer(heater - H0);
  161. #endif
  162. break;
  163. }
  164. #else
  165. UNUSED(heater);
  166. #endif
  167. }
  168. #if ENABLED(JOYSTICK)
  169. /**
  170. * Jogs in the direction given by the vector (dx, dy, dz).
  171. * The values range from -1 to 1 mapping to the maximum
  172. * feedrate for an axis.
  173. *
  174. * The axis will continue to jog until this function is
  175. * called with all zeros.
  176. */
  177. void jog(const xyz_float_t &dir) {
  178. // The "destination" variable is used as a scratchpad in
  179. // Marlin by GCODE routines, but should remain untouched
  180. // during manual jogging, allowing us to reuse the space
  181. // for our direction vector.
  182. destination = dir;
  183. flags.jogging = !NEAR_ZERO(dir.x) || !NEAR_ZERO(dir.y) || !NEAR_ZERO(dir.z);
  184. }
  185. // Called by the polling routine in "joystick.cpp"
  186. void _joystick_update(xyz_float_t &norm_jog) {
  187. if (flags.jogging) {
  188. #define OUT_OF_RANGE(VALUE) (VALUE < -1.0f || VALUE > 1.0f)
  189. if (OUT_OF_RANGE(destination.x) || OUT_OF_RANGE(destination.y) || OUT_OF_RANGE(destination.z)) {
  190. // If destination on any axis is out of range, it
  191. // probably means the UI forgot to stop jogging and
  192. // ran GCODE that wrote a position to destination.
  193. // To prevent a disaster, stop jogging.
  194. flags.jogging = false;
  195. return;
  196. }
  197. norm_jog = destination;
  198. }
  199. }
  200. #endif
  201. bool isHeaterIdle(const extruder_t extruder) {
  202. return false
  203. #if HOTENDS && HEATER_IDLE_HANDLER
  204. || thermalManager.hotend_idle[extruder - E0].timed_out
  205. #else
  206. ; UNUSED(extruder)
  207. #endif
  208. ;
  209. }
  210. bool isHeaterIdle(const heater_t heater) {
  211. #if HEATER_IDLE_HANDLER
  212. switch (heater) {
  213. #if HAS_HEATED_BED
  214. case BED: return thermalManager.bed_idle.timed_out;
  215. #endif
  216. #if HAS_HEATED_CHAMBER
  217. case CHAMBER: return false; // Chamber has no idle timer
  218. #endif
  219. default:
  220. #if HOTENDS
  221. return thermalManager.hotend_idle[heater - H0].timed_out;
  222. #else
  223. return false;
  224. #endif
  225. }
  226. #else
  227. UNUSED(heater);
  228. return false;
  229. #endif
  230. }
  231. float getActualTemp_celsius(const heater_t heater) {
  232. switch (heater) {
  233. #if HAS_HEATED_BED
  234. case BED: return thermalManager.degBed();
  235. #endif
  236. #if HAS_HEATED_CHAMBER
  237. case CHAMBER: return thermalManager.degChamber();
  238. #endif
  239. default: return thermalManager.degHotend(heater - H0);
  240. }
  241. }
  242. float getActualTemp_celsius(const extruder_t extruder) {
  243. return thermalManager.degHotend(extruder - E0);
  244. }
  245. float getTargetTemp_celsius(const heater_t heater) {
  246. switch (heater) {
  247. #if HAS_HEATED_BED
  248. case BED: return thermalManager.degTargetBed();
  249. #endif
  250. #if HAS_HEATED_CHAMBER
  251. case CHAMBER: return thermalManager.degTargetChamber();
  252. #endif
  253. default: return thermalManager.degTargetHotend(heater - H0);
  254. }
  255. }
  256. float getTargetTemp_celsius(const extruder_t extruder) {
  257. return thermalManager.degTargetHotend(extruder - E0);
  258. }
  259. float getTargetFan_percent(const fan_t fan) {
  260. #if FAN_COUNT > 0
  261. return thermalManager.fanPercent(thermalManager.fan_speed[fan - FAN0]);
  262. #else
  263. UNUSED(fan);
  264. return 0;
  265. #endif
  266. }
  267. float getActualFan_percent(const fan_t fan) {
  268. #if FAN_COUNT > 0
  269. return thermalManager.fanPercent(thermalManager.scaledFanSpeed(fan - FAN0));
  270. #else
  271. UNUSED(fan);
  272. return 0;
  273. #endif
  274. }
  275. float getAxisPosition_mm(const axis_t axis) {
  276. return
  277. #if ENABLED(JOYSTICK)
  278. flags.jogging ? destination[axis] :
  279. #endif
  280. current_position[axis];
  281. }
  282. float getAxisPosition_mm(const extruder_t extruder) {
  283. const extruder_t old_tool = getActiveTool();
  284. setActiveTool(extruder, true);
  285. const float epos = (
  286. #if ENABLED(JOYSTICK)
  287. flags.jogging ? destination.e :
  288. #endif
  289. current_position.e
  290. );
  291. setActiveTool(old_tool, true);
  292. return epos;
  293. }
  294. void setAxisPosition_mm(const float position, const axis_t axis) {
  295. // Start with no limits to movement
  296. float min = current_position[axis] - 1000,
  297. max = current_position[axis] + 1000;
  298. // Limit to software endstops, if enabled
  299. #if HAS_SOFTWARE_ENDSTOPS
  300. if (soft_endstops_enabled) switch (axis) {
  301. case X_AXIS:
  302. #if ENABLED(MIN_SOFTWARE_ENDSTOP_X)
  303. min = soft_endstop.min.x;
  304. #endif
  305. #if ENABLED(MAX_SOFTWARE_ENDSTOP_X)
  306. max = soft_endstop.max.x;
  307. #endif
  308. break;
  309. case Y_AXIS:
  310. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
  311. min = soft_endstop.min.y;
  312. #endif
  313. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
  314. max = soft_endstop.max.y;
  315. #endif
  316. break;
  317. case Z_AXIS:
  318. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
  319. min = soft_endstop.min.z;
  320. #endif
  321. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
  322. max = soft_endstop.max.z;
  323. #endif
  324. default: break;
  325. }
  326. #endif // HAS_SOFTWARE_ENDSTOPS
  327. // Delta limits XY based on the current offset from center
  328. // This assumes the center is 0,0
  329. #if ENABLED(DELTA)
  330. if (axis != Z_AXIS) {
  331. max = SQRT(sq((float)(DELTA_PRINTABLE_RADIUS)) - sq(current_position[Y_AXIS - axis])); // (Y_AXIS - axis) == the other axis
  332. min = -max;
  333. }
  334. #endif
  335. current_position[axis] = constrain(position, min, max);
  336. line_to_current_position(MMM_TO_MMS(manual_feedrate_mm_m[axis]));
  337. }
  338. void setAxisPosition_mm(const float position, const extruder_t extruder) {
  339. setActiveTool(extruder, true);
  340. current_position.e = position;
  341. line_to_current_position(MMM_TO_MMS(manual_feedrate_mm_m.e));
  342. }
  343. void setActiveTool(const extruder_t extruder, bool no_move) {
  344. #if EXTRUDERS > 1
  345. const uint8_t e = extruder - E0;
  346. if (e != active_extruder) tool_change(e, no_move);
  347. active_extruder = e;
  348. #else
  349. UNUSED(extruder);
  350. UNUSED(no_move);
  351. #endif
  352. }
  353. extruder_t getActiveTool() {
  354. switch (active_extruder) {
  355. case 5: return E5;
  356. case 4: return E4;
  357. case 3: return E3;
  358. case 2: return E2;
  359. case 1: return E1;
  360. default: return E0;
  361. }
  362. }
  363. bool isMoving() { return planner.has_blocks_queued(); }
  364. bool canMove(const axis_t axis) {
  365. switch (axis) {
  366. #if IS_KINEMATIC || ENABLED(NO_MOTION_BEFORE_HOMING)
  367. case X: return TEST(axis_homed, X_AXIS);
  368. case Y: return TEST(axis_homed, Y_AXIS);
  369. case Z: return TEST(axis_homed, Z_AXIS);
  370. #else
  371. case X: case Y: case Z: return true;
  372. #endif
  373. default: return false;
  374. }
  375. }
  376. bool canMove(const extruder_t extruder) {
  377. return !thermalManager.tooColdToExtrude(extruder - E0);
  378. }
  379. #if HAS_SOFTWARE_ENDSTOPS
  380. bool getSoftEndstopState() { return soft_endstops_enabled; }
  381. void setSoftEndstopState(const bool value) { soft_endstops_enabled = value; }
  382. #endif
  383. #if HAS_TRINAMIC
  384. float getAxisCurrent_mA(const axis_t axis) {
  385. switch (axis) {
  386. #if AXIS_IS_TMC(X)
  387. case X: return stepperX.getMilliamps();
  388. #endif
  389. #if AXIS_IS_TMC(Y)
  390. case Y: return stepperY.getMilliamps();
  391. #endif
  392. #if AXIS_IS_TMC(Z)
  393. case Z: return stepperZ.getMilliamps();
  394. #endif
  395. default: return NAN;
  396. };
  397. }
  398. float getAxisCurrent_mA(const extruder_t extruder) {
  399. switch (extruder) {
  400. #if AXIS_IS_TMC(E0)
  401. case E0: return stepperE0.getMilliamps();
  402. #endif
  403. #if AXIS_IS_TMC(E1)
  404. case E1: return stepperE1.getMilliamps();
  405. #endif
  406. #if AXIS_IS_TMC(E2)
  407. case E2: return stepperE2.getMilliamps();
  408. #endif
  409. #if AXIS_IS_TMC(E3)
  410. case E3: return stepperE3.getMilliamps();
  411. #endif
  412. #if AXIS_IS_TMC(E4)
  413. case E4: return stepperE4.getMilliamps();
  414. #endif
  415. #if AXIS_IS_TMC(E5)
  416. case E5: return stepperE5.getMilliamps();
  417. #endif
  418. default: return NAN;
  419. };
  420. }
  421. void setAxisCurrent_mA(const float mA, const axis_t axis) {
  422. switch (axis) {
  423. #if AXIS_IS_TMC(X)
  424. case X: stepperX.rms_current(constrain(mA, 500, 1500)); break;
  425. #endif
  426. #if AXIS_IS_TMC(Y)
  427. case Y: stepperY.rms_current(constrain(mA, 500, 1500)); break;
  428. #endif
  429. #if AXIS_IS_TMC(Z)
  430. case Z: stepperZ.rms_current(constrain(mA, 500, 1500)); break;
  431. #endif
  432. default: break;
  433. };
  434. }
  435. void setAxisCurrent_mA(const float mA, const extruder_t extruder) {
  436. switch (extruder) {
  437. #if AXIS_IS_TMC(E0)
  438. case E0: stepperE0.rms_current(constrain(mA, 500, 1500)); break;
  439. #endif
  440. #if AXIS_IS_TMC(E1)
  441. case E1: stepperE1.rms_current(constrain(mA, 500, 1500)); break;
  442. #endif
  443. #if AXIS_IS_TMC(E2)
  444. case E2: stepperE2.rms_current(constrain(mA, 500, 1500)); break;
  445. #endif
  446. #if AXIS_IS_TMC(E3)
  447. case E3: stepperE3.rms_current(constrain(mA, 500, 1500)); break;
  448. #endif
  449. #if AXIS_IS_TMC(E4)
  450. case E4: stepperE4.rms_current(constrain(mA, 500, 1500)); break;
  451. #endif
  452. #if AXIS_IS_TMC(E5)
  453. case E5: stepperE5.rms_current(constrain(mA, 500, 1500)); break;
  454. #endif
  455. default: break;
  456. };
  457. }
  458. int getTMCBumpSensitivity(const axis_t axis) {
  459. switch (axis) {
  460. #if X_SENSORLESS
  461. case X: return stepperX.homing_threshold();
  462. #endif
  463. #if Y_SENSORLESS
  464. case Y: return stepperY.homing_threshold();
  465. #endif
  466. #if Z_SENSORLESS
  467. case Z: return stepperZ.homing_threshold();
  468. #endif
  469. default: return 0;
  470. }
  471. }
  472. void setTMCBumpSensitivity(const float value, const axis_t axis) {
  473. switch (axis) {
  474. #if X_SENSORLESS || Y_SENSORLESS || Z_SENSORLESS
  475. #if X_SENSORLESS
  476. case X: stepperX.homing_threshold(value); break;
  477. #endif
  478. #if Y_SENSORLESS
  479. case Y: stepperY.homing_threshold(value); break;
  480. #endif
  481. #if Z_SENSORLESS
  482. case Z: stepperZ.homing_threshold(value); break;
  483. #endif
  484. #else
  485. UNUSED(value);
  486. #endif
  487. default: break;
  488. }
  489. }
  490. #endif
  491. float getAxisSteps_per_mm(const axis_t axis) {
  492. return planner.settings.axis_steps_per_mm[axis];
  493. }
  494. float getAxisSteps_per_mm(const extruder_t extruder) {
  495. UNUSED_E(extruder);
  496. return planner.settings.axis_steps_per_mm[E_AXIS_N(extruder - E0)];
  497. }
  498. void setAxisSteps_per_mm(const float value, const axis_t axis) {
  499. planner.settings.axis_steps_per_mm[axis] = value;
  500. }
  501. void setAxisSteps_per_mm(const float value, const extruder_t extruder) {
  502. UNUSED_E(extruder);
  503. planner.settings.axis_steps_per_mm[E_AXIS_N(axis - E0)] = value;
  504. }
  505. feedRate_t getAxisMaxFeedrate_mm_s(const axis_t axis) {
  506. return planner.settings.max_feedrate_mm_s[axis];
  507. }
  508. feedRate_t getAxisMaxFeedrate_mm_s(const extruder_t extruder) {
  509. UNUSED_E(extruder);
  510. return planner.settings.max_feedrate_mm_s[E_AXIS_N(axis - E0)];
  511. }
  512. void setAxisMaxFeedrate_mm_s(const feedRate_t value, const axis_t axis) {
  513. planner.set_max_feedrate(axis, value);
  514. }
  515. void setAxisMaxFeedrate_mm_s(const feedRate_t value, const extruder_t extruder) {
  516. planner.set_max_feedrate(E_AXIS_N(extruder - E0), value);
  517. }
  518. float getAxisMaxAcceleration_mm_s2(const axis_t axis) {
  519. return planner.settings.max_acceleration_mm_per_s2[axis];
  520. }
  521. float getAxisMaxAcceleration_mm_s2(const extruder_t extruder) {
  522. UNUSED_E(extruder);
  523. return planner.settings.max_acceleration_mm_per_s2[E_AXIS_N(extruder - E0)];
  524. }
  525. void setAxisMaxAcceleration_mm_s2(const float value, const axis_t axis) {
  526. planner.set_max_acceleration(axis, value);
  527. }
  528. void setAxisMaxAcceleration_mm_s2(const float value, const extruder_t extruder) {
  529. planner.set_max_acceleration(E_AXIS_N(extruder - E0), value);
  530. }
  531. #if HAS_FILAMENT_SENSOR
  532. bool getFilamentRunoutEnabled() { return runout.enabled; }
  533. void setFilamentRunoutEnabled(const bool value) { runout.enabled = value; }
  534. #ifdef FILAMENT_RUNOUT_DISTANCE_MM
  535. float getFilamentRunoutDistance_mm() { return runout.runout_distance(); }
  536. void setFilamentRunoutDistance_mm(const float value) { runout.set_runout_distance(constrain(value, 0, 999)); }
  537. #endif
  538. #endif
  539. #if ENABLED(LIN_ADVANCE)
  540. float getLinearAdvance_mm_mm_s(const extruder_t extruder) {
  541. return (extruder < EXTRUDERS) ? planner.extruder_advance_K[extruder - E0] : 0;
  542. }
  543. void setLinearAdvance_mm_mm_s(const float value, const extruder_t extruder) {
  544. if (extruder < EXTRUDERS)
  545. planner.extruder_advance_K[extruder - E0] = constrain(value, 0, 999);
  546. }
  547. #endif
  548. #if DISABLED(CLASSIC_JERK)
  549. float getJunctionDeviation_mm() {
  550. return planner.junction_deviation_mm;
  551. }
  552. void setJunctionDeviation_mm(const float value) {
  553. planner.junction_deviation_mm = constrain(value, 0.01, 0.3);
  554. #if ENABLED(LIN_ADVANCE)
  555. planner.recalculate_max_e_jerk();
  556. #endif
  557. }
  558. #else
  559. float getAxisMaxJerk_mm_s(const axis_t axis) {
  560. return planner.max_jerk[axis];
  561. }
  562. float getAxisMaxJerk_mm_s(const extruder_t) {
  563. return planner.max_jerk.e;
  564. }
  565. void setAxisMaxJerk_mm_s(const float value, const axis_t axis) {
  566. planner.set_max_jerk((AxisEnum)axis, value);
  567. }
  568. void setAxisMaxJerk_mm_s(const float value, const extruder_t) {
  569. planner.set_max_jerk(E_AXIS, value);
  570. }
  571. #endif
  572. feedRate_t getFeedrate_mm_s() { return feedrate_mm_s; }
  573. feedRate_t getMinFeedrate_mm_s() { return planner.settings.min_feedrate_mm_s; }
  574. feedRate_t getMinTravelFeedrate_mm_s() { return planner.settings.min_travel_feedrate_mm_s; }
  575. float getPrintingAcceleration_mm_s2() { return planner.settings.acceleration; }
  576. float getRetractAcceleration_mm_s2() { return planner.settings.retract_acceleration; }
  577. float getTravelAcceleration_mm_s2() { return planner.settings.travel_acceleration; }
  578. void setFeedrate_mm_s(const feedRate_t fr) { feedrate_mm_s = fr; }
  579. void setMinFeedrate_mm_s(const feedRate_t fr) { planner.settings.min_feedrate_mm_s = fr; }
  580. void setMinTravelFeedrate_mm_s(const feedRate_t fr) { planner.settings.min_travel_feedrate_mm_s = fr; }
  581. void setPrintingAcceleration_mm_s2(const float acc) { planner.settings.acceleration = acc; }
  582. void setRetractAcceleration_mm_s2(const float acc) { planner.settings.retract_acceleration = acc; }
  583. void setTravelAcceleration_mm_s2(const float acc) { planner.settings.travel_acceleration = acc; }
  584. #if ENABLED(BABYSTEPPING)
  585. bool babystepAxis_steps(const int16_t steps, const axis_t axis) {
  586. switch (axis) {
  587. #if ENABLED(BABYSTEP_XY)
  588. case X: babystep.add_steps(X_AXIS, steps); break;
  589. case Y: babystep.add_steps(Y_AXIS, steps); break;
  590. #endif
  591. case Z: babystep.add_steps(Z_AXIS, steps); break;
  592. default: return false;
  593. };
  594. return true;
  595. }
  596. /**
  597. * This function adjusts an axis during a print.
  598. *
  599. * When linked_nozzles is false, each nozzle in a multi-nozzle
  600. * printer can be babystepped independently of the others. This
  601. * lets the user to fine tune the Z-offset and Nozzle Offsets
  602. * while observing the first layer of a print, regardless of
  603. * what nozzle is printing.
  604. */
  605. void smartAdjustAxis_steps(const int16_t steps, const axis_t axis, bool linked_nozzles) {
  606. const float mm = steps * planner.steps_to_mm[axis];
  607. if (!babystepAxis_steps(steps, axis)) return;
  608. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  609. // Make it so babystepping in Z adjusts the Z probe offset.
  610. if (axis == Z
  611. #if EXTRUDERS > 1
  612. && (linked_nozzles || active_extruder == 0)
  613. #endif
  614. ) probe_offset.z += mm;
  615. #else
  616. UNUSED(mm);
  617. #endif
  618. #if EXTRUDERS > 1 && HAS_HOTEND_OFFSET
  619. /**
  620. * When linked_nozzles is false, as an axis is babystepped
  621. * adjust the hotend offsets so that the other nozzles are
  622. * unaffected by the babystepping of the active nozzle.
  623. */
  624. if (!linked_nozzles) {
  625. HOTEND_LOOP()
  626. if (e != active_extruder)
  627. hotend_offset[e][axis] += mm;
  628. normalizeNozzleOffset(X);
  629. normalizeNozzleOffset(Y);
  630. normalizeNozzleOffset(Z);
  631. }
  632. #else
  633. UNUSED(linked_nozzles);
  634. UNUSED(mm);
  635. #endif
  636. }
  637. /**
  638. * Converts a mm displacement to a number of whole number of
  639. * steps that is at least mm long.
  640. */
  641. int16_t mmToWholeSteps(const float mm, const axis_t axis) {
  642. const float steps = mm / planner.steps_to_mm[axis];
  643. return steps > 0 ? ceil(steps) : floor(steps);
  644. }
  645. #endif
  646. float getZOffset_mm() {
  647. #if HAS_BED_PROBE
  648. return probe_offset.z;
  649. #elif ENABLED(BABYSTEP_DISPLAY_TOTAL)
  650. return babystep.axis_total[BS_TOTAL_AXIS(Z_AXIS) + 1];
  651. #else
  652. return 0.0;
  653. #endif
  654. }
  655. void setZOffset_mm(const float value) {
  656. #if HAS_BED_PROBE
  657. if (WITHIN(value, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX))
  658. probe_offset.z = value;
  659. #elif ENABLED(BABYSTEP_DISPLAY_TOTAL)
  660. babystep.add_mm(Z_AXIS, (value - babystep.axis_total[BS_TOTAL_AXIS(Z_AXIS) + 1]));
  661. #else
  662. UNUSED(value);
  663. #endif
  664. }
  665. #if HAS_HOTEND_OFFSET
  666. float getNozzleOffset_mm(const axis_t axis, const extruder_t extruder) {
  667. if (extruder - E0 >= HOTENDS) return 0;
  668. return hotend_offset[extruder - E0][axis];
  669. }
  670. void setNozzleOffset_mm(const float value, const axis_t axis, const extruder_t extruder) {
  671. if (extruder - E0 >= HOTENDS) return;
  672. hotend_offset[extruder - E0][axis] = value;
  673. }
  674. /**
  675. * The UI should call this if needs to guarantee the first
  676. * nozzle offset is zero (such as when it doesn't allow the
  677. * user to edit the offset the first nozzle).
  678. */
  679. void normalizeNozzleOffset(const axis_t axis) {
  680. const float offs = hotend_offset[0][axis];
  681. HOTEND_LOOP() hotend_offset[e][axis] -= offs;
  682. }
  683. #endif // HAS_HOTEND_OFFSET
  684. #if ENABLED(BACKLASH_GCODE)
  685. float getAxisBacklash_mm(const axis_t axis) { return backlash.distance_mm[axis]; }
  686. void setAxisBacklash_mm(const float value, const axis_t axis)
  687. { backlash.distance_mm[axis] = constrain(value,0,5); }
  688. float getBacklashCorrection_percent() { return ui8_to_percent(backlash.correction); }
  689. void setBacklashCorrection_percent(const float value) { backlash.correction = map(constrain(value, 0, 100), 0, 100, 0, 255); }
  690. #ifdef BACKLASH_SMOOTHING_MM
  691. float getBacklashSmoothing_mm() { return backlash.smoothing_mm; }
  692. void setBacklashSmoothing_mm(const float value) { backlash.smoothing_mm = constrain(value, 0, 999); }
  693. #endif
  694. #endif
  695. uint8_t getProgress_percent() {
  696. return ui.get_progress();
  697. }
  698. uint32_t getProgress_seconds_elapsed() {
  699. const duration_t elapsed = print_job_timer.duration();
  700. return elapsed.value;
  701. }
  702. #if HAS_LEVELING
  703. bool getLevelingActive() { return planner.leveling_active; }
  704. void setLevelingActive(const bool state) { set_bed_leveling_enabled(state); }
  705. bool getMeshValid() { return leveling_is_valid(); }
  706. #if HAS_MESH
  707. bed_mesh_t& getMeshArray() { return Z_VALUES_ARR; }
  708. float getMeshPoint(const xy_uint8_t &pos) { return Z_VALUES(pos.x, pos.y); }
  709. void setMeshPoint(const xy_uint8_t &pos, const float zoff) {
  710. if (WITHIN(pos.x, 0, GRID_MAX_POINTS_X) && WITHIN(pos.y, 0, GRID_MAX_POINTS_Y)) {
  711. Z_VALUES(pos.x, pos.y) = zoff;
  712. #if ENABLED(ABL_BILINEAR_SUBDIVISION)
  713. bed_level_virt_interpolate();
  714. #endif
  715. }
  716. }
  717. #endif
  718. #endif
  719. #if ENABLED(HOST_PROMPT_SUPPORT)
  720. void setHostResponse(const uint8_t response) { host_response_handler(response); }
  721. #endif
  722. #if ENABLED(PRINTCOUNTER)
  723. char* getTotalPrints_str(char buffer[21]) { strcpy(buffer,i16tostr3left(print_job_timer.getStats().totalPrints)); return buffer; }
  724. char* getFinishedPrints_str(char buffer[21]) { strcpy(buffer,i16tostr3left(print_job_timer.getStats().finishedPrints)); return buffer; }
  725. char* getTotalPrintTime_str(char buffer[21]) { return duration_t(print_job_timer.getStats().printTime).toString(buffer); }
  726. char* getLongestPrint_str(char buffer[21]) { return duration_t(print_job_timer.getStats().longestPrint).toString(buffer); }
  727. char* getFilamentUsed_str(char buffer[21]) {
  728. printStatistics stats = print_job_timer.getStats();
  729. sprintf_P(buffer, PSTR("%ld.%im"), long(stats.filamentUsed / 1000), int16_t(stats.filamentUsed / 100) % 10);
  730. return buffer;
  731. }
  732. #endif
  733. float getFeedrate_percent() { return feedrate_percentage; }
  734. void injectCommands_P(PGM_P const gcode) {
  735. queue.inject_P(gcode);
  736. }
  737. bool commandsInQueue() { return (planner.movesplanned() || queue.has_commands_queued()); }
  738. bool isAxisPositionKnown(const axis_t axis) {
  739. return TEST(axis_known_position, axis);
  740. }
  741. bool isAxisPositionKnown(const extruder_t) {
  742. return TEST(axis_known_position, E_AXIS);
  743. }
  744. bool isPositionKnown() { return all_axes_known(); }
  745. bool isMachineHomed() { return all_axes_homed(); }
  746. PGM_P getFirmwareName_str() {
  747. static const char firmware_name[] PROGMEM = "Marlin " SHORT_BUILD_VERSION;
  748. return firmware_name;
  749. }
  750. void setTargetTemp_celsius(float value, const heater_t heater) {
  751. enableHeater(heater);
  752. #if HAS_HEATED_BED
  753. if (heater == BED)
  754. thermalManager.setTargetBed(constrain(value, 0, BED_MAXTEMP - 10));
  755. else
  756. #endif
  757. {
  758. #if HOTENDS
  759. static constexpr int16_t heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP, HEATER_5_MAXTEMP);
  760. const int16_t e = heater - H0;
  761. thermalManager.setTargetHotend(constrain(value, 0, heater_maxtemp[e] - 15), e);
  762. #endif
  763. }
  764. }
  765. void setTargetTemp_celsius(float value, const extruder_t extruder) {
  766. #if HOTENDS
  767. constexpr int16_t heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP, HEATER_5_MAXTEMP);
  768. const int16_t e = extruder - E0;
  769. enableHeater(extruder);
  770. thermalManager.setTargetHotend(constrain(value, 0, heater_maxtemp[e] - 15), e);
  771. #endif
  772. }
  773. void setTargetFan_percent(const float value, const fan_t fan) {
  774. #if FAN_COUNT > 0
  775. if (fan < FAN_COUNT)
  776. thermalManager.set_fan_speed(fan - FAN0, map(constrain(value, 0, 100), 0, 100, 0, 255));
  777. #else
  778. UNUSED(value);
  779. UNUSED(fan);
  780. #endif
  781. }
  782. void setFeedrate_percent(const float value) {
  783. feedrate_percentage = constrain(value, 10, 500);
  784. }
  785. void setUserConfirmed() {
  786. #if HAS_RESUME_CONTINUE
  787. wait_for_user = false;
  788. #endif
  789. }
  790. void printFile(const char *filename) {
  791. IFSD(card.openAndPrintFile(filename), NOOP);
  792. }
  793. bool isPrintingFromMediaPaused() {
  794. return IFSD(isPrintingFromMedia() && !IS_SD_PRINTING(), false);
  795. }
  796. bool isPrintingFromMedia() {
  797. return IFSD(card.isFileOpen(), false);
  798. }
  799. bool isPrinting() {
  800. return (planner.movesplanned() || isPrintingFromMedia() || IFSD(IS_SD_PRINTING(), false));
  801. }
  802. bool isMediaInserted() {
  803. return IFSD(IS_SD_INSERTED() && card.isMounted(), false);
  804. }
  805. void pausePrint() {
  806. ui.pause_print();
  807. }
  808. void resumePrint() {
  809. ui.resume_print();
  810. }
  811. void stopPrint() {
  812. ui.abort_print();
  813. }
  814. void onUserConfirmRequired_P(PGM_P const pstr) {
  815. char msg[strlen_P(pstr) + 1];
  816. strcpy_P(msg, pstr);
  817. onUserConfirmRequired(msg);
  818. }
  819. FileList::FileList() { refresh(); }
  820. void FileList::refresh() { num_files = 0xFFFF; }
  821. bool FileList::seek(const uint16_t pos, const bool skip_range_check) {
  822. #if ENABLED(SDSUPPORT)
  823. if (!skip_range_check && (pos + 1) > count()) return false;
  824. const uint16_t nr =
  825. #if ENABLED(SDCARD_RATHERRECENTFIRST) && DISABLED(SDCARD_SORT_ALPHA)
  826. count() - 1 -
  827. #endif
  828. pos;
  829. card.getfilename_sorted(nr);
  830. return card.filename[0] != '\0';
  831. #else
  832. return false;
  833. #endif
  834. }
  835. const char* FileList::filename() {
  836. return IFSD(card.longFilename[0] ? card.longFilename : card.filename, "");
  837. }
  838. const char* FileList::shortFilename() {
  839. return IFSD(card.filename, "");
  840. }
  841. const char* FileList::longFilename() {
  842. return IFSD(card.longFilename, "");
  843. }
  844. bool FileList::isDir() {
  845. return IFSD(card.flag.filenameIsDir, false);
  846. }
  847. uint16_t FileList::count() {
  848. return IFSD((num_files = (num_files == 0xFFFF ? card.get_num_Files() : num_files)), 0);
  849. }
  850. bool FileList::isAtRootDir() {
  851. return (true
  852. #if ENABLED(SDSUPPORT)
  853. && card.flag.workDirIsRoot
  854. #endif
  855. );
  856. }
  857. void FileList::upDir() {
  858. #if ENABLED(SDSUPPORT)
  859. card.cdup();
  860. num_files = 0xFFFF;
  861. #endif
  862. }
  863. void FileList::changeDir(const char * const dirname) {
  864. #if ENABLED(SDSUPPORT)
  865. card.cd(dirname);
  866. num_files = 0xFFFF;
  867. #endif
  868. }
  869. } // namespace ExtUI
  870. // At the moment, we piggy-back off the ultralcd calls, but this could be cleaned up in the future
  871. void MarlinUI::init() {
  872. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  873. SET_INPUT_PULLUP(SD_DETECT_PIN);
  874. #endif
  875. ExtUI::onStartup();
  876. }
  877. void MarlinUI::update() {
  878. #if ENABLED(SDSUPPORT)
  879. static bool last_sd_status;
  880. const bool sd_status = IS_SD_INSERTED();
  881. if (sd_status != last_sd_status) {
  882. last_sd_status = sd_status;
  883. if (sd_status) {
  884. card.mount();
  885. if (card.isMounted())
  886. ExtUI::onMediaInserted();
  887. else
  888. ExtUI::onMediaError();
  889. }
  890. else {
  891. const bool ok = card.isMounted();
  892. card.release();
  893. if (ok) ExtUI::onMediaRemoved();
  894. }
  895. }
  896. #endif // SDSUPPORT
  897. ExtUI::onIdle();
  898. }
  899. void MarlinUI::kill_screen(PGM_P const error, PGM_P const component) {
  900. using namespace ExtUI;
  901. if (!flags.printer_killed) {
  902. flags.printer_killed = true;
  903. onPrinterKilled(error, component);
  904. }
  905. }
  906. #endif // EXTENSIBLE_UI