My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ultralcd.cpp 89KB

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