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 38KB

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