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.

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