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.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #pragma once
  23. /* DGUS implementation written by coldtobi in 2019 for Marlin */
  24. #include "../../../../inc/MarlinConfigPre.h"
  25. #include "../../../../MarlinCore.h"
  26. #if HAS_BED_PROBE
  27. #include "../../../../module/probe.h"
  28. #endif
  29. #include "DGUSVPVariable.h"
  30. enum DGUSLCD_Screens : uint8_t;
  31. #define DEBUG_OUT ENABLED(DEBUG_DGUSLCD)
  32. #include "../../../../core/debug_out.h"
  33. typedef enum : uint8_t {
  34. DGUS_IDLE, //< waiting for DGUS_HEADER1.
  35. DGUS_HEADER1_SEEN, //< DGUS_HEADER1 received
  36. DGUS_HEADER2_SEEN, //< DGUS_HEADER2 received
  37. DGUS_WAIT_TELEGRAM, //< LEN received, Waiting for to receive all bytes.
  38. } rx_datagram_state_t;
  39. // Low-Level access to the display.
  40. class DGUSDisplay {
  41. public:
  42. DGUSDisplay() = default;
  43. static void InitDisplay();
  44. // Variable access.
  45. static void WriteVariable(uint16_t adr, const void* values, uint8_t valueslen, bool isstr=false);
  46. static void WriteVariablePGM(uint16_t adr, const void* values, uint8_t valueslen, bool isstr=false);
  47. template<typename T>
  48. static void WriteVariable(uint16_t adr, T value) {
  49. WriteVariable(adr, static_cast<const void*>(&value), sizeof(T));
  50. }
  51. // Until now I did not need to actively read from the display. That's why there is no ReadVariable
  52. // (I extensively use the auto upload of the display)
  53. // Force display into another screen.
  54. // (And trigger update of containing VPs)
  55. // (to implement a pop up message, which may not be nested)
  56. static void RequestScreen(DGUSLCD_Screens screen);
  57. // Periodic tasks, eg. Rx-Queue handling.
  58. static void loop();
  59. public:
  60. // Helper for users of this class to estimate if an interaction would be blocking.
  61. static size_t GetFreeTxBuffer();
  62. // Checks two things: Can we confirm the presence of the display and has we initiliazed it.
  63. // (both boils down that the display answered to our chatting)
  64. static inline bool isInitialized() { return Initialized; }
  65. private:
  66. static void WriteHeader(uint16_t adr, uint8_t cmd, uint8_t payloadlen);
  67. static void WritePGM(const char str[], uint8_t len);
  68. static void ProcessRx();
  69. static rx_datagram_state_t rx_datagram_state;
  70. static uint8_t rx_datagram_len;
  71. static bool Initialized, no_reentrance;
  72. };
  73. extern DGUSDisplay dgusdisplay;
  74. // compile-time x^y
  75. constexpr float cpow(const float x, const int y) { return y == 0 ? 1.0 : x * cpow(x, y - 1); }
  76. class DGUSScreenVariableHandler {
  77. public:
  78. DGUSScreenVariableHandler() = default;
  79. static bool loop();
  80. /// Send all 4 strings that are displayed on the infoscreen, confirmation screen and kill screen
  81. /// The bools specifing whether the strings are in RAM or FLASH.
  82. static void sendinfoscreen(const char* line1, const char* line2, const char* line3, const char* line4, bool l1inflash, bool l2inflash, bool l3inflash, bool liinflash);
  83. static void HandleUserConfirmationPopUp(uint16_t ConfirmVP, const char* line1, const char* line2, const char* line3, const char* line4, bool l1inflash, bool l2inflash, bool l3inflash, bool liinflash);
  84. /// "M117" Message -- msg is a RAM ptr.
  85. static void setstatusmessage(const char* msg);
  86. /// The same for messages from Flash
  87. static void setstatusmessagePGM(PGM_P const msg);
  88. // Callback for VP "Display wants to change screen on idle printer"
  89. static void ScreenChangeHookIfIdle(DGUS_VP_Variable &var, void *val_ptr);
  90. // Callback for VP "Screen has been changed"
  91. static void ScreenChangeHook(DGUS_VP_Variable &var, void *val_ptr);
  92. // Callback for VP "All Heaters Off"
  93. static void HandleAllHeatersOff(DGUS_VP_Variable &var, void *val_ptr);
  94. // Hook for "Change this temperature"
  95. static void HandleTemperatureChanged(DGUS_VP_Variable &var, void *val_ptr);
  96. // Hook for "Change Flowrate"
  97. static void HandleFlowRateChanged(DGUS_VP_Variable &var, void *val_ptr);
  98. #if ENABLED(DUGS_UI_MOVE_DIS_OPTION)
  99. // Hook for manual move option
  100. static void HandleManualMoveOption(DGUS_VP_Variable &var, void *val_ptr);
  101. #endif
  102. // Hook for manual move.
  103. static void HandleManualMove(DGUS_VP_Variable &var, void *val_ptr);
  104. // Hook for manual extrude.
  105. static void HandleManualExtrude(DGUS_VP_Variable &var, void *val_ptr);
  106. // Hook for motor lock and unlook
  107. static void HandleMotorLockUnlock(DGUS_VP_Variable &var, void *val_ptr);
  108. #if ENABLED(POWER_LOSS_RECOVERY)
  109. // Hook for power loss recovery.
  110. static void HandlePowerLossRecovery(DGUS_VP_Variable &var, void *val_ptr);
  111. #endif
  112. // Hook for settings
  113. static void HandleSettings(DGUS_VP_Variable &var, void *val_ptr);
  114. static void HandleStepPerMMChanged(DGUS_VP_Variable &var, void *val_ptr);
  115. static void HandleStepPerMMExtruderChanged(DGUS_VP_Variable &var, void *val_ptr);
  116. #if HAS_PID_HEATING
  117. // Hook for "Change this temperature PID para"
  118. static void HandleTemperaturePIDChanged(DGUS_VP_Variable &var, void *val_ptr);
  119. // Hook for PID autotune
  120. static void HandlePIDAutotune(DGUS_VP_Variable &var, void *val_ptr);
  121. #endif
  122. #if HAS_BED_PROBE
  123. // Hook for "Change probe offset z"
  124. static void HandleProbeOffsetZChanged(DGUS_VP_Variable &var, void *val_ptr);
  125. #endif
  126. #if ENABLED(BABYSTEPPING)
  127. // Hook for live z adjust action
  128. static void HandleLiveAdjustZ(DGUS_VP_Variable &var, void *val_ptr);
  129. #endif
  130. #if FAN_COUNT > 0
  131. // Hook for fan control
  132. static void HandleFanControl(DGUS_VP_Variable &var, void *val_ptr);
  133. #endif
  134. // Hook for heater control
  135. static void HandleHeaterControl(DGUS_VP_Variable &var, void *val_ptr);
  136. #if ENABLED(DGUS_PREHEAT_UI)
  137. // Hook for preheat
  138. static void HandlePreheat(DGUS_VP_Variable &var, void *val_ptr);
  139. #endif
  140. #if ENABLED(DGUS_FILAMENT_LOADUNLOAD)
  141. // Hook for filament load and unload filament option
  142. static void HandleFilamentOption(DGUS_VP_Variable &var, void *val_ptr);
  143. // Hook for filament load and unload
  144. static void HandleFilamentLoadUnload(DGUS_VP_Variable &var);
  145. #endif
  146. #if ENABLED(SDSUPPORT)
  147. // Callback for VP "Display wants to change screen when there is a SD card"
  148. static void ScreenChangeHookIfSD(DGUS_VP_Variable &var, void *val_ptr);
  149. /// Scroll buttons on the file listing screen.
  150. static void DGUSLCD_SD_ScrollFilelist(DGUS_VP_Variable &var, void *val_ptr);
  151. /// File touched.
  152. static void DGUSLCD_SD_FileSelected(DGUS_VP_Variable &var, void *val_ptr);
  153. /// start print after confirmation received.
  154. static void DGUSLCD_SD_StartPrint(DGUS_VP_Variable &var, void *val_ptr);
  155. /// User hit the pause, resume or abort button.
  156. static void DGUSLCD_SD_ResumePauseAbort(DGUS_VP_Variable &var, void *val_ptr);
  157. /// User confirmed the abort action
  158. static void DGUSLCD_SD_ReallyAbort(DGUS_VP_Variable &var, void *val_ptr);
  159. /// User hit the tune button
  160. static void DGUSLCD_SD_PrintTune(DGUS_VP_Variable &var, void *val_ptr);
  161. /// Send a single filename to the display.
  162. static void DGUSLCD_SD_SendFilename(DGUS_VP_Variable &var);
  163. /// Marlin informed us that a new SD has been inserted.
  164. static void SDCardInserted();
  165. /// Marlin informed us that the SD Card has been removed().
  166. static void SDCardRemoved();
  167. /// Marlin informed us about a bad SD Card.
  168. static void SDCardError();
  169. #endif
  170. // OK Button the Confirm screen.
  171. static void ScreenConfirmedOK(DGUS_VP_Variable &var, void *val_ptr);
  172. // Update data after went to new screen (by display or by GotoScreen)
  173. // remember: store the last-displayed screen, so it can get returned to.
  174. // (e.g for pop up messages)
  175. static void UpdateNewScreen(DGUSLCD_Screens newscreen, bool popup=false);
  176. // Recall the remembered screen.
  177. static void PopToOldScreen();
  178. // Make the display show the screen and update all VPs in it.
  179. static void GotoScreen(DGUSLCD_Screens screen, bool ispopup = false);
  180. static void UpdateScreenVPData();
  181. // Helpers to convert and transfer data to the display.
  182. static void DGUSLCD_SendWordValueToDisplay(DGUS_VP_Variable &var);
  183. static void DGUSLCD_SendStringToDisplay(DGUS_VP_Variable &var);
  184. static void DGUSLCD_SendStringToDisplayPGM(DGUS_VP_Variable &var);
  185. static void DGUSLCD_SendTemperaturePID(DGUS_VP_Variable &var);
  186. static void DGUSLCD_SendPercentageToDisplay(DGUS_VP_Variable &var);
  187. static void DGUSLCD_SendPrintProgressToDisplay(DGUS_VP_Variable &var);
  188. static void DGUSLCD_SendPrintTimeToDisplay(DGUS_VP_Variable &var);
  189. #if ENABLED(PRINTCOUNTER)
  190. static void DGUSLCD_SendPrintAccTimeToDisplay(DGUS_VP_Variable &var);
  191. static void DGUSLCD_SendPrintsTotalToDisplay(DGUS_VP_Variable &var);
  192. #endif
  193. #if FAN_COUNT > 0
  194. static void DGUSLCD_SendFanStatusToDisplay(DGUS_VP_Variable &var);
  195. #endif
  196. static void DGUSLCD_SendHeaterStatusToDisplay(DGUS_VP_Variable &var);
  197. #if ENABLED(DGUS_UI_WAITING)
  198. static void DGUSLCD_SendWaitingStatusToDisplay(DGUS_VP_Variable &var);
  199. #endif
  200. /// Send a value from 0..100 to a variable with a range from 0..255
  201. static void DGUSLCD_PercentageToUint8(DGUS_VP_Variable &var, void *val_ptr);
  202. template<typename T>
  203. static void DGUSLCD_SetValueDirectly(DGUS_VP_Variable &var, void *val_ptr) {
  204. if (!var.memadr) return;
  205. union { unsigned char tmp[sizeof(T)]; T t; } x;
  206. unsigned char *ptr = (unsigned char*)val_ptr;
  207. for (uint8_t i = 0; i < sizeof(T); i++) x.tmp[i] = ptr[sizeof(T) - i - 1];
  208. *(T*)var.memadr = x.t;
  209. }
  210. /// Send a float value to the display.
  211. /// Display will get a 4-byte integer scaled to the number of digits:
  212. /// Tell the display the number of digits and it cheats by displaying a dot between...
  213. template<unsigned int decimals>
  214. static void DGUSLCD_SendFloatAsLongValueToDisplay(DGUS_VP_Variable &var) {
  215. if (var.memadr) {
  216. float f = *(float *)var.memadr;
  217. f *= cpow(10, decimals);
  218. union { long l; char lb[4]; } endian;
  219. char tmp[4];
  220. endian.l = f;
  221. tmp[0] = endian.lb[3];
  222. tmp[1] = endian.lb[2];
  223. tmp[2] = endian.lb[1];
  224. tmp[3] = endian.lb[0];
  225. dgusdisplay.WriteVariable(var.VP, tmp, 4);
  226. }
  227. }
  228. /// Send a float value to the display.
  229. /// Display will get a 2-byte integer scaled to the number of digits:
  230. /// Tell the display the number of digits and it cheats by displaying a dot between...
  231. template<unsigned int decimals>
  232. static void DGUSLCD_SendFloatAsIntValueToDisplay(DGUS_VP_Variable &var) {
  233. if (var.memadr) {
  234. float f = *(float *)var.memadr;
  235. DEBUG_ECHOLNPAIR_F(" >> ", f, 6);
  236. f *= cpow(10, decimals);
  237. union { int16_t i; char lb[2]; } endian;
  238. char tmp[2];
  239. endian.i = f;
  240. tmp[0] = endian.lb[1];
  241. tmp[1] = endian.lb[0];
  242. dgusdisplay.WriteVariable(var.VP, tmp, 2);
  243. }
  244. }
  245. /// Force an update of all VP on the current screen.
  246. static inline void ForceCompleteUpdate() { update_ptr = 0; ScreenComplete = false; }
  247. /// Has all VPs sent to the screen
  248. static inline bool IsScreenComplete() { return ScreenComplete; }
  249. static inline DGUSLCD_Screens getCurrentScreen() { return current_screen; }
  250. static inline void SetupConfirmAction( void (*f)()) { confirm_action_cb = f; }
  251. private:
  252. static DGUSLCD_Screens current_screen; ///< currently on screen
  253. static constexpr uint8_t NUM_PAST_SCREENS = 4;
  254. static DGUSLCD_Screens past_screens[NUM_PAST_SCREENS]; ///< LIFO with past screens for the "back" button.
  255. static uint8_t update_ptr; ///< Last sent entry in the VPList for the actual screen.
  256. static uint16_t skipVP; ///< When updating the screen data, skip this one, because the user is interacting with it.
  257. static bool ScreenComplete; ///< All VPs sent to screen?
  258. static uint16_t ConfirmVP; ///< context for confirm screen (VP that will be emulated-sent on "OK").
  259. #if ENABLED(SDSUPPORT)
  260. static int16_t top_file; ///< file on top of file chooser
  261. static int16_t file_to_print; ///< touched file to be confirmed
  262. #endif
  263. static void (*confirm_action_cb)();
  264. };
  265. extern DGUSScreenVariableHandler ScreenHandler;
  266. /// Find the flash address of a DGUS_VP_Variable for the VP.
  267. extern const DGUS_VP_Variable* DGUSLCD_FindVPVar(const uint16_t vp);
  268. /// Helper to populate a DGUS_VP_Variable for a given VP. Return false if not found.
  269. extern bool populate_VPVar(const uint16_t VP, DGUS_VP_Variable * const ramcopy);