My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DGUSDisplay.h 13KB

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