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

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