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.

extui_malyan_lcd.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * extui_malyan_lcd.cpp
  24. *
  25. * LCD implementation for Malyan's LCD, a separate ESP8266 MCU running
  26. * on Serial1 for the M200 board. This module outputs a pseudo-gcode
  27. * wrapped in curly braces which the LCD implementation translates into
  28. * actual G-code commands.
  29. *
  30. * Added to Marlin for Mini/Malyan M200
  31. * Unknown commands as of Jan 2018: {H:}
  32. * Not currently implemented:
  33. * {E:} when sent by LCD. Meaning unknown.
  34. *
  35. * Notes for connecting to boards that are not Malyan:
  36. * The LCD is 3.3v, so if powering from a RAMPS 1.4 board or
  37. * other 5v/12v board, use a buck converter to power the LCD and
  38. * the 3.3v side of a logic level shifter. Aux1 on the RAMPS board
  39. * has Serial1 and 12v, making it perfect for this.
  40. * Copyright (c) 2017 Jason Nelson (xC0000005)
  41. */
  42. #include "../inc/MarlinConfigPre.h"
  43. #if ENABLED(MALYAN_LCD)
  44. #define DEBUG_MALYAN_LCD
  45. #include "extui/ui_api.h"
  46. #include "ultralcd.h"
  47. #include "../sd/cardreader.h"
  48. #include "../module/temperature.h"
  49. #include "../module/stepper.h"
  50. #include "../module/motion.h"
  51. #include "../libs/duration_t.h"
  52. #include "../module/printcounter.h"
  53. #include "../gcode/queue.h"
  54. #define DEBUG_OUT ENABLED(DEBUG_MALYAN_LCD)
  55. #include "../core/debug_out.h"
  56. // On the Malyan M200, this will be Serial1. On a RAMPS board,
  57. // it might not be.
  58. #define LCD_SERIAL Serial1
  59. // This is based on longest sys command + a filename, plus some buffer
  60. // in case we encounter some data we don't recognize
  61. // There is no evidence a line will ever be this long, but better safe than sorry
  62. #define MAX_CURLY_COMMAND (32 + LONG_FILENAME_LENGTH) * 2
  63. // Track incoming command bytes from the LCD
  64. int inbound_count;
  65. // For sending print completion messages
  66. bool last_printing_status = false;
  67. // Everything written needs the high bit set.
  68. void write_to_lcd_P(PGM_P const message) {
  69. char encoded_message[MAX_CURLY_COMMAND];
  70. uint8_t message_length = _MIN(strlen_P(message), sizeof(encoded_message));
  71. LOOP_L_N(i, message_length)
  72. encoded_message[i] = pgm_read_byte(&message[i]) | 0x80;
  73. LCD_SERIAL.Print::write(encoded_message, message_length);
  74. }
  75. void write_to_lcd(const char * const message) {
  76. char encoded_message[MAX_CURLY_COMMAND];
  77. const uint8_t message_length = _MIN(strlen(message), sizeof(encoded_message));
  78. LOOP_L_N(i, message_length)
  79. encoded_message[i] = message[i] | 0x80;
  80. LCD_SERIAL.Print::write(encoded_message, message_length);
  81. }
  82. /**
  83. * Process an LCD 'C' command.
  84. * These are currently all temperature commands
  85. * {C:T0190}
  86. * Set temp for hotend to 190
  87. * {C:P050}
  88. * Set temp for bed to 50
  89. *
  90. * {C:S09} set feedrate to 90 %.
  91. * {C:S12} set feedrate to 120 %.
  92. *
  93. * the command portion begins after the :
  94. */
  95. void process_lcd_c_command(const char* command) {
  96. const int target_val = command[1] ? atoi(command + 1) : -1;
  97. if (target_val < 0) {
  98. DEBUG_ECHOLNPAIR("UNKNOWN C COMMAND ", command);
  99. return;
  100. }
  101. switch (command[0]) {
  102. case 'C': // Cope with both V1 early rev and later LCDs.
  103. case 'S':
  104. feedrate_percentage = target_val * 10;
  105. LIMIT(feedrate_percentage, 10, 999);
  106. break;
  107. case 'T':
  108. // Sometimes the LCD will send commands to turn off both extruder and bed, though
  109. // this should not happen since the printing screen is up. Better safe than sorry.
  110. if (!print_job_timer.isRunning() || target_val > 0)
  111. ExtUI::setTargetTemp_celsius(target_val, ExtUI::extruder_t::E0);
  112. break;
  113. #if HAS_HEATED_BED
  114. case 'P': ExtUI::setTargetTemp_celsius(target_val, ExtUI::heater_t::BED); break;
  115. #endif
  116. default: DEBUG_ECHOLNPAIR("UNKNOWN C COMMAND ", command);
  117. }
  118. }
  119. /**
  120. * Process an LCD 'B' command.
  121. * {B:0} results in: {T0:008/195}{T1:000/000}{TP:000/000}{TQ:000C}{TT:000000}
  122. * T0/T1 are hot end temperatures, TP is bed, TQ is percent, and TT is probably
  123. * time remaining (HH:MM:SS). The UI can't handle displaying a second hotend,
  124. * but the stock firmware always sends it, and it's always zero.
  125. */
  126. void process_lcd_eb_command(const char* command) {
  127. char elapsed_buffer[10];
  128. static uint8_t iteration = 0;
  129. duration_t elapsed;
  130. switch (command[0]) {
  131. case '0': {
  132. elapsed = print_job_timer.duration();
  133. sprintf_P(elapsed_buffer, PSTR("%02u%02u%02u"), uint16_t(elapsed.hour()), uint16_t(elapsed.minute()) % 60, uint16_t(elapsed.second()) % 60);
  134. char message_buffer[MAX_CURLY_COMMAND];
  135. uint8_t done_pct = print_job_timer.isRunning() ? (iteration * 10) : 100;
  136. iteration = (iteration + 1) % 10; // Provide progress animation
  137. #if ENABLED(SDSUPPORT)
  138. if (ExtUI::isPrintingFromMedia() || ExtUI::isPrintingFromMediaPaused())
  139. done_pct = card.percentDone();
  140. #endif
  141. sprintf_P(message_buffer,
  142. PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}{TQ:%03i}{TT:%s}"),
  143. int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0),
  144. #if HAS_HEATED_BED
  145. int(thermalManager.degBed()), thermalManager.degTargetBed(),
  146. #else
  147. 0, 0,
  148. #endif
  149. #if ENABLED(SDSUPPORT)
  150. done_pct,
  151. #else
  152. 0,
  153. #endif
  154. elapsed_buffer
  155. );
  156. write_to_lcd(message_buffer);
  157. } break;
  158. default: DEBUG_ECHOLNPAIR("UNKNOWN E/B COMMAND ", command);
  159. }
  160. }
  161. /**
  162. * Process an LCD 'J' command.
  163. * These are currently all movement commands.
  164. * The command portion begins after the :
  165. * Move X Axis
  166. *
  167. * {J:E}{J:X-200}{J:E}
  168. * {J:E}{J:X+200}{J:E}
  169. * X, Y, Z, A (extruder)
  170. */
  171. void process_lcd_j_command(const char* command) {
  172. auto move_axis = [command](const auto axis) {
  173. const float dist = atof(command + 1) / 10.0;
  174. ExtUI::setAxisPosition_mm(ExtUI::getAxisPosition_mm(axis) + dist, axis);
  175. };
  176. switch (command[0]) {
  177. case 'E': break;
  178. case 'A': move_axis(ExtUI::extruder_t::E0); break;
  179. case 'Y': move_axis(ExtUI::axis_t::Y); break;
  180. case 'Z': move_axis(ExtUI::axis_t::Z); break;
  181. case 'X': move_axis(ExtUI::axis_t::X); break;
  182. default: DEBUG_ECHOLNPAIR("UNKNOWN J COMMAND ", command);
  183. }
  184. }
  185. /**
  186. * Process an LCD 'P' command, related to homing and printing.
  187. * Cancel:
  188. * {P:X}
  189. *
  190. * Home all axes:
  191. * {P:H}
  192. *
  193. * Print a file:
  194. * {P:000}
  195. * The File number is specified as a three digit value.
  196. * Printer responds with:
  197. * {PRINTFILE:Mini_SNES_Bottom.gcode}
  198. * {SYS:BUILD}echo:Now fresh file: Mini_SNES_Bottom.gcode
  199. * File opened: Mini_SNES_Bottom.gcode Size: 5805813
  200. * File selected
  201. * {SYS:BUILD}
  202. * T:-2526.8 E:0
  203. * T:-2533.0 E:0
  204. * T:-2537.4 E:0
  205. * Note only the curly brace stuff matters.
  206. */
  207. void process_lcd_p_command(const char* command) {
  208. switch (command[0]) {
  209. case 'P':
  210. ExtUI::pausePrint();
  211. write_to_lcd_P(PSTR("{SYS:PAUSED}"));
  212. break;
  213. case 'R':
  214. ExtUI::resumePrint();
  215. write_to_lcd_P(PSTR("{SYS:RESUMED}"));
  216. break;
  217. case 'X':
  218. write_to_lcd_P(PSTR("{SYS:CANCELING}"));
  219. ExtUI::stopPrint();
  220. write_to_lcd_P(PSTR("{SYS:STARTED}"));
  221. break;
  222. case 'H': queue.enqueue_now_P(G28_STR); break; // Home all axes
  223. default: {
  224. #if ENABLED(SDSUPPORT)
  225. // Print file 000 - a three digit number indicating which
  226. // file to print in the SD card. If it's a directory,
  227. // then switch to the directory.
  228. // Find the name of the file to print.
  229. // It's needed to echo the PRINTFILE option.
  230. // The {S:L} command should've ensured the SD card was mounted.
  231. card.selectFileByIndex(atoi(command));
  232. // There may be a difference in how V1 and V2 LCDs handle subdirectory
  233. // prints. Investigate more. This matches the V1 motion controller actions
  234. // but the V2 LCD switches to "print" mode on {SYS:DIR} response.
  235. if (card.flag.filenameIsDir) {
  236. card.cd(card.filename);
  237. write_to_lcd_P(PSTR("{SYS:DIR}"));
  238. }
  239. else {
  240. char message_buffer[MAX_CURLY_COMMAND];
  241. sprintf_P(message_buffer, PSTR("{PRINTFILE:%s}"), card.longest_filename());
  242. write_to_lcd(message_buffer);
  243. write_to_lcd_P(PSTR("{SYS:BUILD}"));
  244. card.openAndPrintFile(card.filename);
  245. }
  246. #endif
  247. } break; // default
  248. } // switch
  249. }
  250. /**
  251. * Handle an lcd 'S' command
  252. * {S:I} - Temperature request
  253. * {T0:999/000}{T1:000/000}{TP:004/000}
  254. *
  255. * {S:L} - File Listing request
  256. * Printer Response:
  257. * {FILE:buttons.gcode}
  258. * {FILE:update.bin}
  259. * {FILE:nupdate.bin}
  260. * {FILE:fcupdate.flg}
  261. * {SYS:OK}
  262. */
  263. void process_lcd_s_command(const char* command) {
  264. switch (command[0]) {
  265. case 'I': {
  266. // temperature information
  267. char message_buffer[MAX_CURLY_COMMAND];
  268. sprintf_P(message_buffer, PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}"),
  269. int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0),
  270. #if HAS_HEATED_BED
  271. int(thermalManager.degBed()), thermalManager.degTargetBed()
  272. #else
  273. 0, 0
  274. #endif
  275. );
  276. write_to_lcd(message_buffer);
  277. } break;
  278. case 'L': {
  279. #if ENABLED(SDSUPPORT)
  280. if (!card.isMounted()) card.mount();
  281. // A more efficient way to do this would be to
  282. // implement a callback in the ls_SerialPrint code, but
  283. // that requires changes to the core cardreader class that
  284. // would not benefit the majority of users. Since one can't
  285. // select a file for printing during a print, there's
  286. // little reason not to do it this way.
  287. char message_buffer[MAX_CURLY_COMMAND];
  288. uint16_t file_count = card.get_num_Files();
  289. for (uint16_t i = 0; i < file_count; i++) {
  290. card.selectFileByIndex(i);
  291. sprintf_P(message_buffer, card.flag.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.longest_filename());
  292. write_to_lcd(message_buffer);
  293. }
  294. write_to_lcd_P(PSTR("{SYS:OK}"));
  295. #endif
  296. } break;
  297. default: DEBUG_ECHOLNPAIR("UNKNOWN S COMMAND ", command);
  298. }
  299. }
  300. /**
  301. * Receive a curly brace command and translate to G-code.
  302. * Currently {E:0} is not handled. Its function is unknown,
  303. * but it occurs during the temp window after a sys build.
  304. */
  305. void process_lcd_command(const char* command) {
  306. const char *current = command;
  307. byte command_code = *current++;
  308. if (*current == ':') {
  309. current++; // skip the :
  310. switch (command_code) {
  311. case 'S': process_lcd_s_command(current); break;
  312. case 'J': process_lcd_j_command(current); break;
  313. case 'P': process_lcd_p_command(current); break;
  314. case 'C': process_lcd_c_command(current); break;
  315. case 'B':
  316. case 'E': process_lcd_eb_command(current); break;
  317. default: DEBUG_ECHOLNPAIR("UNKNOWN COMMAND ", command);
  318. }
  319. }
  320. else
  321. DEBUG_ECHOLNPAIR("UNKNOWN COMMAND FORMAT ", command);
  322. }
  323. // Parse LCD commands mixed with G-Code
  324. void parse_lcd_byte(byte b) {
  325. static bool parsing_lcd_cmd = false;
  326. static char inbound_buffer[MAX_CURLY_COMMAND];
  327. if (!parsing_lcd_cmd) {
  328. if (b == '{' || b == '\n' || b == '\r') { // A line-ending or opening brace
  329. parsing_lcd_cmd = b == '{'; // Brace opens an LCD command
  330. if (inbound_count) { // Looks like a G-code is in the buffer
  331. inbound_buffer[inbound_count] = '\0'; // Reset before processing
  332. inbound_count = 0;
  333. queue.enqueue_one_now(inbound_buffer); // Handle the G-code command
  334. }
  335. }
  336. }
  337. else if (b == '}') { // Closing brace on an LCD command
  338. parsing_lcd_cmd = false; // Unflag and...
  339. inbound_buffer[inbound_count] = '\0'; // reset before processing
  340. inbound_count = 0;
  341. process_lcd_command(inbound_buffer); // Handle the LCD command
  342. }
  343. else if (inbound_count < MAX_CURLY_COMMAND - 2)
  344. inbound_buffer[inbound_count++] = b; // Buffer only if space remains
  345. }
  346. /**
  347. * UC means connected.
  348. * UD means disconnected
  349. * The stock firmware considers USB initialized as "connected."
  350. */
  351. void update_usb_status(const bool forceUpdate) {
  352. static bool last_usb_connected_status = false;
  353. // This is mildly different than stock, which
  354. // appears to use the usb discovery status.
  355. // This is more logical.
  356. if (last_usb_connected_status != MYSERIAL0 || forceUpdate) {
  357. last_usb_connected_status = MYSERIAL0;
  358. write_to_lcd_P(last_usb_connected_status ? PSTR("{R:UC}\r\n") : PSTR("{R:UD}\r\n"));
  359. }
  360. }
  361. namespace ExtUI {
  362. void onStartup() {
  363. /**
  364. * The Malyan LCD actually runs as a separate MCU on Serial 1.
  365. * This code's job is to siphon the weird curly-brace commands from
  366. * it and translate into ExtUI operations where possible.
  367. */
  368. inbound_count = 0;
  369. LCD_SERIAL.begin(500000);
  370. // Signal init
  371. write_to_lcd_P(PSTR("{SYS:STARTED}\r\n"));
  372. // send a version that says "unsupported"
  373. write_to_lcd_P(PSTR("{VER:99}\r\n"));
  374. // No idea why it does this twice.
  375. write_to_lcd_P(PSTR("{SYS:STARTED}\r\n"));
  376. update_usb_status(true);
  377. }
  378. void onIdle() {
  379. /**
  380. * - from printer on startup:
  381. * {SYS:STARTED}{VER:29}{SYS:STARTED}{R:UD}
  382. */
  383. // First report USB status.
  384. update_usb_status(false);
  385. // now drain commands...
  386. while (LCD_SERIAL.available()) {
  387. parse_lcd_byte((byte)LCD_SERIAL.read() & 0x7F);
  388. }
  389. #if ENABLED(SDSUPPORT)
  390. // The way last printing status works is simple:
  391. // The UI needs to see at least one TQ which is not 100%
  392. // and then when the print is complete, one which is.
  393. static uint8_t last_percent_done = 100;
  394. // If there was a print in progress, we need to emit the final
  395. // print status as {TQ:100}. Reset last percent done so a new print will
  396. // issue a percent of 0.
  397. const uint8_t percent_done = (ExtUI::isPrinting() || ExtUI::isPrintingFromMediaPaused()) ? ExtUI::getProgress_percent() : last_printing_status ? 100 : 0;
  398. if (percent_done != last_percent_done) {
  399. char message_buffer[16];
  400. sprintf_P(message_buffer, PSTR("{TQ:%03i}"), percent_done);
  401. write_to_lcd(message_buffer);
  402. last_percent_done = percent_done;
  403. last_printing_status = ExtUI::isPrinting();
  404. }
  405. #endif
  406. }
  407. // {E:<msg>} is for error states.
  408. void onPrinterKilled(PGM_P error, PGM_P component) {
  409. write_to_lcd_P(PSTR("{E:"));
  410. write_to_lcd_P(error);
  411. write_to_lcd_P(PSTR(" "));
  412. write_to_lcd_P(component);
  413. write_to_lcd_P("}");
  414. }
  415. void onPrintTimerStarted() { write_to_lcd_P(PSTR("{SYS:BUILD}")); }
  416. void onPrintTimerPaused() {}
  417. void onPrintTimerStopped() { write_to_lcd_P(PSTR("{TQ:100}")); }
  418. // Not needed for Malyan LCD
  419. void onStatusChanged(const char * const) {}
  420. void onMediaInserted() {};
  421. void onMediaError() {};
  422. void onMediaRemoved() {};
  423. void onPlayTone(const uint16_t, const uint16_t) {}
  424. void onFilamentRunout(const extruder_t extruder) {}
  425. void onUserConfirmRequired(const char * const) {}
  426. void onFactoryReset() {}
  427. void onStoreSettings(char*) {}
  428. void onLoadSettings(const char*) {}
  429. void onConfigurationStoreWritten(bool) {}
  430. void onConfigurationStoreRead(bool) {}
  431. void OnPidTuning(const result_t) {}
  432. }
  433. #endif // MALYAN_LCD