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

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