malyanlcd.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. * malyanlcd.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 "MarlinConfig.h"
  43. #if ENABLED(MALYAN_LCD)
  44. #if ENABLED(SDSUPPORT)
  45. #include "cardreader.h"
  46. #include "SdFatConfig.h"
  47. #else
  48. #define LONG_FILENAME_LENGTH 0
  49. #endif
  50. #include "temperature.h"
  51. #include "planner.h"
  52. #include "stepper.h"
  53. #include "duration_t.h"
  54. #include "printcounter.h"
  55. #include "parser.h"
  56. #include "configuration_store.h"
  57. #include "Marlin.h"
  58. #if USE_MARLINSERIAL
  59. // Make an exception to use HardwareSerial too
  60. #undef HardwareSerial_h
  61. #include <HardwareSerial.h>
  62. #define USB_STATUS true
  63. #else
  64. #define USB_STATUS Serial
  65. #endif
  66. // On the Malyan M200, this will be Serial1. On a RAMPS board,
  67. // it might not be.
  68. #define LCD_SERIAL Serial1
  69. // This is based on longest sys command + a filename, plus some buffer
  70. // in case we encounter some data we don't recognize
  71. // There is no evidence a line will ever be this long, but better safe than sorry
  72. #define MAX_CURLY_COMMAND (32 + LONG_FILENAME_LENGTH) * 2
  73. // Track incoming command bytes from the LCD
  74. int inbound_count;
  75. // For sending print completion messages
  76. bool last_printing_status = false;
  77. // Everything written needs the high bit set.
  78. void write_to_lcd_P(const char * const message) {
  79. char encoded_message[MAX_CURLY_COMMAND];
  80. uint8_t message_length = MIN(strlen_P(message), sizeof(encoded_message));
  81. for (uint8_t i = 0; i < message_length; i++)
  82. encoded_message[i] = pgm_read_byte(&message[i]) | 0x80;
  83. LCD_SERIAL.Print::write(encoded_message, message_length);
  84. }
  85. void write_to_lcd(const char * const message) {
  86. char encoded_message[MAX_CURLY_COMMAND];
  87. const uint8_t message_length = MIN(strlen(message), sizeof(encoded_message));
  88. for (uint8_t i = 0; i < message_length; i++)
  89. encoded_message[i] = message[i] | 0x80;
  90. LCD_SERIAL.Print::write(encoded_message, message_length);
  91. }
  92. /**
  93. * Process an LCD 'C' command.
  94. * These are currently all temperature commands
  95. * {C:T0190}
  96. * Set temp for hotend to 190
  97. * {C:P050}
  98. * Set temp for bed to 50
  99. *
  100. * {C:S09} set feedrate to 90 %.
  101. * {C:S12} set feedrate to 120 %.
  102. *
  103. * the command portion begins after the :
  104. */
  105. void process_lcd_c_command(const char* command) {
  106. switch (command[0]) {
  107. case 'C': {
  108. int raw_feedrate = atoi(command + 1);
  109. feedrate_percentage = raw_feedrate * 10;
  110. feedrate_percentage = constrain(feedrate_percentage, 10, 999);
  111. } break;
  112. case 'T': {
  113. thermalManager.setTargetHotend(atoi(command + 1), 0);
  114. } break;
  115. case 'P': {
  116. thermalManager.setTargetBed(atoi(command + 1));
  117. } break;
  118. default:
  119. SERIAL_ECHOLNPAIR("UNKNOWN C COMMAND", command);
  120. return;
  121. }
  122. }
  123. /**
  124. * Process an LCD 'B' command.
  125. * {B:0} results in: {T0:008/195}{T1:000/000}{TP:000/000}{TQ:000C}{TT:000000}
  126. * T0/T1 are hot end temperatures, TP is bed, TQ is percent, and TT is probably
  127. * time remaining (HH:MM:SS). The UI can't handle displaying a second hotend,
  128. * but the stock firmware always sends it, and it's always zero.
  129. */
  130. void process_lcd_eb_command(const char* command) {
  131. char elapsed_buffer[10];
  132. duration_t elapsed;
  133. switch (command[0]) {
  134. case '0': {
  135. elapsed = print_job_timer.duration();
  136. sprintf_P(elapsed_buffer, PSTR("%02u%02u%02u"), uint16_t(elapsed.hour()), uint16_t(elapsed.minute()) % 60UL, elapsed.second());
  137. char message_buffer[MAX_CURLY_COMMAND];
  138. sprintf_P(message_buffer,
  139. PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}{TQ:%03i}{TT:%s}"),
  140. thermalManager.degHotend(0),
  141. thermalManager.degTargetHotend(0),
  142. #if HAS_HEATED_BED
  143. thermalManager.degBed(),
  144. thermalManager.degTargetBed(),
  145. #else
  146. 0, 0,
  147. #endif
  148. #if ENABLED(SDSUPPORT)
  149. card.percentDone(),
  150. #else
  151. 0,
  152. #endif
  153. elapsed_buffer);
  154. write_to_lcd(message_buffer);
  155. } break;
  156. default:
  157. SERIAL_ECHOLNPAIR("UNKNOWN E/B COMMAND", command);
  158. return;
  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. static bool steppers_enabled = false;
  173. char axis = command[0];
  174. switch (axis) {
  175. case 'E':
  176. // enable or disable steppers
  177. // switch to relative
  178. enqueue_and_echo_commands_now_P(PSTR("G91"));
  179. enqueue_and_echo_commands_now_P(steppers_enabled ? PSTR("M18") : PSTR("M17"));
  180. steppers_enabled = !steppers_enabled;
  181. break;
  182. case 'A':
  183. axis = 'E';
  184. // fallthru
  185. case 'Y':
  186. case 'Z':
  187. case 'X': {
  188. // G0 <AXIS><distance>
  189. // The M200 class UI seems to send movement in .1mm values.
  190. char cmd[20];
  191. sprintf_P(cmd, PSTR("G1 %c%03.1f"), axis, atof(command + 1) / 10.0);
  192. enqueue_and_echo_command_now(cmd);
  193. } break;
  194. default:
  195. SERIAL_ECHOLNPAIR("UNKNOWN J COMMAND", command);
  196. return;
  197. }
  198. }
  199. /**
  200. * Process an LCD 'P' command, related to homing and printing.
  201. * Cancel:
  202. * {P:X}
  203. *
  204. * Home all axes:
  205. * {P:H}
  206. *
  207. * Print a file:
  208. * {P:000}
  209. * The File number is specified as a three digit value.
  210. * Printer responds with:
  211. * {PRINTFILE:Mini_SNES_Bottom.gcode}
  212. * {SYS:BUILD}echo:Now fresh file: Mini_SNES_Bottom.gcode
  213. * File opened: Mini_SNES_Bottom.gcode Size: 5805813
  214. * File selected
  215. * {SYS:BUILD}
  216. * T:-2526.8 E:0
  217. * T:-2533.0 E:0
  218. * T:-2537.4 E:0
  219. * Note only the curly brace stuff matters.
  220. */
  221. void process_lcd_p_command(const char* command) {
  222. switch (command[0]) {
  223. case 'X':
  224. #if ENABLED(SDSUPPORT)
  225. // cancel print
  226. write_to_lcd_P(PSTR("{SYS:CANCELING}"));
  227. last_printing_status = false;
  228. card.stopSDPrint(
  229. #if SD_RESORT
  230. true
  231. #endif
  232. );
  233. clear_command_queue();
  234. quickstop_stepper();
  235. print_job_timer.stop();
  236. thermalManager.disable_all_heaters();
  237. #if FAN_COUNT > 0
  238. for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0;
  239. #endif
  240. wait_for_heatup = false;
  241. write_to_lcd_P(PSTR("{SYS:STARTED}"));
  242. #endif
  243. break;
  244. case 'H':
  245. // Home all axis
  246. enqueue_and_echo_commands_now_P(PSTR("G28"));
  247. break;
  248. default: {
  249. #if ENABLED(SDSUPPORT)
  250. // Print file 000 - a three digit number indicating which
  251. // file to print in the SD card. If it's a directory,
  252. // then switch to the directory.
  253. // Find the name of the file to print.
  254. // It's needed to echo the PRINTFILE option.
  255. // The {S:L} command should've ensured the SD card was mounted.
  256. card.getfilename(atoi(command));
  257. // There may be a difference in how V1 and V2 LCDs handle subdirectory
  258. // prints. Investigate more. This matches the V1 motion controller actions
  259. // but the V2 LCD switches to "print" mode on {SYS:DIR} response.
  260. if (card.filenameIsDir) {
  261. card.chdir(card.filename);
  262. write_to_lcd_P(PSTR("{SYS:DIR}"));
  263. }
  264. else {
  265. char message_buffer[MAX_CURLY_COMMAND];
  266. sprintf_P(message_buffer, PSTR("{PRINTFILE:%s}"), card.longest_filename());
  267. write_to_lcd(message_buffer);
  268. write_to_lcd_P(PSTR("{SYS:BUILD}"));
  269. card.openAndPrintFile(card.filename);
  270. }
  271. #endif
  272. } break; // default
  273. } // switch
  274. }
  275. /**
  276. * Handle an lcd 'S' command
  277. * {S:I} - Temperature request
  278. * {T0:999/000}{T1:000/000}{TP:004/000}
  279. *
  280. * {S:L} - File Listing request
  281. * Printer Response:
  282. * {FILE:buttons.gcode}
  283. * {FILE:update.bin}
  284. * {FILE:nupdate.bin}
  285. * {FILE:fcupdate.flg}
  286. * {SYS:OK}
  287. */
  288. void process_lcd_s_command(const char* command) {
  289. switch (command[0]) {
  290. case 'I': {
  291. // temperature information
  292. char message_buffer[MAX_CURLY_COMMAND];
  293. sprintf_P(message_buffer, PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}"),
  294. thermalManager.degHotend(0), thermalManager.degTargetHotend(0),
  295. #if HAS_HEATED_BED
  296. thermalManager.degBed(), thermalManager.degTargetBed()
  297. #else
  298. 0, 0
  299. #endif
  300. );
  301. write_to_lcd(message_buffer);
  302. } break;
  303. case 'H':
  304. // Home all axis
  305. enqueue_and_echo_command("G28");
  306. break;
  307. case 'L': {
  308. #if ENABLED(SDSUPPORT)
  309. if (!card.cardOK) card.initsd();
  310. // A more efficient way to do this would be to
  311. // implement a callback in the ls_SerialPrint code, but
  312. // that requires changes to the core cardreader class that
  313. // would not benefit the majority of users. Since one can't
  314. // select a file for printing during a print, there's
  315. // little reason not to do it this way.
  316. char message_buffer[MAX_CURLY_COMMAND];
  317. uint16_t file_count = card.get_num_Files();
  318. for (uint16_t i = 0; i < file_count; i++) {
  319. card.getfilename(i);
  320. sprintf_P(message_buffer, card.filenameIsDir ? PSTR("{DIR:%s}") : PSTR("{FILE:%s}"), card.longest_filename());
  321. write_to_lcd(message_buffer);
  322. }
  323. write_to_lcd_P(PSTR("{SYS:OK}"));
  324. #endif
  325. } break;
  326. default:
  327. SERIAL_ECHOLNPAIR("UNKNOWN S COMMAND", command);
  328. return;
  329. }
  330. }
  331. /**
  332. * Receive a curly brace command and translate to G-code.
  333. * Currently {E:0} is not handled. Its function is unknown,
  334. * but it occurs during the temp window after a sys build.
  335. */
  336. void process_lcd_command(const char* command) {
  337. const char *current = command;
  338. current++; // skip the leading {. The trailing one is already gone.
  339. byte command_code = *current++;
  340. if (*current != ':') {
  341. SERIAL_ECHOLNPAIR("UNKNOWN COMMAND FORMAT", command);
  342. return;
  343. }
  344. current++; // skip the :
  345. switch (command_code) {
  346. case 'S':
  347. process_lcd_s_command(current);
  348. break;
  349. case 'J':
  350. process_lcd_j_command(current);
  351. break;
  352. case 'P':
  353. process_lcd_p_command(current);
  354. break;
  355. case 'C':
  356. process_lcd_c_command(current);
  357. break;
  358. case 'B':
  359. case 'E':
  360. process_lcd_eb_command(current);
  361. break;
  362. default:
  363. SERIAL_ECHOLNPAIR("UNKNOWN COMMAND", command);
  364. return;
  365. }
  366. }
  367. /**
  368. * UC means connected.
  369. * UD means disconnected
  370. * The stock firmware considers USB initialized as "connected."
  371. */
  372. void update_usb_status(const bool forceUpdate) {
  373. static bool last_usb_connected_status = false;
  374. // This is mildly different than stock, which
  375. // appears to use the usb discovery status.
  376. // This is more logical.
  377. if (last_usb_connected_status != USB_STATUS || forceUpdate) {
  378. last_usb_connected_status = USB_STATUS;
  379. write_to_lcd_P(last_usb_connected_status ? PSTR("{R:UC}\r\n") : PSTR("{R:UD}\r\n"));
  380. }
  381. }
  382. /**
  383. * - from printer on startup:
  384. * {SYS:STARTED}{VER:29}{SYS:STARTED}{R:UD}
  385. * The optimize attribute fixes a register Compile
  386. * error for amtel.
  387. */
  388. void _O2 lcd_update() {
  389. static char inbound_buffer[MAX_CURLY_COMMAND];
  390. // First report USB status.
  391. update_usb_status(false);
  392. // now drain commands...
  393. while (LCD_SERIAL.available()) {
  394. const byte b = (byte)LCD_SERIAL.read() & 0x7F;
  395. inbound_buffer[inbound_count++] = b;
  396. if (b == '}' || inbound_count == sizeof(inbound_buffer) - 1) {
  397. inbound_buffer[inbound_count - 1] = '\0';
  398. process_lcd_command(inbound_buffer);
  399. inbound_count = 0;
  400. inbound_buffer[0] = 0;
  401. }
  402. }
  403. #if ENABLED(SDSUPPORT)
  404. // The way last printing status works is simple:
  405. // The UI needs to see at least one TQ which is not 100%
  406. // and then when the print is complete, one which is.
  407. static uint8_t last_percent_done = 100;
  408. // If there was a print in progress, we need to emit the final
  409. // print status as {TQ:100}. Reset last percent done so a new print will
  410. // issue a percent of 0.
  411. const uint8_t percent_done = card.sdprinting ? card.percentDone() : last_printing_status ? 100 : 0;
  412. if (percent_done != last_percent_done) {
  413. char message_buffer[10];
  414. sprintf_P(message_buffer, PSTR("{TQ:%03i}"), percent_done);
  415. write_to_lcd(message_buffer);
  416. last_percent_done = percent_done;
  417. last_printing_status = card.sdprinting;
  418. }
  419. #endif
  420. }
  421. /**
  422. * The Malyan LCD actually runs as a separate MCU on Serial 1.
  423. * This code's job is to siphon the weird curly-brace commands from
  424. * it and translate into gcode, which then gets injected into
  425. * the command queue where possible.
  426. */
  427. void lcd_init() {
  428. inbound_count = 0;
  429. LCD_SERIAL.begin(500000);
  430. // Signal init
  431. write_to_lcd_P(PSTR("{SYS:STARTED}\r\n"));
  432. // send a version that says "unsupported"
  433. write_to_lcd_P(PSTR("{VER:99}\r\n"));
  434. // No idea why it does this twice.
  435. write_to_lcd_P(PSTR("{SYS:STARTED}\r\n"));
  436. update_usb_status(true);
  437. }
  438. /**
  439. * Set an alert.
  440. */
  441. void lcd_setalertstatusPGM(const char* message) {
  442. char message_buffer[MAX_CURLY_COMMAND];
  443. sprintf_P(message_buffer, PSTR("{E:%s}"), message);
  444. write_to_lcd(message_buffer);
  445. }
  446. #endif // MALYAN_LCD