ultralcd.cpp 84 KB

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