ultralcd.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. #include "temperature.h"
  2. #include "ultralcd.h"
  3. #ifdef ULTRA_LCD
  4. #include "Marlin.h"
  5. #include "language.h"
  6. #include "cardreader.h"
  7. #include "temperature.h"
  8. #include "stepper.h"
  9. #include "ConfigurationStore.h"
  10. #include <string.h>
  11. //#include "Configuration.h"
  12. #define _STRINGIFY(s) #s
  13. int8_t encoderDiff; /* encoderDiff is updated from interrupt context and added to encoderPosition every LCD update */
  14. extern int lcd_change_fil_state;
  15. int8_t ReInitLCD = 0;
  16. int8_t SDscrool = 0;
  17. /* Configuration settings */
  18. int plaPreheatHotendTemp;
  19. int plaPreheatHPBTemp;
  20. int plaPreheatFanSpeed;
  21. int absPreheatHotendTemp;
  22. int absPreheatHPBTemp;
  23. int absPreheatFanSpeed;
  24. int ppPreheatHotendTemp = PP_PREHEAT_HOTEND_TEMP;
  25. int ppPreheatHPBTemp = PP_PREHEAT_HPB_TEMP;
  26. int ppPreheatFanSpeed = PP_PREHEAT_FAN_SPEED;
  27. int petPreheatHotendTemp = PET_PREHEAT_HOTEND_TEMP;
  28. int petPreheatHPBTemp = PET_PREHEAT_HPB_TEMP;
  29. int petPreheatFanSpeed = PET_PREHEAT_FAN_SPEED;
  30. int hipsPreheatHotendTemp = HIPS_PREHEAT_HOTEND_TEMP;
  31. int hipsPreheatHPBTemp = HIPS_PREHEAT_HPB_TEMP;
  32. int hipsPreheatFanSpeed = HIPS_PREHEAT_FAN_SPEED;
  33. int flexPreheatHotendTemp = FLEX_PREHEAT_HOTEND_TEMP;
  34. int flexPreheatHPBTemp = FLEX_PREHEAT_HPB_TEMP;
  35. int flexPreheatFanSpeed = FLEX_PREHEAT_FAN_SPEED;
  36. #ifdef FILAMENT_LCD_DISPLAY
  37. unsigned long message_millis = 0;
  38. #endif
  39. #ifdef ULTIPANEL
  40. static float manual_feedrate[] = MANUAL_FEEDRATE;
  41. #endif // ULTIPANEL
  42. /* !Configuration settings */
  43. //Function pointer to menu functions.
  44. typedef void (*menuFunc_t)();
  45. uint8_t lcd_status_message_level;
  46. char lcd_status_message[LCD_WIDTH+1] = WELCOME_MSG;
  47. #ifdef DOGLCD
  48. #include "dogm_lcd_implementation.h"
  49. #else
  50. #include "ultralcd_implementation_hitachi_HD44780.h"
  51. #endif
  52. /** forward declarations **/
  53. void copy_and_scalePID_i();
  54. void copy_and_scalePID_d();
  55. /* Different menus */
  56. static void lcd_status_screen();
  57. #ifdef ULTIPANEL
  58. extern bool powersupply;
  59. static void lcd_main_menu();
  60. static void lcd_tune_menu();
  61. static void lcd_prepare_menu();
  62. static void lcd_move_menu();
  63. static void lcd_control_menu();
  64. static void lcd_settings_menu();
  65. static void lcd_control_temperature_menu();
  66. static void lcd_control_temperature_preheat_pla_settings_menu();
  67. static void lcd_control_temperature_preheat_abs_settings_menu();
  68. static void lcd_control_motion_menu();
  69. static void lcd_control_volumetric_menu();
  70. #ifdef DOGLCD
  71. static void lcd_set_contrast();
  72. #endif
  73. static void lcd_control_retract_menu();
  74. static void lcd_sdcard_menu();
  75. #ifdef DELTA_CALIBRATION_MENU
  76. static void lcd_delta_calibrate_menu();
  77. #endif // DELTA_CALIBRATION_MENU
  78. static void lcd_quick_feedback();//Cause an LCD refresh, and give the user visual or audible feedback that something has happened
  79. /* Different types of actions that can be used in menu items. */
  80. static void menu_action_back(menuFunc_t data);
  81. static void menu_action_submenu(menuFunc_t data);
  82. static void menu_action_gcode(const char* pgcode);
  83. static void menu_action_function(menuFunc_t data);
  84. static void menu_action_sdfile(const char* filename, char* longFilename);
  85. static void menu_action_sddirectory(const char* filename, char* longFilename);
  86. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr);
  87. static void menu_action_setting_edit_int3(const char* pstr, int* ptr, int minValue, int maxValue);
  88. static void menu_action_setting_edit_float3(const char* pstr, float* ptr, float minValue, float maxValue);
  89. static void menu_action_setting_edit_float32(const char* pstr, float* ptr, float minValue, float maxValue);
  90. static void menu_action_setting_edit_float43(const char* pstr, float* ptr, float minValue, float maxValue);
  91. static void menu_action_setting_edit_float5(const char* pstr, float* ptr, float minValue, float maxValue);
  92. static void menu_action_setting_edit_float51(const char* pstr, float* ptr, float minValue, float maxValue);
  93. static void menu_action_setting_edit_float52(const char* pstr, float* ptr, float minValue, float maxValue);
  94. static void menu_action_setting_edit_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue);
  95. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, menuFunc_t callbackFunc);
  96. static void menu_action_setting_edit_callback_int3(const char* pstr, int* ptr, int minValue, int maxValue, menuFunc_t callbackFunc);
  97. static void menu_action_setting_edit_callback_float3(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  98. static void menu_action_setting_edit_callback_float32(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  99. static void menu_action_setting_edit_callback_float43(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  100. static void menu_action_setting_edit_callback_float5(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  101. static void menu_action_setting_edit_callback_float51(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  102. static void menu_action_setting_edit_callback_float52(const char* pstr, float* ptr, float minValue, float maxValue, menuFunc_t callbackFunc);
  103. static void menu_action_setting_edit_callback_long5(const char* pstr, unsigned long* ptr, unsigned long minValue, unsigned long maxValue, menuFunc_t callbackFunc);
  104. #define ENCODER_FEEDRATE_DEADZONE 10
  105. #if !defined(LCD_I2C_VIKI)
  106. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  107. #define ENCODER_STEPS_PER_MENU_ITEM 5
  108. #endif
  109. #ifndef ENCODER_PULSES_PER_STEP
  110. #define ENCODER_PULSES_PER_STEP 1
  111. #endif
  112. #else
  113. #ifndef ENCODER_STEPS_PER_MENU_ITEM
  114. #define ENCODER_STEPS_PER_MENU_ITEM 2 // VIKI LCD rotary encoder uses a different number of steps per rotation
  115. #endif
  116. #ifndef ENCODER_PULSES_PER_STEP
  117. #define ENCODER_PULSES_PER_STEP 1
  118. #endif
  119. #endif
  120. /* Helper macros for menus */
  121. #define START_MENU() do { \
  122. if (encoderPosition > 0x8000) encoderPosition = 0; \
  123. if (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM < currentMenuViewOffset) currentMenuViewOffset = encoderPosition / ENCODER_STEPS_PER_MENU_ITEM;\
  124. uint8_t _lineNr = currentMenuViewOffset, _menuItemNr; \
  125. bool wasClicked = LCD_CLICKED;\
  126. for(uint8_t _drawLineNr = 0; _drawLineNr < LCD_HEIGHT; _drawLineNr++, _lineNr++) { \
  127. _menuItemNr = 0;
  128. #define MENU_ITEM(type, label, args...) do { \
  129. if (_menuItemNr == _lineNr) { \
  130. if (lcdDrawUpdate) { \
  131. const char* _label_pstr = PSTR(label); \
  132. if ((encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) == _menuItemNr) { \
  133. lcd_implementation_drawmenu_ ## type ## _selected (_drawLineNr, _label_pstr , ## args ); \
  134. }else{\
  135. lcd_implementation_drawmenu_ ## type (_drawLineNr, _label_pstr , ## args ); \
  136. }\
  137. }\
  138. if (wasClicked && (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM) == _menuItemNr) {\
  139. lcd_quick_feedback(); \
  140. menu_action_ ## type ( args ); \
  141. return;\
  142. }\
  143. }\
  144. _menuItemNr++;\
  145. } while(0)
  146. #define MENU_ITEM_DUMMY() do { _menuItemNr++; } while(0)
  147. #define MENU_ITEM_EDIT(type, label, args...) MENU_ITEM(setting_edit_ ## type, label, PSTR(label) , ## args )
  148. #define MENU_ITEM_EDIT_CALLBACK(type, label, args...) MENU_ITEM(setting_edit_callback_ ## type, label, PSTR(label) , ## args )
  149. #define END_MENU() \
  150. if (encoderPosition / ENCODER_STEPS_PER_MENU_ITEM >= _menuItemNr) encoderPosition = _menuItemNr * ENCODER_STEPS_PER_MENU_ITEM - 1; \
  151. 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; } \
  152. } } while(0)
  153. /** Used variables to keep track of the menu */
  154. #ifndef REPRAPWORLD_KEYPAD
  155. volatile uint8_t buttons;//Contains the bits of the currently pressed buttons.
  156. #else
  157. volatile uint8_t buttons_reprapworld_keypad; // to store the reprapworld_keypad shift register values
  158. #endif
  159. #ifdef LCD_HAS_SLOW_BUTTONS
  160. volatile uint8_t slow_buttons;//Contains the bits of the currently pressed buttons.
  161. #endif
  162. uint8_t currentMenuViewOffset; /* scroll offset in the current menu */
  163. uint32_t blocking_enc;
  164. uint8_t lastEncoderBits;
  165. uint32_t encoderPosition;
  166. #if (SDCARDDETECT > 0)
  167. bool lcd_oldcardstatus;
  168. #endif
  169. #endif //ULTIPANEL
  170. menuFunc_t currentMenu = lcd_status_screen; /* function pointer to the currently active menu */
  171. uint32_t lcd_next_update_millis;
  172. uint8_t lcd_status_update_delay;
  173. bool ignore_click = false;
  174. bool wait_for_unclick;
  175. 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) */
  176. //prevMenu and prevEncoderPosition are used to store the previous menu location when editing settings.
  177. menuFunc_t prevMenu = NULL;
  178. uint16_t prevEncoderPosition;
  179. //Variables used when editing values.
  180. const char* editLabel;
  181. void* editValue;
  182. int32_t minEditValue, maxEditValue;
  183. menuFunc_t callbackFunc;
  184. // place-holders for Ki and Kd edits
  185. float raw_Ki, raw_Kd;
  186. static void lcd_goto_menu(menuFunc_t menu, const uint32_t encoder=0, const bool feedback=true) {
  187. if (currentMenu != menu) {
  188. currentMenu = menu;
  189. encoderPosition = encoder;
  190. if (feedback) lcd_quick_feedback();
  191. // For LCD_PROGRESS_BAR re-initialize the custom characters
  192. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  193. lcd_set_custom_characters(menu == lcd_status_screen);
  194. #endif
  195. }
  196. }
  197. /* Main status screen. It's up to the implementation specific part to show what is needed. As this is very display dependent */
  198. static void lcd_status_screen()
  199. {
  200. if (lcd_status_update_delay)
  201. lcd_status_update_delay--;
  202. else
  203. lcdDrawUpdate = 1;
  204. if (lcdDrawUpdate)
  205. {
  206. ReInitLCD++;
  207. if(ReInitLCD == 30){
  208. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  209. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  210. currentMenu == lcd_status_screen
  211. #endif
  212. );
  213. ReInitLCD =0 ;
  214. }else{
  215. if((ReInitLCD%10) == 0){
  216. //lcd_implementation_nodisplay();
  217. lcd_implementation_init_noclear( // to maybe revive the LCD if static electricity killed it.
  218. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  219. currentMenu == lcd_status_screen
  220. #endif
  221. );
  222. }
  223. }
  224. //lcd_implementation_display();
  225. lcd_implementation_status_screen();
  226. //lcd_implementation_clear();
  227. 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 */
  228. }
  229. #ifdef ULTIPANEL
  230. bool current_click = LCD_CLICKED;
  231. if (ignore_click) {
  232. if (wait_for_unclick) {
  233. if (!current_click) {
  234. ignore_click = wait_for_unclick = false;
  235. }
  236. else {
  237. current_click = false;
  238. }
  239. }
  240. else if (current_click) {
  241. lcd_quick_feedback();
  242. wait_for_unclick = true;
  243. current_click = false;
  244. }
  245. }
  246. if (current_click)
  247. {
  248. lcd_goto_menu(lcd_main_menu);
  249. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  250. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  251. currentMenu == lcd_status_screen
  252. #endif
  253. );
  254. #ifdef FILAMENT_LCD_DISPLAY
  255. message_millis = millis(); // get status message to show up for a while
  256. #endif
  257. }
  258. #ifdef ULTIPANEL_FEEDMULTIPLY
  259. // Dead zone at 100% feedrate
  260. if ((feedmultiply < 100 && (feedmultiply + int(encoderPosition)) > 100) ||
  261. (feedmultiply > 100 && (feedmultiply + int(encoderPosition)) < 100))
  262. {
  263. encoderPosition = 0;
  264. feedmultiply = 100;
  265. }
  266. if (feedmultiply == 100 && int(encoderPosition) > ENCODER_FEEDRATE_DEADZONE)
  267. {
  268. feedmultiply += int(encoderPosition) - ENCODER_FEEDRATE_DEADZONE;
  269. encoderPosition = 0;
  270. }
  271. else if (feedmultiply == 100 && int(encoderPosition) < -ENCODER_FEEDRATE_DEADZONE)
  272. {
  273. feedmultiply += int(encoderPosition) + ENCODER_FEEDRATE_DEADZONE;
  274. encoderPosition = 0;
  275. }
  276. else if (feedmultiply != 100)
  277. {
  278. feedmultiply += int(encoderPosition);
  279. encoderPosition = 0;
  280. }
  281. #endif //ULTIPANEL_FEEDMULTIPLY
  282. if (feedmultiply < 10)
  283. feedmultiply = 10;
  284. else if (feedmultiply > 999)
  285. feedmultiply = 999;
  286. #endif //ULTIPANEL
  287. }
  288. #ifdef ULTIPANEL
  289. static void lcd_return_to_status() {
  290. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  291. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  292. currentMenu == lcd_status_screen
  293. #endif
  294. );
  295. lcd_goto_menu(lcd_status_screen, 0, false);
  296. }
  297. static void lcd_sdcard_pause() { card.pauseSDPrint(); }
  298. static void lcd_sdcard_resume() { card.startFileprint(); }
  299. float move_menu_scale;
  300. static void lcd_move_menu_axis();
  301. static void lcd_sdcard_stop()
  302. {
  303. card.sdprinting = false;
  304. card.closefile();
  305. quickStop();
  306. if(SD_FINISHED_STEPPERRELEASE)
  307. {
  308. enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  309. }
  310. autotempShutdown();
  311. cancel_heatup = true;
  312. lcd_setstatus(MSG_PRINT_ABORTED);
  313. enquecommand_P(PSTR("M84"));
  314. }
  315. /* Menu implementation */
  316. void lcd_preheat_pla()
  317. {
  318. setTargetHotend0(plaPreheatHotendTemp);
  319. setTargetBed(plaPreheatHPBTemp);
  320. fanSpeed = 0;
  321. lcd_return_to_status();
  322. setWatch(); // heater sanity check timer
  323. }
  324. void lcd_preheat_abs()
  325. {
  326. setTargetHotend0(absPreheatHotendTemp);
  327. setTargetBed(absPreheatHPBTemp);
  328. fanSpeed = 0;
  329. lcd_return_to_status();
  330. setWatch(); // heater sanity check timer
  331. }
  332. void lcd_preheat_pp()
  333. {
  334. setTargetHotend0(ppPreheatHotendTemp);
  335. setTargetBed(ppPreheatHPBTemp);
  336. fanSpeed = 0;
  337. lcd_return_to_status();
  338. setWatch(); // heater sanity check timer
  339. }
  340. void lcd_preheat_pet()
  341. {
  342. setTargetHotend0(petPreheatHotendTemp);
  343. setTargetBed(petPreheatHPBTemp);
  344. fanSpeed = 0;
  345. lcd_return_to_status();
  346. setWatch(); // heater sanity check timer
  347. }
  348. void lcd_preheat_hips()
  349. {
  350. setTargetHotend0(hipsPreheatHotendTemp);
  351. setTargetBed(hipsPreheatHPBTemp);
  352. fanSpeed = 0;
  353. lcd_return_to_status();
  354. setWatch(); // heater sanity check timer
  355. }
  356. void lcd_preheat_flex()
  357. {
  358. setTargetHotend0(flexPreheatHotendTemp);
  359. setTargetBed(flexPreheatHPBTemp);
  360. fanSpeed = 0;
  361. lcd_return_to_status();
  362. setWatch(); // heater sanity check timer
  363. }
  364. void lcd_cooldown()
  365. {
  366. setTargetHotend0(0);
  367. setTargetHotend1(0);
  368. setTargetHotend2(0);
  369. setTargetBed(0);
  370. fanSpeed = 0;
  371. lcd_return_to_status();
  372. }
  373. static void lcd_preheat_menu()
  374. {
  375. START_MENU();
  376. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  377. MENU_ITEM(function, "ABS - " STRINGIFY(ABS_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(ABS_PREHEAT_HPB_TEMP), lcd_preheat_abs);
  378. MENU_ITEM(function, "PLA - " STRINGIFY(PLA_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(PLA_PREHEAT_HPB_TEMP), lcd_preheat_pla);
  379. MENU_ITEM(function, "PET - " STRINGIFY(PET_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(PET_PREHEAT_HPB_TEMP), lcd_preheat_pet);
  380. MENU_ITEM(function, "HIPS - " STRINGIFY(HIPS_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(HIPS_PREHEAT_HPB_TEMP), lcd_preheat_hips);
  381. MENU_ITEM(function, "PP - " STRINGIFY(PP_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(PP_PREHEAT_HPB_TEMP), lcd_preheat_pp);
  382. MENU_ITEM(function, "FLEX - " STRINGIFY(FLEX_PREHEAT_HOTEND_TEMP) "/" STRINGIFY(FLEX_PREHEAT_HPB_TEMP), lcd_preheat_flex);
  383. MENU_ITEM(function, MSG_COOLDOWN, lcd_cooldown);
  384. END_MENU();
  385. }
  386. static void lcd_support_menu()
  387. {
  388. START_MENU();
  389. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  390. MENU_ITEM(back, MSG_FW_VERSION " - " FW_version, lcd_main_menu);
  391. MENU_ITEM(back, "prusa3d.com", lcd_main_menu);
  392. MENU_ITEM(back, "forum.prusa3d.com", lcd_main_menu);
  393. MENU_ITEM(back, "howto.prusa3d.com", lcd_main_menu);
  394. MENU_ITEM(back, "Rev: " REVISION, lcd_main_menu);
  395. END_MENU();
  396. }
  397. void lcd_unLoadFilament()
  398. {
  399. if(degHotend0() > EXTRUDE_MINTEMP){
  400. enquecommand_P(PSTR(UNLOAD_FILAMENT_0));
  401. enquecommand_P(PSTR(UNLOAD_FILAMENT_1));
  402. }else{
  403. lcd_implementation_clear();
  404. lcd.setCursor(0, 0);
  405. lcd.print(MSG_ERROR);
  406. lcd.setCursor(0, 2);
  407. lcd.print(MSG_PREHEAT_NOZZLE);
  408. delay(2000);
  409. lcd_implementation_clear();
  410. }
  411. lcd_return_to_status();
  412. }
  413. void lcd_change_filament(){
  414. lcd_implementation_clear();
  415. lcd.setCursor(0, 1);
  416. lcd.print(MSG_CHANGING_FILAMENT);
  417. }
  418. void lcd_wait_interact(){
  419. lcd_implementation_clear();
  420. lcd.setCursor(0, 1);
  421. lcd.print(MSG_INSERT_FILAMENT);
  422. lcd.setCursor(0, 2);
  423. lcd.print(MSG_PRESS);
  424. }
  425. void lcd_change_success(){
  426. lcd_implementation_clear();
  427. lcd.setCursor(0, 2);
  428. lcd.print(MSG_CHANGE_SUCCESS);
  429. }
  430. void lcd_loading_color(){
  431. lcd_implementation_clear();
  432. lcd.setCursor(0, 0);
  433. lcd.print(MSG_LOADING_COLOR);
  434. lcd.setCursor(0, 2);
  435. lcd.print(MSG_PLEASE_WAIT);
  436. for(int i = 0; i<20; i++){
  437. lcd.setCursor(i, 3);
  438. lcd.print(".");
  439. for(int j = 0;j<10 ; j++){
  440. manage_heater();
  441. manage_inactivity(true);
  442. delay(85);
  443. }
  444. }
  445. }
  446. void lcd_loading_filament(){
  447. lcd_implementation_clear();
  448. lcd.setCursor(0, 0);
  449. lcd.print(MSG_LOADING_FILAMENT);
  450. lcd.setCursor(0, 2);
  451. lcd.print(MSG_PLEASE_WAIT);
  452. for(int i = 0; i<20; i++){
  453. lcd.setCursor(i, 3);
  454. lcd.print(".");
  455. for(int j = 0;j<10 ; j++){
  456. manage_heater();
  457. manage_inactivity(true);
  458. delay(110);
  459. }
  460. }
  461. }
  462. void lcd_alright(){
  463. int enc_dif = 0;
  464. int cursor_pos = 1;
  465. lcd_implementation_clear();
  466. lcd.setCursor(0, 0);
  467. lcd.print(MSG_CORRECTLY);
  468. lcd.setCursor(1, 1);
  469. lcd.print(MSG_YES);
  470. lcd.setCursor(1, 2);
  471. lcd.print(MSG_NOT_LOADED);
  472. lcd.setCursor(1, 3);
  473. lcd.print(MSG_NOT_COLOR);
  474. lcd.setCursor(0, 1);
  475. lcd.print(">");
  476. enc_dif = encoderDiff;
  477. while(lcd_change_fil_state == 0){
  478. manage_heater();
  479. manage_inactivity(true);
  480. if( enc_dif != encoderDiff ){
  481. if ( (abs(enc_dif-encoderDiff)) > 1 ){
  482. if (enc_dif > encoderDiff ){
  483. cursor_pos --;
  484. }
  485. if (enc_dif < encoderDiff ){
  486. cursor_pos ++;
  487. }
  488. if(cursor_pos >3){
  489. cursor_pos = 3;
  490. }
  491. if(cursor_pos <1){
  492. cursor_pos = 1;
  493. }
  494. lcd.setCursor(0, 1);
  495. lcd.print(" ");
  496. lcd.setCursor(0, 2);
  497. lcd.print(" ");
  498. lcd.setCursor(0, 3);
  499. lcd.print(" ");
  500. lcd.setCursor(0, cursor_pos);
  501. lcd.print(">");
  502. enc_dif = encoderDiff;
  503. delay(100);
  504. }
  505. }
  506. if(lcd_clicked()){
  507. lcd_change_fil_state = cursor_pos;
  508. delay(500);
  509. }
  510. };
  511. lcd_implementation_clear();
  512. lcd_return_to_status();
  513. }
  514. void lcd_LoadFilament()
  515. {
  516. if(degHotend0() > EXTRUDE_MINTEMP){
  517. enquecommand_P(PSTR(LOAD_FILAMENT_0));
  518. enquecommand_P(PSTR(LOAD_FILAMENT_1));
  519. enquecommand_P(PSTR(LOAD_FILAMENT_2));
  520. }else{
  521. lcd_implementation_clear();
  522. lcd.setCursor(0, 0);
  523. lcd.print(MSG_ERROR);
  524. lcd.setCursor(0, 2);
  525. lcd.print(MSG_PREHEAT_NOZZLE);
  526. delay(2000);
  527. lcd_implementation_clear();
  528. }
  529. lcd_return_to_status();
  530. }
  531. static void _lcd_move(const char *name, int axis, int min, int max) {
  532. if (encoderPosition != 0) {
  533. refresh_cmd_timeout();
  534. current_position[axis] += float((int)encoderPosition) * move_menu_scale;
  535. if (min_software_endstops && current_position[axis] < min) current_position[axis] = min;
  536. if (max_software_endstops && current_position[axis] > max) current_position[axis] = max;
  537. encoderPosition = 0;
  538. #ifdef DELTA
  539. calculate_delta(current_position);
  540. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis]/60, active_extruder);
  541. #else
  542. 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);
  543. #endif
  544. lcdDrawUpdate = 1;
  545. }
  546. if (lcdDrawUpdate) lcd_implementation_drawedit(name, ftostr31(current_position[axis]));
  547. if (LCD_CLICKED) lcd_goto_menu(lcd_move_menu_axis);
  548. }
  549. static void lcd_move_e()
  550. {
  551. if (encoderPosition != 0)
  552. {
  553. current_position[E_AXIS] += float((int)encoderPosition) * move_menu_scale;
  554. encoderPosition = 0;
  555. #ifdef DELTA
  556. calculate_delta(current_position);
  557. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], manual_feedrate[E_AXIS]/60, active_extruder);
  558. #else
  559. 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);
  560. #endif
  561. lcdDrawUpdate = 1;
  562. }
  563. if (lcdDrawUpdate)
  564. {
  565. lcd_implementation_drawedit(PSTR("Extruder"), ftostr31(current_position[E_AXIS]));
  566. }
  567. if (LCD_CLICKED) lcd_goto_menu(lcd_move_menu_axis);
  568. }
  569. static void lcd_move_x() { _lcd_move(PSTR("X"), X_AXIS, X_MIN_POS, X_MAX_POS); }
  570. static void lcd_move_y() { _lcd_move(PSTR("Y"), Y_AXIS, Y_MIN_POS, Y_MAX_POS); }
  571. static void lcd_move_z() { _lcd_move(PSTR("Z"), Z_AXIS, Z_MIN_POS, Z_MAX_POS); }
  572. void lcd_move_menu_axis()
  573. {
  574. START_MENU();
  575. MENU_ITEM(back, MSG_SETTINGS, lcd_settings_menu);
  576. MENU_ITEM(submenu, MSG_MOVE_X, lcd_move_x);
  577. MENU_ITEM(submenu, MSG_MOVE_Y, lcd_move_y);
  578. if (move_menu_scale < 10.0)
  579. {
  580. MENU_ITEM(submenu, MSG_MOVE_Z, lcd_move_z);
  581. MENU_ITEM(submenu, MSG_MOVE_E, lcd_move_e);
  582. }
  583. END_MENU();
  584. }
  585. static void lcd_move_menu_1mm()
  586. {
  587. move_menu_scale = 1.0;
  588. lcd_move_menu_axis();
  589. }
  590. static void lcd_settings_menu()
  591. {
  592. START_MENU();
  593. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  594. MENU_ITEM(submenu, MSG_TEMPERATURE, lcd_control_temperature_menu);
  595. MENU_ITEM(submenu, MSG_MOVE_AXIS, lcd_move_menu_1mm);
  596. MENU_ITEM(gcode, MSG_HOMEYZ, PSTR("G28 Z"));
  597. MENU_ITEM(gcode, MSG_DISABLE_STEPPERS, PSTR("M84"));
  598. MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28"));
  599. END_MENU();
  600. }
  601. static void lcd_main_menu()
  602. {
  603. SDscrool = 0;
  604. START_MENU();
  605. // Majkl superawesome menu
  606. MENU_ITEM(back, MSG_WATCH, lcd_status_screen);
  607. if (movesplanned() || IS_SD_PRINTING)
  608. {
  609. MENU_ITEM(submenu, MSG_TUNE, lcd_tune_menu);
  610. }else{
  611. MENU_ITEM(submenu, MSG_PREHEAT, lcd_preheat_menu);
  612. }
  613. #ifdef SDSUPPORT
  614. if (card.cardOK)
  615. {
  616. if (card.isFileOpen())
  617. {
  618. if (card.sdprinting)
  619. MENU_ITEM(function, MSG_PAUSE_PRINT, lcd_sdcard_pause);
  620. else
  621. MENU_ITEM(function, MSG_RESUME_PRINT, lcd_sdcard_resume);
  622. MENU_ITEM(function, MSG_STOP_PRINT, lcd_sdcard_stop);
  623. }else{
  624. MENU_ITEM(submenu, MSG_CARD_MENU, lcd_sdcard_menu);
  625. #if SDCARDDETECT < 1
  626. MENU_ITEM(gcode, MSG_CNG_SDCARD, PSTR("M21")); // SD-card changed by user
  627. #endif
  628. }
  629. }else{
  630. MENU_ITEM(submenu, MSG_NO_CARD, lcd_sdcard_menu);
  631. #if SDCARDDETECT < 1
  632. MENU_ITEM(gcode, MSG_INIT_SDCARD, PSTR("M21")); // Manually initialize the SD-card via user interface
  633. #endif
  634. }
  635. #endif
  636. if (IS_SD_PRINTING)
  637. {
  638. }else{
  639. MENU_ITEM(function, MSG_LOAD_FILAMENT, lcd_LoadFilament);
  640. MENU_ITEM(function, MSG_UNLOAD_FILAMENT, lcd_unLoadFilament);
  641. MENU_ITEM(submenu, MSG_SETTINGS, lcd_settings_menu);
  642. }
  643. MENU_ITEM(submenu, MSG_SUPPORT, lcd_support_menu);
  644. END_MENU();
  645. }
  646. #ifdef SDSUPPORT
  647. static void lcd_autostart_sd()
  648. {
  649. card.lastnr=0;
  650. card.setroot();
  651. card.checkautostart(true);
  652. }
  653. #endif
  654. static void lcd_tune_menu()
  655. {
  656. START_MENU();
  657. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  658. MENU_ITEM_EDIT(int3, MSG_SPEED, &feedmultiply, 10, 999);
  659. #if TEMP_SENSOR_0 != 0
  660. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 3);
  661. #endif
  662. #if TEMP_SENSOR_1 != 0
  663. MENU_ITEM_EDIT(int3, MSG_NOZZLE1, &target_temperature[1], 0, HEATER_1_MAXTEMP - 3);
  664. #endif
  665. #if TEMP_SENSOR_2 != 0
  666. MENU_ITEM_EDIT(int3, MSG_NOZZLE2, &target_temperature[2], 0, HEATER_2_MAXTEMP - 3);
  667. #endif
  668. #if TEMP_SENSOR_BED != 0
  669. MENU_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 3);
  670. #endif
  671. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &fanSpeed, 0, 255);
  672. MENU_ITEM_EDIT(int3, MSG_FLOW, &extrudemultiply, 10, 999);
  673. #ifdef FILAMENTCHANGEENABLE
  674. MENU_ITEM(gcode, MSG_FILAMENTCHANGE, PSTR("M600"));
  675. #endif
  676. END_MENU();
  677. }
  678. static void lcd_move_menu_01mm()
  679. {
  680. move_menu_scale = 0.1;
  681. lcd_move_menu_axis();
  682. }
  683. static void lcd_control_temperature_menu()
  684. {
  685. #ifdef PIDTEMP
  686. // set up temp variables - undo the default scaling
  687. raw_Ki = unscalePID_i(Ki);
  688. raw_Kd = unscalePID_d(Kd);
  689. #endif
  690. START_MENU();
  691. MENU_ITEM(back, MSG_SETTINGS, lcd_settings_menu);
  692. //MENU_ITEM(back, MSG_CONTROL, lcd_control_menu);
  693. #if TEMP_SENSOR_0 != 0
  694. MENU_ITEM_EDIT(int3, MSG_NOZZLE, &target_temperature[0], 0, HEATER_0_MAXTEMP - 3);
  695. #endif
  696. #if TEMP_SENSOR_1 != 0
  697. MENU_ITEM_EDIT(int3, MSG_NOZZLE1, &target_temperature[1], 0, HEATER_1_MAXTEMP - 3);
  698. #endif
  699. #if TEMP_SENSOR_2 != 0
  700. MENU_ITEM_EDIT(int3, MSG_NOZZLE2, &target_temperature[2], 0, HEATER_2_MAXTEMP - 3);
  701. #endif
  702. #if TEMP_SENSOR_BED != 0
  703. MENU_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 3);
  704. #endif
  705. MENU_ITEM_EDIT(int3, MSG_FAN_SPEED, &fanSpeed, 0, 255);
  706. #if defined AUTOTEMP && (TEMP_SENSOR_0 != 0)
  707. MENU_ITEM_EDIT(bool, MSG_AUTOTEMP, &autotemp_enabled);
  708. MENU_ITEM_EDIT(float3, MSG_MIN, &autotemp_min, 0, HEATER_0_MAXTEMP - 3);
  709. MENU_ITEM_EDIT(float3, MSG_MAX, &autotemp_max, 0, HEATER_0_MAXTEMP - 3);
  710. MENU_ITEM_EDIT(float32, MSG_FACTOR, &autotemp_factor, 0.0, 1.0);
  711. #endif
  712. END_MENU();
  713. }
  714. #if SDCARDDETECT == -1
  715. static void lcd_sd_refresh()
  716. {
  717. card.initsd();
  718. currentMenuViewOffset = 0;
  719. }
  720. #endif
  721. static void lcd_sd_updir()
  722. {
  723. SDscrool = 0;
  724. card.updir();
  725. currentMenuViewOffset = 0;
  726. }
  727. void lcd_sdcard_menu()
  728. {
  729. int tempScrool = 0;
  730. if (lcdDrawUpdate == 0 && LCD_CLICKED == 0)
  731. //delay(100);
  732. return; // nothing to do (so don't thrash the SD card)
  733. uint16_t fileCnt = card.getnrfilenames();
  734. START_MENU();
  735. MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
  736. card.getWorkDirName();
  737. if(card.filename[0]=='/')
  738. {
  739. #if SDCARDDETECT == -1
  740. MENU_ITEM(function, LCD_STR_REFRESH MSG_REFRESH, lcd_sd_refresh);
  741. #endif
  742. }else{
  743. MENU_ITEM(function, LCD_STR_FOLDER "..", lcd_sd_updir);
  744. }
  745. for(uint16_t i=0;i<fileCnt;i++)
  746. {
  747. if (_menuItemNr == _lineNr)
  748. {
  749. #ifndef SDCARD_RATHERRECENTFIRST
  750. card.getfilename(i);
  751. #else
  752. card.getfilename(fileCnt-1-i);
  753. #endif
  754. if (card.filenameIsDir)
  755. {
  756. MENU_ITEM(sddirectory, MSG_CARD_MENU, card.filename, card.longFilename);
  757. }else{
  758. MENU_ITEM(sdfile, MSG_CARD_MENU, card.filename, card.longFilename);
  759. }
  760. }else{
  761. MENU_ITEM_DUMMY();
  762. }
  763. }
  764. END_MENU();
  765. }
  766. #define menu_edit_type(_type, _name, _strFunc, scale) \
  767. void menu_edit_ ## _name () \
  768. { \
  769. if ((int32_t)encoderPosition < 0) encoderPosition = 0; \
  770. if ((int32_t)encoderPosition > maxEditValue) encoderPosition = maxEditValue; \
  771. if (lcdDrawUpdate) \
  772. lcd_implementation_drawedit(editLabel, _strFunc(((_type)((int32_t)encoderPosition + minEditValue)) / scale)); \
  773. if (LCD_CLICKED) \
  774. { \
  775. *((_type*)editValue) = ((_type)((int32_t)encoderPosition + minEditValue)) / scale; \
  776. lcd_goto_menu(prevMenu, prevEncoderPosition); \
  777. } \
  778. } \
  779. void menu_edit_callback_ ## _name () { \
  780. menu_edit_ ## _name (); \
  781. if (LCD_CLICKED) (*callbackFunc)(); \
  782. } \
  783. static void menu_action_setting_edit_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue) \
  784. { \
  785. prevMenu = currentMenu; \
  786. prevEncoderPosition = encoderPosition; \
  787. \
  788. lcdDrawUpdate = 2; \
  789. currentMenu = menu_edit_ ## _name; \
  790. \
  791. editLabel = pstr; \
  792. editValue = ptr; \
  793. minEditValue = minValue * scale; \
  794. maxEditValue = maxValue * scale - minEditValue; \
  795. encoderPosition = (*ptr) * scale - minEditValue; \
  796. }\
  797. static void menu_action_setting_edit_callback_ ## _name (const char* pstr, _type* ptr, _type minValue, _type maxValue, menuFunc_t callback) \
  798. { \
  799. prevMenu = currentMenu; \
  800. prevEncoderPosition = encoderPosition; \
  801. \
  802. lcdDrawUpdate = 2; \
  803. currentMenu = menu_edit_callback_ ## _name; \
  804. \
  805. editLabel = pstr; \
  806. editValue = ptr; \
  807. minEditValue = minValue * scale; \
  808. maxEditValue = maxValue * scale - minEditValue; \
  809. encoderPosition = (*ptr) * scale - minEditValue; \
  810. callbackFunc = callback;\
  811. }
  812. menu_edit_type(int, int3, itostr3, 1)
  813. menu_edit_type(float, float3, ftostr3, 1)
  814. menu_edit_type(float, float32, ftostr32, 100)
  815. menu_edit_type(float, float43, ftostr43, 1000)
  816. menu_edit_type(float, float5, ftostr5, 0.01)
  817. menu_edit_type(float, float51, ftostr51, 10)
  818. menu_edit_type(float, float52, ftostr52, 100)
  819. menu_edit_type(unsigned long, long5, ftostr5, 0.01)
  820. /** End of menus **/
  821. static void lcd_quick_feedback()
  822. {
  823. lcdDrawUpdate = 2;
  824. blocking_enc = millis() + 500;
  825. lcd_implementation_quick_feedback();
  826. }
  827. /** Menu action functions **/
  828. static void menu_action_back(menuFunc_t data) { lcd_goto_menu(data); }
  829. static void menu_action_submenu(menuFunc_t data) { lcd_goto_menu(data); }
  830. static void menu_action_gcode(const char* pgcode) { enquecommand_P(pgcode); }
  831. static void menu_action_function(menuFunc_t data) { (*data)(); }
  832. static void menu_action_sdfile(const char* filename, char* longFilename)
  833. {
  834. char cmd[30];
  835. char* c;
  836. sprintf_P(cmd, PSTR("M23 %s"), filename);
  837. for(c = &cmd[4]; *c; c++)
  838. *c = tolower(*c);
  839. enquecommand(cmd);
  840. enquecommand_P(PSTR("M24"));
  841. lcd_return_to_status();
  842. }
  843. static void menu_action_sddirectory(const char* filename, char* longFilename)
  844. {
  845. card.chdir(filename);
  846. encoderPosition = 0;
  847. }
  848. static void menu_action_setting_edit_bool(const char* pstr, bool* ptr)
  849. {
  850. *ptr = !(*ptr);
  851. }
  852. static void menu_action_setting_edit_callback_bool(const char* pstr, bool* ptr, menuFunc_t callback)
  853. {
  854. menu_action_setting_edit_bool(pstr, ptr);
  855. (*callback)();
  856. }
  857. #endif//ULTIPANEL
  858. /** LCD API **/
  859. void lcd_init()
  860. {
  861. lcd_implementation_init();
  862. #ifdef NEWPANEL
  863. SET_INPUT(BTN_EN1);
  864. SET_INPUT(BTN_EN2);
  865. WRITE(BTN_EN1,HIGH);
  866. WRITE(BTN_EN2,HIGH);
  867. #if BTN_ENC > 0
  868. SET_INPUT(BTN_ENC);
  869. WRITE(BTN_ENC,HIGH);
  870. #endif
  871. #ifdef REPRAPWORLD_KEYPAD
  872. pinMode(SHIFT_CLK,OUTPUT);
  873. pinMode(SHIFT_LD,OUTPUT);
  874. pinMode(SHIFT_OUT,INPUT);
  875. WRITE(SHIFT_OUT,HIGH);
  876. WRITE(SHIFT_LD,HIGH);
  877. #endif
  878. #else // Not NEWPANEL
  879. #ifdef SR_LCD_2W_NL // Non latching 2 wire shift register
  880. pinMode (SR_DATA_PIN, OUTPUT);
  881. pinMode (SR_CLK_PIN, OUTPUT);
  882. #elif defined(SHIFT_CLK)
  883. pinMode(SHIFT_CLK,OUTPUT);
  884. pinMode(SHIFT_LD,OUTPUT);
  885. pinMode(SHIFT_EN,OUTPUT);
  886. pinMode(SHIFT_OUT,INPUT);
  887. WRITE(SHIFT_OUT,HIGH);
  888. WRITE(SHIFT_LD,HIGH);
  889. WRITE(SHIFT_EN,LOW);
  890. #else
  891. #ifdef ULTIPANEL
  892. #error ULTIPANEL requires an encoder
  893. #endif
  894. #endif // SR_LCD_2W_NL
  895. #endif//!NEWPANEL
  896. #if defined (SDSUPPORT) && defined(SDCARDDETECT) && (SDCARDDETECT > 0)
  897. pinMode(SDCARDDETECT,INPUT);
  898. WRITE(SDCARDDETECT, HIGH);
  899. lcd_oldcardstatus = IS_SD_INSERTED;
  900. #endif//(SDCARDDETECT > 0)
  901. #ifdef LCD_HAS_SLOW_BUTTONS
  902. slow_buttons = 0;
  903. #endif
  904. lcd_buttons_update();
  905. #ifdef ULTIPANEL
  906. encoderDiff = 0;
  907. #endif
  908. }
  909. void lcd_update()
  910. {
  911. static unsigned long timeoutToStatus = 0;
  912. #ifdef LCD_HAS_SLOW_BUTTONS
  913. slow_buttons = lcd_implementation_read_slow_buttons(); // buttons which take too long to read in interrupt context
  914. #endif
  915. lcd_buttons_update();
  916. #if (SDCARDDETECT > 0)
  917. if((IS_SD_INSERTED != lcd_oldcardstatus && lcd_detected()))
  918. {
  919. lcdDrawUpdate = 2;
  920. lcd_oldcardstatus = IS_SD_INSERTED;
  921. lcd_implementation_init( // to maybe revive the LCD if static electricity killed it.
  922. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  923. currentMenu == lcd_status_screen
  924. #endif
  925. );
  926. if(lcd_oldcardstatus)
  927. {
  928. card.initsd();
  929. LCD_MESSAGEPGM(MSG_SD_INSERTED);
  930. }
  931. else
  932. {
  933. card.release();
  934. LCD_MESSAGEPGM(MSG_SD_REMOVED);
  935. }
  936. }
  937. #endif//CARDINSERTED
  938. if (lcd_next_update_millis < millis())
  939. {
  940. #ifdef ULTIPANEL
  941. #ifdef REPRAPWORLD_KEYPAD
  942. if (REPRAPWORLD_KEYPAD_MOVE_Z_UP) {
  943. reprapworld_keypad_move_z_up();
  944. }
  945. if (REPRAPWORLD_KEYPAD_MOVE_Z_DOWN) {
  946. reprapworld_keypad_move_z_down();
  947. }
  948. if (REPRAPWORLD_KEYPAD_MOVE_X_LEFT) {
  949. reprapworld_keypad_move_x_left();
  950. }
  951. if (REPRAPWORLD_KEYPAD_MOVE_X_RIGHT) {
  952. reprapworld_keypad_move_x_right();
  953. }
  954. if (REPRAPWORLD_KEYPAD_MOVE_Y_DOWN) {
  955. reprapworld_keypad_move_y_down();
  956. }
  957. if (REPRAPWORLD_KEYPAD_MOVE_Y_UP) {
  958. reprapworld_keypad_move_y_up();
  959. }
  960. if (REPRAPWORLD_KEYPAD_MOVE_HOME) {
  961. reprapworld_keypad_move_home();
  962. }
  963. #endif
  964. if (abs(encoderDiff) >= ENCODER_PULSES_PER_STEP)
  965. {
  966. lcdDrawUpdate = 1;
  967. encoderPosition += encoderDiff / ENCODER_PULSES_PER_STEP;
  968. encoderDiff = 0;
  969. timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  970. }
  971. if (LCD_CLICKED)
  972. timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
  973. #endif//ULTIPANEL
  974. #ifdef DOGLCD // Changes due to different driver architecture of the DOGM display
  975. blink++; // Variable for fan animation and alive dot
  976. u8g.firstPage();
  977. do
  978. {
  979. u8g.setFont(u8g_font_6x10_marlin);
  980. u8g.setPrintPos(125,0);
  981. if (blink % 2) u8g.setColorIndex(1); else u8g.setColorIndex(0); // Set color for the alive dot
  982. u8g.drawPixel(127,63); // draw alive dot
  983. u8g.setColorIndex(1); // black on white
  984. (*currentMenu)();
  985. if (!lcdDrawUpdate) break; // Terminate display update, when nothing new to draw. This must be done before the last dogm.next()
  986. } while( u8g.nextPage() );
  987. #else
  988. (*currentMenu)();
  989. #endif
  990. #ifdef LCD_HAS_STATUS_INDICATORS
  991. lcd_implementation_update_indicators();
  992. #endif
  993. #ifdef ULTIPANEL
  994. if(timeoutToStatus < millis() && currentMenu != lcd_status_screen)
  995. {
  996. lcd_return_to_status();
  997. lcdDrawUpdate = 2;
  998. }
  999. #endif//ULTIPANEL
  1000. if (lcdDrawUpdate == 2) lcd_implementation_clear();
  1001. if (lcdDrawUpdate) lcdDrawUpdate--;
  1002. lcd_next_update_millis = millis() + LCD_UPDATE_INTERVAL;
  1003. }
  1004. }
  1005. void lcd_ignore_click(bool b)
  1006. {
  1007. ignore_click = b;
  1008. wait_for_unclick = false;
  1009. }
  1010. void lcd_finishstatus() {
  1011. int len = strlen(lcd_status_message);
  1012. if (len > 0) {
  1013. while (len < LCD_WIDTH) {
  1014. lcd_status_message[len++] = ' ';
  1015. }
  1016. }
  1017. lcd_status_message[LCD_WIDTH] = '\0';
  1018. #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
  1019. #if PROGRESS_MSG_EXPIRE > 0
  1020. messageTick =
  1021. #endif
  1022. progressBarTick = millis();
  1023. #endif
  1024. lcdDrawUpdate = 2;
  1025. #ifdef FILAMENT_LCD_DISPLAY
  1026. message_millis = millis(); //get status message to show up for a while
  1027. #endif
  1028. }
  1029. void lcd_setstatus(const char* message)
  1030. {
  1031. if (lcd_status_message_level > 0)
  1032. return;
  1033. strncpy(lcd_status_message, message, LCD_WIDTH);
  1034. lcd_finishstatus();
  1035. }
  1036. void lcd_setstatuspgm(const char* message)
  1037. {
  1038. if (lcd_status_message_level > 0)
  1039. return;
  1040. strncpy_P(lcd_status_message, message, LCD_WIDTH);
  1041. lcd_finishstatus();
  1042. }
  1043. void lcd_setalertstatuspgm(const char* message)
  1044. {
  1045. lcd_setstatuspgm(message);
  1046. lcd_status_message_level = 1;
  1047. #ifdef ULTIPANEL
  1048. lcd_return_to_status();
  1049. #endif//ULTIPANEL
  1050. }
  1051. void lcd_reset_alert_level()
  1052. {
  1053. lcd_status_message_level = 0;
  1054. }
  1055. #ifdef DOGLCD
  1056. void lcd_setcontrast(uint8_t value)
  1057. {
  1058. lcd_contrast = value & 63;
  1059. u8g.setContrast(lcd_contrast);
  1060. }
  1061. #endif
  1062. #ifdef ULTIPANEL
  1063. /* Warning: This function is called from interrupt context */
  1064. void lcd_buttons_update()
  1065. {
  1066. #ifdef NEWPANEL
  1067. uint8_t newbutton=0;
  1068. if(READ(BTN_EN1)==0) newbutton|=EN_A;
  1069. if(READ(BTN_EN2)==0) newbutton|=EN_B;
  1070. #if BTN_ENC > 0
  1071. if((blocking_enc<millis()) && (READ(BTN_ENC)==0))
  1072. newbutton |= EN_C;
  1073. #endif
  1074. buttons = newbutton;
  1075. #ifdef LCD_HAS_SLOW_BUTTONS
  1076. buttons |= slow_buttons;
  1077. #endif
  1078. #ifdef REPRAPWORLD_KEYPAD
  1079. // for the reprapworld_keypad
  1080. uint8_t newbutton_reprapworld_keypad=0;
  1081. WRITE(SHIFT_LD,LOW);
  1082. WRITE(SHIFT_LD,HIGH);
  1083. for(int8_t i=0;i<8;i++) {
  1084. newbutton_reprapworld_keypad = newbutton_reprapworld_keypad>>1;
  1085. if(READ(SHIFT_OUT))
  1086. newbutton_reprapworld_keypad|=(1<<7);
  1087. WRITE(SHIFT_CLK,HIGH);
  1088. WRITE(SHIFT_CLK,LOW);
  1089. }
  1090. buttons_reprapworld_keypad=~newbutton_reprapworld_keypad; //invert it, because a pressed switch produces a logical 0
  1091. #endif
  1092. #else //read it from the shift register
  1093. uint8_t newbutton=0;
  1094. WRITE(SHIFT_LD,LOW);
  1095. WRITE(SHIFT_LD,HIGH);
  1096. unsigned char tmp_buttons=0;
  1097. for(int8_t i=0;i<8;i++)
  1098. {
  1099. newbutton = newbutton>>1;
  1100. if(READ(SHIFT_OUT))
  1101. newbutton|=(1<<7);
  1102. WRITE(SHIFT_CLK,HIGH);
  1103. WRITE(SHIFT_CLK,LOW);
  1104. }
  1105. buttons=~newbutton; //invert it, because a pressed switch produces a logical 0
  1106. #endif//!NEWPANEL
  1107. //manage encoder rotation
  1108. uint8_t enc=0;
  1109. if (buttons & EN_A) enc |= B01;
  1110. if (buttons & EN_B) enc |= B10;
  1111. if(enc != lastEncoderBits)
  1112. {
  1113. switch(enc)
  1114. {
  1115. case encrot0:
  1116. if(lastEncoderBits==encrot3)
  1117. encoderDiff++;
  1118. else if(lastEncoderBits==encrot1)
  1119. encoderDiff--;
  1120. break;
  1121. case encrot1:
  1122. if(lastEncoderBits==encrot0)
  1123. encoderDiff++;
  1124. else if(lastEncoderBits==encrot2)
  1125. encoderDiff--;
  1126. break;
  1127. case encrot2:
  1128. if(lastEncoderBits==encrot1)
  1129. encoderDiff++;
  1130. else if(lastEncoderBits==encrot3)
  1131. encoderDiff--;
  1132. break;
  1133. case encrot3:
  1134. if(lastEncoderBits==encrot2)
  1135. encoderDiff++;
  1136. else if(lastEncoderBits==encrot0)
  1137. encoderDiff--;
  1138. break;
  1139. }
  1140. }
  1141. lastEncoderBits = enc;
  1142. }
  1143. bool lcd_detected(void)
  1144. {
  1145. #if (defined(LCD_I2C_TYPE_MCP23017) || defined(LCD_I2C_TYPE_MCP23008)) && defined(DETECT_DEVICE)
  1146. return lcd.LcdDetected() == 1;
  1147. #else
  1148. return true;
  1149. #endif
  1150. }
  1151. void lcd_buzz(long duration, uint16_t freq)
  1152. {
  1153. #ifdef LCD_USE_I2C_BUZZER
  1154. lcd.buzz(duration,freq);
  1155. #endif
  1156. }
  1157. bool lcd_clicked()
  1158. {
  1159. return LCD_CLICKED;
  1160. }
  1161. #endif//ULTIPANEL
  1162. /********************************/
  1163. /** Float conversion utilities **/
  1164. /********************************/
  1165. // convert float to string with +123.4 format
  1166. char conv[8];
  1167. char *ftostr3(const float &x)
  1168. {
  1169. return itostr3((int)x);
  1170. }
  1171. char *itostr2(const uint8_t &x)
  1172. {
  1173. //sprintf(conv,"%5.1f",x);
  1174. int xx=x;
  1175. conv[0]=(xx/10)%10+'0';
  1176. conv[1]=(xx)%10+'0';
  1177. conv[2]=0;
  1178. return conv;
  1179. }
  1180. // Convert float to string with 123.4 format, dropping sign
  1181. char *ftostr31(const float &x)
  1182. {
  1183. int xx=x*10;
  1184. conv[0]=(xx>=0)?'+':'-';
  1185. xx=abs(xx);
  1186. conv[1]=(xx/1000)%10+'0';
  1187. conv[2]=(xx/100)%10+'0';
  1188. conv[3]=(xx/10)%10+'0';
  1189. conv[4]='.';
  1190. conv[5]=(xx)%10+'0';
  1191. conv[6]=0;
  1192. return conv;
  1193. }
  1194. // Convert float to string with 123.4 format
  1195. char *ftostr31ns(const float &x)
  1196. {
  1197. int xx=x*10;
  1198. //conv[0]=(xx>=0)?'+':'-';
  1199. xx=abs(xx);
  1200. conv[0]=(xx/1000)%10+'0';
  1201. conv[1]=(xx/100)%10+'0';
  1202. conv[2]=(xx/10)%10+'0';
  1203. conv[3]='.';
  1204. conv[4]=(xx)%10+'0';
  1205. conv[5]=0;
  1206. return conv;
  1207. }
  1208. char *ftostr32(const float &x)
  1209. {
  1210. long xx=x*100;
  1211. if (xx >= 0)
  1212. conv[0]=(xx/10000)%10+'0';
  1213. else
  1214. conv[0]='-';
  1215. xx=abs(xx);
  1216. conv[1]=(xx/1000)%10+'0';
  1217. conv[2]=(xx/100)%10+'0';
  1218. conv[3]='.';
  1219. conv[4]=(xx/10)%10+'0';
  1220. conv[5]=(xx)%10+'0';
  1221. conv[6]=0;
  1222. return conv;
  1223. }
  1224. // Convert float to string with 1.234 format
  1225. char *ftostr43(const float &x)
  1226. {
  1227. long xx = x * 1000;
  1228. if (xx >= 0)
  1229. conv[0] = (xx / 1000) % 10 + '0';
  1230. else
  1231. conv[0] = '-';
  1232. xx = abs(xx);
  1233. conv[1] = '.';
  1234. conv[2] = (xx / 100) % 10 + '0';
  1235. conv[3] = (xx / 10) % 10 + '0';
  1236. conv[4] = (xx) % 10 + '0';
  1237. conv[5] = 0;
  1238. return conv;
  1239. }
  1240. //Float to string with 1.23 format
  1241. char *ftostr12ns(const float &x)
  1242. {
  1243. long xx=x*100;
  1244. xx=abs(xx);
  1245. conv[0]=(xx/100)%10+'0';
  1246. conv[1]='.';
  1247. conv[2]=(xx/10)%10+'0';
  1248. conv[3]=(xx)%10+'0';
  1249. conv[4]=0;
  1250. return conv;
  1251. }
  1252. // convert float to space-padded string with -_23.4_ format
  1253. char *ftostr32sp(const float &x) {
  1254. long xx = abs(x * 100);
  1255. uint8_t dig;
  1256. if (x < 0) { // negative val = -_0
  1257. conv[0] = '-';
  1258. dig = (xx / 1000) % 10;
  1259. conv[1] = dig ? '0' + dig : ' ';
  1260. }
  1261. else { // positive val = __0
  1262. dig = (xx / 10000) % 10;
  1263. if (dig) {
  1264. conv[0] = '0' + dig;
  1265. conv[1] = '0' + (xx / 1000) % 10;
  1266. }
  1267. else {
  1268. conv[0] = ' ';
  1269. dig = (xx / 1000) % 10;
  1270. conv[1] = dig ? '0' + dig : ' ';
  1271. }
  1272. }
  1273. conv[2] = '0' + (xx / 100) % 10; // lsd always
  1274. dig = xx % 10;
  1275. if (dig) { // 2 decimal places
  1276. conv[5] = '0' + dig;
  1277. conv[4] = '0' + (xx / 10) % 10;
  1278. conv[3] = '.';
  1279. }
  1280. else { // 1 or 0 decimal place
  1281. dig = (xx / 10) % 10;
  1282. if (dig) {
  1283. conv[4] = '0' + dig;
  1284. conv[3] = '.';
  1285. }
  1286. else {
  1287. conv[3] = conv[4] = ' ';
  1288. }
  1289. conv[5] = ' ';
  1290. }
  1291. conv[6] = '\0';
  1292. return conv;
  1293. }
  1294. char *itostr31(const int &xx)
  1295. {
  1296. conv[0]=(xx>=0)?'+':'-';
  1297. conv[1]=(xx/1000)%10+'0';
  1298. conv[2]=(xx/100)%10+'0';
  1299. conv[3]=(xx/10)%10+'0';
  1300. conv[4]='.';
  1301. conv[5]=(xx)%10+'0';
  1302. conv[6]=0;
  1303. return conv;
  1304. }
  1305. // Convert int to rj string with 123 or -12 format
  1306. char *itostr3(const int &x)
  1307. {
  1308. int xx = x;
  1309. if (xx < 0) {
  1310. conv[0]='-';
  1311. xx = -xx;
  1312. } else if (xx >= 100)
  1313. conv[0]=(xx/100)%10+'0';
  1314. else
  1315. conv[0]=' ';
  1316. if (xx >= 10)
  1317. conv[1]=(xx/10)%10+'0';
  1318. else
  1319. conv[1]=' ';
  1320. conv[2]=(xx)%10+'0';
  1321. conv[3]=0;
  1322. return conv;
  1323. }
  1324. // Convert int to lj string with 123 format
  1325. char *itostr3left(const int &xx)
  1326. {
  1327. if (xx >= 100)
  1328. {
  1329. conv[0]=(xx/100)%10+'0';
  1330. conv[1]=(xx/10)%10+'0';
  1331. conv[2]=(xx)%10+'0';
  1332. conv[3]=0;
  1333. }
  1334. else if (xx >= 10)
  1335. {
  1336. conv[0]=(xx/10)%10+'0';
  1337. conv[1]=(xx)%10+'0';
  1338. conv[2]=0;
  1339. }
  1340. else
  1341. {
  1342. conv[0]=(xx)%10+'0';
  1343. conv[1]=0;
  1344. }
  1345. return conv;
  1346. }
  1347. // Convert int to rj string with 1234 format
  1348. char *itostr4(const int &xx) {
  1349. conv[0] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  1350. conv[1] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  1351. conv[2] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  1352. conv[3] = xx % 10 + '0';
  1353. conv[4] = 0;
  1354. return conv;
  1355. }
  1356. // Convert float to rj string with 12345 format
  1357. char *ftostr5(const float &x) {
  1358. long xx = abs(x);
  1359. conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
  1360. conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  1361. conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  1362. conv[3] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  1363. conv[4] = xx % 10 + '0';
  1364. conv[5] = 0;
  1365. return conv;
  1366. }
  1367. // Convert float to string with +1234.5 format
  1368. char *ftostr51(const float &x)
  1369. {
  1370. long xx=x*10;
  1371. conv[0]=(xx>=0)?'+':'-';
  1372. xx=abs(xx);
  1373. conv[1]=(xx/10000)%10+'0';
  1374. conv[2]=(xx/1000)%10+'0';
  1375. conv[3]=(xx/100)%10+'0';
  1376. conv[4]=(xx/10)%10+'0';
  1377. conv[5]='.';
  1378. conv[6]=(xx)%10+'0';
  1379. conv[7]=0;
  1380. return conv;
  1381. }
  1382. // Convert float to string with +123.45 format
  1383. char *ftostr52(const float &x)
  1384. {
  1385. long xx=x*100;
  1386. conv[0]=(xx>=0)?'+':'-';
  1387. xx=abs(xx);
  1388. conv[1]=(xx/10000)%10+'0';
  1389. conv[2]=(xx/1000)%10+'0';
  1390. conv[3]=(xx/100)%10+'0';
  1391. conv[4]='.';
  1392. conv[5]=(xx/10)%10+'0';
  1393. conv[6]=(xx)%10+'0';
  1394. conv[7]=0;
  1395. return conv;
  1396. }
  1397. // Callback for after editing PID i value
  1398. // grab the PID i value out of the temp variable; scale it; then update the PID driver
  1399. void copy_and_scalePID_i()
  1400. {
  1401. #ifdef PIDTEMP
  1402. Ki = scalePID_i(raw_Ki);
  1403. updatePID();
  1404. #endif
  1405. }
  1406. // Callback for after editing PID d value
  1407. // grab the PID d value out of the temp variable; scale it; then update the PID driver
  1408. void copy_and_scalePID_d()
  1409. {
  1410. #ifdef PIDTEMP
  1411. Kd = scalePID_d(raw_Kd);
  1412. updatePID();
  1413. #endif
  1414. }
  1415. #endif //ULTRA_LCD