ultralcd.cpp 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351
  1. #include "temperature.h"
  2. #include "ultralcd.h"
  3. #ifdef ULTRA_LCD
  4. #include "Marlin.h"
  5. #include "language.h"
  6. #include "cardreader.h"
  7. #include "temperature.h"
  8. #include "stepper.h"
  9. #include "ConfigurationStore.h"
  10. #include <string.h>
  11. #include "util.h"
  12. //#include "Configuration.h"
  13. #define _STRINGIFY(s) #s
  14. int8_t encoderDiff; /* encoderDiff is updated from interrupt context and added to encoderPosition every LCD update */
  15. extern int lcd_change_fil_state;
  16. int babystepMem[3];
  17. float babystepMemMM[3];
  18. union Data
  19. {
  20. byte b[2];
  21. int value;
  22. };
  23. int8_t ReInitLCD = 0;
  24. int8_t SDscrool = 0;
  25. int8_t SilentModeMenu = 0;
  26. int lcd_commands_type=0;
  27. int lcd_commands_step=0;
  28. bool isPrintPaused = false;
  29. bool menuExiting = false;
  30. /* Configuration settings */
  31. int plaPreheatHotendTemp;
  32. int plaPreheatHPBTemp;
  33. int plaPreheatFanSpeed;
  34. int absPreheatHotendTemp;
  35. int absPreheatHPBTemp;
  36. int absPreheatFanSpeed;
  37. int ppPreheatHotendTemp = PP_PREHEAT_HOTEND_TEMP;
  38. int ppPreheatHPBTemp = PP_PREHEAT_HPB_TEMP;
  39. int ppPreheatFanSpeed = PP_PREHEAT_FAN_SPEED;
  40. int petPreheatHotendTemp = PET_PREHEAT_HOTEND_TEMP;
  41. int petPreheatHPBTemp = PET_PREHEAT_HPB_TEMP;
  42. int petPreheatFanSpeed = PET_PREHEAT_FAN_SPEED;
  43. int hipsPreheatHotendTemp = HIPS_PREHEAT_HOTEND_TEMP;
  44. int hipsPreheatHPBTemp = HIPS_PREHEAT_HPB_TEMP;
  45. int hipsPreheatFanSpeed = HIPS_PREHEAT_FAN_SPEED;
  46. int flexPreheatHotendTemp = FLEX_PREHEAT_HOTEND_TEMP;
  47. int flexPreheatHPBTemp = FLEX_PREHEAT_HPB_TEMP;
  48. int flexPreheatFanSpeed = FLEX_PREHEAT_FAN_SPEED;
  49. #ifdef FILAMENT_LCD_DISPLAY
  50. unsigned long message_millis = 0;
  51. #endif
  52. #ifdef ULTIPANEL
  53. static float manual_feedrate[] = MANUAL_FEEDRATE;
  54. #endif // ULTIPANEL
  55. /* !Configuration settings */
  56. //Function pointer to menu functions.
  57. typedef void (*menuFunc_t)();
  58. uint8_t lcd_status_message_level;
  59. char lcd_status_message[LCD_WIDTH + 1] = ""; //////WELCOME!
  60. unsigned char firstrun = 1;
  61. #ifdef DOGLCD
  62. #include "dogm_lcd_implementation.h"
  63. #else
  64. #include "ultralcd_implementation_hitachi_HD44780.h"
  65. #endif
  66. /** forward declarations **/
  67. void copy_and_scalePID_i();
  68. void copy_and_scalePID_d();
  69. /* Different menus */
  70. static void lcd_status_screen();
  71. #ifdef ULTIPANEL
  72. extern bool powersupply;
  73. static void lcd_main_menu();
  74. static void lcd_tune_menu();
  75. static void lcd_prepare_menu();
  76. static void lcd_move_menu();
  77. static void lcd_control_menu();
  78. static void lcd_settings_menu();
  79. static void lcd_language_menu();
  80. static void lcd_control_temperature_menu();
  81. static void lcd_control_temperature_preheat_pla_settings_menu();
  82. static void lcd_control_temperature_preheat_abs_settings_menu();
  83. static void lcd_control_motion_menu();
  84. static void lcd_control_volumetric_menu();
  85. #ifdef DOGLCD
  86. static void lcd_set_contrast();
  87. #endif
  88. static void lcd_control_retract_menu();
  89. static void lcd_sdcard_menu();
  90. #ifdef DELTA_CALIBRATION_MENU
  91. static void lcd_delta_calibrate_menu();
  92. #endif // DELTA_CALIBRATION_MENU
  93. static void lcd_quick_feedback();//Cause an LCD refresh, and give the user visual or audible feedback that something has happened
  94. /* Different types of actions that can be used in menu items. */
  95. static void menu_action_back(menuFunc_t data);
  96. static void menu_action_submenu(menuFunc_t data);
  97. static void menu_action_gcode(const char* pgcode);
  98. static void menu_action_function(menuFunc_t data);
  99. static void menu_action_setlang(unsigned char lang);
  100. static void menu_action_sdfile(const char* filename, char* longFilename);
  101. static void menu_action_sddirectory(const char* filename, char* longFilename);
  102. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr);
  103. static void menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  104. static void menu_action_setting_edit_float3(const char* pstr, float* ptr, float minValue, float maxValue);
  105. static void menu_action_setting_edit_float32(const char* pstr, float* ptr, float minValue, float maxValue);
  106. static void menu_action_setting_edit_float43(const char* pstr, float* ptr, float minValue, float maxValue);
  107. static void menu_action_setting_edit_float5(const char* pstr, float* ptr, float minValue, float maxValue);
  108. static void menu_action_setting_edit_float51(const char* pstr, float* ptr, float minValue, float maxValue);
  109. static void menu_action_setting_edit_float52(const char* pstr, float* ptr, float minValue, float maxValue);
  110. static void menu_action_setting_edit_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue);
  111. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, menuFunc_t callbackFunc);
  112. static void menu_action_setting_edit_callback_int3(const char* pstr, int* ptr, int minValue, int maxValue, menuFunc_t callbackFunc);
  113. static void menu_action_setting_edit_callback_float3(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  114. static void menu_action_setting_edit_callback_float32(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  115. static void menu_action_setting_edit_callback_float43(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  116. static void menu_action_setting_edit_callback_float5(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  117. static void menu_action_setting_edit_callback_float51(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  118. static void menu_action_setting_edit_callback_float52(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  119. static void menu_action_setting_edit_callback_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue, menuFunc_t callbackFunc);
  120. #define ENCODER_FEEDRATE_DEADZONE 10
  121. #if !defined(LCD_I2C_VIKI)
  122. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  123. #define ENCODER_STEPS_PER_MENU_ITEM 5
  124. #endif
  125. #ifndef ENCODER_PULSES_PER_STEP
  126. #define ENCODER_PULSES_PER_STEP 1
  127. #endif
  128. #else
  129. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  130. #define ENCODER_STEPS_PER_MENU_ITEM 2 // VIKI LCD rotary encoder uses a different number of steps per rotation
  131. #endif
  132. #ifndef ENCODER_PULSES_PER_STEP
  133. #define ENCODER_PULSES_PER_STEP 1
  134. #endif
  135. #endif
  136. /* Helper macros for menus */
  137. #define START_MENU() do { \
  138. if (encoderPosition > 0x8000) encoderPosition = 0; \
  139. if (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM < currentMenuViewOffset) currentMenuViewOffset = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM;\
  140. uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
  141. bool wasClicked = LCD_CLICKED;\
  142. for(uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \
  143. _menuItemNr = 0;
  144. #define MENU_ITEM(type, label, args...) do { \
  145. if (_menuItemNr == _lineNr) { \
  146. if (lcdDrawUpdate) { \
  147. const char* _label_pstr = (label); \
  148. if ((encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) == _menuItemNr) { \
  149. lcd_implementation_drawmenu_ ## type ## _selected (_drawLineNr, _label_pstr , ## args ); \
  150. }else{\
  151. lcd_implementation_drawmenu_ ## type (_drawLineNr, _label_pstr , ## args ); \
  152. }\
  153. }\
  154. if (wasClicked && (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) == _menuItemNr) {\
  155. lcd_quick_feedback(); \
  156. menu_action_ ## type ( args ); \
  157. return;\
  158. }\
  159. }\
  160. _menuItemNr++;\
  161. } while(0)
  162. #define MENU_ITEM_DUMMY() do { _menuItemNr++; } while(0)
  163. #define MENU_ITEM_EDIT(type, label, args...) MENU_ITEM(setting_edit_ ## type, label, (label) , ## args )
  164. #define MENU_ITEM_EDIT_CALLBACK(type, label, args...) MENU_ITEM(setting_edit_callback_ ## type, label, (label) , ## args )
  165. #define END_MENU() \
  166. if (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM >= _menuItemNr) encoderPosition = _menuItemNr * ENCODER_STEPS_PER_MENU_ITEM - 1; \
  167. if ((uint8_t)(encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) >= currentMenuViewOffset + LCD_HEIGHT) { currentMenuViewOffset = (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) - LCD_HEIGHT + 1; lcdDrawUpdate = 1; _lineNr = currentMenuViewOffset - 1; _drawLineNr = -1; } \
  168. } } while(0)
  169. /** Used variables to keep track of the menu */
  170. #ifndef REPRAPWORLD_KEYPAD
  171. volatile uint8_t buttons;//Contains the bits of the currently pressed buttons.
  172. #else
  173. volatile uint8_t buttons_reprapworld_keypad; // to store the reprapworld_keypad shift register values
  174. #endif
  175. #ifdef LCD_HAS_SLOW_BUTTONS
  176. volatile uint8_t slow_buttons;//Contains the bits of the currently pressed buttons.
  177. #endif
  178. uint8_t currentMenuViewOffset; /* scroll offset in the current menu */
  179. uint32_t blocking_enc;
  180. uint8_t lastEncoderBits;
  181. uint32_t encoderPosition;
  182. #if (SDCARDDETECT > 0)
  183. bool lcd_oldcardstatus;
  184. #endif
  185. #endif //ULTIPANEL
  186. menuFunc_t currentMenu = lcd_status_screen; /* function pointer to the currently active menu */
  187. uint32_t lcd_next_update_millis;
  188. uint8_t lcd_status_update_delay;
  189. bool ignore_click = false;
  190. bool wait_for_unclick;
  191. uint8_t lcdDrawUpdate = 2; /* Set to none-zero when the LCD needs to draw, decreased after every draw. Set to 2 in LCD routines so the LCD gets at least 1 full redraw (first redraw is partial) */
  192. //prevMenu and prevEncoderPosition are used to store the previous menu location when editing settings.
  193. menuFunc_t prevMenu = NULL;
  194. uint16_t prevEncoderPosition;
  195. //Variables used when editing values.
  196. const char* editLabel;
  197. void* editValue;
  198. int32_t minEditValue, maxEditValue;
  199. menuFunc_t callbackFunc;
  200. // place-holders for Ki and Kd edits
  201. float raw_Ki, raw_Kd;
  202. static void lcd_goto_menu(menuFunc_t menu, const uint32_t encoder = 0, const bool feedback = true) {
  203. if (currentMenu != menu) {
  204. currentMenu = menu;
  205. encoderPosition = encoder;
  206. if (feedback) lcd_quick_feedback();
  207. // For LCD_PROGRESS_BAR re-initialize the custom characters
  208. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  209. lcd_set_custom_characters(menu == lcd_status_screen);
  210. #endif
  211. }
  212. }
  213. /* Main status screen. It's up to the implementation specific part to show what is needed. As this is very display dependent */
  214. /*
  215. extern char langbuffer[];
  216. void lcd_printPGM(const char *s1) {
  217. strncpy_P(langbuffer,s1,LCD_WIDTH);
  218. lcd.print(langbuffer);
  219. }
  220. */
  221. unsigned char langsel;
  222. void set_language_from_EEPROM() {
  223. unsigned char eep = eeprom_read_byte((unsigned char*)EEPROM_LANG);
  224. if (eep < LANG_NUM)
  225. {
  226. lang_selected = eep;
  227. langsel = 0;
  228. }
  229. else
  230. {
  231. lang_selected = 1;
  232. langsel = 1;
  233. }
  234. }
  235. void lcd_mylang();
  236. static void lcd_status_screen()
  237. {
  238. if (firstrun == 1)
  239. {
  240. firstrun = 0;
  241. set_language_from_EEPROM();
  242. strncpy_P(lcd_status_message, WELCOME_MSG, LCD_WIDTH);
  243. if (eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 1) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 2) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 3) == 255)
  244. {
  245. eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
  246. eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
  247. }
  248. if (langsel) {
  249. //strncpy_P(lcd_status_message, PSTR(">>>>>>>>>>>> PRESS v"), LCD_WIDTH);
  250. lcd_mylang();
  251. }
  252. }
  253. if (lcd_status_update_delay)
  254. lcd_status_update_delay--;
  255. else
  256. lcdDrawUpdate = 1;
  257. if (lcdDrawUpdate)
  258. {
  259. ReInitLCD++;
  260. if (ReInitLCD == 30) {
  261. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  262. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  263. currentMenu == lcd_status_screen
  264. #endif
  265. );
  266. ReInitLCD = 0 ;
  267. } else {
  268. if ((ReInitLCD % 10) == 0) {
  269. //lcd_implementation_nodisplay();
  270. lcd_implementation_init_noclear( // to maybe revive the LCD if static electricity killed it.
  271. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  272. currentMenu == lcd_status_screen
  273. #endif
  274. );
  275. }
  276. }
  277. //lcd_implementation_display();
  278. lcd_implementation_status_screen();
  279. //lcd_implementation_clear();
  280. lcd_status_update_delay = 10; /* redraw the main screen every second. This is easier then trying keep track of all things that change on the screen */
  281. if (lcd_commands_type != 0)
  282. {
  283. lcd_commands();
  284. }
  285. }
  286. #ifdef ULTIPANEL
  287. bool current_click = LCD_CLICKED;
  288. if (ignore_click) {
  289. if (wait_for_unclick) {
  290. if (!current_click) {
  291. ignore_click = wait_for_unclick = false;
  292. }
  293. else {
  294. current_click = false;
  295. }
  296. }
  297. else if (current_click) {
  298. lcd_quick_feedback();
  299. wait_for_unclick = true;
  300. current_click = false;
  301. }
  302. }
  303. //if (--langsel ==0) {langsel=1;current_click=true;}
  304. if (current_click)
  305. {
  306. lcd_goto_menu(lcd_main_menu);
  307. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  308. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  309. currentMenu == lcd_status_screen
  310. #endif
  311. );
  312. #ifdef FILAMENT_LCD_DISPLAY
  313. message_millis = millis(); // get status message to show up for a while
  314. #endif
  315. }
  316. #ifdef ULTIPANEL_FEEDMULTIPLY
  317. // Dead zone at 100% feedrate
  318. if ((feedmultiply < 100 && (feedmultiply + int(encoderPosition)) > 100) ||
  319. (feedmultiply > 100 && (feedmultiply + int(encoderPosition)) < 100))
  320. {
  321. encoderPosition = 0;
  322. feedmultiply = 100;
  323. }
  324. if (feedmultiply == 100 && int(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  325. {
  326. feedmultiply += int(encoderPosition) - ENCODER_FEEDRATE_DEADZONE;
  327. encoderPosition = 0;
  328. }
  329. else if (feedmultiply == 100 && int(encoderPosition) < -ENCODER_FEEDRATE_DEADZONE)
  330. {
  331. feedmultiply += int(encoderPosition) + ENCODER_FEEDRATE_DEADZONE;
  332. encoderPosition = 0;
  333. }
  334. else if (feedmultiply != 100)
  335. {
  336. feedmultiply += int(encoderPosition);
  337. encoderPosition = 0;
  338. }
  339. #endif //ULTIPANEL_FEEDMULTIPLY
  340. if (feedmultiply < 10)
  341. feedmultiply = 10;
  342. else if (feedmultiply > 999)
  343. feedmultiply = 999;
  344. #endif //ULTIPANEL
  345. }
  346. #ifdef ULTIPANEL
  347. void lcd_commands()
  348. {
  349. if (lcd_commands_type == 1) //// load filament sequence
  350. {
  351. if (lcd_commands_step == 0) { lcd_commands_step = 5; custom_message = true; }
  352. if (lcd_commands_step == 1 && !blocks_queued())
  353. {
  354. lcd_commands_step = 0;
  355. lcd_commands_type = 0;
  356. lcd_setstatuspgm(WELCOME_MSG);
  357. disable_z();
  358. custom_message = false;
  359. custom_message_type = 0;
  360. }
  361. if (lcd_commands_step == 2 && !blocks_queued())
  362. {
  363. lcd_setstatuspgm(MSG_LOADING_FILAMENT);
  364. enquecommand_P(PSTR(LOAD_FILAMENT_2));
  365. lcd_commands_step = 1;
  366. }
  367. if (lcd_commands_step == 3 && !blocks_queued())
  368. {
  369. enquecommand_P(PSTR(LOAD_FILAMENT_1));
  370. lcd_commands_step = 2;
  371. }
  372. if (lcd_commands_step == 4 && !blocks_queued())
  373. {
  374. lcd_setstatuspgm(MSG_INSERT_FILAMENT);
  375. enquecommand_P(PSTR(LOAD_FILAMENT_0));
  376. lcd_commands_step = 3;
  377. }
  378. if (lcd_commands_step == 5 && !blocks_queued())
  379. {
  380. lcd_setstatuspgm(MSG_PLEASE_WAIT);
  381. enable_z();
  382. custom_message = true;
  383. custom_message_type = 2;
  384. lcd_commands_step = 4;
  385. }
  386. }
  387. if (lcd_commands_type == 2) /// stop print
  388. {
  389. if (lcd_commands_step == 0) { lcd_commands_step = 6; custom_message = true; }
  390. if (lcd_commands_step == 1 && !blocks_queued())
  391. {
  392. lcd_commands_step = 0;
  393. lcd_commands_type = 0;
  394. lcd_setstatuspgm(WELCOME_MSG);
  395. custom_message = false;
  396. }
  397. if (lcd_commands_step == 2 && !blocks_queued())
  398. {
  399. setTargetBed(0);
  400. setTargetHotend(0, 0);
  401. setTargetHotend(0, 1);
  402. setTargetHotend(0, 2);
  403. manage_heater();
  404. lcd_setstatuspgm(WELCOME_MSG);
  405. cancel_heatup = false;
  406. lcd_commands_step = 1;
  407. }
  408. if (lcd_commands_step == 3 && !blocks_queued())
  409. {
  410. enquecommand_P(PSTR("M84"));
  411. autotempShutdown();
  412. lcd_commands_step = 2;
  413. }
  414. if (lcd_commands_step == 4 && !blocks_queued())
  415. {
  416. enquecommand_P(PSTR("G90"));
  417. #ifdef X_CANCEL_POS
  418. enquecommand_P(PSTR("G1 X" STRINGIFY(X_CANCEL_POS) " Y" STRINGIFY(Y_CANCEL_POS) " E0 F7000"));
  419. #else
  420. enquecommand_P(PSTR("G1 X50 Y" STRINGIFY(Y_MAX_POS) " E0 F7000"));
  421. #endif
  422. lcd_ignore_click(false);
  423. lcd_commands_step = 3;
  424. }
  425. if (lcd_commands_step == 5 && !blocks_queued())
  426. {
  427. lcd_setstatuspgm(MSG_PRINT_ABORTED);
  428. enquecommand_P(PSTR("G91"));
  429. enquecommand_P(PSTR("G1 Z15 F1500"));
  430. lcd_commands_step = 4;
  431. }
  432. if (lcd_commands_step == 6 && !blocks_queued())
  433. {
  434. lcd_setstatuspgm(MSG_PRINT_ABORTED);
  435. cancel_heatup = true;
  436. setTargetBed(0);
  437. setTargetHotend(0, 0);
  438. setTargetHotend(0, 1);
  439. setTargetHotend(0, 2);
  440. manage_heater();
  441. lcd_commands_step = 5;
  442. }
  443. }
  444. if (lcd_commands_type == 3)
  445. {
  446. lcd_commands_type = 0;
  447. }
  448. }
  449. static void lcd_return_to_status() {
  450. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  451. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  452. currentMenu == lcd_status_screen
  453. #endif
  454. );
  455. lcd_goto_menu(lcd_status_screen, 0, false);
  456. }
  457. static void lcd_sdcard_pause() {
  458. card.pauseSDPrint();
  459. isPrintPaused = true;
  460. lcdDrawUpdate = 3;
  461. }
  462. static void lcd_sdcard_resume() {
  463. card.startFileprint();
  464. isPrintPaused = false;
  465. lcdDrawUpdate = 3;
  466. }
  467. float move_menu_scale;
  468. static void lcd_move_menu_axis();
  469. /* Menu implementation */
  470. void lcd_preheat_pla()
  471. {
  472. setTargetHotend0(plaPreheatHotendTemp);
  473. setTargetBed(plaPreheatHPBTemp);
  474. fanSpeed = 0;
  475. lcd_return_to_status();
  476. setWatch(); // heater sanity check timer
  477. }
  478. void lcd_preheat_abs()
  479. {
  480. setTargetHotend0(absPreheatHotendTemp);
  481. setTargetBed(absPreheatHPBTemp);
  482. fanSpeed = 0;
  483. lcd_return_to_status();
  484. setWatch(); // heater sanity check timer
  485. }
  486. void lcd_preheat_pp()
  487. {
  488. setTargetHotend0(ppPreheatHotendTemp);
  489. setTargetBed(ppPreheatHPBTemp);
  490. fanSpeed = 0;
  491. lcd_return_to_status();
  492. setWatch(); // heater sanity check timer
  493. }
  494. void lcd_preheat_pet()
  495. {
  496. setTargetHotend0(petPreheatHotendTemp);
  497. setTargetBed(petPreheatHPBTemp);
  498. fanSpeed = 0;
  499. lcd_return_to_status();
  500. setWatch(); // heater sanity check timer
  501. }
  502. void lcd_preheat_hips()
  503. {
  504. setTargetHotend0(hipsPreheatHotendTemp);
  505. setTargetBed(hipsPreheatHPBTemp);
  506. fanSpeed = 0;
  507. lcd_return_to_status();
  508. setWatch(); // heater sanity check timer
  509. }
  510. void lcd_preheat_flex()
  511. {
  512. setTargetHotend0(flexPreheatHotendTemp);
  513. setTargetBed(flexPreheatHPBTemp);
  514. fanSpeed = 0;
  515. lcd_return_to_status();
  516. setWatch(); // heater sanity check timer
  517. }
  518. void lcd_cooldown()
  519. {
  520. setTargetHotend0(0);
  521. setTargetHotend1(0);
  522. setTargetHotend2(0);
  523. setTargetBed(0);
  524. fanSpeed = 0;
  525. lcd_return_to_status();
  526. }
  527. static void lcd_preheat_menu()
  528. {
  529. START_MENU();
  530. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  531. MENU_ITEM(function, PSTR("ABS - " STRINGIFY(ABS_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(ABS_PREHEAT_HPB_TEMP)), lcd_preheat_abs);
  532. MENU_ITEM(function, PSTR("PLA - " STRINGIFY(PLA_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(PLA_PREHEAT_HPB_TEMP)), lcd_preheat_pla);
  533. MENU_ITEM(function, PSTR("PET - " STRINGIFY(PET_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(PET_PREHEAT_HPB_TEMP)), lcd_preheat_pet);
  534. MENU_ITEM(function, PSTR("HIPS - " STRINGIFY(HIPS_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(HIPS_PREHEAT_HPB_TEMP)), lcd_preheat_hips);
  535. MENU_ITEM(function, PSTR("PP - " STRINGIFY(PP_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(PP_PREHEAT_HPB_TEMP)), lcd_preheat_pp);
  536. MENU_ITEM(function, PSTR("FLEX - " STRINGIFY(FLEX_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(FLEX_PREHEAT_HPB_TEMP)), lcd_preheat_flex);
  537. MENU_ITEM(function, MSG_COOLDOWN, lcd_cooldown);
  538. END_MENU();
  539. }
  540. static void lcd_support_menu()
  541. {
  542. START_MENU();
  543. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  544. // Ideally this block would be optimized out by the compiler.
  545. const uint8_t fw_string_len = strlen_P(FW_VERSION_STR_P());
  546. if (fw_string_len < 6) {
  547. MENU_ITEM(back, PSTR(MSG_FW_VERSION " - " FW_version), lcd_main_menu);
  548. } else {
  549. MENU_ITEM(back, PSTR("FW - " FW_version), lcd_main_menu);
  550. }
  551. MENU_ITEM(back, MSG_PRUSA3D, lcd_main_menu);
  552. MENU_ITEM(back, MSG_PRUSA3D_FORUM, lcd_main_menu);
  553. MENU_ITEM(back, MSG_PRUSA3D_HOWTO, lcd_main_menu);
  554. MENU_ITEM(back, PSTR("------------"), lcd_main_menu);
  555. MENU_ITEM(back, PSTR(FILAMENT_SIZE), lcd_main_menu);
  556. MENU_ITEM(back, PSTR(ELECTRONICS),lcd_main_menu);
  557. MENU_ITEM(back, PSTR(NOZZLE_TYPE),lcd_main_menu);
  558. MENU_ITEM(back, PSTR("------------"), lcd_main_menu);
  559. MENU_ITEM(back, PSTR("Date: "), lcd_main_menu);
  560. MENU_ITEM(back, PSTR(__DATE__), lcd_main_menu);
  561. END_MENU();
  562. }
  563. void lcd_unLoadFilament()
  564. {
  565. if (degHotend0() > EXTRUDE_MINTEMP) {
  566. enquecommand_P(PSTR(UNLOAD_FILAMENT_0));
  567. enquecommand_P(PSTR(UNLOAD_FILAMENT_1));
  568. } else {
  569. lcd_implementation_clear();
  570. lcd.setCursor(0, 0);
  571. lcd_printPGM(MSG_ERROR);
  572. lcd.setCursor(0, 2);
  573. lcd_printPGM(MSG_PREHEAT_NOZZLE);
  574. delay(2000);
  575. lcd_implementation_clear();
  576. }
  577. lcd_return_to_status();
  578. }
  579. void lcd_change_filament() {
  580. lcd_implementation_clear();
  581. lcd.setCursor(0, 1);
  582. lcd_printPGM(MSG_CHANGING_FILAMENT);
  583. }
  584. void lcd_wait_interact() {
  585. lcd_implementation_clear();
  586. lcd.setCursor(0, 1);
  587. lcd_printPGM(MSG_INSERT_FILAMENT);
  588. lcd.setCursor(0, 2);
  589. lcd_printPGM(MSG_PRESS);
  590. }
  591. void lcd_change_success() {
  592. lcd_implementation_clear();
  593. lcd.setCursor(0, 2);
  594. lcd_printPGM(MSG_CHANGE_SUCCESS);
  595. }
  596. void lcd_loading_color() {
  597. lcd_implementation_clear();
  598. lcd.setCursor(0, 0);
  599. lcd_printPGM(MSG_LOADING_COLOR);
  600. lcd.setCursor(0, 2);
  601. lcd_printPGM(MSG_PLEASE_WAIT);
  602. for (int i = 0; i < 20; i++) {
  603. lcd.setCursor(i, 3);
  604. lcd.print(".");
  605. for (int j = 0; j < 10 ; j++) {
  606. manage_heater();
  607. manage_inactivity(true);
  608. delay(85);
  609. }
  610. }
  611. }
  612. void lcd_loading_filament() {
  613. lcd_implementation_clear();
  614. lcd.setCursor(0, 0);
  615. lcd_printPGM(MSG_LOADING_FILAMENT);
  616. lcd.setCursor(0, 2);
  617. lcd_printPGM(MSG_PLEASE_WAIT);
  618. for (int i = 0; i < 20; i++) {
  619. lcd.setCursor(i, 3);
  620. lcd.print(".");
  621. for (int j = 0; j < 10 ; j++) {
  622. manage_heater();
  623. manage_inactivity(true);
  624. delay(110);
  625. }
  626. }
  627. }
  628. void lcd_alright() {
  629. int enc_dif = 0;
  630. int cursor_pos = 1;
  631. lcd_implementation_clear();
  632. lcd.setCursor(0, 0);
  633. lcd_printPGM(MSG_CORRECTLY);
  634. lcd.setCursor(1, 1);
  635. lcd_printPGM(MSG_YES);
  636. lcd.setCursor(1, 2);
  637. lcd_printPGM(MSG_NOT_LOADED);
  638. lcd.setCursor(1, 3);
  639. lcd_printPGM(MSG_NOT_COLOR);
  640. lcd.setCursor(0, 1);
  641. lcd.print(">");
  642. enc_dif = encoderDiff;
  643. while (lcd_change_fil_state == 0) {
  644. manage_heater();
  645. manage_inactivity(true);
  646. if ( abs((enc_dif - encoderDiff)) > 4 ) {
  647. if ( (abs(enc_dif - encoderDiff)) > 1 ) {
  648. if (enc_dif > encoderDiff ) {
  649. cursor_pos --;
  650. }
  651. if (enc_dif < encoderDiff ) {
  652. cursor_pos ++;
  653. }
  654. if (cursor_pos > 3) {
  655. cursor_pos = 3;
  656. }
  657. if (cursor_pos < 1) {
  658. cursor_pos = 1;
  659. }
  660. lcd.setCursor(0, 1);
  661. lcd.print(" ");
  662. lcd.setCursor(0, 2);
  663. lcd.print(" ");
  664. lcd.setCursor(0, 3);
  665. lcd.print(" ");
  666. lcd.setCursor(0, cursor_pos);
  667. lcd.print(">");
  668. enc_dif = encoderDiff;
  669. delay(100);
  670. }
  671. }
  672. if (lcd_clicked()) {
  673. lcd_change_fil_state = cursor_pos;
  674. delay(500);
  675. }
  676. };
  677. lcd_implementation_clear();
  678. lcd_return_to_status();
  679. }
  680. void lcd_LoadFilament()
  681. {
  682. if (degHotend0() > EXTRUDE_MINTEMP)
  683. {
  684. custom_message = true;
  685. lcd_commands_type = 1;
  686. SERIAL_ECHOLN("Loading filament");
  687. // commands() will handle the rest
  688. }
  689. else
  690. {
  691. lcd_implementation_clear();
  692. lcd.setCursor(0, 0);
  693. lcd_printPGM(MSG_ERROR);
  694. lcd.setCursor(0, 2);
  695. lcd_printPGM(MSG_PREHEAT_NOZZLE);
  696. delay(2000);
  697. lcd_implementation_clear();
  698. }
  699. lcd_return_to_status();
  700. }
  701. static void lcd_menu_statistics()
  702. {
  703. if (IS_SD_PRINTING)
  704. {
  705. int _met = total_filament_used / 100000;
  706. int _cm = (total_filament_used - (_met * 100000))/10;
  707. int _t = (millis() - starttime) / 1000;
  708. int _h = _t / 3600;
  709. int _m = (_t - (_h * 60)) / 60;
  710. int _s = _t - ((_h * 3600) + (_m * 60));
  711. lcd.setCursor(0, 0);
  712. lcd_printPGM(MSG_STATS_FILAMENTUSED);
  713. lcd.setCursor(6, 1);
  714. lcd.print(itostr3(_met));
  715. lcd.print("m ");
  716. lcd.print(ftostr32ns(_cm));
  717. lcd.print("cm");
  718. lcd.setCursor(0, 2);
  719. lcd_printPGM(MSG_STATS_PRINTTIME);
  720. lcd.setCursor(8, 3);
  721. lcd.print(itostr2(_h));
  722. lcd.print("h ");
  723. lcd.print(itostr2(_m));
  724. lcd.print("m ");
  725. lcd.print(itostr2(_s));
  726. lcd.print("s");
  727. if (lcd_clicked())
  728. {
  729. lcd_quick_feedback();
  730. lcd_return_to_status();
  731. }
  732. }
  733. else
  734. {
  735. unsigned long _filament = eeprom_read_dword((uint32_t *)EEPROM_FILAMENTUSED);
  736. unsigned long _time = eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME);
  737. uint8_t _days, _hours, _minutes;
  738. float _filament_m = (float)_filament;
  739. int _filament_km = (_filament >= 100000) ? _filament / 100000 : 0;
  740. if (_filament_km > 0) _filament_m = _filament - (_filament_km * 100000);
  741. _days = _time / 1440;
  742. _hours = (_time - (_days * 1440)) / 60;
  743. _minutes = _time - ((_days * 1440) + (_hours * 60));
  744. lcd_implementation_clear();
  745. lcd.setCursor(0, 0);
  746. lcd_printPGM(MSG_STATS_TOTALFILAMENT);
  747. lcd.setCursor(17 - strlen(ftostr32ns(_filament_m)), 1);
  748. lcd.print(ftostr32ns(_filament_m));
  749. if (_filament_km > 0)
  750. {
  751. lcd.setCursor(17 - strlen(ftostr32ns(_filament_m)) - 3, 1);
  752. lcd.print("km");
  753. lcd.setCursor(17 - strlen(ftostr32ns(_filament_m)) - 8, 1);
  754. lcd.print(itostr4(_filament_km));
  755. }
  756. lcd.setCursor(18, 1);
  757. lcd.print("m");
  758. lcd.setCursor(0, 2);
  759. lcd_printPGM(MSG_STATS_TOTALPRINTTIME);;
  760. lcd.setCursor(18, 3);
  761. lcd.print("m");
  762. lcd.setCursor(14, 3);
  763. lcd.print(itostr3(_minutes));
  764. lcd.setCursor(14, 3);
  765. lcd.print(":");
  766. lcd.setCursor(12, 3);
  767. lcd.print("h");
  768. lcd.setCursor(9, 3);
  769. lcd.print(itostr3(_hours));
  770. lcd.setCursor(9, 3);
  771. lcd.print(":");
  772. lcd.setCursor(7, 3);
  773. lcd.print("d");
  774. lcd.setCursor(4, 3);
  775. lcd.print(itostr3(_days));
  776. while (!lcd_clicked())
  777. {
  778. manage_heater();
  779. manage_inactivity(true);
  780. delay(100);
  781. }
  782. lcd_quick_feedback();
  783. lcd_return_to_status();
  784. }
  785. }
  786. static void _lcd_move(const char *name, int axis, int min, int max) {
  787. if (encoderPosition != 0) {
  788. refresh_cmd_timeout();
  789. current_position[axis] += float((int)encoderPosition) * move_menu_scale;
  790. if (min_software_endstops && current_position[axis] < min) current_position[axis] = min;
  791. if (max_software_endstops && current_position[axis] > max) current_position[axis] = max;
  792. encoderPosition = 0;
  793. world2machine_clamp(current_position[X_AXIS], current_position[Y_AXIS]);
  794. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis] / 60, active_extruder);
  795. lcdDrawUpdate = 1;
  796. }
  797. if (lcdDrawUpdate) lcd_implementation_drawedit(name, ftostr31(current_position[axis]));
  798. if (LCD_CLICKED) lcd_goto_menu(lcd_move_menu_axis);
  799. }
  800. static void lcd_move_e()
  801. {
  802. if (encoderPosition != 0)
  803. {
  804. current_position[E_AXIS] += float((int)encoderPosition) * move_menu_scale;
  805. encoderPosition = 0;
  806. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[E_AXIS] / 60, active_extruder);
  807. lcdDrawUpdate = 1;
  808. }
  809. if (lcdDrawUpdate)
  810. {
  811. lcd_implementation_drawedit(PSTR("Extruder"), ftostr31(current_position[E_AXIS]));
  812. }
  813. if (LCD_CLICKED) lcd_goto_menu(lcd_move_menu_axis);
  814. }
  815. // Save a single axis babystep value.
  816. void EEPROM_save_B(int pos, int* value)
  817. {
  818. union Data data;
  819. data.value = *value;
  820. eeprom_update_byte((unsigned char*)pos, data.b[0]);
  821. eeprom_update_byte((unsigned char*)pos + 1, data.b[1]);
  822. }
  823. // Read a single axis babystep value.
  824. void EEPROM_read_B(int pos, int* value)
  825. {
  826. union Data data;
  827. data.b[0] = eeprom_read_byte((unsigned char*)pos);
  828. data.b[1] = eeprom_read_byte((unsigned char*)pos + 1);
  829. *value = data.value;
  830. }
  831. static void lcd_move_x() {
  832. _lcd_move(PSTR("X"), X_AXIS, X_MIN_POS, X_MAX_POS);
  833. }
  834. static void lcd_move_y() {
  835. _lcd_move(PSTR("Y"), Y_AXIS, Y_MIN_POS, Y_MAX_POS);
  836. }
  837. static void lcd_move_z() {
  838. _lcd_move(PSTR("Z"), Z_AXIS, Z_MIN_POS, Z_MAX_POS);
  839. }
  840. static void _lcd_babystep(int axis, const char *msg) {
  841. if (encoderPosition != 0)
  842. {
  843. CRITICAL_SECTION_START
  844. babystepsTodo[axis] += (int)encoderPosition;
  845. CRITICAL_SECTION_END
  846. babystepMem[axis] += (int)encoderPosition;
  847. babystepMemMM[axis] = babystepMem[axis]/axis_steps_per_unit[Z_AXIS];
  848. delay(50);
  849. encoderPosition = 0;
  850. lcdDrawUpdate = 1;
  851. }
  852. if (lcdDrawUpdate) lcd_implementation_drawedit_2(msg, ftostr13ns(babystepMemMM[axis]));
  853. if (LCD_CLICKED || menuExiting) {
  854. // Only update the EEPROM when leaving the menu.
  855. EEPROM_save_B(
  856. (axis == 0) ? EEPROM_BABYSTEP_X : ((axis == 1) ? EEPROM_BABYSTEP_Y : EEPROM_BABYSTEP_Z),
  857. &babystepMem[axis]);
  858. }
  859. if (LCD_CLICKED) lcd_goto_menu(lcd_main_menu);
  860. }
  861. static void lcd_babystep_x() {
  862. _lcd_babystep(X_AXIS, (MSG_BABYSTEPPING_X));
  863. }
  864. static void lcd_babystep_y() {
  865. _lcd_babystep(Y_AXIS, (MSG_BABYSTEPPING_Y));
  866. }
  867. static void lcd_babystep_z() {
  868. _lcd_babystep(Z_AXIS, (MSG_BABYSTEPPING_Z));
  869. }
  870. void lcd_adjust_z() {
  871. int enc_dif = 0;
  872. int cursor_pos = 1;
  873. int fsm = 0;
  874. lcd_implementation_clear();
  875. lcd.setCursor(0, 0);
  876. lcd_printPGM(MSG_ADJUSTZ);
  877. lcd.setCursor(1, 1);
  878. lcd_printPGM(MSG_YES);
  879. lcd.setCursor(1, 2);
  880. lcd_printPGM(MSG_NO);
  881. lcd.setCursor(0, 1);
  882. lcd.print(">");
  883. enc_dif = encoderDiff;
  884. while (fsm == 0) {
  885. manage_heater();
  886. manage_inactivity(true);
  887. if ( abs((enc_dif - encoderDiff)) > 4 ) {
  888. if ( (abs(enc_dif - encoderDiff)) > 1 ) {
  889. if (enc_dif > encoderDiff ) {
  890. cursor_pos --;
  891. }
  892. if (enc_dif < encoderDiff ) {
  893. cursor_pos ++;
  894. }
  895. if (cursor_pos > 2) {
  896. cursor_pos = 2;
  897. }
  898. if (cursor_pos < 1) {
  899. cursor_pos = 1;
  900. }
  901. lcd.setCursor(0, 1);
  902. lcd.print(" ");
  903. lcd.setCursor(0, 2);
  904. lcd.print(" ");
  905. lcd.setCursor(0, cursor_pos);
  906. lcd.print(">");
  907. enc_dif = encoderDiff;
  908. delay(100);
  909. }
  910. }
  911. if (lcd_clicked()) {
  912. fsm = cursor_pos;
  913. if (fsm == 1) {
  914. EEPROM_read_B(EEPROM_BABYSTEP_X, &babystepMem[0]);
  915. EEPROM_read_B(EEPROM_BABYSTEP_Y, &babystepMem[1]);
  916. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystepMem[2]);
  917. CRITICAL_SECTION_START
  918. babystepsTodo[Z_AXIS] = babystepMem[2];
  919. CRITICAL_SECTION_END
  920. } else {
  921. babystepMem[0] = 0;
  922. babystepMem[1] = 0;
  923. babystepMem[2] = 0;
  924. EEPROM_save_B(EEPROM_BABYSTEP_X, &babystepMem[0]);
  925. EEPROM_save_B(EEPROM_BABYSTEP_Y, &babystepMem[1]);
  926. EEPROM_save_B(EEPROM_BABYSTEP_Z, &babystepMem[2]);
  927. }
  928. delay(500);
  929. }
  930. };
  931. lcd_implementation_clear();
  932. lcd_return_to_status();
  933. }
  934. // Lets the user move the Z carriage up to the end stoppers.
  935. // When done, it sets the current Z to Z_MAX_POS and returns true.
  936. // Otherwise the Z calibration is not changed and false is returned.
  937. bool lcd_calibrate_z_end_stop_manual()
  938. {
  939. bool clean_nozzle_asked = false;
  940. // Don't know where we are. Let's claim we are Z=0, so the soft end stops will not be triggered when moving up.
  941. current_position[Z_AXIS] = 0;
  942. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  943. // Until confirmed by the confirmation dialog.
  944. for (;;) {
  945. unsigned long previous_millis_cmd = millis();
  946. lcd_display_message_fullscreen_P(MSG_MOVE_CARRIAGE_TO_THE_TOP);
  947. // Until the user finishes the z up movement.
  948. encoderDiff = 0;
  949. encoderPosition = 0;
  950. for (;;) {
  951. if (millis() - previous_millis_cmd > LCD_TIMEOUT_TO_STATUS)
  952. goto canceled;
  953. manage_heater();
  954. manage_inactivity(true);
  955. if (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP) {
  956. delay(50);
  957. previous_millis_cmd = millis();
  958. encoderPosition += abs(encoderDiff / ENCODER_PULSES_PER_STEP);
  959. encoderDiff = 0;
  960. // Only move up, whatever the user does.
  961. current_position[Z_AXIS] += fabs(encoderPosition);
  962. encoderPosition = 0;
  963. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[Z_AXIS] / 60, active_extruder);
  964. // Wait for the motors to stop.
  965. st_synchronize();
  966. // Claim we are at Z=0, so the soft end stop will not trigger.
  967. current_position[Z_AXIS] = 0;
  968. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  969. }
  970. if (lcd_clicked()) {
  971. // Wait until the Z up movement is finished.
  972. st_synchronize();
  973. while (lcd_clicked()) ;
  974. delay(10);
  975. while (lcd_clicked()) ;
  976. break;
  977. }
  978. }
  979. if (! clean_nozzle_asked) {
  980. lcd_show_fullscreen_message_and_wait_P(MSG_CONFIRM_NOZZLE_CLEAN);
  981. clean_nozzle_asked = true;
  982. }
  983. // Let the user confirm, that the Z carriage is at the top end stoppers.
  984. int8_t result = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_CONFIRM_CARRIAGE_AT_THE_TOP);
  985. if (result == -1)
  986. goto canceled;
  987. else if (result == 1)
  988. goto calibrated;
  989. // otherwise perform another round of the Z up dialog.
  990. }
  991. calibrated:
  992. // Let the machine think the Z axis is a bit higher than it is, so it will not home into the bed
  993. // during the search for the induction points.
  994. current_position[Z_AXIS] = Z_MAX_POS-3.f;
  995. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  996. return true;
  997. canceled:
  998. return false;
  999. }
  1000. static inline bool pgm_is_whitespace(const char *c)
  1001. {
  1002. return pgm_read_byte(c) == ' ' || pgm_read_byte(c) == '\t' || pgm_read_byte(c) == '\r' || pgm_read_byte(c) == '\n';
  1003. }
  1004. void lcd_display_message_fullscreen_P(const char *msg)
  1005. {
  1006. // Disable update of the screen by the usual lcd_update() routine.
  1007. lcd_update_enable(false);
  1008. lcd_implementation_clear();
  1009. lcd.setCursor(0, 0);
  1010. for (int8_t row = 0; row < 4; ++ row) {
  1011. while (pgm_is_whitespace(msg))
  1012. ++ msg;
  1013. if (pgm_read_byte(msg) == 0)
  1014. // End of the message.
  1015. break;
  1016. lcd.setCursor(0, row);
  1017. const char *msgend2 = msg + min(strlen_P(msg), 20);
  1018. const char *msgend = msgend2;
  1019. if (pgm_read_byte(msgend) != 0 && ! pgm_is_whitespace(msgend)) {
  1020. // Splitting a word. Find the start of the current word.
  1021. while (msgend > msg && ! pgm_is_whitespace(msgend - 1))
  1022. -- msgend;
  1023. if (msgend == msg)
  1024. // Found a single long word, which cannot be split. Just cut it.
  1025. msgend = msgend2;
  1026. }
  1027. for (; msg < msgend; ++ msg) {
  1028. char c = char(pgm_read_byte(msg));
  1029. if (c == '~')
  1030. c = ' ';
  1031. lcd.print(c);
  1032. }
  1033. }
  1034. }
  1035. void lcd_show_fullscreen_message_and_wait_P(const char *msg)
  1036. {
  1037. lcd_display_message_fullscreen_P(msg);
  1038. // Until confirmed by a button click.
  1039. for (;;) {
  1040. delay_keep_alive(50);
  1041. if (lcd_clicked()) {
  1042. while (lcd_clicked()) ;
  1043. delay(10);
  1044. while (lcd_clicked()) ;
  1045. break;
  1046. }
  1047. }
  1048. }
  1049. void lcd_wait_for_click()
  1050. {
  1051. for (;;) {
  1052. manage_heater();
  1053. manage_inactivity(true);
  1054. if (lcd_clicked()) {
  1055. while (lcd_clicked()) ;
  1056. delay(10);
  1057. while (lcd_clicked()) ;
  1058. return;
  1059. }
  1060. }
  1061. }
  1062. int8_t lcd_show_fullscreen_message_yes_no_and_wait_P(const char *msg, bool allow_timeouting)
  1063. {
  1064. lcd_display_message_fullscreen_P(msg);
  1065. lcd.setCursor(1, 2);
  1066. lcd_printPGM(MSG_YES);
  1067. lcd.setCursor(0, 3);
  1068. lcd_printPGM(PSTR(">"));
  1069. lcd_printPGM(MSG_NO);
  1070. bool yes = false;
  1071. // Wait for user confirmation or a timeout.
  1072. unsigned long previous_millis_cmd = millis();
  1073. int8_t enc_dif = encoderDiff;
  1074. for (;;) {
  1075. if (allow_timeouting && millis() - previous_millis_cmd > LCD_TIMEOUT_TO_STATUS)
  1076. return -1;
  1077. manage_heater();
  1078. manage_inactivity(true);
  1079. if (abs((enc_dif - encoderDiff)) > 4) {
  1080. if (abs(enc_dif - encoderDiff) > 1) {
  1081. lcd.setCursor(0, 2);
  1082. if (enc_dif > encoderDiff && yes) {
  1083. lcd_printPGM((PSTR(" ")));
  1084. lcd.setCursor(0, 3);
  1085. lcd_printPGM((PSTR(">")));
  1086. yes = false;
  1087. } else if (enc_dif < encoderDiff && ! yes) {
  1088. lcd_printPGM((PSTR(">")));
  1089. lcd.setCursor(0, 3);
  1090. lcd_printPGM((PSTR(" ")));
  1091. yes = true;
  1092. }
  1093. enc_dif = encoderDiff;
  1094. }
  1095. }
  1096. if (lcd_clicked()) {
  1097. while (lcd_clicked()) ;
  1098. delay(10);
  1099. while (lcd_clicked()) ;
  1100. return yes;
  1101. }
  1102. }
  1103. }
  1104. void lcd_bed_calibration_show_result(BedSkewOffsetDetectionResultType result, uint8_t point_too_far_mask)
  1105. {
  1106. const char *msg = NULL;
  1107. if (result == BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND) {
  1108. lcd_show_fullscreen_message_and_wait_P(MSG_BED_SKEW_OFFSET_DETECTION_POINT_NOT_FOUND);
  1109. } else if (result == BED_SKEW_OFFSET_DETECTION_FITTING_FAILED) {
  1110. if (point_too_far_mask == 0)
  1111. msg = MSG_BED_SKEW_OFFSET_DETECTION_FITTING_FAILED;
  1112. else if (point_too_far_mask == 2 || point_too_far_mask == 7)
  1113. // Only the center point or all the three front points.
  1114. msg = MSG_BED_SKEW_OFFSET_DETECTION_FAILED_FRONT_BOTH_FAR;
  1115. else if (point_too_far_mask & 1 == 0)
  1116. // The right and maybe the center point out of reach.
  1117. msg = MSG_BED_SKEW_OFFSET_DETECTION_FAILED_FRONT_RIGHT_FAR;
  1118. else
  1119. // The left and maybe the center point out of reach.
  1120. msg = MSG_BED_SKEW_OFFSET_DETECTION_FAILED_FRONT_LEFT_FAR;
  1121. lcd_show_fullscreen_message_and_wait_P(msg);
  1122. } else {
  1123. if (point_too_far_mask != 0) {
  1124. if (point_too_far_mask == 2 || point_too_far_mask == 7)
  1125. // Only the center point or all the three front points.
  1126. msg = MSG_BED_SKEW_OFFSET_DETECTION_WARNING_FRONT_BOTH_FAR;
  1127. else if (point_too_far_mask & 1 == 0)
  1128. // The right and maybe the center point out of reach.
  1129. msg = MSG_BED_SKEW_OFFSET_DETECTION_WARNING_FRONT_RIGHT_FAR;
  1130. else
  1131. // The left and maybe the center point out of reach.
  1132. msg = MSG_BED_SKEW_OFFSET_DETECTION_WARNING_FRONT_LEFT_FAR;
  1133. lcd_show_fullscreen_message_and_wait_P(msg);
  1134. }
  1135. if (point_too_far_mask == 0 || result > 0) {
  1136. switch (result) {
  1137. default:
  1138. // should not happen
  1139. msg = MSG_BED_SKEW_OFFSET_DETECTION_FITTING_FAILED;
  1140. break;
  1141. case BED_SKEW_OFFSET_DETECTION_PERFECT:
  1142. msg = MSG_BED_SKEW_OFFSET_DETECTION_PERFECT;
  1143. break;
  1144. case BED_SKEW_OFFSET_DETECTION_SKEW_MILD:
  1145. msg = MSG_BED_SKEW_OFFSET_DETECTION_SKEW_MILD;
  1146. break;
  1147. case BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME:
  1148. msg = MSG_BED_SKEW_OFFSET_DETECTION_SKEW_EXTREME;
  1149. break;
  1150. }
  1151. lcd_show_fullscreen_message_and_wait_P(msg);
  1152. }
  1153. }
  1154. }
  1155. static void lcd_show_end_stops() {
  1156. lcd.setCursor(0, 0);
  1157. lcd_printPGM((PSTR("End stops diag")));
  1158. lcd.setCursor(0, 1);
  1159. lcd_printPGM((READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? (PSTR("X1")) : (PSTR("X0")));
  1160. lcd.setCursor(0, 2);
  1161. lcd_printPGM((READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? (PSTR("Y1")) : (PSTR("Y0")));
  1162. lcd.setCursor(0, 3);
  1163. lcd_printPGM((READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1) ? (PSTR("Z1")) : (PSTR("Z0")));
  1164. }
  1165. static void menu_show_end_stops() {
  1166. lcd_show_end_stops();
  1167. if (LCD_CLICKED) lcd_goto_menu(lcd_settings_menu);
  1168. }
  1169. // Lets the user move the Z carriage up to the end stoppers.
  1170. // When done, it sets the current Z to Z_MAX_POS and returns true.
  1171. // Otherwise the Z calibration is not changed and false is returned.
  1172. void lcd_diag_show_end_stops()
  1173. {
  1174. int enc_dif = encoderDiff;
  1175. lcd_implementation_clear();
  1176. for (;;) {
  1177. manage_heater();
  1178. manage_inactivity(true);
  1179. lcd_show_end_stops();
  1180. if (lcd_clicked()) {
  1181. while (lcd_clicked()) ;
  1182. delay(10);
  1183. while (lcd_clicked()) ;
  1184. break;
  1185. }
  1186. }
  1187. lcd_implementation_clear();
  1188. lcd_return_to_status();
  1189. }
  1190. void lcd_pick_babystep(){
  1191. int enc_dif = 0;
  1192. int cursor_pos = 1;
  1193. int fsm = 0;
  1194. lcd_implementation_clear();
  1195. lcd.setCursor(0, 0);
  1196. lcd_printPGM(MSG_PICK_Z);
  1197. lcd.setCursor(3, 2);
  1198. lcd.print("1");
  1199. lcd.setCursor(3, 3);
  1200. lcd.print("2");
  1201. lcd.setCursor(12, 2);
  1202. lcd.print("3");
  1203. lcd.setCursor(12, 3);
  1204. lcd.print("4");
  1205. lcd.setCursor(1, 2);
  1206. lcd.print(">");
  1207. enc_dif = encoderDiff;
  1208. while (fsm == 0) {
  1209. manage_heater();
  1210. manage_inactivity(true);
  1211. if ( abs((enc_dif - encoderDiff)) > 4 ) {
  1212. if ( (abs(enc_dif - encoderDiff)) > 1 ) {
  1213. if (enc_dif > encoderDiff ) {
  1214. cursor_pos --;
  1215. }
  1216. if (enc_dif < encoderDiff ) {
  1217. cursor_pos ++;
  1218. }
  1219. if (cursor_pos > 4) {
  1220. cursor_pos = 4;
  1221. }
  1222. if (cursor_pos < 1) {
  1223. cursor_pos = 1;
  1224. }
  1225. lcd.setCursor(1, 2);
  1226. lcd.print(" ");
  1227. lcd.setCursor(1, 3);
  1228. lcd.print(" ");
  1229. lcd.setCursor(10, 2);
  1230. lcd.print(" ");
  1231. lcd.setCursor(10, 3);
  1232. lcd.print(" ");
  1233. if (cursor_pos < 3) {
  1234. lcd.setCursor(1, cursor_pos+1);
  1235. lcd.print(">");
  1236. }else{
  1237. lcd.setCursor(10, cursor_pos-1);
  1238. lcd.print(">");
  1239. }
  1240. enc_dif = encoderDiff;
  1241. delay(100);
  1242. }
  1243. }
  1244. if (lcd_clicked()) {
  1245. fsm = cursor_pos;
  1246. EEPROM_read_B(EEPROM_BABYSTEP_Z0+((fsm-1)*2),&babystepMem[2]);
  1247. EEPROM_save_B(EEPROM_BABYSTEP_Z,&babystepMem[2]);
  1248. eeprom_write_byte((unsigned char*)EEPROM_BABYSTEP_Z_SET, 0x01);
  1249. delay(500);
  1250. }
  1251. };
  1252. lcd_implementation_clear();
  1253. lcd_return_to_status();
  1254. }
  1255. void lcd_move_menu_axis()
  1256. {
  1257. START_MENU();
  1258. MENU_ITEM(back, MSG_SETTINGS, lcd_settings_menu);
  1259. MENU_ITEM(submenu, MSG_MOVE_X, lcd_move_x);
  1260. MENU_ITEM(submenu, MSG_MOVE_Y, lcd_move_y);
  1261. if (move_menu_scale < 10.0)
  1262. {
  1263. if (!isPrintPaused)
  1264. {
  1265. MENU_ITEM(submenu, MSG_MOVE_Z, lcd_move_z);
  1266. }
  1267. MENU_ITEM(submenu, MSG_MOVE_E, lcd_move_e);
  1268. }
  1269. END_MENU();
  1270. }
  1271. static void lcd_move_menu_1mm()
  1272. {
  1273. move_menu_scale = 1.0;
  1274. lcd_move_menu_axis();
  1275. }
  1276. void EEPROM_save(int pos, uint8_t* value, uint8_t size)
  1277. {
  1278. do
  1279. {
  1280. eeprom_write_byte((unsigned char*)pos, *value);
  1281. pos++;
  1282. value++;
  1283. } while (--size);
  1284. }
  1285. void EEPROM_read(int pos, uint8_t* value, uint8_t size)
  1286. {
  1287. do
  1288. {
  1289. *value = eeprom_read_byte((unsigned char*)pos);
  1290. pos++;
  1291. value++;
  1292. } while (--size);
  1293. }
  1294. static void lcd_silent_mode_set() {
  1295. SilentModeMenu = !SilentModeMenu;
  1296. EEPROM_save(EEPROM_SILENT, (uint8_t*)&SilentModeMenu, sizeof(SilentModeMenu));
  1297. digipot_init();
  1298. lcd_goto_menu(lcd_settings_menu, 7);
  1299. }
  1300. static void lcd_set_lang(unsigned char lang) {
  1301. lang_selected = lang;
  1302. firstrun = 1;
  1303. eeprom_write_byte((unsigned char *)EEPROM_LANG, lang);/*langsel=0;*/if (langsel == 1)langsel = 2;
  1304. }
  1305. void lcd_force_language_selection() {
  1306. eeprom_write_byte((unsigned char *)EEPROM_LANG, 255);
  1307. }
  1308. static void lcd_language_menu()
  1309. {
  1310. START_MENU();
  1311. if (!langsel) {
  1312. MENU_ITEM(back, MSG_SETTINGS, lcd_settings_menu);
  1313. }
  1314. if (langsel == 2) {
  1315. MENU_ITEM(back, MSG_WATCH, lcd_status_screen);
  1316. }
  1317. for (int i=0;i<LANG_NUM;i++){
  1318. MENU_ITEM(setlang, MSG_LANGUAGE_NAME_EXPLICIT(i), i);
  1319. }
  1320. //MENU_ITEM(setlang, MSG_LANGUAGE_NAME_EXPLICIT(1), 1);
  1321. END_MENU();
  1322. }
  1323. void lcd_mesh_bedleveling()
  1324. {
  1325. enquecommand_P(PSTR("G80"));
  1326. lcd_return_to_status();
  1327. }
  1328. void lcd_mesh_calibration()
  1329. {
  1330. enquecommand_P(PSTR("M45"));
  1331. lcd_return_to_status();
  1332. }
  1333. static void lcd_settings_menu()
  1334. {
  1335. EEPROM_read(EEPROM_SILENT, (uint8_t*)&SilentModeMenu, sizeof(SilentModeMenu));
  1336. START_MENU();
  1337. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  1338. MENU_ITEM(submenu, MSG_TEMPERATURE, lcd_control_temperature_menu);
  1339. MENU_ITEM(submenu, MSG_MOVE_AXIS, lcd_move_menu_1mm);
  1340. if (!isPrintPaused)
  1341. {
  1342. #ifndef MESH_BED_LEVELING
  1343. MENU_ITEM(gcode, MSG_HOMEYZ, PSTR("G28 Z"));
  1344. #else
  1345. MENU_ITEM(submenu, MSG_HOMEYZ, lcd_mesh_bedleveling);
  1346. #endif
  1347. }
  1348. if (!isPrintPaused)
  1349. {
  1350. MENU_ITEM(gcode, MSG_DISABLE_STEPPERS, PSTR("M84"));
  1351. MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
  1352. }
  1353. if (SilentModeMenu == 0) {
  1354. MENU_ITEM(function, MSG_SILENT_MODE_OFF, lcd_silent_mode_set);
  1355. } else {
  1356. MENU_ITEM(function, MSG_SILENT_MODE_ON, lcd_silent_mode_set);
  1357. }
  1358. EEPROM_read_B(EEPROM_BABYSTEP_X, &babystepMem[0]);
  1359. EEPROM_read_B(EEPROM_BABYSTEP_Y, &babystepMem[1]);
  1360. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystepMem[2]);
  1361. babystepMemMM[2] = babystepMem[2]/axis_steps_per_unit[Z_AXIS];
  1362. if (!isPrintPaused)
  1363. {
  1364. MENU_ITEM(submenu, MSG_BABYSTEP_Z, lcd_babystep_z);//8
  1365. }
  1366. MENU_ITEM(submenu, MSG_LANGUAGE_SELECT, lcd_language_menu);
  1367. if (!isPrintPaused)
  1368. {
  1369. MENU_ITEM(submenu, MSG_SELFTEST, lcd_selftest);
  1370. MENU_ITEM(submenu, MSG_SHOW_END_STOPS, menu_show_end_stops);
  1371. MENU_ITEM(submenu, MSG_CALIBRATE_BED, lcd_mesh_calibration);
  1372. MENU_ITEM(gcode, MSG_CALIBRATE_BED_RESET, PSTR("M44"));
  1373. }
  1374. END_MENU();
  1375. }
  1376. /*
  1377. void lcd_mylang_top(int hlaska) {
  1378. lcd.setCursor(0,0);
  1379. lcd.print(" ");
  1380. lcd.setCursor(0,0);
  1381. lcd_printPGM(MSG_ALL[hlaska-1][LANGUAGE_SELECT]);
  1382. }
  1383. void lcd_mylang_drawmenu(int cursor) {
  1384. int first = 0;
  1385. if (cursor>2) first = cursor-2;
  1386. if (cursor==LANG_NUM) first = LANG_NUM-3;
  1387. lcd.setCursor(0, 1);
  1388. lcd.print(" ");
  1389. lcd.setCursor(1, 1);
  1390. lcd_printPGM(MSG_ALL[first][LANGUAGE_NAME]);
  1391. lcd.setCursor(0, 2);
  1392. lcd.print(" ");
  1393. lcd.setCursor(1, 2);
  1394. lcd_printPGM(MSG_ALL[first+1][LANGUAGE_NAME]);
  1395. lcd.setCursor(0, 3);
  1396. lcd.print(" ");
  1397. lcd.setCursor(1, 3);
  1398. lcd_printPGM(MSG_ALL[first+2][LANGUAGE_NAME]);
  1399. if (cursor==1) lcd.setCursor(0, 1);
  1400. if (cursor>1 && cursor<LANG_NUM) lcd.setCursor(0, 2);
  1401. if (cursor==LANG_NUM) lcd.setCursor(0, 3);
  1402. lcd.print(">");
  1403. if (cursor<LANG_NUM-1) {
  1404. lcd.setCursor(19,3);
  1405. lcd.print("\x01");
  1406. }
  1407. if (cursor>2) {
  1408. lcd.setCursor(19,1);
  1409. lcd.print("^");
  1410. }
  1411. }
  1412. */
  1413. void lcd_mylang_drawmenu(int cursor) {
  1414. int first = 0;
  1415. if (cursor>3) first = cursor-3;
  1416. if (cursor==LANG_NUM && LANG_NUM>4) first = LANG_NUM-4;
  1417. if (cursor==LANG_NUM && LANG_NUM==4) first = LANG_NUM-4;
  1418. lcd.setCursor(0, 0);
  1419. lcd.print(" ");
  1420. lcd.setCursor(1, 0);
  1421. lcd_printPGM(MSG_LANGUAGE_NAME_EXPLICIT(first+0));
  1422. lcd.setCursor(0, 1);
  1423. lcd.print(" ");
  1424. lcd.setCursor(1, 1);
  1425. lcd_printPGM(MSG_LANGUAGE_NAME_EXPLICIT(first+1));
  1426. lcd.setCursor(0, 2);
  1427. lcd.print(" ");
  1428. if (LANG_NUM > 2){
  1429. lcd.setCursor(1, 2);
  1430. lcd_printPGM(MSG_LANGUAGE_NAME_EXPLICIT(first+2));
  1431. }
  1432. lcd.setCursor(0, 3);
  1433. lcd.print(" ");
  1434. if (LANG_NUM>3) {
  1435. lcd.setCursor(1, 3);
  1436. lcd_printPGM(MSG_LANGUAGE_NAME_EXPLICIT(first+3));
  1437. }
  1438. if (cursor==1) lcd.setCursor(0, 0);
  1439. if (cursor==2) lcd.setCursor(0, 1);
  1440. if (cursor>2) lcd.setCursor(0, 2);
  1441. if (cursor==LANG_NUM && LANG_NUM>3) lcd.setCursor(0, 3);
  1442. lcd.print(">");
  1443. if (cursor<LANG_NUM-1 && LANG_NUM>4) {
  1444. lcd.setCursor(19,3);
  1445. lcd.print("\x01");
  1446. }
  1447. if (cursor>3 && LANG_NUM>4) {
  1448. lcd.setCursor(19,0);
  1449. lcd.print("^");
  1450. }
  1451. }
  1452. void lcd_set_custom_characters_arrows();
  1453. void lcd_set_custom_characters_degree();
  1454. void lcd_mylang_drawcursor(int cursor) {
  1455. if (cursor==1) lcd.setCursor(0, 1);
  1456. if (cursor>1 && cursor<LANG_NUM) lcd.setCursor(0, 2);
  1457. if (cursor==LANG_NUM) lcd.setCursor(0, 3);
  1458. lcd.print(">");
  1459. }
  1460. void lcd_mylang() {
  1461. int enc_dif = 0;
  1462. int cursor_pos = 1;
  1463. lang_selected=255;
  1464. int hlaska=1;
  1465. int counter=0;
  1466. lcd_set_custom_characters_arrows();
  1467. lcd_implementation_clear();
  1468. //lcd_mylang_top(hlaska);
  1469. lcd_mylang_drawmenu(cursor_pos);
  1470. enc_dif = encoderDiff;
  1471. while ( (lang_selected == 255) && (MYSERIAL.available() < 2) ) {
  1472. manage_heater();
  1473. manage_inactivity(true);
  1474. if ( abs((enc_dif - encoderDiff)) > 4 ) {
  1475. //if ( (abs(enc_dif - encoderDiff)) > 1 ) {
  1476. if (enc_dif > encoderDiff ) {
  1477. cursor_pos --;
  1478. }
  1479. if (enc_dif < encoderDiff ) {
  1480. cursor_pos ++;
  1481. }
  1482. if (cursor_pos > LANG_NUM) {
  1483. cursor_pos = LANG_NUM;
  1484. }
  1485. if (cursor_pos < 1) {
  1486. cursor_pos = 1;
  1487. }
  1488. lcd_mylang_drawmenu(cursor_pos);
  1489. enc_dif = encoderDiff;
  1490. delay(100);
  1491. //}
  1492. } else delay(20);
  1493. if (lcd_clicked()) {
  1494. lcd_set_lang(cursor_pos-1);
  1495. delay(500);
  1496. }
  1497. /*
  1498. if (++counter == 80) {
  1499. hlaska++;
  1500. if(hlaska>LANG_NUM) hlaska=1;
  1501. lcd_mylang_top(hlaska);
  1502. lcd_mylang_drawcursor(cursor_pos);
  1503. counter=0;
  1504. }
  1505. */
  1506. };
  1507. if(MYSERIAL.available() > 1){
  1508. lang_selected = 0;
  1509. firstrun = 0;
  1510. }
  1511. lcd_set_custom_characters_degree();
  1512. lcd_implementation_clear();
  1513. lcd_return_to_status();
  1514. }
  1515. static void lcd_main_menu()
  1516. {
  1517. SDscrool = 0;
  1518. /*
  1519. if (langsel == 1)
  1520. {
  1521. lcd_goto_menu(lcd_language_menu);
  1522. }
  1523. */
  1524. START_MENU();
  1525. // Majkl superawesome menu
  1526. MENU_ITEM(back, MSG_WATCH, lcd_status_screen);
  1527. if ( ( IS_SD_PRINTING || is_usb_printing ) && (current_position[Z_AXIS] < 0.5) )
  1528. {
  1529. EEPROM_read_B(EEPROM_BABYSTEP_X, &babystepMem[0]);
  1530. EEPROM_read_B(EEPROM_BABYSTEP_Y, &babystepMem[1]);
  1531. EEPROM_read_B(EEPROM_BABYSTEP_Z, &babystepMem[2]);
  1532. MENU_ITEM(submenu, MSG_BABYSTEP_Z, lcd_babystep_z);//8
  1533. }
  1534. if ( movesplanned() || IS_SD_PRINTING || is_usb_printing )
  1535. {
  1536. MENU_ITEM(submenu, MSG_TUNE, lcd_tune_menu);
  1537. } else
  1538. {
  1539. MENU_ITEM(submenu, MSG_PREHEAT, lcd_preheat_menu);
  1540. }
  1541. #ifdef SDSUPPORT
  1542. if (card.cardOK)
  1543. {
  1544. if (card.isFileOpen())
  1545. {
  1546. if (card.sdprinting)
  1547. {
  1548. MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause);
  1549. }
  1550. else
  1551. {
  1552. MENU_ITEM(function, MSG_RESUME_PRINT, lcd_sdcard_resume);
  1553. }
  1554. MENU_ITEM(submenu, MSG_STOP_PRINT, lcd_sdcard_stop);
  1555. }
  1556. else
  1557. {
  1558. if (!is_usb_printing)
  1559. {
  1560. MENU_ITEM(submenu, MSG_CARD_MENU, lcd_sdcard_menu);
  1561. }
  1562. #if SDCARDDETECT < 1
  1563. MENU_ITEM(gcode, MSG_CNG_SDCARD, PSTR("M21")); // SD-card changed by user
  1564. #endif
  1565. }
  1566. } else
  1567. {
  1568. MENU_ITEM(submenu, MSG_NO_CARD, lcd_sdcard_menu);
  1569. #if SDCARDDETECT < 1
  1570. MENU_ITEM(gcode, MSG_INIT_SDCARD, PSTR("M21")); // Manually initialize the SD-card via user interface
  1571. #endif
  1572. }
  1573. #endif
  1574. if (IS_SD_PRINTING || is_usb_printing)
  1575. {
  1576. }
  1577. else
  1578. {
  1579. MENU_ITEM(function, MSG_LOAD_FILAMENT, lcd_LoadFilament);
  1580. MENU_ITEM(function, MSG_UNLOAD_FILAMENT, lcd_unLoadFilament);
  1581. MENU_ITEM(submenu, MSG_SETTINGS, lcd_settings_menu);
  1582. }
  1583. if (!is_usb_printing)
  1584. {
  1585. MENU_ITEM(submenu, MSG_STATISTICS, lcd_menu_statistics);
  1586. }
  1587. MENU_ITEM(submenu, MSG_SUPPORT, lcd_support_menu);
  1588. END_MENU();
  1589. }
  1590. #ifdef SDSUPPORT
  1591. static void lcd_autostart_sd()
  1592. {
  1593. card.lastnr = 0;
  1594. card.setroot();
  1595. card.checkautostart(true);
  1596. }
  1597. #endif
  1598. static void lcd_silent_mode_set_tune() {
  1599. SilentModeMenu = !SilentModeMenu;
  1600. EEPROM_save(EEPROM_SILENT, (uint8_t*)&SilentModeMenu, sizeof(SilentModeMenu));
  1601. digipot_init();
  1602. lcd_goto_menu(lcd_tune_menu, 9);
  1603. }
  1604. static void lcd_tune_menu()
  1605. {
  1606. EEPROM_read(EEPROM_SILENT, (uint8_t*)&SilentModeMenu, sizeof(SilentModeMenu));
  1607. START_MENU();
  1608. MENU_ITEM(back, MSG_MAIN, lcd_main_menu); //1
  1609. MENU_ITEM_EDIT(int3, MSG_SPEED, &feedmultiply, 10, 999);//2
  1610. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 10);//3
  1611. MENU_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 10);//4
  1612. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &fanSpeed, 0, 255);//5
  1613. MENU_ITEM_EDIT(int3, MSG_FLOW, &extrudemultiply, 10, 999);//6
  1614. #ifdef FILAMENTCHANGEENABLE
  1615. MENU_ITEM(gcode, MSG_FILAMENTCHANGE, PSTR("M600"));//7
  1616. #endif
  1617. if (SilentModeMenu == 0) {
  1618. MENU_ITEM(function, MSG_SILENT_MODE_OFF, lcd_silent_mode_set_tune);
  1619. } else {
  1620. MENU_ITEM(function, MSG_SILENT_MODE_ON, lcd_silent_mode_set_tune);
  1621. }
  1622. END_MENU();
  1623. }
  1624. static void lcd_move_menu_01mm()
  1625. {
  1626. move_menu_scale = 0.1;
  1627. lcd_move_menu_axis();
  1628. }
  1629. static void lcd_control_temperature_menu()
  1630. {
  1631. #ifdef PIDTEMP
  1632. // set up temp variables - undo the default scaling
  1633. raw_Ki = unscalePID_i(Ki);
  1634. raw_Kd = unscalePID_d(Kd);
  1635. #endif
  1636. START_MENU();
  1637. MENU_ITEM(back, MSG_SETTINGS, lcd_settings_menu);
  1638. //MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  1639. #if TEMP_SENSOR_0 != 0
  1640. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 10);
  1641. #endif
  1642. #if TEMP_SENSOR_1 != 0
  1643. MENU_ITEM_EDIT(int3, MSG_NOZZLE1, &target_temperature[1], 0, HEATER_1_MAXTEMP - 10);
  1644. #endif
  1645. #if TEMP_SENSOR_2 != 0
  1646. MENU_ITEM_EDIT(int3, MSG_NOZZLE2, &target_temperature[2], 0, HEATER_2_MAXTEMP - 10);
  1647. #endif
  1648. #if TEMP_SENSOR_BED != 0
  1649. MENU_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 3);
  1650. #endif
  1651. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &fanSpeed, 0, 255);
  1652. #if defined AUTOTEMP && (TEMP_SENSOR_0 != 0)
  1653. MENU_ITEM_EDIT(bool, MSG_AUTOTEMP, &autotemp_enabled);
  1654. MENU_ITEM_EDIT(float3, MSG_MIN, &autotemp_min, 0, HEATER_0_MAXTEMP - 10);
  1655. MENU_ITEM_EDIT(float3, MSG_MAX, &autotemp_max, 0, HEATER_0_MAXTEMP - 10);
  1656. MENU_ITEM_EDIT(float32, MSG_FACTOR, &autotemp_factor, 0.0, 1.0);
  1657. #endif
  1658. END_MENU();
  1659. }
  1660. #if SDCARDDETECT == -1
  1661. static void lcd_sd_refresh()
  1662. {
  1663. card.initsd();
  1664. currentMenuViewOffset = 0;
  1665. }
  1666. #endif
  1667. static void lcd_sd_updir()
  1668. {
  1669. SDscrool = 0;
  1670. card.updir();
  1671. currentMenuViewOffset = 0;
  1672. }
  1673. void lcd_sdcard_stop()
  1674. {
  1675. lcd.setCursor(0, 0);
  1676. lcd_printPGM(MSG_STOP_PRINT);
  1677. lcd.setCursor(2, 2);
  1678. lcd_printPGM(MSG_NO);
  1679. lcd.setCursor(2, 3);
  1680. lcd_printPGM(MSG_YES);
  1681. lcd.setCursor(0, 2); lcd.print(" ");
  1682. lcd.setCursor(0, 3); lcd.print(" ");
  1683. if ((int32_t)encoderPosition > 2) { encoderPosition = 2; }
  1684. if ((int32_t)encoderPosition < 1) { encoderPosition = 1; }
  1685. lcd.setCursor(0, 1 + encoderPosition);
  1686. lcd.print(">");
  1687. if (lcd_clicked())
  1688. {
  1689. if ((int32_t)encoderPosition == 1)
  1690. {
  1691. lcd_return_to_status();
  1692. }
  1693. if ((int32_t)encoderPosition == 2)
  1694. {
  1695. cancel_heatup = true;
  1696. quickStop();
  1697. lcd_setstatuspgm(MSG_PRINT_ABORTED);
  1698. card.sdprinting = false;
  1699. card.closefile();
  1700. stoptime = millis();
  1701. unsigned long t = (stoptime - starttime) / 1000;
  1702. save_statistics(total_filament_used, t);
  1703. lcd_return_to_status();
  1704. lcd_ignore_click(true);
  1705. lcd_commands_type = 2;
  1706. }
  1707. }
  1708. }
  1709. void lcd_sdcard_menu()
  1710. {
  1711. int tempScrool = 0;
  1712. if (lcdDrawUpdate == 0 && LCD_CLICKED == 0)
  1713. //delay(100);
  1714. return; // nothing to do (so don't thrash the SD card)
  1715. uint16_t fileCnt = card.getnrfilenames();
  1716. START_MENU();
  1717. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  1718. card.getWorkDirName();
  1719. if (card.filename[0] == '/')
  1720. {
  1721. #if SDCARDDETECT == -1
  1722. MENU_ITEM(function, MSG_REFRESH, lcd_sd_refresh);
  1723. #endif
  1724. } else {
  1725. MENU_ITEM(function, PSTR(LCD_STR_FOLDER ".."), lcd_sd_updir);
  1726. }
  1727. for (uint16_t i = 0; i < fileCnt; i++)
  1728. {
  1729. if (_menuItemNr == _lineNr)
  1730. {
  1731. #ifndef SDCARD_RATHERRECENTFIRST
  1732. card.getfilename(i);
  1733. #else
  1734. card.getfilename(fileCnt - 1 - i);
  1735. #endif
  1736. if (card.filenameIsDir)
  1737. {
  1738. MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.longFilename);
  1739. } else {
  1740. MENU_ITEM(sdfile, MSG_CARD_MENU, card.filename, card.longFilename);
  1741. }
  1742. } else {
  1743. MENU_ITEM_DUMMY();
  1744. }
  1745. }
  1746. END_MENU();
  1747. }
  1748. #define menu_edit_type(_type, _name, _strFunc, scale) \
  1749. void menu_edit_ ## _name () \
  1750. { \
  1751. if ((int32_t)encoderPosition < 0) encoderPosition = 0; \
  1752. if ((int32_t)encoderPosition > maxEditValue) encoderPosition = maxEditValue; \
  1753. if (lcdDrawUpdate) \
  1754. lcd_implementation_drawedit(editLabel, _strFunc(((_type)((int32_t)encoderPosition + minEditValue)) / scale)); \
  1755. if (LCD_CLICKED) \
  1756. { \
  1757. *((_type*)editValue) = ((_type)((int32_t)encoderPosition + minEditValue)) / scale; \
  1758. lcd_goto_menu(prevMenu, prevEncoderPosition); \
  1759. } \
  1760. } \
  1761. void menu_edit_callback_ ## _name () { \
  1762. menu_edit_ ## _name (); \
  1763. if (LCD_CLICKED) (*callbackFunc)(); \
  1764. } \
  1765. static void menu_action_setting_edit_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue) \
  1766. { \
  1767. prevMenu = currentMenu; \
  1768. prevEncoderPosition = encoderPosition; \
  1769. \
  1770. lcdDrawUpdate = 2; \
  1771. currentMenu = menu_edit_ ## _name; \
  1772. \
  1773. editLabel = pstr; \
  1774. editValue = ptr; \
  1775. minEditValue = minValue * scale; \
  1776. maxEditValue = maxValue * scale - minEditValue; \
  1777. encoderPosition = (*ptr) * scale - minEditValue; \
  1778. }\
  1779. static void menu_action_setting_edit_callback_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue, menuFunc_t callback) \
  1780. { \
  1781. prevMenu = currentMenu; \
  1782. prevEncoderPosition = encoderPosition; \
  1783. \
  1784. lcdDrawUpdate = 2; \
  1785. currentMenu = menu_edit_callback_ ## _name; \
  1786. \
  1787. editLabel = pstr; \
  1788. editValue = ptr; \
  1789. minEditValue = minValue * scale; \
  1790. maxEditValue = maxValue * scale - minEditValue; \
  1791. encoderPosition = (*ptr) * scale - minEditValue; \
  1792. callbackFunc = callback;\
  1793. }
  1794. menu_edit_type(int, int3, itostr3, 1)
  1795. menu_edit_type(float, float3, ftostr3, 1)
  1796. menu_edit_type(float, float32, ftostr32, 100)
  1797. menu_edit_type(float, float43, ftostr43, 1000)
  1798. menu_edit_type(float, float5, ftostr5, 0.01)
  1799. menu_edit_type(float, float51, ftostr51, 10)
  1800. menu_edit_type(float, float52, ftostr52, 100)
  1801. menu_edit_type(unsigned long, long5, ftostr5, 0.01)
  1802. static void lcd_selftest()
  1803. {
  1804. int _progress = 0;
  1805. bool _result = false;
  1806. _progress = lcd_selftest_screen(-1, _progress, 4, true, 2000);
  1807. _progress = lcd_selftest_screen(0, _progress, 3, true, 2000);
  1808. _result = lcd_selfcheck_endstops();
  1809. if (_result)
  1810. {
  1811. _progress = lcd_selftest_screen(1, _progress, 3, true, 1000);
  1812. _result = lcd_selfcheck_check_heater(false);
  1813. }
  1814. if (_result)
  1815. {
  1816. _progress = lcd_selftest_screen(2, _progress, 3, true, 2000);
  1817. _result = lcd_selfcheck_axis(0, X_MAX_POS);
  1818. }
  1819. if (_result)
  1820. {
  1821. _progress = lcd_selftest_screen(3, _progress, 3, true, 1500);
  1822. _result = lcd_selfcheck_axis(1, Y_MAX_POS);
  1823. }
  1824. if (_result)
  1825. {
  1826. current_position[X_AXIS] = current_position[X_AXIS] - 3;
  1827. current_position[Y_AXIS] = current_position[Y_AXIS] - 14;
  1828. _progress = lcd_selftest_screen(4, _progress, 3, true, 1500);
  1829. _result = lcd_selfcheck_axis(2, Z_MAX_POS);
  1830. }
  1831. if (_result)
  1832. {
  1833. _progress = lcd_selftest_screen(5, _progress, 3, true, 2000);
  1834. _result = lcd_selfcheck_check_heater(true);
  1835. }
  1836. if (_result)
  1837. {
  1838. _progress = lcd_selftest_screen(6, _progress, 3, true, 5000);
  1839. }
  1840. else
  1841. {
  1842. _progress = lcd_selftest_screen(7, _progress, 3, true, 5000);
  1843. }
  1844. lcd_implementation_clear();
  1845. lcd_next_update_millis = millis() + LCD_UPDATE_INTERVAL;
  1846. if (_result)
  1847. {
  1848. LCD_ALERTMESSAGERPGM(MSG_SELFTEST_OK);
  1849. }
  1850. else
  1851. {
  1852. LCD_ALERTMESSAGERPGM(MSG_SELFTEST_FAILED);
  1853. }
  1854. }
  1855. static bool lcd_selfcheck_endstops()
  1856. {
  1857. bool _result = true;
  1858. if (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1 || READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1 || READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1)
  1859. {
  1860. current_position[0] = (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? current_position[0] = current_position[0] + 10 : current_position[0];
  1861. current_position[1] = (READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? current_position[1] = current_position[1] + 10 : current_position[1];
  1862. current_position[2] = (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1) ? current_position[2] = current_position[2] + 10 : current_position[2];
  1863. }
  1864. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[0] / 60, active_extruder);
  1865. delay(500);
  1866. if (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1 || READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1 || READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1)
  1867. {
  1868. _result = false;
  1869. String _error = String((READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? "X" : "") +
  1870. String((READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? "Y" : "") +
  1871. String((READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1) ? "Z" : "");
  1872. lcd_selftest_error(3, _error.c_str(), "");
  1873. }
  1874. manage_heater();
  1875. manage_inactivity();
  1876. return _result;
  1877. }
  1878. static bool lcd_selfcheck_axis(int _axis, int _travel)
  1879. {
  1880. bool _stepdone = false;
  1881. bool _stepresult = false;
  1882. int _progress = 0;
  1883. int _travel_done = 0;
  1884. int _err_endstop = 0;
  1885. int _lcd_refresh = 0;
  1886. _travel = _travel + (_travel / 10);
  1887. do {
  1888. if (_axis == 2)
  1889. {
  1890. current_position[_axis] = current_position[_axis] - 1;
  1891. }
  1892. else
  1893. {
  1894. current_position[_axis] = current_position[_axis] - 3;
  1895. }
  1896. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[3], manual_feedrate[0] / 60, active_extruder);
  1897. st_synchronize();
  1898. if (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1 || READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1 || READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1)
  1899. {
  1900. if (_axis == 0)
  1901. {
  1902. _stepresult = (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? true : false;
  1903. _err_endstop = (READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? 1 : 2;
  1904. disable_x();
  1905. }
  1906. if (_axis == 1)
  1907. {
  1908. _stepresult = (READ(Y_MIN_PIN) ^ Y_MIN_ENDSTOP_INVERTING == 1) ? true : false;
  1909. _err_endstop = (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? 0 : 2;
  1910. disable_y();
  1911. }
  1912. if (_axis == 2)
  1913. {
  1914. _stepresult = (READ(Z_MIN_PIN) ^ Z_MIN_ENDSTOP_INVERTING == 1) ? true : false;
  1915. _err_endstop = (READ(X_MIN_PIN) ^ X_MIN_ENDSTOP_INVERTING == 1) ? 0 : 1;
  1916. disable_z();
  1917. }
  1918. _stepdone = true;
  1919. }
  1920. if (_lcd_refresh < 6)
  1921. {
  1922. _lcd_refresh++;
  1923. }
  1924. else
  1925. {
  1926. _progress = lcd_selftest_screen(2 + _axis, _progress, 3, false, 0);
  1927. _lcd_refresh = 0;
  1928. }
  1929. manage_heater();
  1930. manage_inactivity();
  1931. delay(100);
  1932. (_travel_done <= _travel) ? _travel_done++ : _stepdone = true;
  1933. } while (!_stepdone);
  1934. current_position[_axis] = current_position[_axis] + 15;
  1935. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[3], manual_feedrate[0] / 60, active_extruder);
  1936. if (!_stepresult)
  1937. {
  1938. const char *_error_1;
  1939. const char *_error_2;
  1940. if (_axis == X_AXIS) _error_1 = "X";
  1941. if (_axis == Y_AXIS) _error_1 = "Y";
  1942. if (_axis == Z_AXIS) _error_1 = "Z";
  1943. if (_err_endstop == 0) _error_2 = "X";
  1944. if (_err_endstop == 1) _error_2 = "Y";
  1945. if (_err_endstop == 2) _error_2 = "Z";
  1946. if (_travel_done >= _travel)
  1947. {
  1948. lcd_selftest_error(5, _error_1, _error_2);
  1949. }
  1950. else
  1951. {
  1952. lcd_selftest_error(4, _error_1, _error_2);
  1953. }
  1954. }
  1955. return _stepresult;
  1956. }
  1957. static bool lcd_selfcheck_check_heater(bool _isbed)
  1958. {
  1959. int _counter = 0;
  1960. int _progress = 0;
  1961. bool _stepresult = false;
  1962. bool _docycle = true;
  1963. int _checked_snapshot = (_isbed) ? degBed() : degHotend(0);
  1964. int _opposite_snapshot = (_isbed) ? degHotend(0) : degBed();
  1965. int _cycles = (_isbed) ? 120 : 30;
  1966. target_temperature[0] = (_isbed) ? 0 : 100;
  1967. target_temperature_bed = (_isbed) ? 100 : 0;
  1968. manage_heater();
  1969. manage_inactivity();
  1970. do {
  1971. _counter++;
  1972. (_counter < _cycles) ? _docycle = true : _docycle = false;
  1973. manage_heater();
  1974. manage_inactivity();
  1975. _progress = (_isbed) ? lcd_selftest_screen(5, _progress, 2, false, 400) : lcd_selftest_screen(1, _progress, 2, false, 400);
  1976. } while (_docycle);
  1977. target_temperature[0] = 0;
  1978. target_temperature_bed = 0;
  1979. manage_heater();
  1980. int _checked_result = (_isbed) ? degBed() - _checked_snapshot : degHotend(0) - _checked_snapshot;
  1981. int _opposite_result = (_isbed) ? degHotend(0) - _opposite_snapshot : degBed() - _opposite_snapshot;
  1982. if (_opposite_result < (_isbed) ? 10 : 3)
  1983. {
  1984. if (_checked_result >= (_isbed) ? 3 : 10)
  1985. {
  1986. _stepresult = true;
  1987. }
  1988. else
  1989. {
  1990. lcd_selftest_error(1, "", "");
  1991. }
  1992. }
  1993. else
  1994. {
  1995. lcd_selftest_error(2, "", "");
  1996. }
  1997. manage_heater();
  1998. manage_inactivity();
  1999. return _stepresult;
  2000. }
  2001. static void lcd_selftest_error(int _error_no, const char *_error_1, const char *_error_2)
  2002. {
  2003. lcd_implementation_quick_feedback();
  2004. target_temperature[0] = 0;
  2005. target_temperature_bed = 0;
  2006. manage_heater();
  2007. manage_inactivity();
  2008. lcd_implementation_clear();
  2009. lcd.setCursor(0, 0);
  2010. lcd_printPGM(MSG_SELFTEST_ERROR);
  2011. lcd.setCursor(0, 1);
  2012. lcd_printPGM(MSG_SELFTEST_PLEASECHECK);
  2013. switch (_error_no)
  2014. {
  2015. case 1:
  2016. lcd.setCursor(0, 2);
  2017. lcd_printPGM(MSG_SELFTEST_HEATERTHERMISTOR);
  2018. lcd.setCursor(0, 3);
  2019. lcd_printPGM(MSG_SELFTEST_NOTCONNECTED);
  2020. break;
  2021. case 2:
  2022. lcd.setCursor(0, 2);
  2023. lcd_printPGM(MSG_SELFTEST_BEDHEATER);
  2024. lcd.setCursor(0, 3);
  2025. lcd_printPGM(MSG_SELFTEST_WIRINGERROR);
  2026. break;
  2027. case 3:
  2028. lcd.setCursor(0, 2);
  2029. lcd_printPGM(MSG_SELFTEST_ENDSTOPS);
  2030. lcd.setCursor(0, 3);
  2031. lcd_printPGM(MSG_SELFTEST_WIRINGERROR);
  2032. lcd.setCursor(17, 3);
  2033. lcd.print(_error_1);
  2034. break;
  2035. case 4:
  2036. lcd.setCursor(0, 2);
  2037. lcd_printPGM(MSG_SELFTEST_MOTOR);
  2038. lcd.setCursor(18, 2);
  2039. lcd.print(_error_1);
  2040. lcd.setCursor(0, 3);
  2041. lcd_printPGM(MSG_SELFTEST_ENDSTOP);
  2042. lcd.setCursor(18, 3);
  2043. lcd.print(_error_2);
  2044. break;
  2045. case 5:
  2046. lcd.setCursor(0, 2);
  2047. lcd_printPGM(MSG_SELFTEST_ENDSTOP_NOTHIT);
  2048. lcd.setCursor(0, 3);
  2049. lcd_printPGM(MSG_SELFTEST_MOTOR);
  2050. lcd.setCursor(18, 3);
  2051. lcd.print(_error_1);
  2052. break;
  2053. }
  2054. delay(1000);
  2055. lcd_implementation_quick_feedback();
  2056. do {
  2057. delay(100);
  2058. manage_heater();
  2059. manage_inactivity();
  2060. } while (!lcd_clicked());
  2061. LCD_ALERTMESSAGERPGM(MSG_SELFTEST_FAILED);
  2062. lcd_return_to_status();
  2063. }
  2064. static int lcd_selftest_screen(int _step, int _progress, int _progress_scale, bool _clear, int _delay)
  2065. {
  2066. lcd_next_update_millis = millis() + (LCD_UPDATE_INTERVAL * 10000);
  2067. int _step_block = 0;
  2068. const char *_indicator = (_progress > _progress_scale) ? "-" : "|";
  2069. if (_clear) lcd_implementation_clear();
  2070. lcd.setCursor(0, 0);
  2071. if (_step == -1) lcd_printPGM(MSG_SELFTEST_START);
  2072. if (_step == 0) lcd_printPGM(MSG_SELFTEST_CHECK_ENDSTOPS);
  2073. if (_step == 1) lcd_printPGM(MSG_SELFTEST_CHECK_HOTEND);
  2074. if (_step == 2) lcd_printPGM(MSG_SELFTEST_CHECK_X);
  2075. if (_step == 3) lcd_printPGM(MSG_SELFTEST_CHECK_Y);
  2076. if (_step == 4) lcd_printPGM(MSG_SELFTEST_CHECK_Z);
  2077. if (_step == 5) lcd_printPGM(MSG_SELFTEST_CHECK_BED);
  2078. if (_step == 6) lcd_printPGM(MSG_SELFTEST_CHECK_ALLCORRECT);
  2079. if (_step == 7) lcd_printPGM(MSG_SELFTEST_FAILED);
  2080. lcd.setCursor(0, 1);
  2081. lcd.print("--------------------");
  2082. _step_block = 1;
  2083. lcd_selftest_screen_step(3, 9, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "Hotend", _indicator);
  2084. _step_block = 2;
  2085. lcd_selftest_screen_step(2, 2, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "X", _indicator);
  2086. _step_block = 3;
  2087. lcd_selftest_screen_step(2, 8, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "Y", _indicator);
  2088. _step_block = 4;
  2089. lcd_selftest_screen_step(2, 14, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "Z", _indicator);
  2090. _step_block = 5;
  2091. lcd_selftest_screen_step(3, 0, ((_step == _step_block) ? 1 : (_step < _step_block) ? 0 : 2), "Bed", _indicator);
  2092. if (_delay > 0) delay(_delay);
  2093. _progress++;
  2094. return (_progress > _progress_scale * 2) ? 0 : _progress;
  2095. }
  2096. static void lcd_selftest_screen_step(int _row, int _col, int _state, const char *_name, const char *_indicator)
  2097. {
  2098. lcd.setCursor(_col, _row);
  2099. switch (_state)
  2100. {
  2101. case 1:
  2102. lcd.print(_name);
  2103. lcd.setCursor(_col + strlen(_name), _row);
  2104. lcd.print(":");
  2105. lcd.setCursor(_col + strlen(_name) + 1, _row);
  2106. lcd.print(_indicator);
  2107. break;
  2108. case 2:
  2109. lcd.print(_name);
  2110. lcd.setCursor(_col + strlen(_name), _row);
  2111. lcd.print(":");
  2112. lcd.setCursor(_col + strlen(_name) + 1, _row);
  2113. lcd.print("OK");
  2114. break;
  2115. default:
  2116. lcd.print(_name);
  2117. }
  2118. }
  2119. /** End of menus **/
  2120. static void lcd_quick_feedback()
  2121. {
  2122. lcdDrawUpdate = 2;
  2123. blocking_enc = millis() + 500;
  2124. lcd_implementation_quick_feedback();
  2125. }
  2126. /** Menu action functions **/
  2127. static void menu_action_back(menuFunc_t data) {
  2128. lcd_goto_menu(data);
  2129. }
  2130. static void menu_action_submenu(menuFunc_t data) {
  2131. lcd_goto_menu(data);
  2132. }
  2133. static void menu_action_gcode(const char* pgcode) {
  2134. enquecommand_P(pgcode);
  2135. }
  2136. static void menu_action_setlang(unsigned char lang) {
  2137. lcd_set_lang(lang);
  2138. }
  2139. static void menu_action_function(menuFunc_t data) {
  2140. (*data)();
  2141. }
  2142. static void menu_action_sdfile(const char* filename, char* longFilename)
  2143. {
  2144. char cmd[30];
  2145. char* c;
  2146. sprintf_P(cmd, PSTR("M23 %s"), filename);
  2147. for (c = &cmd[4]; *c; c++)
  2148. *c = tolower(*c);
  2149. enquecommand(cmd);
  2150. enquecommand_P(PSTR("M24"));
  2151. lcd_return_to_status();
  2152. }
  2153. static void menu_action_sddirectory(const char* filename, char* longFilename)
  2154. {
  2155. card.chdir(filename);
  2156. encoderPosition = 0;
  2157. }
  2158. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr)
  2159. {
  2160. *ptr = !(*ptr);
  2161. }
  2162. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, menuFunc_t callback)
  2163. {
  2164. menu_action_setting_edit_bool(pstr, ptr);
  2165. (*callback)();
  2166. }
  2167. #endif//ULTIPANEL
  2168. /** LCD API **/
  2169. void lcd_init()
  2170. {
  2171. lcd_implementation_init();
  2172. #ifdef NEWPANEL
  2173. SET_INPUT(BTN_EN1);
  2174. SET_INPUT(BTN_EN2);
  2175. WRITE(BTN_EN1, HIGH);
  2176. WRITE(BTN_EN2, HIGH);
  2177. #if BTN_ENC > 0
  2178. SET_INPUT(BTN_ENC);
  2179. WRITE(BTN_ENC, HIGH);
  2180. #endif
  2181. #ifdef REPRAPWORLD_KEYPAD
  2182. pinMode(SHIFT_CLK, OUTPUT);
  2183. pinMode(SHIFT_LD, OUTPUT);
  2184. pinMode(SHIFT_OUT, INPUT);
  2185. WRITE(SHIFT_OUT, HIGH);
  2186. WRITE(SHIFT_LD, HIGH);
  2187. #endif
  2188. #else // Not NEWPANEL
  2189. #ifdef SR_LCD_2W_NL // Non latching 2 wire shift register
  2190. pinMode (SR_DATA_PIN, OUTPUT);
  2191. pinMode (SR_CLK_PIN, OUTPUT);
  2192. #elif defined(SHIFT_CLK)
  2193. pinMode(SHIFT_CLK, OUTPUT);
  2194. pinMode(SHIFT_LD, OUTPUT);
  2195. pinMode(SHIFT_EN, OUTPUT);
  2196. pinMode(SHIFT_OUT, INPUT);
  2197. WRITE(SHIFT_OUT, HIGH);
  2198. WRITE(SHIFT_LD, HIGH);
  2199. WRITE(SHIFT_EN, LOW);
  2200. #else
  2201. #ifdef ULTIPANEL
  2202. #error ULTIPANEL requires an encoder
  2203. #endif
  2204. #endif // SR_LCD_2W_NL
  2205. #endif//!NEWPANEL
  2206. #if defined (SDSUPPORT) && defined(SDCARDDETECT) && (SDCARDDETECT > 0)
  2207. pinMode(SDCARDDETECT, INPUT);
  2208. WRITE(SDCARDDETECT, HIGH);
  2209. lcd_oldcardstatus = IS_SD_INSERTED;
  2210. #endif//(SDCARDDETECT > 0)
  2211. #ifdef LCD_HAS_SLOW_BUTTONS
  2212. slow_buttons = 0;
  2213. #endif
  2214. lcd_buttons_update();
  2215. #ifdef ULTIPANEL
  2216. encoderDiff = 0;
  2217. #endif
  2218. }
  2219. //#include <avr/pgmspace.h>
  2220. static volatile bool lcd_update_enabled = true;
  2221. void lcd_update_enable(bool enabled)
  2222. {
  2223. lcd_update_enabled = enabled;
  2224. }
  2225. void lcd_update()
  2226. {
  2227. static unsigned long timeoutToStatus = 0;
  2228. if (! lcd_update_enabled)
  2229. return;
  2230. #ifdef LCD_HAS_SLOW_BUTTONS
  2231. slow_buttons = lcd_implementation_read_slow_buttons(); // buttons which take too long to read in interrupt context
  2232. #endif
  2233. lcd_buttons_update();
  2234. #if (SDCARDDETECT > 0)
  2235. if ((IS_SD_INSERTED != lcd_oldcardstatus && lcd_detected()))
  2236. {
  2237. lcdDrawUpdate = 2;
  2238. lcd_oldcardstatus = IS_SD_INSERTED;
  2239. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  2240. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  2241. currentMenu == lcd_status_screen
  2242. #endif
  2243. );
  2244. if (lcd_oldcardstatus)
  2245. {
  2246. card.initsd();
  2247. LCD_MESSAGERPGM(MSG_SD_INSERTED);
  2248. }
  2249. else
  2250. {
  2251. card.release();
  2252. LCD_MESSAGERPGM(MSG_SD_REMOVED);
  2253. }
  2254. }
  2255. #endif//CARDINSERTED
  2256. if (lcd_next_update_millis < millis())
  2257. {
  2258. #ifdef ULTIPANEL
  2259. #ifdef REPRAPWORLD_KEYPAD
  2260. if (REPRAPWORLD_KEYPAD_MOVE_Z_UP) {
  2261. reprapworld_keypad_move_z_up();
  2262. }
  2263. if (REPRAPWORLD_KEYPAD_MOVE_Z_DOWN) {
  2264. reprapworld_keypad_move_z_down();
  2265. }
  2266. if (REPRAPWORLD_KEYPAD_MOVE_X_LEFT) {
  2267. reprapworld_keypad_move_x_left();
  2268. }
  2269. if (REPRAPWORLD_KEYPAD_MOVE_X_RIGHT) {
  2270. reprapworld_keypad_move_x_right();
  2271. }
  2272. if (REPRAPWORLD_KEYPAD_MOVE_Y_DOWN) {
  2273. reprapworld_keypad_move_y_down();
  2274. }
  2275. if (REPRAPWORLD_KEYPAD_MOVE_Y_UP) {
  2276. reprapworld_keypad_move_y_up();
  2277. }
  2278. if (REPRAPWORLD_KEYPAD_MOVE_HOME) {
  2279. reprapworld_keypad_move_home();
  2280. }
  2281. #endif
  2282. if (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP)
  2283. {
  2284. lcdDrawUpdate = 1;
  2285. encoderPosition += encoderDiff / ENCODER_PULSES_PER_STEP;
  2286. encoderDiff = 0;
  2287. timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  2288. }
  2289. if (LCD_CLICKED)
  2290. timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  2291. #endif//ULTIPANEL
  2292. #ifdef DOGLCD // Changes due to different driver architecture of the DOGM display
  2293. blink++; // Variable for fan animation and alive dot
  2294. u8g.firstPage();
  2295. do
  2296. {
  2297. u8g.setFont(u8g_font_6x10_marlin);
  2298. u8g.setPrintPos(125, 0);
  2299. if (blink % 2) u8g.setColorIndex(1); else u8g.setColorIndex(0); // Set color for the alive dot
  2300. u8g.drawPixel(127, 63); // draw alive dot
  2301. u8g.setColorIndex(1); // black on white
  2302. (*currentMenu)();
  2303. if (!lcdDrawUpdate) break; // Terminate display update, when nothing new to draw. This must be done before the last dogm.next()
  2304. } while (u8g.nextPage());
  2305. #else
  2306. (*currentMenu)();
  2307. #endif
  2308. #ifdef LCD_HAS_STATUS_INDICATORS
  2309. lcd_implementation_update_indicators();
  2310. #endif
  2311. #ifdef ULTIPANEL
  2312. if (timeoutToStatus < millis() && currentMenu != lcd_status_screen)
  2313. {
  2314. // Exiting a menu. Let's call the menu function the last time with menuExiting flag set to true
  2315. // to give it a chance to save its state.
  2316. // This is useful for example, when the babystep value has to be written into EEPROM.
  2317. if (currentMenu != NULL) {
  2318. menuExiting = true;
  2319. (*currentMenu)();
  2320. menuExiting = false;
  2321. }
  2322. lcd_return_to_status();
  2323. lcdDrawUpdate = 2;
  2324. }
  2325. #endif//ULTIPANEL
  2326. if (lcdDrawUpdate == 2) lcd_implementation_clear();
  2327. if (lcdDrawUpdate) lcdDrawUpdate--;
  2328. lcd_next_update_millis = millis() + LCD_UPDATE_INTERVAL;
  2329. }
  2330. }
  2331. void lcd_ignore_click(bool b)
  2332. {
  2333. ignore_click = b;
  2334. wait_for_unclick = false;
  2335. }
  2336. void lcd_finishstatus() {
  2337. int len = strlen(lcd_status_message);
  2338. if (len > 0) {
  2339. while (len < LCD_WIDTH) {
  2340. lcd_status_message[len++] = ' ';
  2341. }
  2342. }
  2343. lcd_status_message[LCD_WIDTH] = '\0';
  2344. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  2345. #if PROGRESS_MSG_EXPIRE > 0
  2346. messageTick =
  2347. #endif
  2348. progressBarTick = millis();
  2349. #endif
  2350. lcdDrawUpdate = 2;
  2351. #ifdef FILAMENT_LCD_DISPLAY
  2352. message_millis = millis(); //get status message to show up for a while
  2353. #endif
  2354. }
  2355. void lcd_setstatus(const char* message)
  2356. {
  2357. if (lcd_status_message_level > 0)
  2358. return;
  2359. strncpy(lcd_status_message, message, LCD_WIDTH);
  2360. lcd_finishstatus();
  2361. }
  2362. void lcd_setstatuspgm(const char* message)
  2363. {
  2364. if (lcd_status_message_level > 0)
  2365. return;
  2366. strncpy_P(lcd_status_message, message, LCD_WIDTH);
  2367. lcd_finishstatus();
  2368. }
  2369. void lcd_setalertstatuspgm(const char* message)
  2370. {
  2371. lcd_setstatuspgm(message);
  2372. lcd_status_message_level = 1;
  2373. #ifdef ULTIPANEL
  2374. lcd_return_to_status();
  2375. #endif//ULTIPANEL
  2376. }
  2377. void lcd_reset_alert_level()
  2378. {
  2379. lcd_status_message_level = 0;
  2380. }
  2381. #ifdef DOGLCD
  2382. void lcd_setcontrast(uint8_t value)
  2383. {
  2384. lcd_contrast = value & 63;
  2385. u8g.setContrast(lcd_contrast);
  2386. }
  2387. #endif
  2388. #ifdef ULTIPANEL
  2389. /* Warning: This function is called from interrupt context */
  2390. void lcd_buttons_update()
  2391. {
  2392. #ifdef NEWPANEL
  2393. uint8_t newbutton = 0;
  2394. if (READ(BTN_EN1) == 0) newbutton |= EN_A;
  2395. if (READ(BTN_EN2) == 0) newbutton |= EN_B;
  2396. #if BTN_ENC > 0
  2397. if ((blocking_enc < millis()) && (READ(BTN_ENC) == 0))
  2398. newbutton |= EN_C;
  2399. #endif
  2400. buttons = newbutton;
  2401. #ifdef LCD_HAS_SLOW_BUTTONS
  2402. buttons |= slow_buttons;
  2403. #endif
  2404. #ifdef REPRAPWORLD_KEYPAD
  2405. // for the reprapworld_keypad
  2406. uint8_t newbutton_reprapworld_keypad = 0;
  2407. WRITE(SHIFT_LD, LOW);
  2408. WRITE(SHIFT_LD, HIGH);
  2409. for (int8_t i = 0; i < 8; i++) {
  2410. newbutton_reprapworld_keypad = newbutton_reprapworld_keypad >> 1;
  2411. if (READ(SHIFT_OUT))
  2412. newbutton_reprapworld_keypad |= (1 << 7);
  2413. WRITE(SHIFT_CLK, HIGH);
  2414. WRITE(SHIFT_CLK, LOW);
  2415. }
  2416. buttons_reprapworld_keypad = ~newbutton_reprapworld_keypad; //invert it, because a pressed switch produces a logical 0
  2417. #endif
  2418. #else //read it from the shift register
  2419. uint8_t newbutton = 0;
  2420. WRITE(SHIFT_LD, LOW);
  2421. WRITE(SHIFT_LD, HIGH);
  2422. unsigned char tmp_buttons = 0;
  2423. for (int8_t i = 0; i < 8; i++)
  2424. {
  2425. newbutton = newbutton >> 1;
  2426. if (READ(SHIFT_OUT))
  2427. newbutton |= (1 << 7);
  2428. WRITE(SHIFT_CLK, HIGH);
  2429. WRITE(SHIFT_CLK, LOW);
  2430. }
  2431. buttons = ~newbutton; //invert it, because a pressed switch produces a logical 0
  2432. #endif//!NEWPANEL
  2433. //manage encoder rotation
  2434. uint8_t enc = 0;
  2435. if (buttons & EN_A) enc |= B01;
  2436. if (buttons & EN_B) enc |= B10;
  2437. if (enc != lastEncoderBits)
  2438. {
  2439. switch (enc)
  2440. {
  2441. case encrot0:
  2442. if (lastEncoderBits == encrot3)
  2443. encoderDiff++;
  2444. else if (lastEncoderBits == encrot1)
  2445. encoderDiff--;
  2446. break;
  2447. case encrot1:
  2448. if (lastEncoderBits == encrot0)
  2449. encoderDiff++;
  2450. else if (lastEncoderBits == encrot2)
  2451. encoderDiff--;
  2452. break;
  2453. case encrot2:
  2454. if (lastEncoderBits == encrot1)
  2455. encoderDiff++;
  2456. else if (lastEncoderBits == encrot3)
  2457. encoderDiff--;
  2458. break;
  2459. case encrot3:
  2460. if (lastEncoderBits == encrot2)
  2461. encoderDiff++;
  2462. else if (lastEncoderBits == encrot0)
  2463. encoderDiff--;
  2464. break;
  2465. }
  2466. }
  2467. lastEncoderBits = enc;
  2468. }
  2469. bool lcd_detected(void)
  2470. {
  2471. #if (defined(LCD_I2C_TYPE_MCP23017) || defined(LCD_I2C_TYPE_MCP23008)) && defined(DETECT_DEVICE)
  2472. return lcd.LcdDetected() == 1;
  2473. #else
  2474. return true;
  2475. #endif
  2476. }
  2477. void lcd_buzz(long duration, uint16_t freq)
  2478. {
  2479. #ifdef LCD_USE_I2C_BUZZER
  2480. lcd.buzz(duration, freq);
  2481. #endif
  2482. }
  2483. bool lcd_clicked()
  2484. {
  2485. return LCD_CLICKED;
  2486. }
  2487. #endif//ULTIPANEL
  2488. /********************************/
  2489. /** Float conversion utilities **/
  2490. /********************************/
  2491. // convert float to string with +123.4 format
  2492. char conv[8];
  2493. char *ftostr3(const float &x)
  2494. {
  2495. return itostr3((int)x);
  2496. }
  2497. char *itostr2(const uint8_t &x)
  2498. {
  2499. //sprintf(conv,"%5.1f",x);
  2500. int xx = x;
  2501. conv[0] = (xx / 10) % 10 + '0';
  2502. conv[1] = (xx) % 10 + '0';
  2503. conv[2] = 0;
  2504. return conv;
  2505. }
  2506. // Convert float to string with 123.4 format, dropping sign
  2507. char *ftostr31(const float &x)
  2508. {
  2509. int xx = x * 10;
  2510. conv[0] = (xx >= 0) ? '+' : '-';
  2511. xx = abs(xx);
  2512. conv[1] = (xx / 1000) % 10 + '0';
  2513. conv[2] = (xx / 100) % 10 + '0';
  2514. conv[3] = (xx / 10) % 10 + '0';
  2515. conv[4] = '.';
  2516. conv[5] = (xx) % 10 + '0';
  2517. conv[6] = 0;
  2518. return conv;
  2519. }
  2520. // Convert float to string with 123.4 format
  2521. char *ftostr31ns(const float &x)
  2522. {
  2523. int xx = x * 10;
  2524. //conv[0]=(xx>=0)?'+':'-';
  2525. xx = abs(xx);
  2526. conv[0] = (xx / 1000) % 10 + '0';
  2527. conv[1] = (xx / 100) % 10 + '0';
  2528. conv[2] = (xx / 10) % 10 + '0';
  2529. conv[3] = '.';
  2530. conv[4] = (xx) % 10 + '0';
  2531. conv[5] = 0;
  2532. return conv;
  2533. }
  2534. char *ftostr32(const float &x)
  2535. {
  2536. long xx = x * 100;
  2537. if (xx >= 0)
  2538. conv[0] = (xx / 10000) % 10 + '0';
  2539. else
  2540. conv[0] = '-';
  2541. xx = abs(xx);
  2542. conv[1] = (xx / 1000) % 10 + '0';
  2543. conv[2] = (xx / 100) % 10 + '0';
  2544. conv[3] = '.';
  2545. conv[4] = (xx / 10) % 10 + '0';
  2546. conv[5] = (xx) % 10 + '0';
  2547. conv[6] = 0;
  2548. return conv;
  2549. }
  2550. //// Convert float to rj string with 123.45 format
  2551. char *ftostr32ns(const float &x) {
  2552. long xx = abs(x);
  2553. conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
  2554. conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  2555. conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : '0';
  2556. conv[3] = '.';
  2557. conv[4] = (xx / 10) % 10 + '0';
  2558. conv[5] = xx % 10 + '0';
  2559. return conv;
  2560. }
  2561. // Convert float to string with 1.234 format
  2562. char *ftostr43(const float &x)
  2563. {
  2564. long xx = x * 1000;
  2565. if (xx >= 0)
  2566. conv[0] = (xx / 1000) % 10 + '0';
  2567. else
  2568. conv[0] = '-';
  2569. xx = abs(xx);
  2570. conv[1] = '.';
  2571. conv[2] = (xx / 100) % 10 + '0';
  2572. conv[3] = (xx / 10) % 10 + '0';
  2573. conv[4] = (xx) % 10 + '0';
  2574. conv[5] = 0;
  2575. return conv;
  2576. }
  2577. //Float to string with 1.23 format
  2578. char *ftostr12ns(const float &x)
  2579. {
  2580. long xx = x * 100;
  2581. xx = abs(xx);
  2582. conv[0] = (xx / 100) % 10 + '0';
  2583. conv[1] = '.';
  2584. conv[2] = (xx / 10) % 10 + '0';
  2585. conv[3] = (xx) % 10 + '0';
  2586. conv[4] = 0;
  2587. return conv;
  2588. }
  2589. //Float to string with 1.234 format
  2590. char *ftostr13ns(const float &x)
  2591. {
  2592. long xx = x * 1000;
  2593. if (xx >= 0)
  2594. conv[0] = ' ';
  2595. else
  2596. conv[0] = '-';
  2597. xx = abs(xx);
  2598. conv[1] = (xx / 1000) % 10 + '0';
  2599. conv[2] = '.';
  2600. conv[3] = (xx / 100) % 10 + '0';
  2601. conv[4] = (xx / 10) % 10 + '0';
  2602. conv[5] = (xx) % 10 + '0';
  2603. conv[6] = 0;
  2604. return conv;
  2605. }
  2606. // convert float to space-padded string with -_23.4_ format
  2607. char *ftostr32sp(const float &x) {
  2608. long xx = abs(x * 100);
  2609. uint8_t dig;
  2610. if (x < 0) { // negative val = -_0
  2611. conv[0] = '-';
  2612. dig = (xx / 1000) % 10;
  2613. conv[1] = dig ? '0' + dig : ' ';
  2614. }
  2615. else { // positive val = __0
  2616. dig = (xx / 10000) % 10;
  2617. if (dig) {
  2618. conv[0] = '0' + dig;
  2619. conv[1] = '0' + (xx / 1000) % 10;
  2620. }
  2621. else {
  2622. conv[0] = ' ';
  2623. dig = (xx / 1000) % 10;
  2624. conv[1] = dig ? '0' + dig : ' ';
  2625. }
  2626. }
  2627. conv[2] = '0' + (xx / 100) % 10; // lsd always
  2628. dig = xx % 10;
  2629. if (dig) { // 2 decimal places
  2630. conv[5] = '0' + dig;
  2631. conv[4] = '0' + (xx / 10) % 10;
  2632. conv[3] = '.';
  2633. }
  2634. else { // 1 or 0 decimal place
  2635. dig = (xx / 10) % 10;
  2636. if (dig) {
  2637. conv[4] = '0' + dig;
  2638. conv[3] = '.';
  2639. }
  2640. else {
  2641. conv[3] = conv[4] = ' ';
  2642. }
  2643. conv[5] = ' ';
  2644. }
  2645. conv[6] = '\0';
  2646. return conv;
  2647. }
  2648. char *itostr31(const int &xx)
  2649. {
  2650. conv[0] = (xx >= 0) ? '+' : '-';
  2651. conv[1] = (xx / 1000) % 10 + '0';
  2652. conv[2] = (xx / 100) % 10 + '0';
  2653. conv[3] = (xx / 10) % 10 + '0';
  2654. conv[4] = '.';
  2655. conv[5] = (xx) % 10 + '0';
  2656. conv[6] = 0;
  2657. return conv;
  2658. }
  2659. // Convert int to rj string with 123 or -12 format
  2660. char *itostr3(const int &x)
  2661. {
  2662. int xx = x;
  2663. if (xx < 0) {
  2664. conv[0] = '-';
  2665. xx = -xx;
  2666. } else if (xx >= 100)
  2667. conv[0] = (xx / 100) % 10 + '0';
  2668. else
  2669. conv[0] = ' ';
  2670. if (xx >= 10)
  2671. conv[1] = (xx / 10) % 10 + '0';
  2672. else
  2673. conv[1] = ' ';
  2674. conv[2] = (xx) % 10 + '0';
  2675. conv[3] = 0;
  2676. return conv;
  2677. }
  2678. // Convert int to lj string with 123 format
  2679. char *itostr3left(const int &xx)
  2680. {
  2681. if (xx >= 100)
  2682. {
  2683. conv[0] = (xx / 100) % 10 + '0';
  2684. conv[1] = (xx / 10) % 10 + '0';
  2685. conv[2] = (xx) % 10 + '0';
  2686. conv[3] = 0;
  2687. }
  2688. else if (xx >= 10)
  2689. {
  2690. conv[0] = (xx / 10) % 10 + '0';
  2691. conv[1] = (xx) % 10 + '0';
  2692. conv[2] = 0;
  2693. }
  2694. else
  2695. {
  2696. conv[0] = (xx) % 10 + '0';
  2697. conv[1] = 0;
  2698. }
  2699. return conv;
  2700. }
  2701. // Convert int to rj string with 1234 format
  2702. char *itostr4(const int &xx) {
  2703. conv[0] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  2704. conv[1] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  2705. conv[2] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  2706. conv[3] = xx % 10 + '0';
  2707. conv[4] = 0;
  2708. return conv;
  2709. }
  2710. // Convert float to rj string with 12345 format
  2711. char *ftostr5(const float &x) {
  2712. long xx = abs(x);
  2713. conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
  2714. conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  2715. conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  2716. conv[3] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  2717. conv[4] = xx % 10 + '0';
  2718. conv[5] = 0;
  2719. return conv;
  2720. }
  2721. // Convert float to string with +1234.5 format
  2722. char *ftostr51(const float &x)
  2723. {
  2724. long xx = x * 10;
  2725. conv[0] = (xx >= 0) ? '+' : '-';
  2726. xx = abs(xx);
  2727. conv[1] = (xx / 10000) % 10 + '0';
  2728. conv[2] = (xx / 1000) % 10 + '0';
  2729. conv[3] = (xx / 100) % 10 + '0';
  2730. conv[4] = (xx / 10) % 10 + '0';
  2731. conv[5] = '.';
  2732. conv[6] = (xx) % 10 + '0';
  2733. conv[7] = 0;
  2734. return conv;
  2735. }
  2736. // Convert float to string with +123.45 format
  2737. char *ftostr52(const float &x)
  2738. {
  2739. long xx = x * 100;
  2740. conv[0] = (xx >= 0) ? '+' : '-';
  2741. xx = abs(xx);
  2742. conv[1] = (xx / 10000) % 10 + '0';
  2743. conv[2] = (xx / 1000) % 10 + '0';
  2744. conv[3] = (xx / 100) % 10 + '0';
  2745. conv[4] = '.';
  2746. conv[5] = (xx / 10) % 10 + '0';
  2747. conv[6] = (xx) % 10 + '0';
  2748. conv[7] = 0;
  2749. return conv;
  2750. }
  2751. // Callback for after editing PID i value
  2752. // grab the PID i value out of the temp variable; scale it; then update the PID driver
  2753. void copy_and_scalePID_i()
  2754. {
  2755. #ifdef PIDTEMP
  2756. Ki = scalePID_i(raw_Ki);
  2757. updatePID();
  2758. #endif
  2759. }
  2760. // Callback for after editing PID d value
  2761. // grab the PID d value out of the temp variable; scale it; then update the PID driver
  2762. void copy_and_scalePID_d()
  2763. {
  2764. #ifdef PIDTEMP
  2765. Kd = scalePID_d(raw_Kd);
  2766. updatePID();
  2767. #endif
  2768. }
  2769. #endif //ULTRA_LCD