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

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