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

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