My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ui_api.cpp 33KB

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