My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ultralcd.cpp 41KB

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