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

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