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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <http://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. #define dgusserial DGUS_SERIAL
  52. void DGUSDisplay::InitDisplay() {
  53. dgusserial.begin(DGUS_BAUDRATE);
  54. if (true
  55. #if ENABLED(POWER_LOSS_RECOVERY)
  56. && !recovery.valid()
  57. #endif
  58. )
  59. RequestScreen(
  60. #if ENABLED(SHOW_BOOTSCREEN)
  61. DGUSLCD_SCREEN_BOOT
  62. #else
  63. DGUSLCD_SCREEN_MAIN
  64. #endif
  65. );
  66. }
  67. void DGUSDisplay::WriteVariable(uint16_t adr, const void* values, uint8_t valueslen, bool isstr) {
  68. const char* myvalues = static_cast<const char*>(values);
  69. bool strend = !myvalues;
  70. WriteHeader(adr, DGUS_CMD_WRITEVAR, valueslen);
  71. while (valueslen--) {
  72. char x;
  73. if (!strend) x = *myvalues++;
  74. if ((isstr && !x) || strend) {
  75. strend = true;
  76. x = ' ';
  77. }
  78. dgusserial.write(x);
  79. }
  80. }
  81. void DGUSDisplay::WriteVariable(uint16_t adr, uint16_t value) {
  82. WriteVariable(adr, static_cast<const void*>(&value), sizeof(uint16_t));
  83. }
  84. void DGUSDisplay::WriteVariablePGM(uint16_t adr, const void* values, uint8_t valueslen, bool isstr) {
  85. const char* myvalues = static_cast<const char*>(values);
  86. bool strend = !myvalues;
  87. WriteHeader(adr, DGUS_CMD_WRITEVAR, valueslen);
  88. while (valueslen--) {
  89. char x;
  90. if (!strend) x = pgm_read_byte(myvalues++);
  91. if ((isstr && !x) || strend) {
  92. strend = true;
  93. x = ' ';
  94. }
  95. dgusserial.write(x);
  96. }
  97. }
  98. void DGUSDisplay::ProcessRx() {
  99. #if ENABLED(DGUS_SERIAL_STATS_RX_BUFFER_OVERRUNS)
  100. if (!dgusserial.available() && dgusserial.buffer_overruns()) {
  101. // Overrun, but reset the flag only when the buffer is empty
  102. // We want to extract as many as valid datagrams possible...
  103. DEBUG_ECHOPGM("OVFL");
  104. rx_datagram_state = DGUS_IDLE;
  105. //dgusserial.reset_rx_overun();
  106. dgusserial.flush();
  107. }
  108. #endif
  109. uint8_t receivedbyte;
  110. while (dgusserial.available()) {
  111. switch (rx_datagram_state) {
  112. case DGUS_IDLE: // Waiting for the first header byte
  113. receivedbyte = dgusserial.read();
  114. //DEBUG_ECHOPAIR("< ",x);
  115. if (DGUS_HEADER1 == receivedbyte) rx_datagram_state = DGUS_HEADER1_SEEN;
  116. break;
  117. case DGUS_HEADER1_SEEN: // Waiting for the second header byte
  118. receivedbyte = dgusserial.read();
  119. //DEBUG_ECHOPAIR(" ",x);
  120. rx_datagram_state = (DGUS_HEADER2 == receivedbyte) ? DGUS_HEADER2_SEEN : DGUS_IDLE;
  121. break;
  122. case DGUS_HEADER2_SEEN: // Waiting for the length byte
  123. rx_datagram_len = dgusserial.read();
  124. DEBUG_ECHOPAIR(" (", rx_datagram_len, ") ");
  125. // Telegram min len is 3 (command and one word of payload)
  126. rx_datagram_state = WITHIN(rx_datagram_len, 3, DGUS_RX_BUFFER_SIZE) ? DGUS_WAIT_TELEGRAM : DGUS_IDLE;
  127. break;
  128. case DGUS_WAIT_TELEGRAM: // wait for complete datagram to arrive.
  129. if (dgusserial.available() < rx_datagram_len) return;
  130. Initialized = true; // We've talked to it, so we defined it as initialized.
  131. uint8_t command = dgusserial.read();
  132. DEBUG_ECHOPAIR("# ", command);
  133. uint8_t readlen = rx_datagram_len - 1; // command is part of len.
  134. unsigned char tmp[rx_datagram_len - 1];
  135. unsigned char *ptmp = tmp;
  136. while (readlen--) {
  137. receivedbyte = dgusserial.read();
  138. DEBUG_ECHOPAIR(" ", receivedbyte);
  139. *ptmp++ = receivedbyte;
  140. }
  141. DEBUG_ECHOPGM(" # ");
  142. // mostly we'll get this: 5A A5 03 82 4F 4B -- ACK on 0x82, so discard it.
  143. if (command == DGUS_CMD_WRITEVAR && 'O' == tmp[0] && 'K' == tmp[1]) {
  144. DEBUG_ECHOLNPGM(">");
  145. rx_datagram_state = DGUS_IDLE;
  146. break;
  147. }
  148. /* AutoUpload, (and answer to) Command 0x83 :
  149. | tmp[0 1 2 3 4 ... ]
  150. | Example 5A A5 06 83 20 01 01 78 01 ……
  151. | / / | | \ / | \ \
  152. | Header | | | | \_____\_ DATA (Words!)
  153. | DatagramLen / VPAdr |
  154. | Command DataLen (in Words) */
  155. if (command == DGUS_CMD_READVAR) {
  156. const uint16_t vp = tmp[0] << 8 | tmp[1];
  157. //const uint8_t dlen = tmp[2] << 1; // Convert to Bytes. (Display works with words)
  158. //DEBUG_ECHOPAIR(" vp=", vp, " dlen=", dlen);
  159. DGUS_VP_Variable ramcopy;
  160. if (populate_VPVar(vp, &ramcopy)) {
  161. if (ramcopy.set_by_display_handler)
  162. ramcopy.set_by_display_handler(ramcopy, &tmp[3]);
  163. else
  164. DEBUG_ECHOLNPGM(" VPVar found, no handler.");
  165. }
  166. else
  167. DEBUG_ECHOLNPAIR(" VPVar not found:", vp);
  168. rx_datagram_state = DGUS_IDLE;
  169. break;
  170. }
  171. // discard anything else
  172. rx_datagram_state = DGUS_IDLE;
  173. }
  174. }
  175. }
  176. size_t DGUSDisplay::GetFreeTxBuffer() { return DGUS_SERIAL_GET_TX_BUFFER_FREE(); }
  177. void DGUSDisplay::WriteHeader(uint16_t adr, uint8_t cmd, uint8_t payloadlen) {
  178. dgusserial.write(DGUS_HEADER1);
  179. dgusserial.write(DGUS_HEADER2);
  180. dgusserial.write(payloadlen + 3);
  181. dgusserial.write(cmd);
  182. dgusserial.write(adr >> 8);
  183. dgusserial.write(adr & 0xFF);
  184. }
  185. void DGUSDisplay::WritePGM(const char str[], uint8_t len) {
  186. while (len--) dgusserial.write(pgm_read_byte(str++));
  187. }
  188. void DGUSDisplay::loop() {
  189. // protect against recursion… ProcessRx() may indirectly call idle() when injecting gcode commands.
  190. if (!no_reentrance) {
  191. no_reentrance = true;
  192. ProcessRx();
  193. no_reentrance = false;
  194. }
  195. }
  196. rx_datagram_state_t DGUSDisplay::rx_datagram_state = DGUS_IDLE;
  197. uint8_t DGUSDisplay::rx_datagram_len = 0;
  198. bool DGUSDisplay::Initialized = false;
  199. bool DGUSDisplay::no_reentrance = false;
  200. // A SW memory barrier, to ensure GCC does not overoptimize loops
  201. #define sw_barrier() asm volatile("": : :"memory");
  202. bool populate_VPVar(const uint16_t VP, DGUS_VP_Variable * const ramcopy) {
  203. // DEBUG_ECHOPAIR("populate_VPVar ", VP);
  204. const DGUS_VP_Variable *pvp = DGUSLCD_FindVPVar(VP);
  205. // DEBUG_ECHOLNPAIR(" pvp ", (uint16_t )pvp);
  206. if (!pvp) return false;
  207. memcpy_P(ramcopy, pvp, sizeof(DGUS_VP_Variable));
  208. return true;
  209. }
  210. #endif // HAS_DGUS_LCD