My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ui_api.cpp 32KB

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