My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "ultralcd.h"
  23. #if ENABLED(ULTRA_LCD)
  24. #include "Marlin.h"
  25. #include "language.h"
  26. #include "cardreader.h"
  27. #include "temperature.h"
  28. #include "stepper.h"
  29. #include "configuration_store.h"
  30. int plaPreheatHotendTemp;
  31. int plaPreheatHPBTemp;
  32. int plaPreheatFanSpeed;
  33. int absPreheatHotendTemp;
  34. int absPreheatHPBTemp;
  35. int absPreheatFanSpeed;
  36. #if ENABLED(FILAMENT_LCD_DISPLAY)
  37. millis_t previous_lcd_status_ms = 0;
  38. #endif
  39. uint8_t lcd_status_message_level;
  40. char lcd_status_message[3 * (LCD_WIDTH) + 1] = WELCOME_MSG; // worst case is kana with up to 3*LCD_WIDTH+1
  41. #if ENABLED(DOGLCD)
  42. #include "dogm_lcd_implementation.h"
  43. #else
  44. #include "ultralcd_implementation_hitachi_HD44780.h"
  45. #endif
  46. // The main status screen
  47. static void lcd_status_screen();
  48. millis_t next_lcd_update_ms;
  49. enum LCDViewAction {
  50. LCDVIEW_NONE,
  51. LCDVIEW_REDRAW_NOW,
  52. LCDVIEW_CALL_REDRAW_NEXT,
  53. LCDVIEW_CLEAR_CALL_REDRAW,
  54. LCDVIEW_CALL_NO_REDRAW
  55. };
  56. uint8_t lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; // Set when the LCD needs to draw, decrements after every draw. Set to 2 in LCD routines so the LCD gets at least 1 full redraw (first redraw is partial)
  57. #if ENABLED(ULTIPANEL)
  58. // place-holders for Ki and Kd edits
  59. float raw_Ki, raw_Kd;
  60. /**
  61. * REVERSE_MENU_DIRECTION
  62. *
  63. * To reverse the menu direction we need a general way to reverse
  64. * the direction of the encoder everywhere. So encoderDirection is
  65. * added to allow the encoder to go the other way.
  66. *
  67. * This behavior is limited to scrolling Menus and SD card listings,
  68. * and is disabled in other contexts.
  69. */
  70. #if ENABLED(REVERSE_MENU_DIRECTION)
  71. int8_t encoderDirection = 1;
  72. #define ENCODER_DIRECTION_NORMAL() (encoderDirection = 1)
  73. #define ENCODER_DIRECTION_MENUS() (encoderDirection = -1)
  74. #else
  75. #define ENCODER_DIRECTION_NORMAL() ;
  76. #define ENCODER_DIRECTION_MENUS() ;
  77. #endif
  78. int8_t encoderDiff; // updated from interrupt context and added to encoderPosition every LCD update
  79. int8_t manual_move_axis = (int8_t)NO_AXIS;
  80. millis_t manual_move_start_time = 0;
  81. bool encoderRateMultiplierEnabled;
  82. int32_t lastEncoderMovementMillis;
  83. #if HAS_POWER_SWITCH
  84. extern bool powersupply;
  85. #endif
  86. const float manual_feedrate[] = MANUAL_FEEDRATE;
  87. static void lcd_main_menu();
  88. static void lcd_tune_menu();
  89. static void lcd_prepare_menu();
  90. static void lcd_move_menu();
  91. static void lcd_control_menu();
  92. static void lcd_control_temperature_menu();
  93. static void lcd_control_temperature_preheat_pla_settings_menu();
  94. static void lcd_control_temperature_preheat_abs_settings_menu();
  95. static void lcd_control_motion_menu();
  96. static void lcd_control_volumetric_menu();
  97. #if HAS_LCD_CONTRAST
  98. static void lcd_set_contrast();
  99. #endif
  100. #if ENABLED(FWRETRACT)
  101. static void lcd_control_retract_menu();
  102. #endif
  103. #if ENABLED(DELTA_CALIBRATION_MENU)
  104. static void lcd_delta_calibrate_menu();
  105. #endif
  106. #if ENABLED(MANUAL_BED_LEVELING)
  107. #include "mesh_bed_leveling.h"
  108. #endif
  109. // Function pointer to menu functions.
  110. typedef void (*screenFunc_t)();
  111. // Different types of actions that can be used in menu items.
  112. static void menu_action_back();
  113. static void menu_action_submenu(screenFunc_t data);
  114. static void menu_action_gcode(const char* pgcode);
  115. static void menu_action_function(screenFunc_t data);
  116. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr);
  117. static void menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  118. static void menu_action_setting_edit_float3(const char* pstr, float* ptr, float minValue, float maxValue);
  119. static void menu_action_setting_edit_float32(const char* pstr, float* ptr, float minValue, float maxValue);
  120. static void menu_action_setting_edit_float43(const char* pstr, float* ptr, float minValue, float maxValue);
  121. static void menu_action_setting_edit_float5(const char* pstr, float* ptr, float minValue, float maxValue);
  122. static void menu_action_setting_edit_float51(const char* pstr, float* ptr, float minValue, float maxValue);
  123. static void menu_action_setting_edit_float52(const char* pstr, float* ptr, float minValue, float maxValue);
  124. static void menu_action_setting_edit_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue);
  125. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, screenFunc_t callbackFunc);
  126. static void menu_action_setting_edit_callback_int3(const char* pstr, int* ptr, int minValue, int maxValue, screenFunc_t callbackFunc);
  127. static void menu_action_setting_edit_callback_float3(const char* pstr, float* ptr, float minValue, float maxValue, screenFunc_t callbackFunc);
  128. static void menu_action_setting_edit_callback_float32(const char* pstr, float* ptr, float minValue, float maxValue, screenFunc_t callbackFunc);
  129. static void menu_action_setting_edit_callback_float43(const char* pstr, float* ptr, float minValue, float maxValue, screenFunc_t callbackFunc);
  130. static void menu_action_setting_edit_callback_float5(const char* pstr, float* ptr, float minValue, float maxValue, screenFunc_t callbackFunc);
  131. static void menu_action_setting_edit_callback_float51(const char* pstr, float* ptr, float minValue, float maxValue, screenFunc_t callbackFunc);
  132. static void menu_action_setting_edit_callback_float52(const char* pstr, float* ptr, float minValue, float maxValue, screenFunc_t callbackFunc);
  133. static void menu_action_setting_edit_callback_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue, screenFunc_t callbackFunc);
  134. #if ENABLED(SDSUPPORT)
  135. static void lcd_sdcard_menu();
  136. static void menu_action_sdfile(const char* filename, char* longFilename);
  137. static void menu_action_sddirectory(const char* filename, char* longFilename);
  138. #endif
  139. #define ENCODER_FEEDRATE_DEADZONE 10
  140. #if DISABLED(LCD_I2C_VIKI)
  141. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  142. #define ENCODER_STEPS_PER_MENU_ITEM 5
  143. #endif
  144. #ifndef ENCODER_PULSES_PER_STEP
  145. #define ENCODER_PULSES_PER_STEP 1
  146. #endif
  147. #else
  148. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  149. #define ENCODER_STEPS_PER_MENU_ITEM 2 // VIKI LCD rotary encoder uses a different number of steps per rotation
  150. #endif
  151. #ifndef ENCODER_PULSES_PER_STEP
  152. #define ENCODER_PULSES_PER_STEP 1
  153. #endif
  154. #endif
  155. /* Helper macros for menus */
  156. /**
  157. * START_MENU generates the init code for a menu function
  158. */
  159. #define START_MENU() do { \
  160. ENCODER_DIRECTION_MENUS(); \
  161. encoderRateMultiplierEnabled = false; \
  162. if (encoderPosition > 0x8000) encoderPosition = 0; \
  163. uint8_t encoderLine = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM; \
  164. NOMORE(currentMenuViewOffset, encoderLine); \
  165. uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
  166. bool wasClicked = LCD_CLICKED, itemSelected; \
  167. for (uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \
  168. _menuItemNr = 0;
  169. /**
  170. * MENU_ITEM generates draw & handler code for a menu item, potentially calling:
  171. *
  172. * lcd_implementation_drawmenu_[type](sel, row, label, arg3...)
  173. * menu_action_[type](arg3...)
  174. *
  175. * Examples:
  176. * MENU_ITEM(back, MSG_WATCH)
  177. * lcd_implementation_drawmenu_back(sel, row, PSTR(MSG_WATCH))
  178. * menu_action_back()
  179. *
  180. * MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause)
  181. * lcd_implementation_drawmenu_function(sel, row, PSTR(MSG_PAUSE_PRINT), lcd_sdcard_pause)
  182. * menu_action_function(lcd_sdcard_pause)
  183. *
  184. * MENU_ITEM_EDIT(int3, MSG_SPEED, &feedrate_multiplier, 10, 999)
  185. * MENU_ITEM(setting_edit_int3, MSG_SPEED, PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  186. * lcd_implementation_drawmenu_setting_edit_int3(sel, row, PSTR(MSG_SPEED), PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  187. * menu_action_setting_edit_int3(PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  188. *
  189. */
  190. #define _MENU_ITEM_PART_1(type, label, args...) \
  191. if (_menuItemNr == _lineNr) { \
  192. itemSelected = encoderLine == _menuItemNr; \
  193. if (lcdDrawUpdate) \
  194. lcd_implementation_drawmenu_ ## type(itemSelected, _drawLineNr, PSTR(label), ## args); \
  195. if (wasClicked && itemSelected) { \
  196. lcd_quick_feedback()
  197. #define _MENU_ITEM_PART_2(type, args...) \
  198. menu_action_ ## type(args); \
  199. return; \
  200. } \
  201. } \
  202. _menuItemNr++
  203. #define MENU_ITEM(type, label, args...) do { \
  204. _MENU_ITEM_PART_1(type, label, ## args); \
  205. _MENU_ITEM_PART_2(type, ## args); \
  206. } while(0)
  207. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  208. //#define ENCODER_RATE_MULTIPLIER_DEBUG // If defined, output the encoder steps per second value
  209. /**
  210. * MENU_MULTIPLIER_ITEM generates drawing and handling code for a multiplier menu item
  211. */
  212. #define MENU_MULTIPLIER_ITEM(type, label, args...) do { \
  213. _MENU_ITEM_PART_1(type, label, ## args); \
  214. encoderRateMultiplierEnabled = true; \
  215. lastEncoderMovementMillis = 0; \
  216. _MENU_ITEM_PART_2(type, ## args); \
  217. } while(0)
  218. #endif //ENCODER_RATE_MULTIPLIER
  219. #define MENU_ITEM_DUMMY() do { _menuItemNr++; } while(0)
  220. #define MENU_ITEM_EDIT(type, label, args...) MENU_ITEM(setting_edit_ ## type, label, PSTR(label), ## args)
  221. #define MENU_ITEM_EDIT_CALLBACK(type, label, args...) MENU_ITEM(setting_edit_callback_ ## type, label, PSTR(label), ## args)
  222. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  223. #define MENU_MULTIPLIER_ITEM_EDIT(type, label, args...) MENU_MULTIPLIER_ITEM(setting_edit_ ## type, label, PSTR(label), ## args)
  224. #define MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(type, label, args...) MENU_MULTIPLIER_ITEM(setting_edit_callback_ ## type, label, PSTR(label), ## args)
  225. #else //!ENCODER_RATE_MULTIPLIER
  226. #define MENU_MULTIPLIER_ITEM_EDIT(type, label, args...) MENU_ITEM(setting_edit_ ## type, label, PSTR(label), ## args)
  227. #define MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(type, label, args...) MENU_ITEM(setting_edit_callback_ ## type, label, PSTR(label), ## args)
  228. #endif //!ENCODER_RATE_MULTIPLIER
  229. #define END_MENU() \
  230. if (encoderLine >= _menuItemNr) { encoderPosition = _menuItemNr * (ENCODER_STEPS_PER_MENU_ITEM) - 1; encoderLine = _menuItemNr - 1; }\
  231. if (encoderLine >= currentMenuViewOffset + LCD_HEIGHT) { currentMenuViewOffset = encoderLine - (LCD_HEIGHT) + 1; lcdDrawUpdate = LCDVIEW_CALL_REDRAW_NEXT; _lineNr = currentMenuViewOffset - 1; _drawLineNr = -1; } \
  232. } } while(0)
  233. /** Used variables to keep track of the menu */
  234. volatile uint8_t buttons; //the last checked buttons in a bit array.
  235. #if ENABLED(REPRAPWORLD_KEYPAD)
  236. volatile uint8_t buttons_reprapworld_keypad; // to store the keypad shift register values
  237. #endif
  238. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  239. volatile uint8_t slow_buttons; // Bits of the pressed buttons.
  240. #endif
  241. uint8_t currentMenuViewOffset; /* scroll offset in the current menu */
  242. millis_t next_button_update_ms;
  243. uint8_t lastEncoderBits;
  244. uint32_t encoderPosition;
  245. #if PIN_EXISTS(SD_DETECT)
  246. uint8_t lcd_sd_status;
  247. #endif
  248. typedef struct {
  249. screenFunc_t menu_function;
  250. uint32_t encoder_position;
  251. } menuPosition;
  252. screenFunc_t currentScreen = lcd_status_screen; // pointer to the currently active menu handler
  253. menuPosition screen_history[10];
  254. uint8_t screen_history_depth = 0;
  255. bool ignore_click = false;
  256. bool wait_for_unclick;
  257. bool defer_return_to_status = false;
  258. // Variables used when editing values.
  259. const char* editLabel;
  260. void* editValue;
  261. int32_t minEditValue, maxEditValue;
  262. screenFunc_t callbackFunc; // call this after editing
  263. /**
  264. * General function to go directly to a menu
  265. * Remembers the previous position
  266. */
  267. static void lcd_goto_screen(screenFunc_t screen, const bool feedback = false, const uint32_t encoder = 0) {
  268. if (currentScreen != screen) {
  269. currentScreen = screen;
  270. lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
  271. #if ENABLED(NEWPANEL)
  272. encoderPosition = encoder;
  273. if (feedback) lcd_quick_feedback();
  274. #endif
  275. if (screen == lcd_status_screen) {
  276. defer_return_to_status = false;
  277. screen_history_depth = 0;
  278. }
  279. #if ENABLED(LCD_PROGRESS_BAR)
  280. // For LCD_PROGRESS_BAR re-initialize custom characters
  281. lcd_set_custom_characters(screen == lcd_status_screen);
  282. #endif
  283. }
  284. }
  285. static void lcd_return_to_status() { lcd_goto_screen(lcd_status_screen); }
  286. inline void lcd_save_previous_menu() {
  287. if (screen_history_depth < COUNT(screen_history)) {
  288. screen_history[screen_history_depth].menu_function = currentScreen;
  289. #if ENABLED(ULTIPANEL)
  290. screen_history[screen_history_depth].encoder_position = encoderPosition;
  291. #endif
  292. ++screen_history_depth;
  293. }
  294. }
  295. static void lcd_goto_previous_menu(bool feedback=false) {
  296. if (screen_history_depth > 0) {
  297. --screen_history_depth;
  298. lcd_goto_screen(screen_history[screen_history_depth].menu_function, feedback
  299. #if ENABLED(ULTIPANEL)
  300. , screen_history[screen_history_depth].encoder_position
  301. #endif
  302. );
  303. }
  304. else
  305. lcd_return_to_status();
  306. }
  307. void lcd_ignore_click(bool b) {
  308. ignore_click = b;
  309. wait_for_unclick = false;
  310. }
  311. #endif // ULTIPANEL
  312. /**
  313. *
  314. * "Info Screen"
  315. *
  316. * This is very display-dependent, so the lcd implementation draws this.
  317. */
  318. static void lcd_status_screen() {
  319. #if ENABLED(ULTIPANEL)
  320. ENCODER_DIRECTION_NORMAL();
  321. encoderRateMultiplierEnabled = false;
  322. #endif
  323. #if ENABLED(LCD_PROGRESS_BAR)
  324. millis_t ms = millis();
  325. #if DISABLED(PROGRESS_MSG_ONCE)
  326. if (ELAPSED(ms, progress_bar_ms + PROGRESS_BAR_MSG_TIME + PROGRESS_BAR_BAR_TIME)) {
  327. progress_bar_ms = ms;
  328. }
  329. #endif
  330. #if PROGRESS_MSG_EXPIRE > 0
  331. // Handle message expire
  332. if (expire_status_ms > 0) {
  333. #if ENABLED(SDSUPPORT)
  334. if (card.isFileOpen()) {
  335. // Expire the message when printing is active
  336. if (IS_SD_PRINTING) {
  337. if (ELAPSED(ms, expire_status_ms)) {
  338. lcd_status_message[0] = '\0';
  339. expire_status_ms = 0;
  340. }
  341. }
  342. else {
  343. expire_status_ms += LCD_UPDATE_INTERVAL;
  344. }
  345. }
  346. else {
  347. expire_status_ms = 0;
  348. }
  349. #else
  350. expire_status_ms = 0;
  351. #endif //SDSUPPORT
  352. }
  353. #endif
  354. #endif //LCD_PROGRESS_BAR
  355. lcd_implementation_status_screen();
  356. #if ENABLED(ULTIPANEL)
  357. bool current_click = LCD_CLICKED;
  358. if (ignore_click) {
  359. if (wait_for_unclick) {
  360. if (!current_click)
  361. ignore_click = wait_for_unclick = false;
  362. else
  363. current_click = false;
  364. }
  365. else if (current_click) {
  366. lcd_quick_feedback();
  367. wait_for_unclick = true;
  368. current_click = false;
  369. }
  370. }
  371. if (current_click) {
  372. lcd_goto_screen(lcd_main_menu, true);
  373. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  374. #if ENABLED(LCD_PROGRESS_BAR) && ENABLED(ULTIPANEL)
  375. currentScreen == lcd_status_screen
  376. #endif
  377. );
  378. #if ENABLED(FILAMENT_LCD_DISPLAY)
  379. previous_lcd_status_ms = millis(); // get status message to show up for a while
  380. #endif
  381. }
  382. #if ENABLED(ULTIPANEL_FEEDMULTIPLY)
  383. int new_frm = feedrate_multiplier + (int32_t)encoderPosition;
  384. // Dead zone at 100% feedrate
  385. if ((feedrate_multiplier < 100 && new_frm > 100) || (feedrate_multiplier > 100 && new_frm < 100)) {
  386. feedrate_multiplier = 100;
  387. encoderPosition = 0;
  388. }
  389. else if (feedrate_multiplier == 100) {
  390. if ((int32_t)encoderPosition > ENCODER_FEEDRATE_DEADZONE) {
  391. feedrate_multiplier += (int32_t)encoderPosition - (ENCODER_FEEDRATE_DEADZONE);
  392. encoderPosition = 0;
  393. }
  394. else if ((int32_t)encoderPosition < -(ENCODER_FEEDRATE_DEADZONE)) {
  395. feedrate_multiplier += (int32_t)encoderPosition + ENCODER_FEEDRATE_DEADZONE;
  396. encoderPosition = 0;
  397. }
  398. }
  399. else {
  400. feedrate_multiplier = new_frm;
  401. encoderPosition = 0;
  402. }
  403. #endif // ULTIPANEL_FEEDMULTIPLY
  404. feedrate_multiplier = constrain(feedrate_multiplier, 10, 999);
  405. #endif //ULTIPANEL
  406. }
  407. #if ENABLED(ULTIPANEL)
  408. inline void line_to_current(AxisEnum axis) {
  409. #if ENABLED(DELTA)
  410. calculate_delta(current_position);
  411. planner.buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis]/60, active_extruder);
  412. #else // !DELTA
  413. planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis]/60, active_extruder);
  414. #endif // !DELTA
  415. }
  416. #if ENABLED(SDSUPPORT)
  417. static void lcd_sdcard_pause() {
  418. card.pauseSDPrint();
  419. print_job_timer.pause();
  420. }
  421. static void lcd_sdcard_resume() {
  422. card.startFileprint();
  423. print_job_timer.start();
  424. }
  425. static void lcd_sdcard_stop() {
  426. stepper.quick_stop();
  427. #if DISABLED(DELTA) && DISABLED(SCARA)
  428. set_current_position_from_planner();
  429. #endif // !DELTA && !SCARA
  430. clear_command_queue();
  431. card.sdprinting = false;
  432. card.closefile();
  433. print_job_timer.stop();
  434. thermalManager.autotempShutdown();
  435. cancel_heatup = true;
  436. lcd_setstatus(MSG_PRINT_ABORTED, true);
  437. }
  438. #endif //SDSUPPORT
  439. /**
  440. *
  441. * "Main" menu
  442. *
  443. */
  444. static void lcd_main_menu() {
  445. START_MENU();
  446. MENU_ITEM(back, MSG_WATCH);
  447. if (planner.movesplanned() || IS_SD_PRINTING) {
  448. MENU_ITEM(submenu, MSG_TUNE, lcd_tune_menu);
  449. }
  450. else {
  451. MENU_ITEM(submenu, MSG_PREPARE, lcd_prepare_menu);
  452. #if ENABLED(DELTA_CALIBRATION_MENU)
  453. MENU_ITEM(submenu, MSG_DELTA_CALIBRATE, lcd_delta_calibrate_menu);
  454. #endif
  455. }
  456. MENU_ITEM(submenu, MSG_CONTROL, lcd_control_menu);
  457. #if ENABLED(SDSUPPORT)
  458. if (card.cardOK) {
  459. if (card.isFileOpen()) {
  460. if (card.sdprinting)
  461. MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause);
  462. else
  463. MENU_ITEM(function, MSG_RESUME_PRINT, lcd_sdcard_resume);
  464. MENU_ITEM(function, MSG_STOP_PRINT, lcd_sdcard_stop);
  465. }
  466. else {
  467. MENU_ITEM(submenu, MSG_CARD_MENU, lcd_sdcard_menu);
  468. #if !PIN_EXISTS(SD_DETECT)
  469. MENU_ITEM(gcode, MSG_CNG_SDCARD, PSTR("M21")); // SD-card changed by user
  470. #endif
  471. }
  472. }
  473. else {
  474. MENU_ITEM(submenu, MSG_NO_CARD, lcd_sdcard_menu);
  475. #if !PIN_EXISTS(SD_DETECT)
  476. MENU_ITEM(gcode, MSG_INIT_SDCARD, PSTR("M21")); // Manually initialize the SD-card via user interface
  477. #endif
  478. }
  479. #endif //SDSUPPORT
  480. END_MENU();
  481. }
  482. /**
  483. *
  484. * "Tune" submenu items
  485. *
  486. */
  487. /**
  488. * Set the home offset based on the current_position
  489. */
  490. void lcd_set_home_offsets() {
  491. // M428 Command
  492. enqueue_and_echo_commands_P(PSTR("M428"));
  493. lcd_return_to_status();
  494. }
  495. #if ENABLED(BABYSTEPPING)
  496. int babysteps_done = 0;
  497. static void _lcd_babystep(const AxisEnum axis, const char* msg) {
  498. ENCODER_DIRECTION_NORMAL();
  499. if (encoderPosition) {
  500. int distance = (int32_t)encoderPosition * BABYSTEP_MULTIPLICATOR;
  501. encoderPosition = 0;
  502. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  503. thermalManager.babystep_axis(axis, distance);
  504. babysteps_done += distance;
  505. }
  506. if (lcdDrawUpdate) lcd_implementation_drawedit(msg, itostr3sign(babysteps_done));
  507. if (LCD_CLICKED) lcd_goto_previous_menu(true);
  508. }
  509. #if ENABLED(BABYSTEP_XY)
  510. static void _lcd_babystep_x() { _lcd_babystep(X_AXIS, PSTR(MSG_BABYSTEPPING_X)); }
  511. static void _lcd_babystep_y() { _lcd_babystep(Y_AXIS, PSTR(MSG_BABYSTEPPING_Y)); }
  512. static void lcd_babystep_x() { babysteps_done = 0; lcd_goto_screen(_lcd_babystep_x); }
  513. static void lcd_babystep_y() { babysteps_done = 0; lcd_goto_screen(_lcd_babystep_y); }
  514. #endif
  515. static void _lcd_babystep_z() { _lcd_babystep(Z_AXIS, PSTR(MSG_BABYSTEPPING_Z)); }
  516. static void lcd_babystep_z() { babysteps_done = 0; lcd_goto_screen(_lcd_babystep_z); }
  517. #endif //BABYSTEPPING
  518. /**
  519. * Watch temperature callbacks
  520. */
  521. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  522. #if TEMP_SENSOR_0 != 0
  523. void watch_temp_callback_E0() { thermalManager.start_watching_heater(0); }
  524. #endif
  525. #if HOTENDS > 1 && TEMP_SENSOR_1 != 0
  526. void watch_temp_callback_E1() { thermalManager.start_watching_heater(1); }
  527. #endif // HOTENDS > 1
  528. #if HOTENDS > 2 && TEMP_SENSOR_2 != 0
  529. void watch_temp_callback_E2() { thermalManager.start_watching_heater(2); }
  530. #endif // HOTENDS > 2
  531. #if HOTENDS > 3 && TEMP_SENSOR_3 != 0
  532. void watch_temp_callback_E3() { thermalManager.start_watching_heater(3); }
  533. #endif // HOTENDS > 3
  534. #else
  535. #if TEMP_SENSOR_0 != 0
  536. void watch_temp_callback_E0() {}
  537. #endif
  538. #if HOTENDS > 1 && TEMP_SENSOR_1 != 0
  539. void watch_temp_callback_E1() {}
  540. #endif // HOTENDS > 1
  541. #if HOTENDS > 2 && TEMP_SENSOR_2 != 0
  542. void watch_temp_callback_E2() {}
  543. #endif // HOTENDS > 2
  544. #if HOTENDS > 3 && TEMP_SENSOR_3 != 0
  545. void watch_temp_callback_E3() {}
  546. #endif // HOTENDS > 3
  547. #endif
  548. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  549. #if TEMP_SENSOR_BED != 0
  550. void watch_temp_callback_bed() { thermalManager.start_watching_bed(); }
  551. #endif
  552. #else
  553. #if TEMP_SENSOR_BED != 0
  554. void watch_temp_callback_bed() {}
  555. #endif
  556. #endif
  557. /**
  558. *
  559. * "Tune" submenu
  560. *
  561. */
  562. static void lcd_tune_menu() {
  563. START_MENU();
  564. //
  565. // ^ Main
  566. //
  567. MENU_ITEM(back, MSG_MAIN);
  568. //
  569. // Speed:
  570. //
  571. MENU_ITEM_EDIT(int3, MSG_SPEED, &feedrate_multiplier, 10, 999);
  572. // Manual bed leveling, Bed Z:
  573. #if ENABLED(MANUAL_BED_LEVELING)
  574. MENU_ITEM_EDIT(float43, MSG_BED_Z, &mbl.z_offset, -1, 1);
  575. #endif
  576. //
  577. // Nozzle:
  578. // Nozzle [1-4]:
  579. //
  580. #if HOTENDS == 1
  581. #if TEMP_SENSOR_0 != 0
  582. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE, &thermalManager.target_temperature[0], 0, HEATER_0_MAXTEMP - 15, watch_temp_callback_E0);
  583. #endif
  584. #else //HOTENDS > 1
  585. #if TEMP_SENSOR_0 != 0
  586. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N1, &thermalManager.target_temperature[0], 0, HEATER_0_MAXTEMP - 15, watch_temp_callback_E0);
  587. #endif
  588. #if TEMP_SENSOR_1 != 0
  589. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N2, &thermalManager.target_temperature[1], 0, HEATER_1_MAXTEMP - 15, watch_temp_callback_E1);
  590. #endif
  591. #if HOTENDS > 2
  592. #if TEMP_SENSOR_2 != 0
  593. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N3, &thermalManager.target_temperature[2], 0, HEATER_2_MAXTEMP - 15, watch_temp_callback_E2);
  594. #endif
  595. #if HOTENDS > 3
  596. #if TEMP_SENSOR_3 != 0
  597. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N4, &thermalManager.target_temperature[3], 0, HEATER_3_MAXTEMP - 15, watch_temp_callback_E3);
  598. #endif
  599. #endif // HOTENDS > 3
  600. #endif // HOTENDS > 2
  601. #endif // HOTENDS > 1
  602. //
  603. // Bed:
  604. //
  605. #if TEMP_SENSOR_BED != 0
  606. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_BED, &thermalManager.target_temperature_bed, 0, BED_MAXTEMP - 15, watch_temp_callback_bed);
  607. #endif
  608. //
  609. // Fan Speed:
  610. //
  611. #if FAN_COUNT > 0
  612. #if HAS_FAN0
  613. #if FAN_COUNT > 1
  614. #define MSG_1ST_FAN_SPEED MSG_FAN_SPEED " 1"
  615. #else
  616. #define MSG_1ST_FAN_SPEED MSG_FAN_SPEED
  617. #endif
  618. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_1ST_FAN_SPEED, &fanSpeeds[0], 0, 255);
  619. #endif
  620. #if HAS_FAN1
  621. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_FAN_SPEED " 2", &fanSpeeds[1], 0, 255);
  622. #endif
  623. #if HAS_FAN2
  624. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_FAN_SPEED " 3", &fanSpeeds[2], 0, 255);
  625. #endif
  626. #endif // FAN_COUNT > 0
  627. //
  628. // Flow:
  629. // Flow 1:
  630. // Flow 2:
  631. // Flow 3:
  632. // Flow 4:
  633. //
  634. #if EXTRUDERS == 1
  635. MENU_ITEM_EDIT(int3, MSG_FLOW, &extruder_multiplier[0], 10, 999);
  636. #else // EXTRUDERS > 1
  637. MENU_ITEM_EDIT(int3, MSG_FLOW, &extruder_multiplier[active_extruder], 10, 999);
  638. MENU_ITEM_EDIT(int3, MSG_FLOW MSG_N1, &extruder_multiplier[0], 10, 999);
  639. MENU_ITEM_EDIT(int3, MSG_FLOW MSG_N2, &extruder_multiplier[1], 10, 999);
  640. #if EXTRUDERS > 2
  641. MENU_ITEM_EDIT(int3, MSG_FLOW MSG_N3, &extruder_multiplier[2], 10, 999);
  642. #if EXTRUDERS > 3
  643. MENU_ITEM_EDIT(int3, MSG_FLOW MSG_N4, &extruder_multiplier[3], 10, 999);
  644. #endif //EXTRUDERS > 3
  645. #endif //EXTRUDERS > 2
  646. #endif //EXTRUDERS > 1
  647. //
  648. // Babystep X:
  649. // Babystep Y:
  650. // Babystep Z:
  651. //
  652. #if ENABLED(BABYSTEPPING)
  653. #if ENABLED(BABYSTEP_XY)
  654. MENU_ITEM(submenu, MSG_BABYSTEP_X, lcd_babystep_x);
  655. MENU_ITEM(submenu, MSG_BABYSTEP_Y, lcd_babystep_y);
  656. #endif //BABYSTEP_XY
  657. MENU_ITEM(submenu, MSG_BABYSTEP_Z, lcd_babystep_z);
  658. #endif
  659. //
  660. // Change filament
  661. //
  662. #if ENABLED(FILAMENTCHANGEENABLE)
  663. MENU_ITEM(gcode, MSG_FILAMENTCHANGE, PSTR("M600"));
  664. #endif
  665. END_MENU();
  666. }
  667. /**
  668. *
  669. * "Prepare" submenu items
  670. *
  671. */
  672. void _lcd_preheat(int endnum, const float temph, const float tempb, const int fan) {
  673. if (temph > 0) thermalManager.setTargetHotend(temph, endnum);
  674. #if TEMP_SENSOR_BED != 0
  675. thermalManager.setTargetBed(tempb);
  676. #else
  677. UNUSED(tempb);
  678. #endif
  679. #if FAN_COUNT > 0
  680. #if FAN_COUNT > 1
  681. fanSpeeds[active_extruder < FAN_COUNT ? active_extruder : 0] = fan;
  682. #else
  683. fanSpeeds[0] = fan;
  684. #endif
  685. #else
  686. UNUSED(fan);
  687. #endif
  688. lcd_return_to_status();
  689. }
  690. #if TEMP_SENSOR_0 != 0
  691. void lcd_preheat_pla0() { _lcd_preheat(0, plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  692. void lcd_preheat_abs0() { _lcd_preheat(0, absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed); }
  693. #endif
  694. #if HOTENDS > 1
  695. void lcd_preheat_pla1() { _lcd_preheat(1, plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  696. void lcd_preheat_abs1() { _lcd_preheat(1, absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed); }
  697. #if HOTENDS > 2
  698. void lcd_preheat_pla2() { _lcd_preheat(2, plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  699. void lcd_preheat_abs2() { _lcd_preheat(2, absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed); }
  700. #if HOTENDS > 3
  701. void lcd_preheat_pla3() { _lcd_preheat(3, plaPreheatHotendTemp, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  702. void lcd_preheat_abs3() { _lcd_preheat(3, absPreheatHotendTemp, absPreheatHPBTemp, absPreheatFanSpeed); }
  703. #endif
  704. #endif
  705. void lcd_preheat_pla0123() {
  706. #if HOTENDS > 1
  707. thermalManager.setTargetHotend(plaPreheatHotendTemp, 1);
  708. #if HOTENDS > 2
  709. thermalManager.setTargetHotend(plaPreheatHotendTemp, 2);
  710. #if HOTENDS > 3
  711. thermalManager.setTargetHotend(plaPreheatHotendTemp, 3);
  712. #endif
  713. #endif
  714. #endif
  715. lcd_preheat_pla0();
  716. }
  717. void lcd_preheat_abs0123() {
  718. #if HOTENDS > 1
  719. thermalManager.setTargetHotend(absPreheatHotendTemp, 1);
  720. #if HOTENDS > 2
  721. thermalManager.setTargetHotend(absPreheatHotendTemp, 2);
  722. #if HOTENDS > 3
  723. thermalManager.setTargetHotend(absPreheatHotendTemp, 3);
  724. #endif
  725. #endif
  726. #endif
  727. lcd_preheat_abs0();
  728. }
  729. #endif // HOTENDS > 1
  730. #if TEMP_SENSOR_BED != 0
  731. void lcd_preheat_pla_bedonly() { _lcd_preheat(0, 0, plaPreheatHPBTemp, plaPreheatFanSpeed); }
  732. void lcd_preheat_abs_bedonly() { _lcd_preheat(0, 0, absPreheatHPBTemp, absPreheatFanSpeed); }
  733. #endif
  734. #if TEMP_SENSOR_0 != 0 && (TEMP_SENSOR_1 != 0 || TEMP_SENSOR_2 != 0 || TEMP_SENSOR_3 != 0 || TEMP_SENSOR_BED != 0)
  735. static void lcd_preheat_pla_menu() {
  736. START_MENU();
  737. MENU_ITEM(back, MSG_PREPARE);
  738. #if HOTENDS == 1
  739. MENU_ITEM(function, MSG_PREHEAT_PLA, lcd_preheat_pla0);
  740. #else
  741. MENU_ITEM(function, MSG_PREHEAT_PLA_N MSG_H1, lcd_preheat_pla0);
  742. MENU_ITEM(function, MSG_PREHEAT_PLA_N MSG_H2, lcd_preheat_pla1);
  743. #if HOTENDS > 2
  744. MENU_ITEM(function, MSG_PREHEAT_PLA_N MSG_H3, lcd_preheat_pla2);
  745. #if HOTENDS > 3
  746. MENU_ITEM(function, MSG_PREHEAT_PLA_N MSG_H4, lcd_preheat_pla3);
  747. #endif
  748. #endif
  749. MENU_ITEM(function, MSG_PREHEAT_PLA_ALL, lcd_preheat_pla0123);
  750. #endif
  751. #if TEMP_SENSOR_BED != 0
  752. MENU_ITEM(function, MSG_PREHEAT_PLA_BEDONLY, lcd_preheat_pla_bedonly);
  753. #endif
  754. END_MENU();
  755. }
  756. static void lcd_preheat_abs_menu() {
  757. START_MENU();
  758. MENU_ITEM(back, MSG_PREPARE);
  759. #if HOTENDS == 1
  760. MENU_ITEM(function, MSG_PREHEAT_ABS, lcd_preheat_abs0);
  761. #else
  762. MENU_ITEM(function, MSG_PREHEAT_ABS_N MSG_H1, lcd_preheat_abs0);
  763. MENU_ITEM(function, MSG_PREHEAT_ABS_N MSG_H2, lcd_preheat_abs1);
  764. #if HOTENDS > 2
  765. MENU_ITEM(function, MSG_PREHEAT_ABS_N MSG_H3, lcd_preheat_abs2);
  766. #if HOTENDS > 3
  767. MENU_ITEM(function, MSG_PREHEAT_ABS_N MSG_H4, lcd_preheat_abs3);
  768. #endif
  769. #endif
  770. MENU_ITEM(function, MSG_PREHEAT_ABS_ALL, lcd_preheat_abs0123);
  771. #endif
  772. #if TEMP_SENSOR_BED != 0
  773. MENU_ITEM(function, MSG_PREHEAT_ABS_BEDONLY, lcd_preheat_abs_bedonly);
  774. #endif
  775. END_MENU();
  776. }
  777. #endif // TEMP_SENSOR_0 && (TEMP_SENSOR_1 || TEMP_SENSOR_2 || TEMP_SENSOR_3 || TEMP_SENSOR_BED)
  778. void lcd_cooldown() {
  779. #if FAN_COUNT > 0
  780. for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0;
  781. #endif
  782. thermalManager.disable_all_heaters();
  783. lcd_return_to_status();
  784. }
  785. #if ENABLED(SDSUPPORT) && ENABLED(MENU_ADDAUTOSTART)
  786. static void lcd_autostart_sd() {
  787. card.autostart_index = 0;
  788. card.setroot();
  789. card.checkautostart(true);
  790. }
  791. #endif
  792. #if ENABLED(MANUAL_BED_LEVELING)
  793. /**
  794. *
  795. * "Prepare" > "Bed Leveling" handlers
  796. *
  797. */
  798. static uint8_t _lcd_level_bed_position;
  799. // Utility to go to the next mesh point
  800. // A raise is added between points if MIN_Z_HEIGHT_FOR_HOMING is in use
  801. // Note: During Manual Bed Leveling the homed Z position is MESH_HOME_SEARCH_Z
  802. // Z position will be restored with the final action, a G28
  803. inline void _mbl_goto_xy(float x, float y) {
  804. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z
  805. #if MIN_Z_HEIGHT_FOR_HOMING > 0
  806. + MIN_Z_HEIGHT_FOR_HOMING
  807. #endif
  808. ;
  809. line_to_current(Z_AXIS);
  810. current_position[X_AXIS] = x + home_offset[X_AXIS];
  811. current_position[Y_AXIS] = y + home_offset[Y_AXIS];
  812. line_to_current(manual_feedrate[X_AXIS] <= manual_feedrate[Y_AXIS] ? X_AXIS : Y_AXIS);
  813. #if MIN_Z_HEIGHT_FOR_HOMING > 0
  814. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
  815. line_to_current(Z_AXIS);
  816. #endif
  817. stepper.synchronize();
  818. }
  819. static void _lcd_level_goto_next_point();
  820. static void _lcd_level_bed_done() {
  821. if (lcdDrawUpdate) lcd_implementation_drawedit(PSTR(MSG_LEVEL_BED_DONE));
  822. lcdDrawUpdate =
  823. #if ENABLED(DOGLCD)
  824. LCDVIEW_CALL_REDRAW_NEXT
  825. #else
  826. LCDVIEW_CALL_NO_REDRAW
  827. #endif
  828. ;
  829. }
  830. /**
  831. * Step 7: Get the Z coordinate, then goto next point or exit
  832. */
  833. static void _lcd_level_bed_get_z() {
  834. ENCODER_DIRECTION_NORMAL();
  835. // Encoder wheel adjusts the Z position
  836. if (encoderPosition) {
  837. refresh_cmd_timeout();
  838. current_position[Z_AXIS] += float((int32_t)encoderPosition) * (MBL_Z_STEP);
  839. NOLESS(current_position[Z_AXIS], 0);
  840. NOMORE(current_position[Z_AXIS], MESH_HOME_SEARCH_Z * 2);
  841. line_to_current(Z_AXIS);
  842. lcdDrawUpdate =
  843. #if ENABLED(DOGLCD)
  844. LCDVIEW_CALL_REDRAW_NEXT
  845. #else
  846. LCDVIEW_REDRAW_NOW
  847. #endif
  848. ;
  849. encoderPosition = 0;
  850. }
  851. static bool debounce_click = false;
  852. if (LCD_CLICKED) {
  853. if (!debounce_click) {
  854. debounce_click = true; // ignore multiple "clicks" in a row
  855. mbl.set_zigzag_z(_lcd_level_bed_position++, current_position[Z_AXIS]);
  856. if (_lcd_level_bed_position == (MESH_NUM_X_POINTS) * (MESH_NUM_Y_POINTS)) {
  857. lcd_goto_screen(_lcd_level_bed_done, true);
  858. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z
  859. #if MIN_Z_HEIGHT_FOR_HOMING > 0
  860. + MIN_Z_HEIGHT_FOR_HOMING
  861. #endif
  862. ;
  863. line_to_current(Z_AXIS);
  864. stepper.synchronize();
  865. mbl.set_has_mesh(true);
  866. enqueue_and_echo_commands_P(PSTR("G28"));
  867. lcd_return_to_status();
  868. //LCD_MESSAGEPGM(MSG_LEVEL_BED_DONE);
  869. #if HAS_BUZZER
  870. buzzer.tone(200, 659);
  871. buzzer.tone(200, 698);
  872. #endif
  873. }
  874. else {
  875. lcd_goto_screen(_lcd_level_goto_next_point, true);
  876. }
  877. }
  878. }
  879. else {
  880. debounce_click = false;
  881. }
  882. // Update on first display, then only on updates to Z position
  883. // Show message above on clicks instead
  884. if (lcdDrawUpdate) {
  885. float v = current_position[Z_AXIS] - MESH_HOME_SEARCH_Z;
  886. lcd_implementation_drawedit(PSTR(MSG_MOVE_Z), ftostr43sign(v + (v < 0 ? -0.0001 : 0.0001), '+'));
  887. }
  888. }
  889. /**
  890. * Step 6: Display "Next point: 1 / 9" while waiting for move to finish
  891. */
  892. static void _lcd_level_bed_moving() {
  893. if (lcdDrawUpdate) {
  894. char msg[10];
  895. sprintf_P(msg, PSTR("%i / %u"), (int)(_lcd_level_bed_position + 1), (MESH_NUM_X_POINTS) * (MESH_NUM_Y_POINTS));
  896. lcd_implementation_drawedit(PSTR(MSG_LEVEL_BED_NEXT_POINT), msg);
  897. }
  898. lcdDrawUpdate =
  899. #if ENABLED(DOGLCD)
  900. LCDVIEW_CALL_REDRAW_NEXT
  901. #else
  902. LCDVIEW_CALL_NO_REDRAW
  903. #endif
  904. ;
  905. }
  906. /**
  907. * Step 5: Initiate a move to the next point
  908. */
  909. static void _lcd_level_goto_next_point() {
  910. // Set the menu to display ahead of blocking call
  911. lcd_goto_screen(_lcd_level_bed_moving);
  912. // _mbl_goto_xy runs the menu loop until the move is done
  913. int8_t px, py;
  914. mbl.zigzag(_lcd_level_bed_position, px, py);
  915. _mbl_goto_xy(mbl.get_probe_x(px), mbl.get_probe_y(py));
  916. // After the blocking function returns, change menus
  917. lcd_goto_screen(_lcd_level_bed_get_z);
  918. }
  919. /**
  920. * Step 4: Display "Click to Begin", wait for click
  921. * Move to the first probe position
  922. */
  923. static void _lcd_level_bed_homing_done() {
  924. if (lcdDrawUpdate) lcd_implementation_drawedit(PSTR(MSG_LEVEL_BED_WAITING));
  925. if (LCD_CLICKED) {
  926. _lcd_level_bed_position = 0;
  927. current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
  928. planner.set_position_mm(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  929. lcd_goto_screen(_lcd_level_goto_next_point, true);
  930. }
  931. }
  932. /**
  933. * Step 3: Display "Homing XYZ" - Wait for homing to finish
  934. */
  935. static void _lcd_level_bed_homing() {
  936. if (lcdDrawUpdate) lcd_implementation_drawedit(PSTR(MSG_LEVEL_BED_HOMING), NULL);
  937. lcdDrawUpdate =
  938. #if ENABLED(DOGLCD)
  939. LCDVIEW_CALL_REDRAW_NEXT
  940. #else
  941. LCDVIEW_CALL_NO_REDRAW
  942. #endif
  943. ;
  944. if (axis_homed[X_AXIS] && axis_homed[Y_AXIS] && axis_homed[Z_AXIS])
  945. lcd_goto_screen(_lcd_level_bed_homing_done);
  946. }
  947. /**
  948. * Step 2: Continue Bed Leveling...
  949. */
  950. static void _lcd_level_bed_continue() {
  951. defer_return_to_status = true;
  952. axis_homed[X_AXIS] = axis_homed[Y_AXIS] = axis_homed[Z_AXIS] = false;
  953. mbl.reset();
  954. enqueue_and_echo_commands_P(PSTR("G28"));
  955. lcd_goto_screen(_lcd_level_bed_homing);
  956. }
  957. /**
  958. * Step 1: MBL entry-point: "Cancel" or "Level Bed"
  959. */
  960. static void lcd_level_bed() {
  961. START_MENU();
  962. MENU_ITEM(back, MSG_LEVEL_BED_CANCEL);
  963. MENU_ITEM(submenu, MSG_LEVEL_BED, _lcd_level_bed_continue);
  964. END_MENU();
  965. }
  966. #endif // MANUAL_BED_LEVELING
  967. /**
  968. *
  969. * "Prepare" submenu
  970. *
  971. */
  972. static void lcd_prepare_menu() {
  973. START_MENU();
  974. //
  975. // ^ Main
  976. //
  977. MENU_ITEM(back, MSG_MAIN);
  978. //
  979. // Auto Home
  980. //
  981. MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
  982. #if ENABLED(INDIVIDUAL_AXIS_HOMING_MENU)
  983. MENU_ITEM(gcode, MSG_AUTO_HOME_X, PSTR("G28 X"));
  984. MENU_ITEM(gcode, MSG_AUTO_HOME_Y, PSTR("G28 Y"));
  985. MENU_ITEM(gcode, MSG_AUTO_HOME_Z, PSTR("G28 Z"));
  986. #endif
  987. //
  988. // Set Home Offsets
  989. //
  990. MENU_ITEM(function, MSG_SET_HOME_OFFSETS, lcd_set_home_offsets);
  991. //MENU_ITEM(gcode, MSG_SET_ORIGIN, PSTR("G92 X0 Y0 Z0"));
  992. //
  993. // Level Bed
  994. //
  995. #if ENABLED(AUTO_BED_LEVELING_FEATURE)
  996. MENU_ITEM(gcode, MSG_LEVEL_BED,
  997. axis_homed[X_AXIS] && axis_homed[Y_AXIS] ? PSTR("G29") : PSTR("G28\nG29")
  998. );
  999. #elif ENABLED(MANUAL_BED_LEVELING)
  1000. MENU_ITEM(submenu, MSG_LEVEL_BED, lcd_level_bed);
  1001. #endif
  1002. //
  1003. // Move Axis
  1004. //
  1005. MENU_ITEM(submenu, MSG_MOVE_AXIS, lcd_move_menu);
  1006. //
  1007. // Disable Steppers
  1008. //
  1009. MENU_ITEM(gcode, MSG_DISABLE_STEPPERS, PSTR("M84"));
  1010. //
  1011. // Preheat PLA
  1012. // Preheat ABS
  1013. //
  1014. #if TEMP_SENSOR_0 != 0
  1015. #if TEMP_SENSOR_1 != 0 || TEMP_SENSOR_2 != 0 || TEMP_SENSOR_3 != 0 || TEMP_SENSOR_BED != 0
  1016. MENU_ITEM(submenu, MSG_PREHEAT_PLA, lcd_preheat_pla_menu);
  1017. MENU_ITEM(submenu, MSG_PREHEAT_ABS, lcd_preheat_abs_menu);
  1018. #else
  1019. MENU_ITEM(function, MSG_PREHEAT_PLA, lcd_preheat_pla0);
  1020. MENU_ITEM(function, MSG_PREHEAT_ABS, lcd_preheat_abs0);
  1021. #endif
  1022. #endif
  1023. //
  1024. // Cooldown
  1025. //
  1026. MENU_ITEM(function, MSG_COOLDOWN, lcd_cooldown);
  1027. //
  1028. // Switch power on/off
  1029. //
  1030. #if HAS_POWER_SWITCH
  1031. if (powersupply)
  1032. MENU_ITEM(gcode, MSG_SWITCH_PS_OFF, PSTR("M81"));
  1033. else
  1034. MENU_ITEM(gcode, MSG_SWITCH_PS_ON, PSTR("M80"));
  1035. #endif
  1036. //
  1037. // Autostart
  1038. //
  1039. #if ENABLED(SDSUPPORT) && ENABLED(MENU_ADDAUTOSTART)
  1040. MENU_ITEM(function, MSG_AUTOSTART, lcd_autostart_sd);
  1041. #endif
  1042. END_MENU();
  1043. }
  1044. #if ENABLED(DELTA_CALIBRATION_MENU)
  1045. static void lcd_delta_calibrate_menu() {
  1046. START_MENU();
  1047. MENU_ITEM(back, MSG_MAIN);
  1048. MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
  1049. MENU_ITEM(gcode, MSG_DELTA_CALIBRATE_X, PSTR("G0 F8000 X-77.94 Y-45 Z0"));
  1050. MENU_ITEM(gcode, MSG_DELTA_CALIBRATE_Y, PSTR("G0 F8000 X77.94 Y-45 Z0"));
  1051. MENU_ITEM(gcode, MSG_DELTA_CALIBRATE_Z, PSTR("G0 F8000 X0 Y90 Z0"));
  1052. MENU_ITEM(gcode, MSG_DELTA_CALIBRATE_CENTER, PSTR("G0 F8000 X0 Y0 Z0"));
  1053. END_MENU();
  1054. }
  1055. #endif // DELTA_CALIBRATION_MENU
  1056. /**
  1057. * If the most recent manual move hasn't been fed to the planner yet,
  1058. * and the planner can accept one, send immediately
  1059. */
  1060. inline void manage_manual_move() {
  1061. if (manual_move_axis != (int8_t)NO_AXIS && millis() >= manual_move_start_time && !planner.is_full()) {
  1062. #if ENABLED(DELTA)
  1063. calculate_delta(current_position);
  1064. planner.buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], manual_feedrate[manual_move_axis]/60, active_extruder);
  1065. #else
  1066. planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[manual_move_axis]/60, active_extruder);
  1067. #endif
  1068. manual_move_axis = (int8_t)NO_AXIS;
  1069. }
  1070. }
  1071. /**
  1072. * Set a flag that lcd_update() should start a move
  1073. * to "current_position" after a short delay.
  1074. */
  1075. inline void manual_move_to_current(AxisEnum axis) {
  1076. manual_move_start_time = millis() + 500UL; // 1/2 second delay
  1077. manual_move_axis = (int8_t)axis;
  1078. }
  1079. /**
  1080. *
  1081. * "Prepare" > "Move Axis" submenu
  1082. *
  1083. */
  1084. float move_menu_scale;
  1085. static void _lcd_move(const char* name, AxisEnum axis, float min, float max) {
  1086. ENCODER_DIRECTION_NORMAL();
  1087. if (encoderPosition) {
  1088. refresh_cmd_timeout();
  1089. current_position[axis] += float((int32_t)encoderPosition) * move_menu_scale;
  1090. if (min_software_endstops) NOLESS(current_position[axis], min);
  1091. if (max_software_endstops) NOMORE(current_position[axis], max);
  1092. encoderPosition = 0;
  1093. manual_move_to_current(axis);
  1094. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  1095. }
  1096. if (lcdDrawUpdate) lcd_implementation_drawedit(name, ftostr41sign(current_position[axis]));
  1097. if (LCD_CLICKED) lcd_goto_previous_menu(true);
  1098. }
  1099. #if ENABLED(DELTA)
  1100. static float delta_clip_radius_2 = (DELTA_PRINTABLE_RADIUS) * (DELTA_PRINTABLE_RADIUS);
  1101. static int delta_clip( float a ) { return sqrt(delta_clip_radius_2 - a*a); }
  1102. static void lcd_move_x() { int clip = delta_clip(current_position[Y_AXIS]); _lcd_move(PSTR(MSG_MOVE_X), X_AXIS, max(sw_endstop_min[X_AXIS], -clip), min(sw_endstop_max[X_AXIS], clip)); }
  1103. static void lcd_move_y() { int clip = delta_clip(current_position[X_AXIS]); _lcd_move(PSTR(MSG_MOVE_Y), Y_AXIS, max(sw_endstop_min[Y_AXIS], -clip), min(sw_endstop_max[Y_AXIS], clip)); }
  1104. #else
  1105. static void lcd_move_x() { _lcd_move(PSTR(MSG_MOVE_X), X_AXIS, sw_endstop_min[X_AXIS], sw_endstop_max[X_AXIS]); }
  1106. static void lcd_move_y() { _lcd_move(PSTR(MSG_MOVE_Y), Y_AXIS, sw_endstop_min[Y_AXIS], sw_endstop_max[Y_AXIS]); }
  1107. #endif
  1108. static void lcd_move_z() { _lcd_move(PSTR(MSG_MOVE_Z), Z_AXIS, sw_endstop_min[Z_AXIS], sw_endstop_max[Z_AXIS]); }
  1109. static void lcd_move_e(
  1110. #if EXTRUDERS > 1
  1111. uint8_t e
  1112. #endif
  1113. ) {
  1114. ENCODER_DIRECTION_NORMAL();
  1115. #if EXTRUDERS > 1
  1116. unsigned short original_active_extruder = active_extruder;
  1117. active_extruder = e;
  1118. #endif
  1119. if (encoderPosition) {
  1120. current_position[E_AXIS] += float((int32_t)encoderPosition) * move_menu_scale;
  1121. encoderPosition = 0;
  1122. manual_move_to_current(E_AXIS);
  1123. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  1124. }
  1125. if (lcdDrawUpdate) {
  1126. PGM_P pos_label;
  1127. #if EXTRUDERS == 1
  1128. pos_label = PSTR(MSG_MOVE_E);
  1129. #else
  1130. switch (e) {
  1131. case 0: pos_label = PSTR(MSG_MOVE_E MSG_MOVE_E1); break;
  1132. case 1: pos_label = PSTR(MSG_MOVE_E MSG_MOVE_E2); break;
  1133. #if EXTRUDERS > 2
  1134. case 2: pos_label = PSTR(MSG_MOVE_E MSG_MOVE_E3); break;
  1135. #if EXTRUDERS > 3
  1136. case 3: pos_label = PSTR(MSG_MOVE_E MSG_MOVE_E4); break;
  1137. #endif //EXTRUDERS > 3
  1138. #endif //EXTRUDERS > 2
  1139. }
  1140. #endif //EXTRUDERS > 1
  1141. lcd_implementation_drawedit(pos_label, ftostr41sign(current_position[E_AXIS]));
  1142. }
  1143. if (LCD_CLICKED) lcd_goto_previous_menu(true);
  1144. #if EXTRUDERS > 1
  1145. active_extruder = original_active_extruder;
  1146. #endif
  1147. }
  1148. #if EXTRUDERS > 1
  1149. static void lcd_move_e0() { lcd_move_e(0); }
  1150. static void lcd_move_e1() { lcd_move_e(1); }
  1151. #if EXTRUDERS > 2
  1152. static void lcd_move_e2() { lcd_move_e(2); }
  1153. #if EXTRUDERS > 3
  1154. static void lcd_move_e3() { lcd_move_e(3); }
  1155. #endif
  1156. #endif
  1157. #endif // EXTRUDERS > 1
  1158. /**
  1159. *
  1160. * "Prepare" > "Move Xmm" > "Move XYZ" submenu
  1161. *
  1162. */
  1163. #if ENABLED(DELTA) || ENABLED(SCARA)
  1164. #define _MOVE_XYZ_ALLOWED (axis_homed[X_AXIS] && axis_homed[Y_AXIS] && axis_homed[Z_AXIS])
  1165. #else
  1166. #define _MOVE_XYZ_ALLOWED true
  1167. #endif
  1168. static void _lcd_move_menu_axis() {
  1169. START_MENU();
  1170. MENU_ITEM(back, MSG_MOVE_AXIS);
  1171. if (_MOVE_XYZ_ALLOWED) {
  1172. MENU_ITEM(submenu, MSG_MOVE_X, lcd_move_x);
  1173. MENU_ITEM(submenu, MSG_MOVE_Y, lcd_move_y);
  1174. }
  1175. if (move_menu_scale < 10.0) {
  1176. if (_MOVE_XYZ_ALLOWED) MENU_ITEM(submenu, MSG_MOVE_Z, lcd_move_z);
  1177. #if EXTRUDERS == 1
  1178. MENU_ITEM(submenu, MSG_MOVE_E, lcd_move_e);
  1179. #else
  1180. MENU_ITEM(submenu, MSG_MOVE_E MSG_MOVE_E1, lcd_move_e0);
  1181. MENU_ITEM(submenu, MSG_MOVE_E MSG_MOVE_E2, lcd_move_e1);
  1182. #if EXTRUDERS > 2
  1183. MENU_ITEM(submenu, MSG_MOVE_E MSG_MOVE_E3, lcd_move_e2);
  1184. #if EXTRUDERS > 3
  1185. MENU_ITEM(submenu, MSG_MOVE_E MSG_MOVE_E4, lcd_move_e3);
  1186. #endif
  1187. #endif
  1188. #endif // EXTRUDERS > 1
  1189. }
  1190. END_MENU();
  1191. }
  1192. static void lcd_move_menu_10mm() {
  1193. move_menu_scale = 10.0;
  1194. _lcd_move_menu_axis();
  1195. }
  1196. static void lcd_move_menu_1mm() {
  1197. move_menu_scale = 1.0;
  1198. _lcd_move_menu_axis();
  1199. }
  1200. static void lcd_move_menu_01mm() {
  1201. move_menu_scale = 0.1;
  1202. _lcd_move_menu_axis();
  1203. }
  1204. /**
  1205. *
  1206. * "Prepare" > "Move Axis" submenu
  1207. *
  1208. */
  1209. static void lcd_move_menu() {
  1210. START_MENU();
  1211. MENU_ITEM(back, MSG_PREPARE);
  1212. if (_MOVE_XYZ_ALLOWED)
  1213. MENU_ITEM(submenu, MSG_MOVE_10MM, lcd_move_menu_10mm);
  1214. MENU_ITEM(submenu, MSG_MOVE_1MM, lcd_move_menu_1mm);
  1215. MENU_ITEM(submenu, MSG_MOVE_01MM, lcd_move_menu_01mm);
  1216. //TODO:X,Y,Z,E
  1217. END_MENU();
  1218. }
  1219. /**
  1220. *
  1221. * "Control" submenu
  1222. *
  1223. */
  1224. static void lcd_control_menu() {
  1225. START_MENU();
  1226. MENU_ITEM(back, MSG_MAIN);
  1227. MENU_ITEM(submenu, MSG_TEMPERATURE, lcd_control_temperature_menu);
  1228. MENU_ITEM(submenu, MSG_MOTION, lcd_control_motion_menu);
  1229. MENU_ITEM(submenu, MSG_VOLUMETRIC, lcd_control_volumetric_menu);
  1230. #if HAS_LCD_CONTRAST
  1231. //MENU_ITEM_EDIT(int3, MSG_CONTRAST, &lcd_contrast, 0, 63);
  1232. MENU_ITEM(submenu, MSG_CONTRAST, lcd_set_contrast);
  1233. #endif
  1234. #if ENABLED(FWRETRACT)
  1235. MENU_ITEM(submenu, MSG_RETRACT, lcd_control_retract_menu);
  1236. #endif
  1237. #if ENABLED(EEPROM_SETTINGS)
  1238. MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
  1239. MENU_ITEM(function, MSG_LOAD_EPROM, Config_RetrieveSettings);
  1240. #endif
  1241. MENU_ITEM(function, MSG_RESTORE_FAILSAFE, Config_ResetDefault);
  1242. END_MENU();
  1243. }
  1244. /**
  1245. *
  1246. * "Temperature" submenu
  1247. *
  1248. */
  1249. #if ENABLED(PID_AUTOTUNE_MENU)
  1250. #if ENABLED(PIDTEMP)
  1251. int autotune_temp[HOTENDS] = ARRAY_BY_HOTENDS1(150);
  1252. const int heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP);
  1253. #endif
  1254. #if ENABLED(PIDTEMPBED)
  1255. int autotune_temp_bed = 70;
  1256. #endif
  1257. static void _lcd_autotune(int e) {
  1258. char cmd[30];
  1259. sprintf_P(cmd, PSTR("M303 U1 E%i S%i"), e,
  1260. #if HAS_PID_FOR_BOTH
  1261. e < 0 ? autotune_temp_bed : autotune_temp[e]
  1262. #elif ENABLED(PIDTEMPBED)
  1263. autotune_temp_bed
  1264. #else
  1265. autotune_temp[e]
  1266. #endif
  1267. );
  1268. enqueue_and_echo_command(cmd);
  1269. }
  1270. #endif //PID_AUTOTUNE_MENU
  1271. #if ENABLED(PIDTEMP)
  1272. // Helpers for editing PID Ki & Kd values
  1273. // grab the PID value out of the temp variable; scale it; then update the PID driver
  1274. void copy_and_scalePID_i(int e) {
  1275. #if DISABLED(PID_PARAMS_PER_HOTEND)
  1276. UNUSED(e);
  1277. #endif
  1278. PID_PARAM(Ki, e) = scalePID_i(raw_Ki);
  1279. thermalManager.updatePID();
  1280. }
  1281. void copy_and_scalePID_d(int e) {
  1282. #if DISABLED(PID_PARAMS_PER_HOTEND)
  1283. UNUSED(e);
  1284. #endif
  1285. PID_PARAM(Kd, e) = scalePID_d(raw_Kd);
  1286. thermalManager.updatePID();
  1287. }
  1288. #define _PIDTEMP_BASE_FUNCTIONS(eindex) \
  1289. void copy_and_scalePID_i_E ## eindex() { copy_and_scalePID_i(eindex); } \
  1290. void copy_and_scalePID_d_E ## eindex() { copy_and_scalePID_d(eindex); }
  1291. #if ENABLED(PID_AUTOTUNE_MENU)
  1292. #define _PIDTEMP_FUNCTIONS(eindex) \
  1293. _PIDTEMP_BASE_FUNCTIONS(eindex); \
  1294. void lcd_autotune_callback_E ## eindex() { _lcd_autotune(eindex); }
  1295. #else
  1296. #define _PIDTEMP_FUNCTIONS(eindex) _PIDTEMP_BASE_FUNCTIONS(eindex)
  1297. #endif
  1298. _PIDTEMP_FUNCTIONS(0);
  1299. #if ENABLED(PID_PARAMS_PER_HOTEND)
  1300. #if HOTENDS > 1
  1301. _PIDTEMP_FUNCTIONS(1);
  1302. #if HOTENDS > 2
  1303. _PIDTEMP_FUNCTIONS(2);
  1304. #if HOTENDS > 3
  1305. _PIDTEMP_FUNCTIONS(3);
  1306. #endif //HOTENDS > 3
  1307. #endif //HOTENDS > 2
  1308. #endif //HOTENDS > 1
  1309. #endif //PID_PARAMS_PER_HOTEND
  1310. #endif //PIDTEMP
  1311. /**
  1312. *
  1313. * "Control" > "Temperature" submenu
  1314. *
  1315. */
  1316. static void lcd_control_temperature_menu() {
  1317. START_MENU();
  1318. //
  1319. // ^ Control
  1320. //
  1321. MENU_ITEM(back, MSG_CONTROL);
  1322. //
  1323. // Nozzle:
  1324. // Nozzle [1-4]:
  1325. //
  1326. #if HOTENDS == 1
  1327. #if TEMP_SENSOR_0 != 0
  1328. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE, &thermalManager.target_temperature[0], 0, HEATER_0_MAXTEMP - 15, watch_temp_callback_E0);
  1329. #endif
  1330. #else //HOTENDS > 1
  1331. #if TEMP_SENSOR_0 != 0
  1332. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N1, &thermalManager.target_temperature[0], 0, HEATER_0_MAXTEMP - 15, watch_temp_callback_E0);
  1333. #endif
  1334. #if TEMP_SENSOR_1 != 0
  1335. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N2, &thermalManager.target_temperature[1], 0, HEATER_1_MAXTEMP - 15, watch_temp_callback_E1);
  1336. #endif
  1337. #if HOTENDS > 2
  1338. #if TEMP_SENSOR_2 != 0
  1339. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N3, &thermalManager.target_temperature[2], 0, HEATER_2_MAXTEMP - 15, watch_temp_callback_E2);
  1340. #endif
  1341. #if HOTENDS > 3
  1342. #if TEMP_SENSOR_3 != 0
  1343. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_NOZZLE MSG_N4, &thermalManager.target_temperature[3], 0, HEATER_3_MAXTEMP - 15, watch_temp_callback_E3);
  1344. #endif
  1345. #endif // HOTENDS > 3
  1346. #endif // HOTENDS > 2
  1347. #endif // HOTENDS > 1
  1348. //
  1349. // Bed:
  1350. //
  1351. #if TEMP_SENSOR_BED != 0
  1352. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_BED, &thermalManager.target_temperature_bed, 0, BED_MAXTEMP - 15);
  1353. #endif
  1354. //
  1355. // Fan Speed:
  1356. //
  1357. #if FAN_COUNT > 0
  1358. #if HAS_FAN0
  1359. #if FAN_COUNT > 1
  1360. #define MSG_1ST_FAN_SPEED MSG_FAN_SPEED " 1"
  1361. #else
  1362. #define MSG_1ST_FAN_SPEED MSG_FAN_SPEED
  1363. #endif
  1364. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_1ST_FAN_SPEED, &fanSpeeds[0], 0, 255);
  1365. #endif
  1366. #if HAS_FAN1
  1367. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_FAN_SPEED " 2", &fanSpeeds[1], 0, 255);
  1368. #endif
  1369. #if HAS_FAN2
  1370. MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_FAN_SPEED " 3", &fanSpeeds[2], 0, 255);
  1371. #endif
  1372. #endif // FAN_COUNT > 0
  1373. //
  1374. // Autotemp, Min, Max, Fact
  1375. //
  1376. #if ENABLED(AUTOTEMP) && (TEMP_SENSOR_0 != 0)
  1377. MENU_ITEM_EDIT(bool, MSG_AUTOTEMP, &planner.autotemp_enabled);
  1378. MENU_ITEM_EDIT(float3, MSG_MIN, &planner.autotemp_min, 0, HEATER_0_MAXTEMP - 15);
  1379. MENU_ITEM_EDIT(float3, MSG_MAX, &planner.autotemp_max, 0, HEATER_0_MAXTEMP - 15);
  1380. MENU_ITEM_EDIT(float32, MSG_FACTOR, &planner.autotemp_factor, 0.0, 1.0);
  1381. #endif
  1382. //
  1383. // PID-P, PID-I, PID-D, PID-C, PID Autotune
  1384. // PID-P E1, PID-I E1, PID-D E1, PID-C E1, PID Autotune E1
  1385. // PID-P E2, PID-I E2, PID-D E2, PID-C E2, PID Autotune E2
  1386. // PID-P E3, PID-I E3, PID-D E3, PID-C E3, PID Autotune E3
  1387. // PID-P E4, PID-I E4, PID-D E4, PID-C E4, PID Autotune E4
  1388. //
  1389. #if ENABLED(PIDTEMP)
  1390. #define _PID_BASE_MENU_ITEMS(ELABEL, eindex) \
  1391. raw_Ki = unscalePID_i(PID_PARAM(Ki, eindex)); \
  1392. raw_Kd = unscalePID_d(PID_PARAM(Kd, eindex)); \
  1393. MENU_ITEM_EDIT(float52, MSG_PID_P ELABEL, &PID_PARAM(Kp, eindex), 1, 9990); \
  1394. MENU_ITEM_EDIT_CALLBACK(float52, MSG_PID_I ELABEL, &raw_Ki, 0.01, 9990, copy_and_scalePID_i_E ## eindex); \
  1395. MENU_ITEM_EDIT_CALLBACK(float52, MSG_PID_D ELABEL, &raw_Kd, 1, 9990, copy_and_scalePID_d_E ## eindex)
  1396. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  1397. #define _PID_MENU_ITEMS(ELABEL, eindex) \
  1398. _PID_BASE_MENU_ITEMS(ELABEL, eindex); \
  1399. MENU_ITEM_EDIT(float3, MSG_PID_C ELABEL, &PID_PARAM(Kc, eindex), 1, 9990)
  1400. #else
  1401. #define _PID_MENU_ITEMS(ELABEL, eindex) _PID_BASE_MENU_ITEMS(ELABEL, eindex)
  1402. #endif
  1403. #if ENABLED(PID_AUTOTUNE_MENU)
  1404. #define PID_MENU_ITEMS(ELABEL, eindex) \
  1405. _PID_MENU_ITEMS(ELABEL, eindex); \
  1406. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(int3, MSG_PID_AUTOTUNE ELABEL, &autotune_temp[eindex], 150, heater_maxtemp[eindex] - 15, lcd_autotune_callback_E ## eindex)
  1407. #else
  1408. #define PID_MENU_ITEMS(ELABEL, eindex) _PID_MENU_ITEMS(ELABEL, eindex)
  1409. #endif
  1410. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  1411. PID_MENU_ITEMS(MSG_E1, 0);
  1412. PID_MENU_ITEMS(MSG_E2, 1);
  1413. #if HOTENDS > 2
  1414. PID_MENU_ITEMS(MSG_E3, 2);
  1415. #if HOTENDS > 3
  1416. PID_MENU_ITEMS(MSG_E4, 3);
  1417. #endif //HOTENDS > 3
  1418. #endif //HOTENDS > 2
  1419. #else //!PID_PARAMS_PER_HOTEND || HOTENDS == 1
  1420. PID_MENU_ITEMS("", 0);
  1421. #endif //!PID_PARAMS_PER_HOTEND || HOTENDS == 1
  1422. #endif //PIDTEMP
  1423. //
  1424. // Preheat PLA conf
  1425. //
  1426. MENU_ITEM(submenu, MSG_PREHEAT_PLA_SETTINGS, lcd_control_temperature_preheat_pla_settings_menu);
  1427. //
  1428. // Preheat ABS conf
  1429. //
  1430. MENU_ITEM(submenu, MSG_PREHEAT_ABS_SETTINGS, lcd_control_temperature_preheat_abs_settings_menu);
  1431. END_MENU();
  1432. }
  1433. /**
  1434. *
  1435. * "Temperature" > "Preheat PLA conf" submenu
  1436. *
  1437. */
  1438. static void lcd_control_temperature_preheat_pla_settings_menu() {
  1439. START_MENU();
  1440. MENU_ITEM(back, MSG_TEMPERATURE);
  1441. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &plaPreheatFanSpeed, 0, 255);
  1442. #if TEMP_SENSOR_0 != 0
  1443. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &plaPreheatHotendTemp, HEATER_0_MINTEMP, HEATER_0_MAXTEMP - 15);
  1444. #endif
  1445. #if TEMP_SENSOR_BED != 0
  1446. MENU_ITEM_EDIT(int3, MSG_BED, &plaPreheatHPBTemp, BED_MINTEMP, BED_MAXTEMP - 15);
  1447. #endif
  1448. #if ENABLED(EEPROM_SETTINGS)
  1449. MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
  1450. #endif
  1451. END_MENU();
  1452. }
  1453. /**
  1454. *
  1455. * "Temperature" > "Preheat ABS conf" submenu
  1456. *
  1457. */
  1458. static void lcd_control_temperature_preheat_abs_settings_menu() {
  1459. START_MENU();
  1460. MENU_ITEM(back, MSG_TEMPERATURE);
  1461. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &absPreheatFanSpeed, 0, 255);
  1462. #if TEMP_SENSOR_0 != 0
  1463. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &absPreheatHotendTemp, HEATER_0_MINTEMP, HEATER_0_MAXTEMP - 15);
  1464. #endif
  1465. #if TEMP_SENSOR_BED != 0
  1466. MENU_ITEM_EDIT(int3, MSG_BED, &absPreheatHPBTemp, BED_MINTEMP, BED_MAXTEMP - 15);
  1467. #endif
  1468. #if ENABLED(EEPROM_SETTINGS)
  1469. MENU_ITEM(function, MSG_STORE_EPROM, Config_StoreSettings);
  1470. #endif
  1471. END_MENU();
  1472. }
  1473. static void _reset_acceleration_rates() { planner.reset_acceleration_rates(); }
  1474. /**
  1475. *
  1476. * "Control" > "Motion" submenu
  1477. *
  1478. */
  1479. static void lcd_control_motion_menu() {
  1480. START_MENU();
  1481. MENU_ITEM(back, MSG_CONTROL);
  1482. #if ENABLED(AUTO_BED_LEVELING_FEATURE)
  1483. MENU_ITEM_EDIT(float32, MSG_ZPROBE_ZOFFSET, &zprobe_zoffset, Z_PROBE_OFFSET_RANGE_MIN, Z_PROBE_OFFSET_RANGE_MAX);
  1484. #endif
  1485. // Manual bed leveling, Bed Z:
  1486. #if ENABLED(MANUAL_BED_LEVELING)
  1487. MENU_ITEM_EDIT(float43, MSG_BED_Z, &mbl.z_offset, -1, 1);
  1488. #endif
  1489. MENU_ITEM_EDIT(float5, MSG_ACC, &planner.acceleration, 10, 99000);
  1490. MENU_ITEM_EDIT(float3, MSG_VXY_JERK, &planner.max_xy_jerk, 1, 990);
  1491. #if ENABLED(DELTA)
  1492. MENU_ITEM_EDIT(float3, MSG_VZ_JERK, &planner.max_z_jerk, 1, 990);
  1493. #else
  1494. MENU_ITEM_EDIT(float52, MSG_VZ_JERK, &planner.max_z_jerk, 0.1, 990);
  1495. #endif
  1496. MENU_ITEM_EDIT(float3, MSG_VE_JERK, &planner.max_e_jerk, 1, 990);
  1497. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_X, &planner.max_feedrate[X_AXIS], 1, 999);
  1498. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Y, &planner.max_feedrate[Y_AXIS], 1, 999);
  1499. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Z, &planner.max_feedrate[Z_AXIS], 1, 999);
  1500. MENU_ITEM_EDIT(float3, MSG_VMAX MSG_E, &planner.max_feedrate[E_AXIS], 1, 999);
  1501. MENU_ITEM_EDIT(float3, MSG_VMIN, &planner.min_feedrate, 0, 999);
  1502. MENU_ITEM_EDIT(float3, MSG_VTRAV_MIN, &planner.min_travel_feedrate, 0, 999);
  1503. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_X, &planner.max_acceleration_mm_per_s2[X_AXIS], 100, 99000, _reset_acceleration_rates);
  1504. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Y, &planner.max_acceleration_mm_per_s2[Y_AXIS], 100, 99000, _reset_acceleration_rates);
  1505. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Z, &planner.max_acceleration_mm_per_s2[Z_AXIS], 10, 99000, _reset_acceleration_rates);
  1506. MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_E, &planner.max_acceleration_mm_per_s2[E_AXIS], 100, 99000, _reset_acceleration_rates);
  1507. MENU_ITEM_EDIT(float5, MSG_A_RETRACT, &planner.retract_acceleration, 100, 99000);
  1508. MENU_ITEM_EDIT(float5, MSG_A_TRAVEL, &planner.travel_acceleration, 100, 99000);
  1509. MENU_ITEM_EDIT(float52, MSG_XSTEPS, &planner.axis_steps_per_mm[X_AXIS], 5, 9999);
  1510. MENU_ITEM_EDIT(float52, MSG_YSTEPS, &planner.axis_steps_per_mm[Y_AXIS], 5, 9999);
  1511. #if ENABLED(DELTA)
  1512. MENU_ITEM_EDIT(float52, MSG_ZSTEPS, &planner.axis_steps_per_mm[Z_AXIS], 5, 9999);
  1513. #else
  1514. MENU_ITEM_EDIT(float51, MSG_ZSTEPS, &planner.axis_steps_per_mm[Z_AXIS], 5, 9999);
  1515. #endif
  1516. MENU_ITEM_EDIT(float51, MSG_ESTEPS, &planner.axis_steps_per_mm[E_AXIS], 5, 9999);
  1517. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  1518. MENU_ITEM_EDIT(bool, MSG_ENDSTOP_ABORT, &stepper.abort_on_endstop_hit);
  1519. #endif
  1520. #if ENABLED(SCARA)
  1521. MENU_ITEM_EDIT(float74, MSG_XSCALE, &axis_scaling[X_AXIS], 0.5, 2);
  1522. MENU_ITEM_EDIT(float74, MSG_YSCALE, &axis_scaling[Y_AXIS], 0.5, 2);
  1523. #endif
  1524. END_MENU();
  1525. }
  1526. /**
  1527. *
  1528. * "Control" > "Filament" submenu
  1529. *
  1530. */
  1531. static void lcd_control_volumetric_menu() {
  1532. START_MENU();
  1533. MENU_ITEM(back, MSG_CONTROL);
  1534. MENU_ITEM_EDIT_CALLBACK(bool, MSG_VOLUMETRIC_ENABLED, &volumetric_enabled, calculate_volumetric_multipliers);
  1535. if (volumetric_enabled) {
  1536. #if EXTRUDERS == 1
  1537. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM, &filament_size[0], 1.5, 3.25, calculate_volumetric_multipliers);
  1538. #else //EXTRUDERS > 1
  1539. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM MSG_DIAM_E1, &filament_size[0], 1.5, 3.25, calculate_volumetric_multipliers);
  1540. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM MSG_DIAM_E2, &filament_size[1], 1.5, 3.25, calculate_volumetric_multipliers);
  1541. #if EXTRUDERS > 2
  1542. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM MSG_DIAM_E3, &filament_size[2], 1.5, 3.25, calculate_volumetric_multipliers);
  1543. #if EXTRUDERS > 3
  1544. MENU_MULTIPLIER_ITEM_EDIT_CALLBACK(float43, MSG_FILAMENT_DIAM MSG_DIAM_E4, &filament_size[3], 1.5, 3.25, calculate_volumetric_multipliers);
  1545. #endif //EXTRUDERS > 3
  1546. #endif //EXTRUDERS > 2
  1547. #endif //EXTRUDERS > 1
  1548. }
  1549. END_MENU();
  1550. }
  1551. /**
  1552. *
  1553. * "Control" > "Contrast" submenu
  1554. *
  1555. */
  1556. #if HAS_LCD_CONTRAST
  1557. static void lcd_set_contrast() {
  1558. ENCODER_DIRECTION_NORMAL();
  1559. if (encoderPosition) {
  1560. set_lcd_contrast(lcd_contrast + encoderPosition);
  1561. encoderPosition = 0;
  1562. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  1563. }
  1564. if (lcdDrawUpdate) {
  1565. lcd_implementation_drawedit(PSTR(MSG_CONTRAST),
  1566. #if LCD_CONTRAST_MAX >= 100
  1567. itostr3(lcd_contrast)
  1568. #else
  1569. itostr2(lcd_contrast)
  1570. #endif
  1571. );
  1572. }
  1573. if (LCD_CLICKED) lcd_goto_previous_menu(true);
  1574. }
  1575. #endif // HAS_LCD_CONTRAST
  1576. /**
  1577. *
  1578. * "Control" > "Retract" submenu
  1579. *
  1580. */
  1581. #if ENABLED(FWRETRACT)
  1582. static void lcd_control_retract_menu() {
  1583. START_MENU();
  1584. MENU_ITEM(back, MSG_CONTROL);
  1585. MENU_ITEM_EDIT(bool, MSG_AUTORETRACT, &autoretract_enabled);
  1586. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT, &retract_length, 0, 100);
  1587. #if EXTRUDERS > 1
  1588. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_SWAP, &retract_length_swap, 0, 100);
  1589. #endif
  1590. MENU_ITEM_EDIT(float3, MSG_CONTROL_RETRACTF, &retract_feedrate, 1, 999);
  1591. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_ZLIFT, &retract_zlift, 0, 999);
  1592. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_RECOVER, &retract_recover_length, 0, 100);
  1593. #if EXTRUDERS > 1
  1594. MENU_ITEM_EDIT(float52, MSG_CONTROL_RETRACT_RECOVER_SWAP, &retract_recover_length_swap, 0, 100);
  1595. #endif
  1596. MENU_ITEM_EDIT(float3, MSG_CONTROL_RETRACT_RECOVERF, &retract_recover_feedrate, 1, 999);
  1597. END_MENU();
  1598. }
  1599. #endif // FWRETRACT
  1600. #if ENABLED(SDSUPPORT)
  1601. #if !PIN_EXISTS(SD_DETECT)
  1602. static void lcd_sd_refresh() {
  1603. card.initsd();
  1604. currentMenuViewOffset = 0;
  1605. }
  1606. #endif
  1607. static void lcd_sd_updir() {
  1608. card.updir();
  1609. currentMenuViewOffset = 0;
  1610. }
  1611. /**
  1612. *
  1613. * "Print from SD" submenu
  1614. *
  1615. */
  1616. void lcd_sdcard_menu() {
  1617. ENCODER_DIRECTION_MENUS();
  1618. if (lcdDrawUpdate == 0 && LCD_CLICKED == 0) return; // nothing to do (so don't thrash the SD card)
  1619. uint16_t fileCnt = card.getnrfilenames();
  1620. START_MENU();
  1621. MENU_ITEM(back, MSG_MAIN);
  1622. card.getWorkDirName();
  1623. if (card.filename[0] == '/') {
  1624. #if !PIN_EXISTS(SD_DETECT)
  1625. MENU_ITEM(function, LCD_STR_REFRESH MSG_REFRESH, lcd_sd_refresh);
  1626. #endif
  1627. }
  1628. else {
  1629. MENU_ITEM(function, LCD_STR_FOLDER "..", lcd_sd_updir);
  1630. }
  1631. for (uint16_t i = 0; i < fileCnt; i++) {
  1632. if (_menuItemNr == _lineNr) {
  1633. card.getfilename(
  1634. #if ENABLED(SDCARD_RATHERRECENTFIRST)
  1635. fileCnt-1 -
  1636. #endif
  1637. i
  1638. );
  1639. if (card.filenameIsDir)
  1640. MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.longFilename);
  1641. else
  1642. MENU_ITEM(sdfile, MSG_CARD_MENU, card.filename, card.longFilename);
  1643. }
  1644. else {
  1645. MENU_ITEM_DUMMY();
  1646. }
  1647. }
  1648. END_MENU();
  1649. }
  1650. #endif //SDSUPPORT
  1651. /**
  1652. *
  1653. * Functions for editing single values
  1654. *
  1655. * The "menu_edit_type" macro generates the functions needed to edit a numerical value.
  1656. *
  1657. * For example, menu_edit_type(int, int3, itostr3, 1) expands into these functions:
  1658. *
  1659. * bool _menu_edit_int3();
  1660. * void menu_edit_int3(); // edit int (interactively)
  1661. * void menu_edit_callback_int3(); // edit int (interactively) with callback on completion
  1662. * static void _menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  1663. * static void menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  1664. * static void menu_action_setting_edit_callback_int3(const char* pstr, int* ptr, int minValue, int maxValue, screenFunc_t callback); // edit int with callback
  1665. *
  1666. * You can then use one of the menu macros to present the edit interface:
  1667. * MENU_ITEM_EDIT(int3, MSG_SPEED, &feedrate_multiplier, 10, 999)
  1668. *
  1669. * This expands into a more primitive menu item:
  1670. * MENU_ITEM(setting_edit_int3, MSG_SPEED, PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  1671. *
  1672. *
  1673. * Also: MENU_MULTIPLIER_ITEM_EDIT, MENU_ITEM_EDIT_CALLBACK, and MENU_MULTIPLIER_ITEM_EDIT_CALLBACK
  1674. *
  1675. * menu_action_setting_edit_int3(PSTR(MSG_SPEED), &feedrate_multiplier, 10, 999)
  1676. */
  1677. #define menu_edit_type(_type, _name, _strFunc, scale) \
  1678. bool _menu_edit_ ## _name () { \
  1679. ENCODER_DIRECTION_NORMAL(); \
  1680. bool isClicked = LCD_CLICKED; \
  1681. if ((int32_t)encoderPosition < 0) encoderPosition = 0; \
  1682. if ((int32_t)encoderPosition > maxEditValue) encoderPosition = maxEditValue; \
  1683. if (lcdDrawUpdate) \
  1684. lcd_implementation_drawedit(editLabel, _strFunc(((_type)((int32_t)encoderPosition + minEditValue)) / scale)); \
  1685. if (isClicked) { \
  1686. *((_type*)editValue) = ((_type)((int32_t)encoderPosition + minEditValue)) / scale; \
  1687. lcd_goto_previous_menu(true); \
  1688. } \
  1689. return isClicked; \
  1690. } \
  1691. void menu_edit_ ## _name () { _menu_edit_ ## _name(); } \
  1692. void menu_edit_callback_ ## _name () { if (_menu_edit_ ## _name ()) (*callbackFunc)(); } \
  1693. static void _menu_action_setting_edit_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue) { \
  1694. lcd_save_previous_menu(); \
  1695. \
  1696. lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; \
  1697. \
  1698. editLabel = pstr; \
  1699. editValue = ptr; \
  1700. minEditValue = minValue * scale; \
  1701. maxEditValue = maxValue * scale - minEditValue; \
  1702. encoderPosition = (*ptr) * scale - minEditValue; \
  1703. } \
  1704. static void menu_action_setting_edit_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue) { \
  1705. _menu_action_setting_edit_ ## _name(pstr, ptr, minValue, maxValue); \
  1706. currentScreen = menu_edit_ ## _name; \
  1707. }\
  1708. static void menu_action_setting_edit_callback_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue, screenFunc_t callback) { \
  1709. _menu_action_setting_edit_ ## _name(pstr, ptr, minValue, maxValue); \
  1710. currentScreen = menu_edit_callback_ ## _name; \
  1711. callbackFunc = callback; \
  1712. }
  1713. menu_edit_type(int, int3, itostr3, 1);
  1714. menu_edit_type(float, float3, ftostr3, 1);
  1715. menu_edit_type(float, float32, ftostr32, 100);
  1716. menu_edit_type(float, float43, ftostr43sign, 1000);
  1717. menu_edit_type(float, float5, ftostr5rj, 0.01);
  1718. menu_edit_type(float, float51, ftostr51sign, 10);
  1719. menu_edit_type(float, float52, ftostr52sign, 100);
  1720. menu_edit_type(unsigned long, long5, ftostr5rj, 0.01);
  1721. /**
  1722. *
  1723. * Handlers for RepRap World Keypad input
  1724. *
  1725. */
  1726. #if ENABLED(REPRAPWORLD_KEYPAD)
  1727. static void reprapworld_keypad_move_z_up() {
  1728. encoderPosition = 1;
  1729. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1730. lcd_move_z();
  1731. }
  1732. static void reprapworld_keypad_move_z_down() {
  1733. encoderPosition = -1;
  1734. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1735. lcd_move_z();
  1736. }
  1737. static void reprapworld_keypad_move_x_left() {
  1738. encoderPosition = -1;
  1739. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1740. lcd_move_x();
  1741. }
  1742. static void reprapworld_keypad_move_x_right() {
  1743. encoderPosition = 1;
  1744. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1745. lcd_move_x();
  1746. }
  1747. static void reprapworld_keypad_move_y_down() {
  1748. encoderPosition = 1;
  1749. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1750. lcd_move_y();
  1751. }
  1752. static void reprapworld_keypad_move_y_up() {
  1753. encoderPosition = -1;
  1754. move_menu_scale = REPRAPWORLD_KEYPAD_MOVE_STEP;
  1755. lcd_move_y();
  1756. }
  1757. static void reprapworld_keypad_move_home() {
  1758. enqueue_and_echo_commands_P(PSTR("G28")); // move all axes home
  1759. }
  1760. #endif // REPRAPWORLD_KEYPAD
  1761. /**
  1762. *
  1763. * Audio feedback for controller clicks
  1764. *
  1765. */
  1766. #if ENABLED(LCD_USE_I2C_BUZZER)
  1767. void lcd_buzz(long duration, uint16_t freq) { // called from buzz() in Marlin_main.cpp where lcd is unknown
  1768. lcd.buzz(duration, freq);
  1769. }
  1770. #endif
  1771. void lcd_quick_feedback() {
  1772. lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
  1773. next_button_update_ms = millis() + 500;
  1774. #if ENABLED(LCD_USE_I2C_BUZZER)
  1775. lcd.buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  1776. #elif PIN_EXISTS(BEEPER)
  1777. buzzer.tone(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
  1778. #else
  1779. delay(LCD_FEEDBACK_FREQUENCY_DURATION_MS);
  1780. #endif
  1781. }
  1782. /**
  1783. *
  1784. * Menu actions
  1785. *
  1786. */
  1787. static void menu_action_back() { lcd_goto_previous_menu(); }
  1788. static void menu_action_submenu(screenFunc_t func) { lcd_save_previous_menu(); lcd_goto_screen(func); }
  1789. static void menu_action_gcode(const char* pgcode) { enqueue_and_echo_commands_P(pgcode); }
  1790. static void menu_action_function(screenFunc_t func) { (*func)(); }
  1791. #if ENABLED(SDSUPPORT)
  1792. static void menu_action_sdfile(const char* filename, char* longFilename) {
  1793. UNUSED(longFilename);
  1794. card.openAndPrintFile(filename);
  1795. lcd_return_to_status();
  1796. }
  1797. static void menu_action_sddirectory(const char* filename, char* longFilename) {
  1798. UNUSED(longFilename);
  1799. card.chdir(filename);
  1800. encoderPosition = 0;
  1801. }
  1802. #endif //SDSUPPORT
  1803. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr) {UNUSED(pstr); *ptr = !(*ptr); }
  1804. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, screenFunc_t callback) {
  1805. menu_action_setting_edit_bool(pstr, ptr);
  1806. (*callback)();
  1807. }
  1808. #endif //ULTIPANEL
  1809. /** LCD API **/
  1810. void lcd_init() {
  1811. lcd_implementation_init();
  1812. #if ENABLED(NEWPANEL)
  1813. #if BUTTON_EXISTS(EN1)
  1814. SET_INPUT(BTN_EN1);
  1815. WRITE(BTN_EN1, HIGH);
  1816. #endif
  1817. #if BUTTON_EXISTS(EN2)
  1818. SET_INPUT(BTN_EN2);
  1819. WRITE(BTN_EN2, HIGH);
  1820. #endif
  1821. #if BUTTON_EXISTS(ENC)
  1822. SET_INPUT(BTN_ENC);
  1823. WRITE(BTN_ENC, HIGH);
  1824. #endif
  1825. #if ENABLED(REPRAPWORLD_KEYPAD)
  1826. pinMode(SHIFT_CLK, OUTPUT);
  1827. pinMode(SHIFT_LD, OUTPUT);
  1828. pinMode(SHIFT_OUT, INPUT);
  1829. WRITE(SHIFT_OUT, HIGH);
  1830. WRITE(SHIFT_LD, HIGH);
  1831. #endif
  1832. #if BUTTON_EXISTS(UP)
  1833. SET_INPUT(BTN_UP);
  1834. #endif
  1835. #if BUTTON_EXISTS(DWN)
  1836. SET_INPUT(BTN_DWN);
  1837. #endif
  1838. #if BUTTON_EXISTS(LFT)
  1839. SET_INPUT(BTN_LFT);
  1840. #endif
  1841. #if BUTTON_EXISTS(RT)
  1842. SET_INPUT(BTN_RT);
  1843. #endif
  1844. #else // Not NEWPANEL
  1845. #if ENABLED(SR_LCD_2W_NL) // Non latching 2 wire shift register
  1846. pinMode(SR_DATA_PIN, OUTPUT);
  1847. pinMode(SR_CLK_PIN, OUTPUT);
  1848. #elif defined(SHIFT_CLK)
  1849. pinMode(SHIFT_CLK, OUTPUT);
  1850. pinMode(SHIFT_LD, OUTPUT);
  1851. pinMode(SHIFT_EN, OUTPUT);
  1852. pinMode(SHIFT_OUT, INPUT);
  1853. WRITE(SHIFT_OUT, HIGH);
  1854. WRITE(SHIFT_LD, HIGH);
  1855. WRITE(SHIFT_EN, LOW);
  1856. #endif // SR_LCD_2W_NL
  1857. #endif//!NEWPANEL
  1858. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  1859. SET_INPUT(SD_DETECT_PIN);
  1860. WRITE(SD_DETECT_PIN, HIGH);
  1861. lcd_sd_status = 2; // UNKNOWN
  1862. #endif
  1863. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  1864. slow_buttons = 0;
  1865. #endif
  1866. lcd_buttons_update();
  1867. #if ENABLED(ULTIPANEL)
  1868. encoderDiff = 0;
  1869. #endif
  1870. }
  1871. int lcd_strlen(const char* s) {
  1872. int i = 0, j = 0;
  1873. while (s[i]) {
  1874. if ((s[i] & 0xc0) != 0x80) j++;
  1875. i++;
  1876. }
  1877. return j;
  1878. }
  1879. int lcd_strlen_P(const char* s) {
  1880. int j = 0;
  1881. while (pgm_read_byte(s)) {
  1882. if ((pgm_read_byte(s) & 0xc0) != 0x80) j++;
  1883. s++;
  1884. }
  1885. return j;
  1886. }
  1887. bool lcd_blink() {
  1888. static uint8_t blink = 0;
  1889. static millis_t next_blink_ms = 0;
  1890. millis_t ms = millis();
  1891. if (ELAPSED(ms, next_blink_ms)) {
  1892. blink ^= 0xFF;
  1893. next_blink_ms = ms + 1000 - LCD_UPDATE_INTERVAL / 2;
  1894. }
  1895. return blink != 0;
  1896. }
  1897. /**
  1898. * Update the LCD, read encoder buttons, etc.
  1899. * - Read button states
  1900. * - Check the SD Card slot state
  1901. * - Act on RepRap World keypad input
  1902. * - Update the encoder position
  1903. * - Apply acceleration to the encoder position
  1904. * - Set lcdDrawUpdate = LCDVIEW_CALL_REDRAW_NOW on controller events
  1905. * - Reset the Info Screen timeout if there's any input
  1906. * - Update status indicators, if any
  1907. *
  1908. * Run the current LCD menu handler callback function:
  1909. * - Call the handler only if lcdDrawUpdate != LCDVIEW_NONE
  1910. * - Before calling the handler, LCDVIEW_CALL_NO_REDRAW => LCDVIEW_NONE
  1911. * - Call the menu handler. Menu handlers should do the following:
  1912. * - If a value changes, set lcdDrawUpdate to LCDVIEW_REDRAW_NOW and draw the value
  1913. * (Encoder events automatically set lcdDrawUpdate for you.)
  1914. * - if (lcdDrawUpdate) { redraw }
  1915. * - Before exiting the handler set lcdDrawUpdate to:
  1916. * - LCDVIEW_CLEAR_CALL_REDRAW to clear screen and set LCDVIEW_CALL_REDRAW_NEXT.
  1917. * - LCDVIEW_REDRAW_NOW or LCDVIEW_NONE to keep drawingm but only in this loop.
  1918. * - LCDVIEW_REDRAW_NEXT to keep drawing and draw on the next loop also.
  1919. * - LCDVIEW_CALL_NO_REDRAW to keep drawing (or start drawing) with no redraw on the next loop.
  1920. * - NOTE: For graphical displays menu handlers may be called 2 or more times per loop,
  1921. * so don't change lcdDrawUpdate without considering this.
  1922. *
  1923. * After the menu handler callback runs (or not):
  1924. * - Clear the LCD if lcdDrawUpdate == LCDVIEW_CLEAR_CALL_REDRAW
  1925. * - Update lcdDrawUpdate for the next loop (i.e., move one state down, usually)
  1926. *
  1927. * No worries. This function is only called from the main thread.
  1928. */
  1929. void lcd_update() {
  1930. #if ENABLED(ULTIPANEL)
  1931. static millis_t return_to_status_ms = 0;
  1932. manage_manual_move();
  1933. #endif
  1934. lcd_buttons_update();
  1935. #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
  1936. bool sd_status = IS_SD_INSERTED;
  1937. if (sd_status != lcd_sd_status && lcd_detected()) {
  1938. lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
  1939. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  1940. #if ENABLED(LCD_PROGRESS_BAR) && ENABLED(ULTIPANEL)
  1941. currentScreen == lcd_status_screen
  1942. #endif
  1943. );
  1944. if (sd_status) {
  1945. card.initsd();
  1946. if (lcd_sd_status != 2) LCD_MESSAGEPGM(MSG_SD_INSERTED);
  1947. }
  1948. else {
  1949. card.release();
  1950. if (lcd_sd_status != 2) LCD_MESSAGEPGM(MSG_SD_REMOVED);
  1951. }
  1952. lcd_sd_status = sd_status;
  1953. }
  1954. #endif //SDSUPPORT && SD_DETECT_PIN
  1955. millis_t ms = millis();
  1956. if (ELAPSED(ms, next_lcd_update_ms)) {
  1957. next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL;
  1958. #if ENABLED(LCD_HAS_STATUS_INDICATORS)
  1959. lcd_implementation_update_indicators();
  1960. #endif
  1961. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  1962. slow_buttons = lcd_implementation_read_slow_buttons(); // buttons which take too long to read in interrupt context
  1963. #endif
  1964. #if ENABLED(ULTIPANEL)
  1965. #if ENABLED(REPRAPWORLD_KEYPAD)
  1966. #if ENABLED(DELTA) || ENABLED(SCARA)
  1967. #define _KEYPAD_MOVE_ALLOWED (axis_homed[X_AXIS] && axis_homed[Y_AXIS] && axis_homed[Z_AXIS])
  1968. #else
  1969. #define _KEYPAD_MOVE_ALLOWED true
  1970. #endif
  1971. if (REPRAPWORLD_KEYPAD_MOVE_HOME) reprapworld_keypad_move_home();
  1972. if (_KEYPAD_MOVE_ALLOWED) {
  1973. if (REPRAPWORLD_KEYPAD_MOVE_Z_UP) reprapworld_keypad_move_z_up();
  1974. if (REPRAPWORLD_KEYPAD_MOVE_Z_DOWN) reprapworld_keypad_move_z_down();
  1975. if (REPRAPWORLD_KEYPAD_MOVE_X_LEFT) reprapworld_keypad_move_x_left();
  1976. if (REPRAPWORLD_KEYPAD_MOVE_X_RIGHT) reprapworld_keypad_move_x_right();
  1977. if (REPRAPWORLD_KEYPAD_MOVE_Y_DOWN) reprapworld_keypad_move_y_down();
  1978. if (REPRAPWORLD_KEYPAD_MOVE_Y_UP) reprapworld_keypad_move_y_up();
  1979. }
  1980. #endif
  1981. bool encoderPastThreshold = (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP);
  1982. if (encoderPastThreshold || LCD_CLICKED) {
  1983. if (encoderPastThreshold) {
  1984. int32_t encoderMultiplier = 1;
  1985. #if ENABLED(ENCODER_RATE_MULTIPLIER)
  1986. if (encoderRateMultiplierEnabled) {
  1987. int32_t encoderMovementSteps = abs(encoderDiff) / ENCODER_PULSES_PER_STEP;
  1988. if (lastEncoderMovementMillis != 0) {
  1989. // Note that the rate is always calculated between to passes through the
  1990. // loop and that the abs of the encoderDiff value is tracked.
  1991. float encoderStepRate = (float)(encoderMovementSteps) / ((float)(ms - lastEncoderMovementMillis)) * 1000.0;
  1992. if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
  1993. else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
  1994. #if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
  1995. SERIAL_ECHO_START;
  1996. SERIAL_ECHO("Enc Step Rate: ");
  1997. SERIAL_ECHO(encoderStepRate);
  1998. SERIAL_ECHO(" Multiplier: ");
  1999. SERIAL_ECHO(encoderMultiplier);
  2000. SERIAL_ECHO(" ENCODER_10X_STEPS_PER_SEC: ");
  2001. SERIAL_ECHO(ENCODER_10X_STEPS_PER_SEC);
  2002. SERIAL_ECHO(" ENCODER_100X_STEPS_PER_SEC: ");
  2003. SERIAL_ECHOLN(ENCODER_100X_STEPS_PER_SEC);
  2004. #endif //ENCODER_RATE_MULTIPLIER_DEBUG
  2005. }
  2006. lastEncoderMovementMillis = ms;
  2007. } // encoderRateMultiplierEnabled
  2008. #endif //ENCODER_RATE_MULTIPLIER
  2009. encoderPosition += (encoderDiff * encoderMultiplier) / ENCODER_PULSES_PER_STEP;
  2010. encoderDiff = 0;
  2011. }
  2012. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  2013. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  2014. }
  2015. #endif //ULTIPANEL
  2016. // We arrive here every ~100ms when idling often enough.
  2017. // Instead of tracking the changes simply redraw the Info Screen ~1 time a second.
  2018. static int8_t lcd_status_update_delay = 1; // first update one loop delayed
  2019. if (
  2020. #if ENABLED(ULTIPANEL)
  2021. currentScreen == lcd_status_screen &&
  2022. #endif
  2023. !lcd_status_update_delay--) {
  2024. lcd_status_update_delay = 9;
  2025. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  2026. }
  2027. if (lcdDrawUpdate) {
  2028. switch (lcdDrawUpdate) {
  2029. case LCDVIEW_CALL_NO_REDRAW:
  2030. lcdDrawUpdate = LCDVIEW_NONE;
  2031. break;
  2032. case LCDVIEW_CLEAR_CALL_REDRAW: // set by handlers, then altered after (rarely occurs here)
  2033. case LCDVIEW_CALL_REDRAW_NEXT: // set by handlers, then altered after (never occurs here?)
  2034. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  2035. case LCDVIEW_REDRAW_NOW: // set above, or by a handler through LCDVIEW_CALL_REDRAW_NEXT
  2036. case LCDVIEW_NONE:
  2037. break;
  2038. }
  2039. #if ENABLED(DOGLCD) // Changes due to different driver architecture of the DOGM display
  2040. static int8_t dot_color = 0;
  2041. dot_color = 1 - dot_color;
  2042. u8g.firstPage();
  2043. do {
  2044. lcd_setFont(FONT_MENU);
  2045. u8g.setPrintPos(125, 0);
  2046. u8g.setColorIndex(dot_color); // Set color for the alive dot
  2047. u8g.drawPixel(127, 63); // draw alive dot
  2048. u8g.setColorIndex(1); // black on white
  2049. #if ENABLED(ULTIPANEL)
  2050. (*currentScreen)();
  2051. #else
  2052. lcd_status_screen();
  2053. #endif
  2054. } while (u8g.nextPage());
  2055. #else
  2056. #if ENABLED(ULTIPANEL)
  2057. (*currentScreen)();
  2058. #else
  2059. lcd_status_screen();
  2060. #endif
  2061. #endif
  2062. }
  2063. #if ENABLED(ULTIPANEL)
  2064. // Return to Status Screen after a timeout
  2065. if (currentScreen == lcd_status_screen || defer_return_to_status)
  2066. return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS;
  2067. else if (ELAPSED(ms, return_to_status_ms))
  2068. lcd_return_to_status();
  2069. #endif // ULTIPANEL
  2070. switch (lcdDrawUpdate) {
  2071. case LCDVIEW_CLEAR_CALL_REDRAW:
  2072. lcd_implementation_clear();
  2073. case LCDVIEW_CALL_REDRAW_NEXT:
  2074. lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
  2075. break;
  2076. case LCDVIEW_REDRAW_NOW:
  2077. lcdDrawUpdate = LCDVIEW_NONE;
  2078. break;
  2079. case LCDVIEW_NONE:
  2080. break;
  2081. }
  2082. }
  2083. }
  2084. void lcd_finishstatus(bool persist=false) {
  2085. #if !(ENABLED(LCD_PROGRESS_BAR) && (PROGRESS_MSG_EXPIRE > 0))
  2086. UNUSED(persist);
  2087. #endif
  2088. #if ENABLED(LCD_PROGRESS_BAR)
  2089. progress_bar_ms = millis();
  2090. #if PROGRESS_MSG_EXPIRE > 0
  2091. expire_status_ms = persist ? 0 : progress_bar_ms + PROGRESS_MSG_EXPIRE;
  2092. #endif
  2093. #endif
  2094. lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
  2095. #if ENABLED(FILAMENT_LCD_DISPLAY)
  2096. previous_lcd_status_ms = millis(); //get status message to show up for a while
  2097. #endif
  2098. }
  2099. #if ENABLED(LCD_PROGRESS_BAR) && PROGRESS_MSG_EXPIRE > 0
  2100. void dontExpireStatus() { expire_status_ms = 0; }
  2101. #endif
  2102. void set_utf_strlen(char* s, uint8_t n) {
  2103. uint8_t i = 0, j = 0;
  2104. while (s[i] && (j < n)) {
  2105. if ((s[i] & 0xc0u) != 0x80u) j++;
  2106. i++;
  2107. }
  2108. while (j++ < n) s[i++] = ' ';
  2109. s[i] = '\0';
  2110. }
  2111. bool lcd_hasstatus() { return (lcd_status_message[0] != '\0'); }
  2112. void lcd_setstatus(const char* message, bool persist) {
  2113. if (lcd_status_message_level > 0) return;
  2114. strncpy(lcd_status_message, message, 3 * (LCD_WIDTH));
  2115. set_utf_strlen(lcd_status_message, LCD_WIDTH);
  2116. lcd_finishstatus(persist);
  2117. }
  2118. void lcd_setstatuspgm(const char* message, uint8_t level) {
  2119. if (level >= lcd_status_message_level) {
  2120. strncpy_P(lcd_status_message, message, 3 * (LCD_WIDTH));
  2121. set_utf_strlen(lcd_status_message, LCD_WIDTH);
  2122. lcd_status_message_level = level;
  2123. lcd_finishstatus(level > 0);
  2124. }
  2125. }
  2126. void lcd_setalertstatuspgm(const char* message) {
  2127. lcd_setstatuspgm(message, 1);
  2128. #if ENABLED(ULTIPANEL)
  2129. lcd_return_to_status();
  2130. #endif
  2131. }
  2132. void lcd_reset_alert_level() { lcd_status_message_level = 0; }
  2133. #if HAS_LCD_CONTRAST
  2134. void set_lcd_contrast(int value) {
  2135. lcd_contrast = constrain(value, LCD_CONTRAST_MIN, LCD_CONTRAST_MAX);
  2136. u8g.setContrast(lcd_contrast);
  2137. }
  2138. #endif
  2139. #if ENABLED(ULTIPANEL)
  2140. /**
  2141. * Setup Rotary Encoder Bit Values (for two pin encoders to indicate movement)
  2142. * These values are independent of which pins are used for EN_A and EN_B indications
  2143. * The rotary encoder part is also independent to the chipset used for the LCD
  2144. */
  2145. #if defined(EN_A) && defined(EN_B)
  2146. #define encrot0 0
  2147. #define encrot1 2
  2148. #define encrot2 3
  2149. #define encrot3 1
  2150. #endif
  2151. #define GET_BUTTON_STATES(DST) \
  2152. uint8_t new_##DST = 0; \
  2153. WRITE(SHIFT_LD, LOW); \
  2154. WRITE(SHIFT_LD, HIGH); \
  2155. for (int8_t i = 0; i < 8; i++) { \
  2156. new_##DST >>= 1; \
  2157. if (READ(SHIFT_OUT)) SBI(new_##DST, 7); \
  2158. WRITE(SHIFT_CLK, HIGH); \
  2159. WRITE(SHIFT_CLK, LOW); \
  2160. } \
  2161. DST = ~new_##DST; //invert it, because a pressed switch produces a logical 0
  2162. /**
  2163. * Read encoder buttons from the hardware registers
  2164. * Warning: This function is called from interrupt context!
  2165. */
  2166. void lcd_buttons_update() {
  2167. #if ENABLED(NEWPANEL)
  2168. uint8_t newbutton = 0;
  2169. #if BUTTON_EXISTS(EN1)
  2170. if (BUTTON_PRESSED(EN1)) newbutton |= EN_A;
  2171. #endif
  2172. #if BUTTON_EXISTS(EN2)
  2173. if (BUTTON_PRESSED(EN2)) newbutton |= EN_B;
  2174. #endif
  2175. #if LCD_HAS_DIRECTIONAL_BUTTONS || BUTTON_EXISTS(ENC)
  2176. millis_t now = millis();
  2177. #endif
  2178. #if LCD_HAS_DIRECTIONAL_BUTTONS
  2179. if (ELAPSED(now, next_button_update_ms)) {
  2180. if (false) {
  2181. // for the else-ifs below
  2182. }
  2183. #if BUTTON_EXISTS(UP)
  2184. else if (BUTTON_PRESSED(UP)) {
  2185. encoderDiff = -(ENCODER_STEPS_PER_MENU_ITEM);
  2186. next_button_update_ms = now + 300;
  2187. }
  2188. #endif
  2189. #if BUTTON_EXISTS(DWN)
  2190. else if (BUTTON_PRESSED(DWN)) {
  2191. encoderDiff = ENCODER_STEPS_PER_MENU_ITEM;
  2192. next_button_update_ms = now + 300;
  2193. }
  2194. #endif
  2195. #if BUTTON_EXISTS(LFT)
  2196. else if (BUTTON_PRESSED(LFT)) {
  2197. encoderDiff = -(ENCODER_PULSES_PER_STEP);
  2198. next_button_update_ms = now + 300;
  2199. }
  2200. #endif
  2201. #if BUTTON_EXISTS(RT)
  2202. else if (BUTTON_PRESSED(RT)) {
  2203. encoderDiff = ENCODER_PULSES_PER_STEP;
  2204. next_button_update_ms = now + 300;
  2205. }
  2206. #endif
  2207. }
  2208. #endif
  2209. #if BUTTON_EXISTS(ENC)
  2210. if (ELAPSED(now, next_button_update_ms) && BUTTON_PRESSED(ENC)) newbutton |= EN_C;
  2211. #endif
  2212. buttons = newbutton;
  2213. #if ENABLED(LCD_HAS_SLOW_BUTTONS)
  2214. buttons |= slow_buttons;
  2215. #endif
  2216. #if ENABLED(REPRAPWORLD_KEYPAD)
  2217. GET_BUTTON_STATES(buttons_reprapworld_keypad);
  2218. #endif
  2219. #else
  2220. GET_BUTTON_STATES(buttons);
  2221. #endif //!NEWPANEL
  2222. #if ENABLED(REVERSE_MENU_DIRECTION) && ENABLED(REVERSE_ENCODER_DIRECTION)
  2223. #define ENCODER_DIFF_CW (encoderDiff -= encoderDirection)
  2224. #define ENCODER_DIFF_CCW (encoderDiff += encoderDirection)
  2225. #elif ENABLED(REVERSE_MENU_DIRECTION)
  2226. #define ENCODER_DIFF_CW (encoderDiff += encoderDirection)
  2227. #define ENCODER_DIFF_CCW (encoderDiff -= encoderDirection)
  2228. #elif ENABLED(REVERSE_ENCODER_DIRECTION)
  2229. #define ENCODER_DIFF_CW (encoderDiff--)
  2230. #define ENCODER_DIFF_CCW (encoderDiff++)
  2231. #else
  2232. #define ENCODER_DIFF_CW (encoderDiff++)
  2233. #define ENCODER_DIFF_CCW (encoderDiff--)
  2234. #endif
  2235. #define ENCODER_SPIN(_E1, _E2) switch (lastEncoderBits) { case _E1: ENCODER_DIFF_CW; break; case _E2: ENCODER_DIFF_CCW; }
  2236. //manage encoder rotation
  2237. uint8_t enc = 0;
  2238. if (buttons & EN_A) enc |= B01;
  2239. if (buttons & EN_B) enc |= B10;
  2240. if (enc != lastEncoderBits) {
  2241. switch (enc) {
  2242. case encrot0: ENCODER_SPIN(encrot3, encrot1); break;
  2243. case encrot1: ENCODER_SPIN(encrot0, encrot2); break;
  2244. case encrot2: ENCODER_SPIN(encrot1, encrot3); break;
  2245. case encrot3: ENCODER_SPIN(encrot2, encrot0); break;
  2246. }
  2247. }
  2248. lastEncoderBits = enc;
  2249. }
  2250. bool lcd_detected(void) {
  2251. #if (ENABLED(LCD_I2C_TYPE_MCP23017) || ENABLED(LCD_I2C_TYPE_MCP23008)) && ENABLED(DETECT_DEVICE)
  2252. return lcd.LcdDetected() == 1;
  2253. #else
  2254. return true;
  2255. #endif
  2256. }
  2257. bool lcd_clicked() { return LCD_CLICKED; }
  2258. #endif // ULTIPANEL
  2259. /*********************************/
  2260. /** Number to string conversion **/
  2261. /*********************************/
  2262. #define DIGIT(n) ('0' + (n))
  2263. #define DIGIMOD(n) DIGIT((n) % 10)
  2264. char conv[8];
  2265. // Convert float to rj string with 123 or -12 format
  2266. char *ftostr3(const float& x) { return itostr3((int)x); }
  2267. // Convert float to rj string with _123, -123, _-12, or __-1 format
  2268. char *ftostr4sign(const float& x) { return itostr4sign((int)x); }
  2269. // Convert unsigned int to string with 12 format
  2270. char* itostr2(const uint8_t& x) {
  2271. //sprintf(conv,"%5.1f",x);
  2272. int xx = x;
  2273. conv[0] = DIGIMOD(xx / 10);
  2274. conv[1] = DIGIMOD(xx);
  2275. conv[2] = '\0';
  2276. return conv;
  2277. }
  2278. // Convert float to string with +123.4 / -123.4 format
  2279. char* ftostr41sign(const float& x) {
  2280. int xx = int(abs(x * 10)) % 10000;
  2281. conv[0] = x >= 0 ? '+' : '-';
  2282. conv[1] = DIGIMOD(xx / 1000);
  2283. conv[2] = DIGIMOD(xx / 100);
  2284. conv[3] = DIGIMOD(xx / 10);
  2285. conv[4] = '.';
  2286. conv[5] = DIGIMOD(xx);
  2287. conv[6] = '\0';
  2288. return conv;
  2289. }
  2290. // Convert signed float to string with 023.45 / -23.45 format
  2291. char *ftostr32(const float& x) {
  2292. long xx = abs(x * 100);
  2293. conv[0] = x >= 0 ? DIGIMOD(xx / 10000) : '-';
  2294. conv[1] = DIGIMOD(xx / 1000);
  2295. conv[2] = DIGIMOD(xx / 100);
  2296. conv[3] = '.';
  2297. conv[4] = DIGIMOD(xx / 10);
  2298. conv[5] = DIGIMOD(xx);
  2299. conv[6] = '\0';
  2300. return conv;
  2301. }
  2302. // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
  2303. char* ftostr43sign(const float& x, char plus/*=' '*/) {
  2304. long xx = x * 1000;
  2305. if (xx == 0)
  2306. conv[0] = ' ';
  2307. else if (xx > 0)
  2308. conv[0] = plus;
  2309. else {
  2310. xx = -xx;
  2311. conv[0] = '-';
  2312. }
  2313. conv[1] = DIGIMOD(xx / 1000);
  2314. conv[2] = '.';
  2315. conv[3] = DIGIMOD(xx / 100);
  2316. conv[4] = DIGIMOD(xx / 10);
  2317. conv[5] = DIGIMOD(xx);
  2318. conv[6] = '\0';
  2319. return conv;
  2320. }
  2321. // Convert unsigned float to string with 1.23 format
  2322. char* ftostr12ns(const float& x) {
  2323. long xx = x * 100;
  2324. xx = abs(xx);
  2325. conv[0] = DIGIMOD(xx / 100);
  2326. conv[1] = '.';
  2327. conv[2] = DIGIMOD(xx / 10);
  2328. conv[3] = DIGIMOD(xx);
  2329. conv[4] = '\0';
  2330. return conv;
  2331. }
  2332. // Convert signed int to lj string with +012 / -012 format
  2333. char* itostr3sign(const int& x) {
  2334. int xx;
  2335. if (x >= 0) {
  2336. conv[0] = '+';
  2337. xx = x;
  2338. }
  2339. else {
  2340. conv[0] = '-';
  2341. xx = -x;
  2342. }
  2343. conv[1] = DIGIMOD(xx / 100);
  2344. conv[2] = DIGIMOD(xx / 10);
  2345. conv[3] = DIGIMOD(xx);
  2346. conv[4] = '.';
  2347. conv[5] = '0';
  2348. conv[6] = '\0';
  2349. return conv;
  2350. }
  2351. // Convert signed int to rj string with 123 or -12 format
  2352. char* itostr3(const int& x) {
  2353. int xx = x;
  2354. if (xx < 0) {
  2355. conv[0] = '-';
  2356. xx = -xx;
  2357. }
  2358. else
  2359. conv[0] = xx >= 100 ? DIGIMOD(xx / 100) : ' ';
  2360. conv[1] = xx >= 10 ? DIGIMOD(xx / 10) : ' ';
  2361. conv[2] = DIGIMOD(xx);
  2362. conv[3] = '\0';
  2363. return conv;
  2364. }
  2365. // Convert unsigned int to lj string with 123 format
  2366. char* itostr3left(const int& xx) {
  2367. if (xx >= 100) {
  2368. conv[0] = DIGIMOD(xx / 100);
  2369. conv[1] = DIGIMOD(xx / 10);
  2370. conv[2] = DIGIMOD(xx);
  2371. conv[3] = '\0';
  2372. }
  2373. else if (xx >= 10) {
  2374. conv[0] = DIGIMOD(xx / 10);
  2375. conv[1] = DIGIMOD(xx);
  2376. conv[2] = '\0';
  2377. }
  2378. else {
  2379. conv[0] = DIGIMOD(xx);
  2380. conv[1] = '\0';
  2381. }
  2382. return conv;
  2383. }
  2384. // Convert signed int to rj string with _123, -123, _-12, or __-1 format
  2385. char *itostr4sign(const int& x) {
  2386. int xx = abs(x);
  2387. int sign = 0;
  2388. if (xx >= 100) {
  2389. conv[1] = DIGIMOD(xx / 100);
  2390. conv[2] = DIGIMOD(xx / 10);
  2391. }
  2392. else if (xx >= 10) {
  2393. conv[0] = ' ';
  2394. sign = 1;
  2395. conv[2] = DIGIMOD(xx / 10);
  2396. }
  2397. else {
  2398. conv[0] = ' ';
  2399. conv[1] = ' ';
  2400. sign = 2;
  2401. }
  2402. conv[sign] = x < 0 ? '-' : ' ';
  2403. conv[3] = DIGIMOD(xx);
  2404. conv[4] = '\0';
  2405. return conv;
  2406. }
  2407. // Convert unsigned float to rj string with 12345 format
  2408. char* ftostr5rj(const float& x) {
  2409. long xx = abs(x);
  2410. conv[0] = xx >= 10000 ? DIGIMOD(xx / 10000) : ' ';
  2411. conv[1] = xx >= 1000 ? DIGIMOD(xx / 1000) : ' ';
  2412. conv[2] = xx >= 100 ? DIGIMOD(xx / 100) : ' ';
  2413. conv[3] = xx >= 10 ? DIGIMOD(xx / 10) : ' ';
  2414. conv[4] = DIGIMOD(xx);
  2415. conv[5] = '\0';
  2416. return conv;
  2417. }
  2418. // Convert signed float to string with +1234.5 format
  2419. char* ftostr51sign(const float& x) {
  2420. long xx = abs(x * 10);
  2421. conv[0] = (x >= 0) ? '+' : '-';
  2422. conv[1] = DIGIMOD(xx / 10000);
  2423. conv[2] = DIGIMOD(xx / 1000);
  2424. conv[3] = DIGIMOD(xx / 100);
  2425. conv[4] = DIGIMOD(xx / 10);
  2426. conv[5] = '.';
  2427. conv[6] = DIGIMOD(xx);
  2428. conv[7] = '\0';
  2429. return conv;
  2430. }
  2431. // Convert signed float to string with +123.45 format
  2432. char* ftostr52sign(const float& x) {
  2433. long xx = abs(x * 100);
  2434. conv[0] = (x >= 0) ? '+' : '-';
  2435. conv[1] = DIGIMOD(xx / 10000);
  2436. conv[2] = DIGIMOD(xx / 1000);
  2437. conv[3] = DIGIMOD(xx / 100);
  2438. conv[4] = '.';
  2439. conv[5] = DIGIMOD(xx / 10);
  2440. conv[6] = DIGIMOD(xx);
  2441. conv[7] = '\0';
  2442. return conv;
  2443. }
  2444. // Convert signed float to space-padded string with -_23.4_ format
  2445. char* ftostr52sp(const float& x) {
  2446. long xx = x * 100;
  2447. uint8_t dig;
  2448. if (xx < 0) { // negative val = -_0
  2449. xx = -xx;
  2450. conv[0] = '-';
  2451. dig = (xx / 1000) % 10;
  2452. conv[1] = dig ? DIGIT(dig) : ' ';
  2453. }
  2454. else { // positive val = __0
  2455. dig = (xx / 10000) % 10;
  2456. if (dig) {
  2457. conv[0] = DIGIT(dig);
  2458. conv[1] = DIGIMOD(xx / 1000);
  2459. }
  2460. else {
  2461. conv[0] = ' ';
  2462. dig = (xx / 1000) % 10;
  2463. conv[1] = dig ? DIGIT(dig) : ' ';
  2464. }
  2465. }
  2466. conv[2] = DIGIMOD(xx / 100); // lsd always
  2467. dig = xx % 10;
  2468. if (dig) { // 2 decimal places
  2469. conv[5] = DIGIT(dig);
  2470. conv[4] = DIGIMOD(xx / 10);
  2471. conv[3] = '.';
  2472. }
  2473. else { // 1 or 0 decimal place
  2474. dig = (xx / 10) % 10;
  2475. if (dig) {
  2476. conv[4] = DIGIT(dig);
  2477. conv[3] = '.';
  2478. }
  2479. else {
  2480. conv[3] = conv[4] = ' ';
  2481. }
  2482. conv[5] = ' ';
  2483. }
  2484. conv[6] = '\0';
  2485. return conv;
  2486. }
  2487. #endif // ULTRA_LCD