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

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