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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. #if ENABLED(JOYSTICK)
  90. uint8_t jogging : 1;
  91. #endif
  92. #if ENABLED(SDSUPPORT)
  93. uint8_t was_sd_printing : 1;
  94. #endif
  95. } flags;
  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(uint32_t us) { DELAY_US(us); }
  131. void delay_ms(uint32_t 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) thermalManager.manage_heater();
  139. }
  140. void enableHeater(const extruder_t extruder) {
  141. #if HAS_HOTEND && HEATER_IDLE_HANDLER
  142. thermalManager.reset_hotend_idle_timer(extruder - E0);
  143. #else
  144. UNUSED(extruder);
  145. #endif
  146. }
  147. void enableHeater(const heater_t heater) {
  148. #if HEATER_IDLE_HANDLER
  149. switch (heater) {
  150. #if HAS_HEATED_BED
  151. case BED: thermalManager.reset_bed_idle_timer(); return;
  152. #endif
  153. #if ENABLED(HAS_HEATED_CHAMBER)
  154. case CHAMBER: return; // Chamber has no idle timer
  155. #endif
  156. #if ENABLED(HAS_COOLER)
  157. case COOLER: return; // Cooler has no idle timer
  158. #endif
  159. default:
  160. TERN_(HAS_HOTEND, thermalManager.reset_hotend_idle_timer(heater - H0));
  161. break;
  162. }
  163. #else
  164. UNUSED(heater);
  165. #endif
  166. }
  167. #if ENABLED(JOYSTICK)
  168. /**
  169. * Jogs in the direction given by the vector (dx, dy, dz).
  170. * The values range from -1 to 1 mapping to the maximum
  171. * feedrate for an axis.
  172. *
  173. * The axis will continue to jog until this function is
  174. * called with all zeros.
  175. */
  176. void jog(const xyz_float_t &dir) {
  177. // The "destination" variable is used as a scratchpad in
  178. // Marlin by GCODE routines, but should remain untouched
  179. // during manual jogging, allowing us to reuse the space
  180. // for our direction vector.
  181. destination = dir;
  182. flags.jogging = !NEAR_ZERO(dir.x) || !NEAR_ZERO(dir.y) || !NEAR_ZERO(dir.z);
  183. }
  184. // Called by the polling routine in "joystick.cpp"
  185. void _joystick_update(xyz_float_t &norm_jog) {
  186. if (flags.jogging) {
  187. #define OUT_OF_RANGE(VALUE) (VALUE < -1.0f || VALUE > 1.0f)
  188. if (OUT_OF_RANGE(destination.x) || OUT_OF_RANGE(destination.y) || OUT_OF_RANGE(destination.z)) {
  189. // If destination on any axis is out of range, it
  190. // probably means the UI forgot to stop jogging and
  191. // ran GCODE that wrote a position to destination.
  192. // To prevent a disaster, stop jogging.
  193. flags.jogging = false;
  194. return;
  195. }
  196. norm_jog = destination;
  197. }
  198. }
  199. #endif
  200. bool isHeaterIdle(const extruder_t extruder) {
  201. #if HAS_HOTEND && HEATER_IDLE_HANDLER
  202. return thermalManager.heater_idle[extruder - E0].timed_out;
  203. #else
  204. UNUSED(extruder);
  205. return false;
  206. #endif
  207. }
  208. bool isHeaterIdle(const heater_t heater) {
  209. #if HEATER_IDLE_HANDLER
  210. switch (heater) {
  211. #if ENABLED(HAS_HEATED_BED)
  212. case BED: return thermalManager.heater_idle[thermalManager.IDLE_INDEX_BED].timed_out;
  213. #endif
  214. #if ENABLED(HAS_HEATED_CHAMBER)
  215. case CHAMBER: return false; // Chamber has no idle timer
  216. #endif
  217. default:
  218. return TERN0(HAS_HOTEND, thermalManager.heater_idle[heater - H0].timed_out);
  219. }
  220. #else
  221. UNUSED(heater);
  222. return false;
  223. #endif
  224. }
  225. #ifdef TOUCH_UI_LCD_TEMP_SCALING
  226. #define GET_TEMP_ADJUSTMENT(A) (float(A) / (TOUCH_UI_LCD_TEMP_SCALING))
  227. #else
  228. #define GET_TEMP_ADJUSTMENT(A) A
  229. #endif
  230. float getActualTemp_celsius(const heater_t heater) {
  231. switch (heater) {
  232. #if ENABLED(HAS_HEATED_BED)
  233. case BED: return GET_TEMP_ADJUSTMENT(thermalManager.degBed());
  234. #endif
  235. #if ENABLED(HAS_HEATED_CHAMBER)
  236. case CHAMBER: return GET_TEMP_ADJUSTMENT(thermalManager.degChamber());
  237. #endif
  238. default: return GET_TEMP_ADJUSTMENT(thermalManager.degHotend(heater - H0));
  239. }
  240. }
  241. float getActualTemp_celsius(const extruder_t extruder) {
  242. return GET_TEMP_ADJUSTMENT(thermalManager.degHotend(extruder - E0));
  243. }
  244. float getTargetTemp_celsius(const heater_t heater) {
  245. switch (heater) {
  246. #if ENABLED(HAS_HEATED_BED)
  247. case BED: return GET_TEMP_ADJUSTMENT(thermalManager.degTargetBed());
  248. #endif
  249. #if ENABLED(HAS_HEATED_CHAMBER)
  250. case CHAMBER: return GET_TEMP_ADJUSTMENT(thermalManager.degTargetChamber());
  251. #endif
  252. default: return GET_TEMP_ADJUSTMENT(thermalManager.degTargetHotend(heater - H0));
  253. }
  254. }
  255. float getTargetTemp_celsius(const extruder_t extruder) {
  256. return GET_TEMP_ADJUSTMENT(thermalManager.degTargetHotend(extruder - E0));
  257. }
  258. float getTargetFan_percent(const fan_t fan) {
  259. #if HAS_FAN
  260. return thermalManager.fanPercent(thermalManager.fan_speed[fan - FAN0]);
  261. #else
  262. UNUSED(fan);
  263. return 0;
  264. #endif
  265. }
  266. float getActualFan_percent(const fan_t fan) {
  267. #if HAS_FAN
  268. return thermalManager.fanPercent(thermalManager.scaledFanSpeed(fan - FAN0));
  269. #else
  270. UNUSED(fan);
  271. return 0;
  272. #endif
  273. }
  274. float getAxisPosition_mm(const axis_t axis) {
  275. return TERN0(JOYSTICK, flags.jogging) ? destination[axis] : current_position[axis];
  276. }
  277. float getAxisPosition_mm(const extruder_t extruder) {
  278. const extruder_t old_tool = getActiveTool();
  279. setActiveTool(extruder, true);
  280. const float epos = TERN0(JOYSTICK, flags.jogging) ? destination.e : current_position.e;
  281. setActiveTool(old_tool, true);
  282. return epos;
  283. }
  284. void setAxisPosition_mm(const_float_t position, const axis_t axis, const feedRate_t feedrate/*=0*/) {
  285. // Get motion limit from software endstops, if any
  286. float min, max;
  287. soft_endstop.get_manual_axis_limits((AxisEnum)axis, min, max);
  288. // Delta limits XY based on the current offset from center
  289. // This assumes the center is 0,0
  290. #if ENABLED(DELTA)
  291. if (axis != Z) {
  292. max = SQRT(sq(float(DELTA_PRINTABLE_RADIUS)) - sq(current_position[Y - axis])); // (Y - axis) == the other axis
  293. min = -max;
  294. }
  295. #endif
  296. current_position[axis] = constrain(position, min, max);
  297. line_to_current_position(feedrate ?: manual_feedrate_mm_s[axis]);
  298. }
  299. void setAxisPosition_mm(const_float_t position, const extruder_t extruder, const feedRate_t feedrate/*=0*/) {
  300. setActiveTool(extruder, true);
  301. current_position.e = position;
  302. line_to_current_position(feedrate ?: manual_feedrate_mm_s.e);
  303. }
  304. void setActiveTool(const extruder_t extruder, bool no_move) {
  305. #if HAS_MULTI_EXTRUDER
  306. const uint8_t e = extruder - E0;
  307. if (e != active_extruder) tool_change(e, no_move);
  308. active_extruder = e;
  309. #else
  310. UNUSED(extruder);
  311. UNUSED(no_move);
  312. #endif
  313. }
  314. extruder_t getTool(const uint8_t extruder) {
  315. switch (extruder) {
  316. case 7: return E7;
  317. case 6: return E6;
  318. case 5: return E5;
  319. case 4: return E4;
  320. case 3: return E3;
  321. case 2: return E2;
  322. case 1: return E1;
  323. default: return E0;
  324. }
  325. }
  326. extruder_t getActiveTool() { return getTool(active_extruder); }
  327. bool isMoving() { return planner.has_blocks_queued(); }
  328. bool canMove(const axis_t axis) {
  329. switch (axis) {
  330. #if IS_KINEMATIC || ENABLED(NO_MOTION_BEFORE_HOMING)
  331. case X: return axis_should_home(X_AXIS);
  332. case Y: return axis_should_home(Y_AXIS);
  333. case Z: return axis_should_home(Z_AXIS);
  334. #else
  335. case X: case Y: case Z: return true;
  336. #endif
  337. default: return false;
  338. }
  339. }
  340. bool canMove(const extruder_t extruder) {
  341. return !thermalManager.tooColdToExtrude(extruder - E0);
  342. }
  343. #if HAS_SOFTWARE_ENDSTOPS
  344. bool getSoftEndstopState() { return soft_endstop._enabled; }
  345. void setSoftEndstopState(const bool value) { soft_endstop._enabled = value; }
  346. #endif
  347. #if HAS_TRINAMIC_CONFIG
  348. float getAxisCurrent_mA(const axis_t axis) {
  349. switch (axis) {
  350. #if AXIS_IS_TMC(X)
  351. case X: return stepperX.getMilliamps();
  352. #endif
  353. #if AXIS_IS_TMC(X2)
  354. case X2: return stepperX2.getMilliamps();
  355. #endif
  356. #if AXIS_IS_TMC(Y)
  357. case Y: return stepperY.getMilliamps();
  358. #endif
  359. #if AXIS_IS_TMC(Y2)
  360. case Y2: return stepperY2.getMilliamps();
  361. #endif
  362. #if AXIS_IS_TMC(Z)
  363. case Z: return stepperZ.getMilliamps();
  364. #endif
  365. #if AXIS_IS_TMC(Z2)
  366. case Z2: return stepperZ2.getMilliamps();
  367. #endif
  368. default: return NAN;
  369. };
  370. }
  371. float getAxisCurrent_mA(const extruder_t extruder) {
  372. switch (extruder) {
  373. #if AXIS_IS_TMC(E0)
  374. case E0: return stepperE0.getMilliamps();
  375. #endif
  376. #if AXIS_IS_TMC(E1)
  377. case E1: return stepperE1.getMilliamps();
  378. #endif
  379. #if AXIS_IS_TMC(E2)
  380. case E2: return stepperE2.getMilliamps();
  381. #endif
  382. #if AXIS_IS_TMC(E3)
  383. case E3: return stepperE3.getMilliamps();
  384. #endif
  385. #if AXIS_IS_TMC(E4)
  386. case E4: return stepperE4.getMilliamps();
  387. #endif
  388. #if AXIS_IS_TMC(E5)
  389. case E5: return stepperE5.getMilliamps();
  390. #endif
  391. #if AXIS_IS_TMC(E6)
  392. case E6: return stepperE6.getMilliamps();
  393. #endif
  394. #if AXIS_IS_TMC(E7)
  395. case E7: return stepperE7.getMilliamps();
  396. #endif
  397. default: return NAN;
  398. };
  399. }
  400. void setAxisCurrent_mA(const_float_t mA, const axis_t axis) {
  401. switch (axis) {
  402. #if AXIS_IS_TMC(X)
  403. case X: stepperX.rms_current(constrain(mA, 400, 1500)); break;
  404. #endif
  405. #if AXIS_IS_TMC(X2)
  406. case X2: stepperX2.rms_current(constrain(mA, 400, 1500)); break;
  407. #endif
  408. #if AXIS_IS_TMC(Y)
  409. case Y: stepperY.rms_current(constrain(mA, 400, 1500)); break;
  410. #endif
  411. #if AXIS_IS_TMC(Y2)
  412. case Y2: stepperY2.rms_current(constrain(mA, 400, 1500)); break;
  413. #endif
  414. #if AXIS_IS_TMC(Z)
  415. case Z: stepperZ.rms_current(constrain(mA, 400, 1500)); break;
  416. #endif
  417. #if AXIS_IS_TMC(Z2)
  418. case Z2: stepperZ2.rms_current(constrain(mA, 400, 1500)); break;
  419. #endif
  420. default: break;
  421. };
  422. }
  423. void setAxisCurrent_mA(const_float_t mA, const extruder_t extruder) {
  424. switch (extruder) {
  425. #if AXIS_IS_TMC(E0)
  426. case E0: stepperE0.rms_current(constrain(mA, 400, 1500)); break;
  427. #endif
  428. #if AXIS_IS_TMC(E1)
  429. case E1: stepperE1.rms_current(constrain(mA, 400, 1500)); break;
  430. #endif
  431. #if AXIS_IS_TMC(E2)
  432. case E2: stepperE2.rms_current(constrain(mA, 400, 1500)); break;
  433. #endif
  434. #if AXIS_IS_TMC(E3)
  435. case E3: stepperE3.rms_current(constrain(mA, 400, 1500)); break;
  436. #endif
  437. #if AXIS_IS_TMC(E4)
  438. case E4: stepperE4.rms_current(constrain(mA, 400, 1500)); break;
  439. #endif
  440. #if AXIS_IS_TMC(E5)
  441. case E5: stepperE5.rms_current(constrain(mA, 400, 1500)); break;
  442. #endif
  443. #if AXIS_IS_TMC(E6)
  444. case E6: stepperE6.rms_current(constrain(mA, 400, 1500)); break;
  445. #endif
  446. #if AXIS_IS_TMC(E7)
  447. case E7: stepperE7.rms_current(constrain(mA, 400, 1500)); break;
  448. #endif
  449. default: break;
  450. };
  451. }
  452. int getTMCBumpSensitivity(const axis_t axis) {
  453. switch (axis) {
  454. #if ENABLED(X_SENSORLESS)
  455. case X: return stepperX.homing_threshold();
  456. #endif
  457. #if ENABLED(X2_SENSORLESS)
  458. case X2: return stepperX2.homing_threshold();
  459. #endif
  460. #if ENABLED(Y_SENSORLESS)
  461. case Y: return stepperY.homing_threshold();
  462. #endif
  463. #if ENABLED(Y2_SENSORLESS)
  464. case Y2: return stepperY2.homing_threshold();
  465. #endif
  466. #if ENABLED(Z_SENSORLESS)
  467. case Z: return stepperZ.homing_threshold();
  468. #endif
  469. #if ENABLED(Z2_SENSORLESS)
  470. case Z2: return stepperZ2.homing_threshold();
  471. #endif
  472. #if ENABLED(Z3_SENSORLESS)
  473. case Z3: return stepperZ3.homing_threshold();
  474. #endif
  475. #if ENABLED(Z4_SENSORLESS)
  476. case Z4: return stepperZ4.homing_threshold();
  477. #endif
  478. default: return 0;
  479. }
  480. }
  481. void setTMCBumpSensitivity(const_float_t value, const axis_t axis) {
  482. switch (axis) {
  483. #if X_SENSORLESS || Y_SENSORLESS || Z_SENSORLESS
  484. #if X_SENSORLESS
  485. case X: stepperX.homing_threshold(value); break;
  486. #endif
  487. #if X2_SENSORLESS
  488. case X2: stepperX2.homing_threshold(value); break;
  489. #endif
  490. #if Y_SENSORLESS
  491. case Y: stepperY.homing_threshold(value); break;
  492. #endif
  493. #if Y2_SENSORLESS
  494. case Y2: stepperY2.homing_threshold(value); break;
  495. #endif
  496. #if Z_SENSORLESS
  497. case Z: stepperZ.homing_threshold(value); break;
  498. #endif
  499. #if Z2_SENSORLESS
  500. case Z2: stepperZ2.homing_threshold(value); break;
  501. #endif
  502. #if Z3_SENSORLESS
  503. case Z3: stepperZ3.homing_threshold(value); break;
  504. #endif
  505. #if Z4_SENSORLESS
  506. case Z4: stepperZ4.homing_threshold(value); break;
  507. #endif
  508. #else
  509. UNUSED(value);
  510. #endif
  511. default: break;
  512. }
  513. }
  514. #endif
  515. float getAxisSteps_per_mm(const axis_t axis) {
  516. return planner.settings.axis_steps_per_mm[axis];
  517. }
  518. float getAxisSteps_per_mm(const extruder_t extruder) {
  519. UNUSED_E(extruder);
  520. return planner.settings.axis_steps_per_mm[E_AXIS_N(extruder - E0)];
  521. }
  522. void setAxisSteps_per_mm(const_float_t value, const axis_t axis) {
  523. planner.settings.axis_steps_per_mm[axis] = value;
  524. planner.refresh_positioning();
  525. }
  526. void setAxisSteps_per_mm(const_float_t value, const extruder_t extruder) {
  527. UNUSED_E(extruder);
  528. planner.settings.axis_steps_per_mm[E_AXIS_N(extruder - E0)] = value;
  529. planner.refresh_positioning();
  530. }
  531. feedRate_t getAxisMaxFeedrate_mm_s(const axis_t axis) {
  532. return planner.settings.max_feedrate_mm_s[axis];
  533. }
  534. feedRate_t getAxisMaxFeedrate_mm_s(const extruder_t extruder) {
  535. UNUSED_E(extruder);
  536. return planner.settings.max_feedrate_mm_s[E_AXIS_N(extruder - E0)];
  537. }
  538. void setAxisMaxFeedrate_mm_s(const feedRate_t value, const axis_t axis) {
  539. planner.set_max_feedrate(axis, value);
  540. }
  541. void setAxisMaxFeedrate_mm_s(const feedRate_t value, const extruder_t extruder) {
  542. UNUSED_E(extruder);
  543. planner.set_max_feedrate(E_AXIS_N(extruder - E0), value);
  544. }
  545. float getAxisMaxAcceleration_mm_s2(const axis_t axis) {
  546. return planner.settings.max_acceleration_mm_per_s2[axis];
  547. }
  548. float getAxisMaxAcceleration_mm_s2(const extruder_t extruder) {
  549. UNUSED_E(extruder);
  550. return planner.settings.max_acceleration_mm_per_s2[E_AXIS_N(extruder - E0)];
  551. }
  552. void setAxisMaxAcceleration_mm_s2(const_float_t value, const axis_t axis) {
  553. planner.set_max_acceleration(axis, value);
  554. }
  555. void setAxisMaxAcceleration_mm_s2(const_float_t value, const extruder_t extruder) {
  556. UNUSED_E(extruder);
  557. planner.set_max_acceleration(E_AXIS_N(extruder - E0), value);
  558. }
  559. #if HAS_FILAMENT_SENSOR
  560. bool getFilamentRunoutEnabled() { return runout.enabled; }
  561. void setFilamentRunoutEnabled(const bool value) { runout.enabled = value; }
  562. bool getFilamentRunoutState() { return runout.filament_ran_out; }
  563. void setFilamentRunoutState(const bool value) { runout.filament_ran_out = value; }
  564. #if HAS_FILAMENT_RUNOUT_DISTANCE
  565. float getFilamentRunoutDistance_mm() { return runout.runout_distance(); }
  566. void setFilamentRunoutDistance_mm(const_float_t value) { runout.set_runout_distance(constrain(value, 0, 999)); }
  567. #endif
  568. #endif
  569. #if ENABLED(CASE_LIGHT_ENABLE)
  570. bool getCaseLightState() { return caselight.on; }
  571. void setCaseLightState(const bool value) {
  572. caselight.on = value;
  573. caselight.update_enabled();
  574. }
  575. #if CASELIGHT_USES_BRIGHTNESS
  576. float getCaseLightBrightness_percent() { return ui8_to_percent(caselight.brightness); }
  577. void setCaseLightBrightness_percent(const_float_t value) {
  578. caselight.brightness = map(constrain(value, 0, 100), 0, 100, 0, 255);
  579. caselight.update_brightness();
  580. }
  581. #endif
  582. #endif
  583. #if ENABLED(LIN_ADVANCE)
  584. float getLinearAdvance_mm_mm_s(const extruder_t extruder) {
  585. return (extruder < EXTRUDERS) ? planner.extruder_advance_K[extruder - E0] : 0;
  586. }
  587. void setLinearAdvance_mm_mm_s(const_float_t value, const extruder_t extruder) {
  588. if (extruder < EXTRUDERS)
  589. planner.extruder_advance_K[extruder - E0] = constrain(value, 0, 10);
  590. }
  591. #endif
  592. #if HAS_JUNCTION_DEVIATION
  593. float getJunctionDeviation_mm() {
  594. return planner.junction_deviation_mm;
  595. }
  596. void setJunctionDeviation_mm(const_float_t value) {
  597. planner.junction_deviation_mm = constrain(value, 0.001, 0.3);
  598. TERN_(LIN_ADVANCE, planner.recalculate_max_e_jerk());
  599. }
  600. #else
  601. float getAxisMaxJerk_mm_s(const axis_t axis) { return planner.max_jerk[axis]; }
  602. float getAxisMaxJerk_mm_s(const extruder_t) { return planner.max_jerk.e; }
  603. void setAxisMaxJerk_mm_s(const_float_t value, const axis_t axis) { planner.set_max_jerk((AxisEnum)axis, value); }
  604. void setAxisMaxJerk_mm_s(const_float_t value, const extruder_t) { planner.set_max_jerk(E_AXIS, value); }
  605. #endif
  606. #if ENABLED(DUAL_X_CARRIAGE)
  607. uint8_t getIDEX_Mode() { return dual_x_carriage_mode; }
  608. #endif
  609. #if PREHEAT_COUNT
  610. uint16_t getMaterial_preset_E(const uint16_t index) { return ui.material_preset[index].hotend_temp; }
  611. #if HAS_HEATED_BED
  612. uint16_t getMaterial_preset_B(const uint16_t index) { return ui.material_preset[index].bed_temp; }
  613. #endif
  614. #endif
  615. feedRate_t getFeedrate_mm_s() { return feedrate_mm_s; }
  616. int16_t getFlowPercentage(const extruder_t extr) { return planner.flow_percentage[extr]; }
  617. feedRate_t getMinFeedrate_mm_s() { return planner.settings.min_feedrate_mm_s; }
  618. feedRate_t getMinTravelFeedrate_mm_s() { return planner.settings.min_travel_feedrate_mm_s; }
  619. float getPrintingAcceleration_mm_s2() { return planner.settings.acceleration; }
  620. float getRetractAcceleration_mm_s2() { return planner.settings.retract_acceleration; }
  621. float getTravelAcceleration_mm_s2() { return planner.settings.travel_acceleration; }
  622. void setFeedrate_mm_s(const feedRate_t fr) { feedrate_mm_s = fr; }
  623. void setFlow_percent(const int16_t flow, const extruder_t extr) { planner.set_flow(extr, flow); }
  624. void setMinFeedrate_mm_s(const feedRate_t fr) { planner.settings.min_feedrate_mm_s = fr; }
  625. void setMinTravelFeedrate_mm_s(const feedRate_t fr) { planner.settings.min_travel_feedrate_mm_s = fr; }
  626. void setPrintingAcceleration_mm_s2(const_float_t acc) { planner.settings.acceleration = acc; }
  627. void setRetractAcceleration_mm_s2(const_float_t acc) { planner.settings.retract_acceleration = acc; }
  628. void setTravelAcceleration_mm_s2(const_float_t acc) { planner.settings.travel_acceleration = acc; }
  629. #if ENABLED(BABYSTEPPING)
  630. bool babystepAxis_steps(const int16_t steps, const axis_t axis) {
  631. switch (axis) {
  632. #if ENABLED(BABYSTEP_XY)
  633. case X: babystep.add_steps(X_AXIS, steps); break;
  634. case Y: babystep.add_steps(Y_AXIS, steps); break;
  635. #endif
  636. case Z: babystep.add_steps(Z_AXIS, steps); break;
  637. default: return false;
  638. };
  639. return true;
  640. }
  641. /**
  642. * This function adjusts an axis during a print.
  643. *
  644. * When linked_nozzles is false, each nozzle in a multi-nozzle
  645. * printer can be babystepped independently of the others. This
  646. * lets the user to fine tune the Z-offset and Nozzle Offsets
  647. * while observing the first layer of a print, regardless of
  648. * what nozzle is printing.
  649. */
  650. void smartAdjustAxis_steps(const int16_t steps, const axis_t axis, bool linked_nozzles) {
  651. const float mm = steps * planner.steps_to_mm[axis];
  652. UNUSED(mm);
  653. if (!babystepAxis_steps(steps, axis)) return;
  654. #if ENABLED(BABYSTEP_ZPROBE_OFFSET)
  655. // Make it so babystepping in Z adjusts the Z probe offset.
  656. if (axis == Z && TERN1(HAS_MULTI_EXTRUDER, (linked_nozzles || active_extruder == 0)))
  657. probe.offset.z += mm;
  658. #endif
  659. #if HAS_MULTI_EXTRUDER && HAS_HOTEND_OFFSET
  660. /**
  661. * When linked_nozzles is false, as an axis is babystepped
  662. * adjust the hotend offsets so that the other nozzles are
  663. * unaffected by the babystepping of the active nozzle.
  664. */
  665. if (!linked_nozzles) {
  666. HOTEND_LOOP()
  667. if (e != active_extruder)
  668. hotend_offset[e][axis] += mm;
  669. normalizeNozzleOffset(X);
  670. normalizeNozzleOffset(Y);
  671. normalizeNozzleOffset(Z);
  672. }
  673. #else
  674. UNUSED(linked_nozzles);
  675. #endif
  676. }
  677. /**
  678. * Converts a mm displacement to a number of whole number of
  679. * steps that is at least mm long.
  680. */
  681. int16_t mmToWholeSteps(const_float_t mm, const axis_t axis) {
  682. const float steps = mm / planner.steps_to_mm[axis];
  683. return steps > 0 ? CEIL(steps) : FLOOR(steps);
  684. }
  685. #endif // BABYSTEPPING
  686. float getZOffset_mm() {
  687. return (0.0f
  688. #if HAS_BED_PROBE
  689. + probe.offset.z
  690. #elif ENABLED(BABYSTEP_DISPLAY_TOTAL)
  691. + planner.steps_to_mm[Z_AXIS] * babystep.axis_total[BS_AXIS_IND(Z_AXIS)]
  692. #endif
  693. );
  694. }
  695. void setZOffset_mm(const_float_t value) {
  696. #if HAS_BED_PROBE
  697. if (WITHIN(value, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX))
  698. probe.offset.z = value;
  699. #elif ENABLED(BABYSTEP_DISPLAY_TOTAL)
  700. babystep.add_mm(Z_AXIS, value - getZOffset_mm());
  701. #else
  702. UNUSED(value);
  703. #endif
  704. }
  705. #if HAS_HOTEND_OFFSET
  706. float getNozzleOffset_mm(const axis_t axis, const extruder_t extruder) {
  707. if (extruder - E0 >= HOTENDS) return 0;
  708. return hotend_offset[extruder - E0][axis];
  709. }
  710. void setNozzleOffset_mm(const_float_t value, const axis_t axis, const extruder_t extruder) {
  711. if (extruder - E0 >= HOTENDS) return;
  712. hotend_offset[extruder - E0][axis] = value;
  713. }
  714. /**
  715. * The UI should call this if needs to guarantee the first
  716. * nozzle offset is zero (such as when it doesn't allow the
  717. * user to edit the offset the first nozzle).
  718. */
  719. void normalizeNozzleOffset(const axis_t axis) {
  720. const float offs = hotend_offset[0][axis];
  721. HOTEND_LOOP() hotend_offset[e][axis] -= offs;
  722. }
  723. #endif // HAS_HOTEND_OFFSET
  724. #if HAS_BED_PROBE
  725. float getProbeOffset_mm(const axis_t axis) { return probe.offset.pos[axis]; }
  726. void setProbeOffset_mm(const_float_t val, const axis_t axis) { probe.offset.pos[axis] = val; }
  727. #endif
  728. #if ENABLED(BACKLASH_GCODE)
  729. float getAxisBacklash_mm(const axis_t axis) { return backlash.distance_mm[axis]; }
  730. void setAxisBacklash_mm(const_float_t value, const axis_t axis)
  731. { backlash.distance_mm[axis] = constrain(value,0,5); }
  732. float getBacklashCorrection_percent() { return ui8_to_percent(backlash.correction); }
  733. void setBacklashCorrection_percent(const_float_t value) { backlash.correction = map(constrain(value, 0, 100), 0, 100, 0, 255); }
  734. #ifdef BACKLASH_SMOOTHING_MM
  735. float getBacklashSmoothing_mm() { return backlash.smoothing_mm; }
  736. void setBacklashSmoothing_mm(const_float_t value) { backlash.smoothing_mm = constrain(value, 0, 999); }
  737. #endif
  738. #endif
  739. uint32_t getProgress_seconds_elapsed() {
  740. const duration_t elapsed = print_job_timer.duration();
  741. return elapsed.value;
  742. }
  743. #if HAS_LEVELING
  744. bool getLevelingActive() { return planner.leveling_active; }
  745. void setLevelingActive(const bool state) { set_bed_leveling_enabled(state); }
  746. bool getMeshValid() { return leveling_is_valid(); }
  747. #if HAS_MESH
  748. bed_mesh_t& getMeshArray() { return Z_VALUES_ARR; }
  749. float getMeshPoint(const xy_uint8_t &pos) { return Z_VALUES(pos.x, pos.y); }
  750. void setMeshPoint(const xy_uint8_t &pos, const_float_t zoff) {
  751. if (WITHIN(pos.x, 0, (GRID_MAX_POINTS_X) - 1) && WITHIN(pos.y, 0, (GRID_MAX_POINTS_Y) - 1)) {
  752. Z_VALUES(pos.x, pos.y) = zoff;
  753. TERN_(ABL_BILINEAR_SUBDIVISION, bed_level_virt_interpolate());
  754. }
  755. }
  756. void moveToMeshPoint(const xy_uint8_t &pos, const_float_t z) {
  757. #if EITHER(MESH_BED_LEVELING, AUTO_BED_LEVELING_UBL)
  758. const feedRate_t old_feedrate = feedrate_mm_s;
  759. const float x_target = MESH_MIN_X + pos.x * (MESH_X_DIST),
  760. y_target = MESH_MIN_Y + pos.y * (MESH_Y_DIST);
  761. if (x_target != current_position[X_AXIS] || y_target != current_position[Y_AXIS]) {
  762. // If moving across bed, raise nozzle to safe height over bed
  763. feedrate_mm_s = Z_PROBE_FEEDRATE_FAST;
  764. destination = current_position;
  765. destination[Z_AXIS] = Z_CLEARANCE_BETWEEN_PROBES;
  766. prepare_line_to_destination();
  767. feedrate_mm_s = XY_PROBE_FEEDRATE;
  768. destination[X_AXIS] = x_target;
  769. destination[Y_AXIS] = y_target;
  770. prepare_line_to_destination();
  771. }
  772. feedrate_mm_s = Z_PROBE_FEEDRATE_FAST;
  773. destination[Z_AXIS] = z;
  774. prepare_line_to_destination();
  775. feedrate_mm_s = old_feedrate;
  776. #else
  777. UNUSED(pos);
  778. UNUSED(z);
  779. #endif
  780. }
  781. #endif // HAS_MESH
  782. #endif // HAS_LEVELING
  783. #if ENABLED(HOST_PROMPT_SUPPORT)
  784. void setHostResponse(const uint8_t response) { host_response_handler(response); }
  785. #endif
  786. #if ENABLED(PRINTCOUNTER)
  787. char* getFailedPrints_str(char buffer[21]) { strcpy(buffer,i16tostr3left(print_job_timer.getStats().totalPrints - print_job_timer.getStats().finishedPrints)); return buffer; }
  788. char* getTotalPrints_str(char buffer[21]) { strcpy(buffer,i16tostr3left(print_job_timer.getStats().totalPrints)); return buffer; }
  789. char* getFinishedPrints_str(char buffer[21]) { strcpy(buffer,i16tostr3left(print_job_timer.getStats().finishedPrints)); return buffer; }
  790. char* getTotalPrintTime_str(char buffer[21]) { return duration_t(print_job_timer.getStats().printTime).toString(buffer); }
  791. char* getLongestPrint_str(char buffer[21]) { return duration_t(print_job_timer.getStats().longestPrint).toString(buffer); }
  792. char* getFilamentUsed_str(char buffer[21]) {
  793. printStatistics stats = print_job_timer.getStats();
  794. sprintf_P(buffer, PSTR("%ld.%im"), long(stats.filamentUsed / 1000), int16_t(stats.filamentUsed / 100) % 10);
  795. return buffer;
  796. }
  797. #endif
  798. float getFeedrate_percent() { return feedrate_percentage; }
  799. #if ENABLED(PIDTEMP)
  800. float getPIDValues_Kp(const extruder_t tool) { return PID_PARAM(Kp, tool); }
  801. float getPIDValues_Ki(const extruder_t tool) { return unscalePID_i(PID_PARAM(Ki, tool)); }
  802. float getPIDValues_Kd(const extruder_t tool) { return unscalePID_d(PID_PARAM(Kd, tool)); }
  803. void setPIDValues(const_float_t p, const_float_t i, const_float_t d, extruder_t tool) {
  804. thermalManager.temp_hotend[tool].pid.Kp = p;
  805. thermalManager.temp_hotend[tool].pid.Ki = scalePID_i(i);
  806. thermalManager.temp_hotend[tool].pid.Kd = scalePID_d(d);
  807. thermalManager.updatePID();
  808. }
  809. void startPIDTune(const_float_t temp, extruder_t tool) {
  810. thermalManager.PID_autotune(temp, (heater_id_t)tool, 8, true);
  811. }
  812. #endif
  813. #if ENABLED(PIDTEMPBED)
  814. float getBedPIDValues_Kp() { return thermalManager.temp_bed.pid.Kp; }
  815. float getBedPIDValues_Ki() { return unscalePID_i(thermalManager.temp_bed.pid.Ki); }
  816. float getBedPIDValues_Kd() { return unscalePID_d(thermalManager.temp_bed.pid.Kd); }
  817. void setBedPIDValues(const_float_t p, const_float_t i, const_float_t d) {
  818. thermalManager.temp_bed.pid.Kp = p;
  819. thermalManager.temp_bed.pid.Ki = scalePID_i(i);
  820. thermalManager.temp_bed.pid.Kd = scalePID_d(d);
  821. thermalManager.updatePID();
  822. }
  823. void startBedPIDTune(const_float_t temp) {
  824. thermalManager.PID_autotune(temp, H_BED, 4, true);
  825. }
  826. #endif
  827. void injectCommands_P(PGM_P const gcode) { queue.inject_P(gcode); }
  828. void injectCommands(char * const gcode) { queue.inject(gcode); }
  829. bool commandsInQueue() { return (planner.movesplanned() || queue.has_commands_queued()); }
  830. bool isAxisPositionKnown(const axis_t axis) { return axis_is_trusted((AxisEnum)axis); }
  831. bool isAxisPositionKnown(const extruder_t) { return axis_is_trusted(E_AXIS); }
  832. bool isPositionKnown() { return all_axes_trusted(); }
  833. bool isMachineHomed() { return all_axes_homed(); }
  834. PGM_P getFirmwareName_str() {
  835. static PGMSTR(firmware_name, "Marlin " SHORT_BUILD_VERSION);
  836. return firmware_name;
  837. }
  838. void setTargetTemp_celsius(const_float_t inval, const heater_t heater) {
  839. float value = inval;
  840. #ifdef TOUCH_UI_LCD_TEMP_SCALING
  841. value *= TOUCH_UI_LCD_TEMP_SCALING;
  842. #endif
  843. enableHeater(heater);
  844. switch (heater) {
  845. #if HAS_HEATED_CHAMBER
  846. case CHAMBER: thermalManager.setTargetChamber(LROUND(constrain(value, 0, CHAMBER_MAX_TARGET))); break;
  847. #endif
  848. #if HAS_COOLER
  849. case COOLER: thermalManager.setTargetCooler(LROUND(constrain(value, 0, COOLER_MAXTEMP))); break;
  850. #endif
  851. #if HAS_HEATED_BED
  852. case BED: thermalManager.setTargetBed(LROUND(constrain(value, 0, BED_MAX_TARGET))); break;
  853. #endif
  854. default: {
  855. #if HAS_HOTEND
  856. const int16_t e = heater - H0;
  857. thermalManager.setTargetHotend(LROUND(constrain(value, 0, thermalManager.hotend_max_target(e))), e);
  858. #endif
  859. } break;
  860. }
  861. }
  862. void setTargetTemp_celsius(const_float_t inval, const extruder_t extruder) {
  863. float value = inval;
  864. #ifdef TOUCH_UI_LCD_TEMP_SCALING
  865. value *= TOUCH_UI_LCD_TEMP_SCALING;
  866. #endif
  867. #if HAS_HOTEND
  868. const int16_t e = extruder - E0;
  869. enableHeater(extruder);
  870. thermalManager.setTargetHotend(LROUND(constrain(value, 0, thermalManager.hotend_max_target(e))), e);
  871. #endif
  872. }
  873. void setTargetFan_percent(const_float_t value, const fan_t fan) {
  874. #if HAS_FAN
  875. if (fan < FAN_COUNT)
  876. thermalManager.set_fan_speed(fan - FAN0, map(constrain(value, 0, 100), 0, 100, 0, 255));
  877. #else
  878. UNUSED(value);
  879. UNUSED(fan);
  880. #endif
  881. }
  882. void setFeedrate_percent(const_float_t value) { feedrate_percentage = constrain(value, 10, 500); }
  883. void coolDown() {
  884. #if HAS_HOTEND
  885. HOTEND_LOOP() thermalManager.setTargetHotend(0, e);
  886. #endif
  887. TERN_(HAS_HEATED_BED, thermalManager.setTargetBed(0));
  888. TERN_(HAS_FAN, thermalManager.zero_fan_speeds());
  889. }
  890. bool awaitingUserConfirm() { return wait_for_user; }
  891. void setUserConfirmed() { TERN_(HAS_RESUME_CONTINUE, wait_for_user = false); }
  892. void printFile(const char *filename) {
  893. UNUSED(filename);
  894. TERN_(SDSUPPORT, card.openAndPrintFile(filename));
  895. }
  896. bool isPrintingFromMediaPaused() {
  897. return TERN0(SDSUPPORT, isPrintingFromMedia() && !IS_SD_PRINTING());
  898. }
  899. bool isPrintingFromMedia() {
  900. #if ENABLED(SDSUPPORT)
  901. // Account for when IS_SD_PRINTING() reports the end of the
  902. // print when there is still SD card data in the planner.
  903. flags.was_sd_printing = card.isFileOpen() || (flags.was_sd_printing && commandsInQueue());
  904. return flags.was_sd_printing;
  905. #else
  906. return false;
  907. #endif
  908. }
  909. bool isPrinting() {
  910. return (commandsInQueue() || isPrintingFromMedia() || TERN0(SDSUPPORT, IS_SD_PRINTING())) || print_job_timer.isRunning() || print_job_timer.isPaused();
  911. }
  912. bool isPrintingPaused() {
  913. return isPrinting() && (isPrintingFromMediaPaused() || print_job_timer.isPaused());
  914. }
  915. bool isMediaInserted() { return TERN0(SDSUPPORT, IS_SD_INSERTED() && card.isMounted()); }
  916. void pausePrint() { ui.pause_print(); }
  917. void resumePrint() { ui.resume_print(); }
  918. void stopPrint() { ui.abort_print(); }
  919. void onUserConfirmRequired_P(PGM_P const pstr) {
  920. char msg[strlen_P(pstr) + 1];
  921. strcpy_P(msg, pstr);
  922. onUserConfirmRequired(msg);
  923. }
  924. void onStatusChanged_P(PGM_P const pstr) {
  925. char msg[strlen_P(pstr) + 1];
  926. strcpy_P(msg, pstr);
  927. onStatusChanged(msg);
  928. }
  929. FileList::FileList() { refresh(); }
  930. void FileList::refresh() { num_files = 0xFFFF; }
  931. bool FileList::seek(const uint16_t pos, const bool skip_range_check) {
  932. #if ENABLED(SDSUPPORT)
  933. if (!skip_range_check && (pos + 1) > count()) return false;
  934. card.getfilename_sorted(SD_ORDER(pos, count()));
  935. return card.filename[0] != '\0';
  936. #else
  937. UNUSED(pos);
  938. UNUSED(skip_range_check);
  939. return false;
  940. #endif
  941. }
  942. const char* FileList::filename() {
  943. return TERN(SDSUPPORT, card.longest_filename(), "");
  944. }
  945. const char* FileList::shortFilename() {
  946. return TERN(SDSUPPORT, card.filename, "");
  947. }
  948. const char* FileList::longFilename() {
  949. return TERN(SDSUPPORT, card.longFilename, "");
  950. }
  951. bool FileList::isDir() {
  952. return TERN0(SDSUPPORT, card.flag.filenameIsDir);
  953. }
  954. uint16_t FileList::count() {
  955. return TERN0(SDSUPPORT, (num_files = (num_files == 0xFFFF ? card.get_num_Files() : num_files)));
  956. }
  957. bool FileList::isAtRootDir() {
  958. return TERN1(SDSUPPORT, card.flag.workDirIsRoot);
  959. }
  960. void FileList::upDir() {
  961. #if ENABLED(SDSUPPORT)
  962. card.cdup();
  963. num_files = 0xFFFF;
  964. #endif
  965. }
  966. void FileList::changeDir(const char * const dirname) {
  967. #if ENABLED(SDSUPPORT)
  968. card.cd(dirname);
  969. num_files = 0xFFFF;
  970. #else
  971. UNUSED(dirname);
  972. #endif
  973. }
  974. } // namespace ExtUI
  975. // At the moment we hook into MarlinUI methods, but this could be cleaned up in the future
  976. void MarlinUI::init() { ExtUI::onStartup(); }
  977. void MarlinUI::update() { ExtUI::onIdle(); }
  978. void MarlinUI::kill_screen(PGM_P const error, PGM_P const component) {
  979. using namespace ExtUI;
  980. if (!flags.printer_killed) {
  981. flags.printer_killed = true;
  982. onPrinterKilled(error, component);
  983. }
  984. }
  985. #endif // EXTENSIBLE_UI