My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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