My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

extui_malyan_lcd.cpp 15KB

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