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

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