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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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 "../marlinui.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 HAS_MULTI_EXTRUDER
  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. // Get motion limit from software endstops, if any
  265. float min, max;
  266. soft_endstop.get_manual_axis_limits((AxisEnum)axis, min, max);
  267. // Delta limits XY based on the current offset from center
  268. // This assumes the center is 0,0
  269. #if ENABLED(DELTA)
  270. if (axis != Z) {
  271. max = SQRT(sq(float(DELTA_PRINTABLE_RADIUS)) - sq(current_position[Y - axis])); // (Y - axis) == the other axis
  272. min = -max;
  273. }
  274. #endif
  275. current_position[axis] = constrain(position, min, max);
  276. line_to_current_position(feedrate ?: manual_feedrate_mm_s[axis]);
  277. }
  278. void setAxisPosition_mm(const float position, const extruder_t extruder, const feedRate_t feedrate/*=0*/) {
  279. setActiveTool(extruder, true);
  280. current_position.e = position;
  281. line_to_current_position(feedrate ?: manual_feedrate_mm_s.e);
  282. }
  283. void setActiveTool(const extruder_t extruder, bool no_move) {
  284. #if HAS_MULTI_EXTRUDER
  285. const uint8_t e = extruder - E0;
  286. if (e != active_extruder) tool_change(e, no_move);
  287. active_extruder = e;
  288. #else
  289. UNUSED(extruder);
  290. UNUSED(no_move);
  291. #endif
  292. }
  293. extruder_t getActiveTool() {
  294. switch (active_extruder) {
  295. case 5: return E5;
  296. case 4: return E4;
  297. case 3: return E3;
  298. case 2: return E2;
  299. case 1: return E1;
  300. default: return E0;
  301. }
  302. }
  303. bool isMoving() { return planner.has_blocks_queued(); }
  304. bool canMove(const axis_t axis) {
  305. switch (axis) {
  306. #if IS_KINEMATIC || ENABLED(NO_MOTION_BEFORE_HOMING)
  307. case X: return TEST(axis_homed, X_AXIS);
  308. case Y: return TEST(axis_homed, Y_AXIS);
  309. case Z: return TEST(axis_homed, Z_AXIS);
  310. #else
  311. case X: case Y: case Z: return true;
  312. #endif
  313. default: return false;
  314. }
  315. }
  316. bool canMove(const extruder_t extruder) {
  317. return !thermalManager.tooColdToExtrude(extruder - E0);
  318. }
  319. #if HAS_SOFTWARE_ENDSTOPS
  320. bool getSoftEndstopState() { return soft_endstop._enabled; }
  321. void setSoftEndstopState(const bool value) { soft_endstop._enabled = value; }
  322. #endif
  323. #if HAS_TRINAMIC_CONFIG
  324. float getAxisCurrent_mA(const axis_t axis) {
  325. switch (axis) {
  326. #if AXIS_IS_TMC(X)
  327. case X: return stepperX.getMilliamps();
  328. #endif
  329. #if AXIS_IS_TMC(X2)
  330. case X2: return stepperX2.getMilliamps();
  331. #endif
  332. #if AXIS_IS_TMC(Y)
  333. case Y: return stepperY.getMilliamps();
  334. #endif
  335. #if AXIS_IS_TMC(Y2)
  336. case Y2: return stepperY2.getMilliamps();
  337. #endif
  338. #if AXIS_IS_TMC(Z)
  339. case Z: return stepperZ.getMilliamps();
  340. #endif
  341. #if AXIS_IS_TMC(Z2)
  342. case Z2: return stepperZ2.getMilliamps();
  343. #endif
  344. default: return NAN;
  345. };
  346. }
  347. float getAxisCurrent_mA(const extruder_t extruder) {
  348. switch (extruder) {
  349. #if AXIS_IS_TMC(E0)
  350. case E0: return stepperE0.getMilliamps();
  351. #endif
  352. #if AXIS_IS_TMC(E1)
  353. case E1: return stepperE1.getMilliamps();
  354. #endif
  355. #if AXIS_IS_TMC(E2)
  356. case E2: return stepperE2.getMilliamps();
  357. #endif
  358. #if AXIS_IS_TMC(E3)
  359. case E3: return stepperE3.getMilliamps();
  360. #endif
  361. #if AXIS_IS_TMC(E4)
  362. case E4: return stepperE4.getMilliamps();
  363. #endif
  364. #if AXIS_IS_TMC(E5)
  365. case E5: return stepperE5.getMilliamps();
  366. #endif
  367. #if AXIS_IS_TMC(E6)
  368. case E6: return stepperE6.getMilliamps();
  369. #endif
  370. #if AXIS_IS_TMC(E7)
  371. case E7: return stepperE7.getMilliamps();
  372. #endif
  373. default: return NAN;
  374. };
  375. }
  376. void setAxisCurrent_mA(const float mA, const axis_t axis) {
  377. switch (axis) {
  378. #if AXIS_IS_TMC(X)
  379. case X: stepperX.rms_current(constrain(mA, 400, 1500)); break;
  380. #endif
  381. #if AXIS_IS_TMC(X2)
  382. case X2: stepperX2.rms_current(constrain(mA, 400, 1500)); break;
  383. #endif
  384. #if AXIS_IS_TMC(Y)
  385. case Y: stepperY.rms_current(constrain(mA, 400, 1500)); break;
  386. #endif
  387. #if AXIS_IS_TMC(Y2)
  388. case Y2: stepperY2.rms_current(constrain(mA, 400, 1500)); break;
  389. #endif
  390. #if AXIS_IS_TMC(Z)
  391. case Z: stepperZ.rms_current(constrain(mA, 400, 1500)); break;
  392. #endif
  393. #if AXIS_IS_TMC(Z2)
  394. case Z2: stepperZ2.rms_current(constrain(mA, 400, 1500)); break;
  395. #endif
  396. default: break;
  397. };
  398. }
  399. void setAxisCurrent_mA(const float mA, const extruder_t extruder) {
  400. switch (extruder) {
  401. #if AXIS_IS_TMC(E0)
  402. case E0: stepperE0.rms_current(constrain(mA, 400, 1500)); break;
  403. #endif
  404. #if AXIS_IS_TMC(E1)
  405. case E1: stepperE1.rms_current(constrain(mA, 400, 1500)); break;
  406. #endif
  407. #if AXIS_IS_TMC(E2)
  408. case E2: stepperE2.rms_current(constrain(mA, 400, 1500)); break;
  409. #endif
  410. #if AXIS_IS_TMC(E3)
  411. case E3: stepperE3.rms_current(constrain(mA, 400, 1500)); break;
  412. #endif
  413. #if AXIS_IS_TMC(E4)
  414. case E4: stepperE4.rms_current(constrain(mA, 400, 1500)); break;
  415. #endif
  416. #if AXIS_IS_TMC(E5)
  417. case E5: stepperE5.rms_current(constrain(mA, 400, 1500)); break;
  418. #endif
  419. #if AXIS_IS_TMC(E6)
  420. case E6: stepperE6.rms_current(constrain(mA, 400, 1500)); break;
  421. #endif
  422. #if AXIS_IS_TMC(E7)
  423. case E7: stepperE7.rms_current(constrain(mA, 400, 1500)); break;
  424. #endif
  425. default: break;
  426. };
  427. }
  428. int getTMCBumpSensitivity(const axis_t axis) {
  429. switch (axis) {
  430. TERN_(X_SENSORLESS, case X: return stepperX.homing_threshold());
  431. TERN_(X2_SENSORLESS, case X2: return stepperX2.homing_threshold());
  432. TERN_(Y_SENSORLESS, case Y: return stepperY.homing_threshold());
  433. TERN_(Y2_SENSORLESS, case Y2: return stepperY2.homing_threshold());
  434. TERN_(Z_SENSORLESS, case Z: return stepperZ.homing_threshold());
  435. TERN_(Z2_SENSORLESS, case Z2: return stepperZ2.homing_threshold());
  436. TERN_(Z3_SENSORLESS, case Z3: return stepperZ3.homing_threshold());
  437. TERN_(Z4_SENSORLESS, case Z4: return stepperZ4.homing_threshold());
  438. default: return 0;
  439. }
  440. }
  441. void setTMCBumpSensitivity(const float value, const axis_t axis) {
  442. switch (axis) {
  443. #if X_SENSORLESS || Y_SENSORLESS || Z_SENSORLESS
  444. #if X_SENSORLESS
  445. case X: stepperX.homing_threshold(value); break;
  446. #endif
  447. #if X2_SENSORLESS
  448. case X2: stepperX2.homing_threshold(value); break;
  449. #endif
  450. #if Y_SENSORLESS
  451. case Y: stepperY.homing_threshold(value); break;
  452. #endif
  453. #if Y2_SENSORLESS
  454. case Y2: stepperY2.homing_threshold(value); break;
  455. #endif
  456. #if Z_SENSORLESS
  457. case Z: stepperZ.homing_threshold(value); break;
  458. #endif
  459. #if Z2_SENSORLESS
  460. case Z2: stepperZ2.homing_threshold(value); break;
  461. #endif
  462. #if Z3_SENSORLESS
  463. case Z3: stepperZ3.homing_threshold(value); break;
  464. #endif
  465. #if Z4_SENSORLESS
  466. case Z4: stepperZ4.homing_threshold(value); break;
  467. #endif
  468. #else
  469. UNUSED(value);
  470. #endif
  471. default: break;
  472. }
  473. }
  474. #endif
  475. float getAxisSteps_per_mm(const axis_t axis) {
  476. return planner.settings.axis_steps_per_mm[axis];
  477. }
  478. float getAxisSteps_per_mm(const extruder_t extruder) {
  479. UNUSED_E(extruder);
  480. return planner.settings.axis_steps_per_mm[E_AXIS_N(extruder - E0)];
  481. }
  482. void setAxisSteps_per_mm(const float value, const axis_t axis) {
  483. planner.settings.axis_steps_per_mm[axis] = value;
  484. planner.refresh_positioning();
  485. }
  486. void setAxisSteps_per_mm(const float value, const extruder_t extruder) {
  487. UNUSED_E(extruder);
  488. planner.settings.axis_steps_per_mm[E_AXIS_N(extruder - E0)] = value;
  489. planner.refresh_positioning();
  490. }
  491. feedRate_t getAxisMaxFeedrate_mm_s(const axis_t axis) {
  492. return planner.settings.max_feedrate_mm_s[axis];
  493. }
  494. feedRate_t getAxisMaxFeedrate_mm_s(const extruder_t extruder) {
  495. UNUSED_E(extruder);
  496. return planner.settings.max_feedrate_mm_s[E_AXIS_N(extruder - E0)];
  497. }
  498. void setAxisMaxFeedrate_mm_s(const feedRate_t value, const axis_t axis) {
  499. planner.set_max_feedrate(axis, value);
  500. }
  501. void setAxisMaxFeedrate_mm_s(const feedRate_t value, const extruder_t extruder) {
  502. UNUSED_E(extruder);
  503. planner.set_max_feedrate(E_AXIS_N(extruder - E0), value);
  504. }
  505. float getAxisMaxAcceleration_mm_s2(const axis_t axis) {
  506. return planner.settings.max_acceleration_mm_per_s2[axis];
  507. }
  508. float getAxisMaxAcceleration_mm_s2(const extruder_t extruder) {
  509. UNUSED_E(extruder);
  510. return planner.settings.max_acceleration_mm_per_s2[E_AXIS_N(extruder - E0)];
  511. }
  512. void setAxisMaxAcceleration_mm_s2(const float value, const axis_t axis) {
  513. planner.set_max_acceleration(axis, value);
  514. planner.reset_acceleration_rates();
  515. }
  516. void setAxisMaxAcceleration_mm_s2(const float value, const extruder_t extruder) {
  517. UNUSED_E(extruder);
  518. planner.set_max_acceleration(E_AXIS_N(extruder - E0), value);
  519. planner.reset_acceleration_rates();
  520. }
  521. #if HAS_FILAMENT_SENSOR
  522. bool getFilamentRunoutEnabled() { return runout.enabled; }
  523. void setFilamentRunoutEnabled(const bool value) { runout.enabled = value; }
  524. bool getFilamentRunoutState() { return runout.filament_ran_out; }
  525. void setFilamentRunoutState(const bool value) { runout.filament_ran_out = value; }
  526. #if HAS_FILAMENT_RUNOUT_DISTANCE
  527. float getFilamentRunoutDistance_mm() { return runout.runout_distance(); }
  528. void setFilamentRunoutDistance_mm(const float value) { runout.set_runout_distance(constrain(value, 0, 999)); }
  529. #endif
  530. #endif
  531. #if ENABLED(CASE_LIGHT_ENABLE)
  532. bool getCaseLightState() { return caselight.on; }
  533. void setCaseLightState(const bool value) {
  534. caselight.on = value;
  535. caselight.update_enabled();
  536. }
  537. #if DISABLED(CASE_LIGHT_NO_BRIGHTNESS)
  538. float getCaseLightBrightness_percent() { return ui8_to_percent(caselight.brightness); }
  539. void setCaseLightBrightness_percent(const float value) {
  540. caselight.brightness = map(constrain(value, 0, 100), 0, 100, 0, 255);
  541. caselight.update_brightness();
  542. }
  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 HAS_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.001, 0.3);
  560. TERN_(LIN_ADVANCE, planner.recalculate_max_e_jerk());
  561. }
  562. #else
  563. float getAxisMaxJerk_mm_s(const axis_t axis) {
  564. return planner.max_jerk[axis];
  565. }
  566. float getAxisMaxJerk_mm_s(const extruder_t) {
  567. return planner.max_jerk.e;
  568. }
  569. void setAxisMaxJerk_mm_s(const float value, const axis_t axis) {
  570. planner.set_max_jerk((AxisEnum)axis, value);
  571. }
  572. void setAxisMaxJerk_mm_s(const float value, const extruder_t) {
  573. planner.set_max_jerk(E_AXIS, value);
  574. }
  575. #endif
  576. feedRate_t getFeedrate_mm_s() { return feedrate_mm_s; }
  577. int16_t getFlowPercentage(const extruder_t extr) { return planner.flow_percentage[extr]; }
  578. feedRate_t getMinFeedrate_mm_s() { return planner.settings.min_feedrate_mm_s; }
  579. feedRate_t getMinTravelFeedrate_mm_s() { return planner.settings.min_travel_feedrate_mm_s; }
  580. float getPrintingAcceleration_mm_s2() { return planner.settings.acceleration; }
  581. float getRetractAcceleration_mm_s2() { return planner.settings.retract_acceleration; }
  582. float getTravelAcceleration_mm_s2() { return planner.settings.travel_acceleration; }
  583. void setFeedrate_mm_s(const feedRate_t fr) { feedrate_mm_s = fr; }
  584. void setFlow_percent(const int16_t flow, const extruder_t extr) { planner.set_flow(extr, flow); }
  585. void setMinFeedrate_mm_s(const feedRate_t fr) { planner.settings.min_feedrate_mm_s = fr; }
  586. void setMinTravelFeedrate_mm_s(const feedRate_t 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. UNUSED(mm);
  614. if (!babystepAxis_steps(steps, axis)) return;
  615. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  616. // Make it so babystepping in Z adjusts the Z probe offset.
  617. if (axis == Z && TERN1(HAS_MULTI_EXTRUDER, (linked_nozzles || active_extruder == 0)))
  618. probe.offset.z += mm;
  619. #endif
  620. #if HAS_MULTI_EXTRUDER && HAS_HOTEND_OFFSET
  621. /**
  622. * When linked_nozzles is false, as an axis is babystepped
  623. * adjust the hotend offsets so that the other nozzles are
  624. * unaffected by the babystepping of the active nozzle.
  625. */
  626. if (!linked_nozzles) {
  627. HOTEND_LOOP()
  628. if (e != active_extruder)
  629. hotend_offset[e][axis] += mm;
  630. normalizeNozzleOffset(X);
  631. normalizeNozzleOffset(Y);
  632. normalizeNozzleOffset(Z);
  633. }
  634. #else
  635. UNUSED(linked_nozzles);
  636. #endif
  637. }
  638. /**
  639. * Converts a mm displacement to a number of whole number of
  640. * steps that is at least mm long.
  641. */
  642. int16_t mmToWholeSteps(const float mm, const axis_t axis) {
  643. const float steps = mm / planner.steps_to_mm[axis];
  644. return steps > 0 ? CEIL(steps) : FLOOR(steps);
  645. }
  646. #endif
  647. float getZOffset_mm() {
  648. return (0.0f
  649. #if HAS_BED_PROBE
  650. + probe.offset.z
  651. #elif ENABLED(BABYSTEP_DISPLAY_TOTAL)
  652. + planner.steps_to_mm[Z_AXIS] * babystep.axis_total[BS_AXIS_IND(Z_AXIS)]
  653. #endif
  654. );
  655. }
  656. void setZOffset_mm(const float value) {
  657. #if HAS_BED_PROBE
  658. if (WITHIN(value, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX))
  659. probe.offset.z = value;
  660. #elif ENABLED(BABYSTEP_DISPLAY_TOTAL)
  661. babystep.add_mm(Z_AXIS, value - getZOffset_mm());
  662. #else
  663. UNUSED(value);
  664. #endif
  665. }
  666. #if HAS_HOTEND_OFFSET
  667. float getNozzleOffset_mm(const axis_t axis, const extruder_t extruder) {
  668. if (extruder - E0 >= HOTENDS) return 0;
  669. return hotend_offset[extruder - E0][axis];
  670. }
  671. void setNozzleOffset_mm(const float value, const axis_t axis, const extruder_t extruder) {
  672. if (extruder - E0 >= HOTENDS) return;
  673. hotend_offset[extruder - E0][axis] = value;
  674. }
  675. /**
  676. * The UI should call this if needs to guarantee the first
  677. * nozzle offset is zero (such as when it doesn't allow the
  678. * user to edit the offset the first nozzle).
  679. */
  680. void normalizeNozzleOffset(const axis_t axis) {
  681. const float offs = hotend_offset[0][axis];
  682. HOTEND_LOOP() hotend_offset[e][axis] -= offs;
  683. }
  684. #endif // HAS_HOTEND_OFFSET
  685. #if HAS_BED_PROBE
  686. float getProbeOffset_mm(const axis_t axis) {
  687. return probe.offset.pos[axis];
  688. }
  689. void setProbeOffset_mm(const float val, const axis_t axis) {
  690. probe.offset.pos[axis] = val;
  691. }
  692. #endif
  693. #if ENABLED(BACKLASH_GCODE)
  694. float getAxisBacklash_mm(const axis_t axis) { return backlash.distance_mm[axis]; }
  695. void setAxisBacklash_mm(const float value, const axis_t axis)
  696. { backlash.distance_mm[axis] = constrain(value,0,5); }
  697. float getBacklashCorrection_percent() { return ui8_to_percent(backlash.correction); }
  698. void setBacklashCorrection_percent(const float value) { backlash.correction = map(constrain(value, 0, 100), 0, 100, 0, 255); }
  699. #ifdef BACKLASH_SMOOTHING_MM
  700. float getBacklashSmoothing_mm() { return backlash.smoothing_mm; }
  701. void setBacklashSmoothing_mm(const float value) { backlash.smoothing_mm = constrain(value, 0, 999); }
  702. #endif
  703. #endif
  704. uint8_t getProgress_percent() {
  705. return ui.get_progress_percent();
  706. }
  707. uint32_t getProgress_seconds_elapsed() {
  708. const duration_t elapsed = print_job_timer.duration();
  709. return elapsed.value;
  710. }
  711. #if HAS_LEVELING
  712. bool getLevelingActive() { return planner.leveling_active; }
  713. void setLevelingActive(const bool state) { set_bed_leveling_enabled(state); }
  714. bool getMeshValid() { return leveling_is_valid(); }
  715. #if HAS_MESH
  716. bed_mesh_t& getMeshArray() { return Z_VALUES_ARR; }
  717. float getMeshPoint(const xy_uint8_t &pos) { return Z_VALUES(pos.x, pos.y); }
  718. void setMeshPoint(const xy_uint8_t &pos, const float zoff) {
  719. if (WITHIN(pos.x, 0, GRID_MAX_POINTS_X) && WITHIN(pos.y, 0, GRID_MAX_POINTS_Y)) {
  720. Z_VALUES(pos.x, pos.y) = zoff;
  721. TERN_(ABL_BILINEAR_SUBDIVISION, bed_level_virt_interpolate());
  722. }
  723. }
  724. #endif
  725. #endif
  726. #if ENABLED(HOST_PROMPT_SUPPORT)
  727. void setHostResponse(const uint8_t response) { host_response_handler(response); }
  728. #endif
  729. #if ENABLED(PRINTCOUNTER)
  730. char* getTotalPrints_str(char buffer[21]) { strcpy(buffer,i16tostr3left(print_job_timer.getStats().totalPrints)); return buffer; }
  731. char* getFinishedPrints_str(char buffer[21]) { strcpy(buffer,i16tostr3left(print_job_timer.getStats().finishedPrints)); return buffer; }
  732. char* getTotalPrintTime_str(char buffer[21]) { return duration_t(print_job_timer.getStats().printTime).toString(buffer); }
  733. char* getLongestPrint_str(char buffer[21]) { return duration_t(print_job_timer.getStats().longestPrint).toString(buffer); }
  734. char* getFilamentUsed_str(char buffer[21]) {
  735. printStatistics stats = print_job_timer.getStats();
  736. sprintf_P(buffer, PSTR("%ld.%im"), long(stats.filamentUsed / 1000), int16_t(stats.filamentUsed / 100) % 10);
  737. return buffer;
  738. }
  739. #endif
  740. float getFeedrate_percent() { return feedrate_percentage; }
  741. #if ENABLED(PIDTEMP)
  742. float getPIDValues_Kp(const extruder_t tool) { return PID_PARAM(Kp, tool); }
  743. float getPIDValues_Ki(const extruder_t tool) { return unscalePID_i(PID_PARAM(Ki, tool)); }
  744. float getPIDValues_Kd(const extruder_t tool) { return unscalePID_d(PID_PARAM(Kd, tool)); }
  745. void setPIDValues(const float p, const float i, const float d, extruder_t tool) {
  746. thermalManager.temp_hotend[tool].pid.Kp = p;
  747. thermalManager.temp_hotend[tool].pid.Ki = scalePID_i(i);
  748. thermalManager.temp_hotend[tool].pid.Kd = scalePID_d(d);
  749. thermalManager.updatePID();
  750. }
  751. void startPIDTune(const float temp, extruder_t tool) {
  752. thermalManager.PID_autotune(temp, (heater_id_t)tool, 8, true);
  753. }
  754. #endif
  755. #if ENABLED(PIDTEMPBED)
  756. float getBedPIDValues_Kp() { return thermalManager.temp_bed.pid.Kp; }
  757. float getBedPIDValues_Ki() { return unscalePID_i(thermalManager.temp_bed.pid.Ki); }
  758. float getBedPIDValues_Kd() { return unscalePID_d(thermalManager.temp_bed.pid.Kd); }
  759. void setBedPIDValues(const float p, const float i, const float d) {
  760. thermalManager.temp_bed.pid.Kp = p;
  761. thermalManager.temp_bed.pid.Ki = scalePID_i(i);
  762. thermalManager.temp_bed.pid.Kd = scalePID_d(d);
  763. thermalManager.updatePID();
  764. }
  765. void startBedPIDTune(const float temp) {
  766. thermalManager.PID_autotune(temp, H_BED, 4, true);
  767. }
  768. #endif
  769. void injectCommands_P(PGM_P const gcode) { queue.inject_P(gcode); }
  770. void injectCommands(char * const gcode) { queue.inject(gcode); }
  771. bool commandsInQueue() { return (planner.movesplanned() || queue.has_commands_queued()); }
  772. bool isAxisPositionKnown(const axis_t axis) { return TEST(axis_known_position, axis); }
  773. bool isAxisPositionKnown(const extruder_t) { return TEST(axis_known_position, E_AXIS); }
  774. bool isPositionKnown() { return all_axes_known(); }
  775. bool isMachineHomed() { return all_axes_homed(); }
  776. PGM_P getFirmwareName_str() {
  777. static PGMSTR(firmware_name, "Marlin " SHORT_BUILD_VERSION);
  778. return firmware_name;
  779. }
  780. void setTargetTemp_celsius(float value, const heater_t heater) {
  781. #ifdef TOUCH_UI_LCD_TEMP_SCALING
  782. value *= TOUCH_UI_LCD_TEMP_SCALING;
  783. #endif
  784. enableHeater(heater);
  785. #if HAS_HEATED_CHAMBER
  786. if (heater == CHAMBER)
  787. thermalManager.setTargetChamber(LROUND(constrain(value, 0, CHAMBER_MAXTEMP - 10)));
  788. else
  789. #endif
  790. #if HAS_HEATED_BED
  791. if (heater == BED)
  792. thermalManager.setTargetBed(LROUND(constrain(value, 0, BED_MAX_TARGET)));
  793. else
  794. #endif
  795. {
  796. #if HAS_HOTEND
  797. const int16_t e = heater - H0;
  798. thermalManager.setTargetHotend(LROUND(constrain(value, 0, thermalManager.heater_maxtemp[e] - HOTEND_OVERSHOOT)), e);
  799. #endif
  800. }
  801. }
  802. void setTargetTemp_celsius(float value, const extruder_t extruder) {
  803. #ifdef TOUCH_UI_LCD_TEMP_SCALING
  804. value *= TOUCH_UI_LCD_TEMP_SCALING;
  805. #endif
  806. #if HAS_HOTEND
  807. const int16_t e = extruder - E0;
  808. enableHeater(extruder);
  809. thermalManager.setTargetHotend(LROUND(constrain(value, 0, thermalManager.heater_maxtemp[e] - HOTEND_OVERSHOOT)), e);
  810. #endif
  811. }
  812. void setTargetFan_percent(const float value, const fan_t fan) {
  813. #if HAS_FAN
  814. if (fan < FAN_COUNT)
  815. thermalManager.set_fan_speed(fan - FAN0, map(constrain(value, 0, 100), 0, 100, 0, 255));
  816. #else
  817. UNUSED(value);
  818. UNUSED(fan);
  819. #endif
  820. }
  821. void setFeedrate_percent(const float value) {
  822. feedrate_percentage = constrain(value, 10, 500);
  823. }
  824. void setUserConfirmed() {
  825. TERN_(HAS_RESUME_CONTINUE, wait_for_user = false);
  826. }
  827. void printFile(const char *filename) {
  828. UNUSED(filename);
  829. IFSD(card.openAndPrintFile(filename), NOOP);
  830. }
  831. bool isPrintingFromMediaPaused() {
  832. return IFSD(isPrintingFromMedia() && !IS_SD_PRINTING(), false);
  833. }
  834. bool isPrintingFromMedia() {
  835. #if ENABLED(SDSUPPORT)
  836. // Account for when IS_SD_PRINTING() reports the end of the
  837. // print when there is still SD card data in the planner.
  838. flags.was_sd_printing = card.isFileOpen() || (flags.was_sd_printing && commandsInQueue());
  839. return flags.was_sd_printing;
  840. #else
  841. return false;
  842. #endif
  843. }
  844. bool isPrinting() {
  845. return (commandsInQueue() || isPrintingFromMedia() || IFSD(IS_SD_PRINTING(), false));
  846. }
  847. bool isMediaInserted() {
  848. return IFSD(IS_SD_INSERTED() && card.isMounted(), false);
  849. }
  850. void pausePrint() { ui.pause_print(); }
  851. void resumePrint() { ui.resume_print(); }
  852. void stopPrint() { ui.abort_print(); }
  853. void onUserConfirmRequired_P(PGM_P const pstr) {
  854. char msg[strlen_P(pstr) + 1];
  855. strcpy_P(msg, pstr);
  856. onUserConfirmRequired(msg);
  857. }
  858. void onStatusChanged_P(PGM_P const pstr) {
  859. char msg[strlen_P(pstr) + 1];
  860. strcpy_P(msg, pstr);
  861. onStatusChanged(msg);
  862. }
  863. FileList::FileList() { refresh(); }
  864. void FileList::refresh() { num_files = 0xFFFF; }
  865. bool FileList::seek(const uint16_t pos, const bool skip_range_check) {
  866. #if ENABLED(SDSUPPORT)
  867. if (!skip_range_check && (pos + 1) > count()) return false;
  868. card.getfilename_sorted(SD_ORDER(pos, count()));
  869. return card.filename[0] != '\0';
  870. #else
  871. UNUSED(pos);
  872. UNUSED(skip_range_check);
  873. return false;
  874. #endif
  875. }
  876. const char* FileList::filename() {
  877. return IFSD(card.longFilename[0] ? card.longFilename : card.filename, "");
  878. }
  879. const char* FileList::shortFilename() {
  880. return IFSD(card.filename, "");
  881. }
  882. const char* FileList::longFilename() {
  883. return IFSD(card.longFilename, "");
  884. }
  885. bool FileList::isDir() {
  886. return IFSD(card.flag.filenameIsDir, false);
  887. }
  888. uint16_t FileList::count() {
  889. return IFSD((num_files = (num_files == 0xFFFF ? card.get_num_Files() : num_files)), 0);
  890. }
  891. bool FileList::isAtRootDir() {
  892. return IFSD(card.flag.workDirIsRoot, true);
  893. }
  894. void FileList::upDir() {
  895. #if ENABLED(SDSUPPORT)
  896. card.cdup();
  897. num_files = 0xFFFF;
  898. #endif
  899. }
  900. void FileList::changeDir(const char * const dirname) {
  901. #if ENABLED(SDSUPPORT)
  902. card.cd(dirname);
  903. num_files = 0xFFFF;
  904. #else
  905. UNUSED(dirname);
  906. #endif
  907. }
  908. } // namespace ExtUI
  909. // At the moment we hook into MarlinUI methods, but this could be cleaned up in the future
  910. void MarlinUI::init() { ExtUI::onStartup(); }
  911. void MarlinUI::update() { ExtUI::onIdle(); }
  912. void MarlinUI::kill_screen(PGM_P const error, PGM_P const component) {
  913. using namespace ExtUI;
  914. if (!flags.printer_killed) {
  915. flags.printer_killed = true;
  916. onPrinterKilled(error, component);
  917. }
  918. }
  919. #endif // EXTENSIBLE_UI