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

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