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

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