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.

DGUSDisplay.cpp 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../../inc/MarlinConfigPre.h"
  23. #if HAS_DGUS_LCD_CLASSIC
  24. #if HOTENDS > 2
  25. #warning "More than 2 hotends not implemented on DGUS Display UI."
  26. #endif
  27. #include "../ui_api.h"
  28. #include "../../../MarlinCore.h"
  29. #include "../../../module/motion.h"
  30. #include "../../../gcode/queue.h"
  31. #include "../../../module/planner.h"
  32. #include "../../../libs/duration_t.h"
  33. #include "../../../module/printcounter.h"
  34. #if ENABLED(POWER_LOSS_RECOVERY)
  35. #include "../../../feature/powerloss.h"
  36. #endif
  37. #include "DGUSDisplay.h"
  38. #include "DGUSVPVariable.h"
  39. #include "DGUSDisplayDef.h"
  40. DGUSDisplay dgusdisplay;
  41. // Preamble... 2 Bytes, usually 0x5A 0xA5, but configurable
  42. constexpr uint8_t DGUS_HEADER1 = 0x5A;
  43. constexpr uint8_t DGUS_HEADER2 = 0xA5;
  44. constexpr uint8_t DGUS_CMD_WRITEVAR = 0x82;
  45. constexpr uint8_t DGUS_CMD_READVAR = 0x83;
  46. #if ENABLED(DEBUG_DGUSLCD)
  47. bool dguslcd_local_debug; // = false;
  48. #endif
  49. void DGUSDisplay::InitDisplay() {
  50. #ifndef LCD_BAUDRATE
  51. #define LCD_BAUDRATE 115200
  52. #endif
  53. LCD_SERIAL.begin(LCD_BAUDRATE);
  54. if (TERN1(POWER_LOSS_RECOVERY, !recovery.valid())) { // If no Power-Loss Recovery is needed...
  55. TERN_(DGUS_LCD_UI_MKS, delay(LOGO_TIME_DELAY)); // Show the logo for a little while
  56. }
  57. RequestScreen(TERN(SHOW_BOOTSCREEN, DGUSLCD_SCREEN_BOOT, DGUSLCD_SCREEN_MAIN));
  58. }
  59. void DGUSDisplay::WriteVariable(uint16_t adr, const void *values, uint8_t valueslen, bool isstr) {
  60. const char* myvalues = static_cast<const char*>(values);
  61. bool strend = !myvalues;
  62. WriteHeader(adr, DGUS_CMD_WRITEVAR, valueslen);
  63. while (valueslen--) {
  64. char x;
  65. if (!strend) x = *myvalues++;
  66. if ((isstr && !x) || strend) {
  67. strend = true;
  68. x = ' ';
  69. }
  70. LCD_SERIAL.write(x);
  71. }
  72. }
  73. void DGUSDisplay::WriteVariable(uint16_t adr, uint16_t value) {
  74. value = (value & 0xFFU) << 8U | (value >> 8U);
  75. WriteVariable(adr, static_cast<const void*>(&value), sizeof(uint16_t));
  76. }
  77. void DGUSDisplay::WriteVariable(uint16_t adr, int16_t value) {
  78. value = (value & 0xFFU) << 8U | (value >> 8U);
  79. WriteVariable(adr, static_cast<const void*>(&value), sizeof(uint16_t));
  80. }
  81. void DGUSDisplay::WriteVariable(uint16_t adr, uint8_t value) {
  82. WriteVariable(adr, static_cast<const void*>(&value), sizeof(uint8_t));
  83. }
  84. void DGUSDisplay::WriteVariable(uint16_t adr, int8_t value) {
  85. WriteVariable(adr, static_cast<const void*>(&value), sizeof(int8_t));
  86. }
  87. #if ENABLED(DGUS_LCD_UI_MKS)
  88. void DGUSDisplay::MKS_WriteVariable(uint16_t adr, uint8_t value) {
  89. WriteVariable(adr, static_cast<const void *>(&value), sizeof(uint8_t));
  90. }
  91. #endif
  92. void DGUSDisplay::WriteVariable(uint16_t adr, long value) {
  93. union { long l; char lb[4]; } endian;
  94. char tmp[4];
  95. endian.l = value;
  96. tmp[0] = endian.lb[3];
  97. tmp[1] = endian.lb[2];
  98. tmp[2] = endian.lb[1];
  99. tmp[3] = endian.lb[0];
  100. WriteVariable(adr, static_cast<const void*>(&tmp), sizeof(long));
  101. }
  102. void DGUSDisplay::WriteVariablePGM(uint16_t adr, const void *values, uint8_t valueslen, bool isstr) {
  103. const char* myvalues = static_cast<const char*>(values);
  104. bool strend = !myvalues;
  105. WriteHeader(adr, DGUS_CMD_WRITEVAR, valueslen);
  106. while (valueslen--) {
  107. char x;
  108. if (!strend) x = pgm_read_byte(myvalues++);
  109. if ((isstr && !x) || strend) {
  110. strend = true;
  111. x = ' ';
  112. }
  113. LCD_SERIAL.write(x);
  114. }
  115. }
  116. void DGUSDisplay::ProcessRx() {
  117. #if ENABLED(SERIAL_STATS_RX_BUFFER_OVERRUNS)
  118. if (!LCD_SERIAL.available() && LCD_SERIAL.buffer_overruns()) {
  119. // Overrun, but reset the flag only when the buffer is empty
  120. // We want to extract as many as valid datagrams possible...
  121. DEBUG_ECHOPGM("OVFL");
  122. rx_datagram_state = DGUS_IDLE;
  123. //LCD_SERIAL.reset_rx_overun();
  124. LCD_SERIAL.flush();
  125. }
  126. #endif
  127. uint8_t receivedbyte;
  128. while (LCD_SERIAL.available()) {
  129. switch (rx_datagram_state) {
  130. case DGUS_IDLE: // Waiting for the first header byte
  131. receivedbyte = LCD_SERIAL.read();
  132. //DEBUG_ECHOPAIR("< ",x);
  133. if (DGUS_HEADER1 == receivedbyte) rx_datagram_state = DGUS_HEADER1_SEEN;
  134. break;
  135. case DGUS_HEADER1_SEEN: // Waiting for the second header byte
  136. receivedbyte = LCD_SERIAL.read();
  137. //DEBUG_ECHOPAIR(" ",x);
  138. rx_datagram_state = (DGUS_HEADER2 == receivedbyte) ? DGUS_HEADER2_SEEN : DGUS_IDLE;
  139. break;
  140. case DGUS_HEADER2_SEEN: // Waiting for the length byte
  141. rx_datagram_len = LCD_SERIAL.read();
  142. DEBUG_ECHOPAIR(" (", rx_datagram_len, ") ");
  143. // Telegram min len is 3 (command and one word of payload)
  144. rx_datagram_state = WITHIN(rx_datagram_len, 3, DGUS_RX_BUFFER_SIZE) ? DGUS_WAIT_TELEGRAM : DGUS_IDLE;
  145. break;
  146. case DGUS_WAIT_TELEGRAM: // wait for complete datagram to arrive.
  147. if (LCD_SERIAL.available() < rx_datagram_len) return;
  148. Initialized = true; // We've talked to it, so we defined it as initialized.
  149. uint8_t command = LCD_SERIAL.read();
  150. DEBUG_ECHOPAIR("# ", command);
  151. uint8_t readlen = rx_datagram_len - 1; // command is part of len.
  152. unsigned char tmp[rx_datagram_len - 1];
  153. unsigned char *ptmp = tmp;
  154. while (readlen--) {
  155. receivedbyte = LCD_SERIAL.read();
  156. DEBUG_ECHOPAIR(" ", receivedbyte);
  157. *ptmp++ = receivedbyte;
  158. }
  159. DEBUG_ECHOPGM(" # ");
  160. // mostly we'll get this: 5A A5 03 82 4F 4B -- ACK on 0x82, so discard it.
  161. if (command == DGUS_CMD_WRITEVAR && 'O' == tmp[0] && 'K' == tmp[1]) {
  162. DEBUG_ECHOLNPGM(">");
  163. rx_datagram_state = DGUS_IDLE;
  164. break;
  165. }
  166. /* AutoUpload, (and answer to) Command 0x83 :
  167. | tmp[0 1 2 3 4 ... ]
  168. | Example 5A A5 06 83 20 01 01 78 01 ……
  169. | / / | | \ / | \ \
  170. | Header | | | | \_____\_ DATA (Words!)
  171. | DatagramLen / VPAdr |
  172. | Command DataLen (in Words) */
  173. if (command == DGUS_CMD_READVAR) {
  174. const uint16_t vp = tmp[0] << 8 | tmp[1];
  175. //const uint8_t dlen = tmp[2] << 1; // Convert to Bytes. (Display works with words)
  176. //DEBUG_ECHOPAIR(" vp=", vp, " dlen=", dlen);
  177. DGUS_VP_Variable ramcopy;
  178. if (populate_VPVar(vp, &ramcopy)) {
  179. if (ramcopy.set_by_display_handler)
  180. ramcopy.set_by_display_handler(ramcopy, &tmp[3]);
  181. else
  182. DEBUG_ECHOLNPGM(" VPVar found, no handler.");
  183. }
  184. else
  185. DEBUG_ECHOLNPAIR(" VPVar not found:", vp);
  186. rx_datagram_state = DGUS_IDLE;
  187. break;
  188. }
  189. // discard anything else
  190. rx_datagram_state = DGUS_IDLE;
  191. }
  192. }
  193. }
  194. size_t DGUSDisplay::GetFreeTxBuffer() { return SERIAL_GET_TX_BUFFER_FREE(); }
  195. void DGUSDisplay::WriteHeader(uint16_t adr, uint8_t cmd, uint8_t payloadlen) {
  196. LCD_SERIAL.write(DGUS_HEADER1);
  197. LCD_SERIAL.write(DGUS_HEADER2);
  198. LCD_SERIAL.write(payloadlen + 3);
  199. LCD_SERIAL.write(cmd);
  200. LCD_SERIAL.write(adr >> 8);
  201. LCD_SERIAL.write(adr & 0xFF);
  202. }
  203. void DGUSDisplay::WritePGM(const char str[], uint8_t len) {
  204. while (len--) LCD_SERIAL.write(pgm_read_byte(str++));
  205. }
  206. void DGUSDisplay::loop() {
  207. // protect against recursion… ProcessRx() may indirectly call idle() when injecting gcode commands.
  208. if (!no_reentrance) {
  209. no_reentrance = true;
  210. ProcessRx();
  211. no_reentrance = false;
  212. }
  213. }
  214. rx_datagram_state_t DGUSDisplay::rx_datagram_state = DGUS_IDLE;
  215. uint8_t DGUSDisplay::rx_datagram_len = 0;
  216. bool DGUSDisplay::Initialized = false;
  217. bool DGUSDisplay::no_reentrance = false;
  218. // A SW memory barrier, to ensure GCC does not overoptimize loops
  219. #define sw_barrier() asm volatile("": : :"memory");
  220. bool populate_VPVar(const uint16_t VP, DGUS_VP_Variable * const ramcopy) {
  221. // DEBUG_ECHOPAIR("populate_VPVar ", VP);
  222. const DGUS_VP_Variable *pvp = DGUSLCD_FindVPVar(VP);
  223. // DEBUG_ECHOLNPAIR(" pvp ", (uint16_t )pvp);
  224. if (!pvp) return false;
  225. memcpy_P(ramcopy, pvp, sizeof(DGUS_VP_Variable));
  226. return true;
  227. }
  228. #endif // HAS_DGUS_LCD_CLASSIC