My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ultralcd.cpp 41KB

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