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.5KB

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