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

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