ultralcd.cpp 83 KB

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