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

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