My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ultralcd.cpp 89KB

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