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

DGUSDisplay.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2021 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 "config/DGUS_Screen.h"
  25. #include "config/DGUS_Control.h"
  26. #include "definition/DGUS_VP.h"
  27. #include "../../../inc/MarlinConfigPre.h"
  28. #include "../../../MarlinCore.h"
  29. #define DEBUG_OUT ENABLED(DEBUG_DGUSLCD)
  30. #include "../../../core/debug_out.h"
  31. #define Swap16(val) ((uint16_t)(((uint16_t)(val) >> 8) |\
  32. ((uint16_t)(val) << 8)))
  33. // Low-Level access to the display.
  34. class DGUSDisplay {
  35. public:
  36. enum DGUS_ControlType : uint8_t {
  37. VARIABLE_DATA_INPUT = 0x00,
  38. POPUP_WINDOW = 0x01,
  39. INCREMENTAL_ADJUST = 0x02,
  40. SLIDER_ADJUST = 0x03,
  41. RTC_SETTINGS = 0x04,
  42. RETURN_KEY_CODE = 0x05,
  43. TEXT_INPUT = 0x06,
  44. FIRMWARE_SETTINGS = 0x07
  45. };
  46. DGUSDisplay() = default;
  47. static void Init();
  48. static void Read(uint16_t addr, uint8_t size);
  49. static void Write(uint16_t addr, const void* data_ptr, uint8_t size);
  50. static void WriteString(uint16_t addr, const void* data_ptr, uint8_t size, bool left = true, bool right = false, bool use_space = true);
  51. static void WriteStringPGM(uint16_t addr, const void* data_ptr, uint8_t size, bool left = true, bool right = false, bool use_space = true);
  52. template<typename T>
  53. static void Write(uint16_t addr, T data) {
  54. Write(addr, static_cast<const void*>(&data), sizeof(T));
  55. }
  56. // Until now I did not need to actively read from the display. That's why there is no ReadVariable
  57. // (I extensively use the auto upload of the display)
  58. // Force display into another screen.
  59. static void SwitchScreen(DGUS_Screen screen);
  60. // Play sounds using the display speaker.
  61. // start: position at which the sound was stored on the display.
  62. // len: how many sounds to play. Sounds will play consecutively from start to start+len-1.
  63. // volume: playback volume. 0 keeps the current volume.
  64. static void PlaySound(uint8_t start, uint8_t len = 1, uint8_t volume = 0);
  65. // Enable/disable a specific touch control.
  66. // type: control type.
  67. // control: index of the control on the page (set during screen development).
  68. static void EnableControl(DGUS_Screen screen, DGUS_ControlType type, DGUS_Control control);
  69. static void DisableControl(DGUS_Screen screen, DGUS_ControlType type, DGUS_Control control);
  70. static uint8_t GetBrightness();
  71. static uint8_t GetVolume();
  72. // Set the display brightness/volume, ranging 0 - 100
  73. static void SetBrightness(uint8_t brightness);
  74. static void SetVolume(uint8_t volume);
  75. // Periodic tasks, eg. Rx-Queue handling.
  76. static void Loop();
  77. // Helper for users of this class to estimate if an interaction would be blocking.
  78. static size_t GetFreeTxBuffer();
  79. static void FlushTx();
  80. // Checks two things: Can we confirm the presence of the display and has we initiliazed it.
  81. // (both boils down that the display answered to our chatting)
  82. static inline bool IsInitialized() {
  83. return initialized;
  84. }
  85. static uint8_t gui_version;
  86. static uint8_t os_version;
  87. template<typename T>
  88. static T SwapBytes(const T value) {
  89. union {
  90. T val;
  91. char byte[sizeof(T)];
  92. } src, dst;
  93. src.val = value;
  94. LOOP_L_N(i, sizeof(T)) dst.byte[i] = src.byte[sizeof(T) - i - 1];
  95. return dst.val;
  96. }
  97. template<typename T_in, typename T_out, uint8_t decimals>
  98. T_out FromFixedPoint(const T_in value) {
  99. return (T_out)((float)value / POW(10, decimals));
  100. }
  101. template<typename T_in, typename T_out, uint8_t decimals>
  102. T_out ToFixedPoint(const T_in value) {
  103. return (T_out)LROUND((float)value * POW(10, decimals));
  104. }
  105. private:
  106. enum dgus_header : uint8_t {
  107. DGUS_HEADER1 = 0x5A,
  108. DGUS_HEADER2 = 0xA5
  109. };
  110. enum dgus_command : uint8_t {
  111. DGUS_WRITEVAR = 0x82,
  112. DGUS_READVAR = 0x83
  113. };
  114. enum rx_datagram_state_t : uint8_t {
  115. DGUS_IDLE, //< waiting for DGUS_HEADER1.
  116. DGUS_HEADER1_SEEN, //< DGUS_HEADER1 received
  117. DGUS_HEADER2_SEEN, //< DGUS_HEADER2 received
  118. DGUS_WAIT_TELEGRAM, //< LEN received, Waiting for to receive all bytes.
  119. };
  120. enum dgus_system_addr : uint16_t {
  121. DGUS_VERSION = 0x000f // OS/GUI version
  122. };
  123. static void WriteHeader(uint16_t addr, uint8_t command, uint8_t len);
  124. static void ProcessRx();
  125. static uint8_t volume;
  126. static uint8_t brightness;
  127. static rx_datagram_state_t rx_datagram_state;
  128. static uint8_t rx_datagram_len;
  129. static bool initialized;
  130. };
  131. template<> inline uint16_t DGUSDisplay::SwapBytes(const uint16_t value) {
  132. return ((value << 8) | (value >> 8));
  133. }
  134. extern DGUSDisplay dgus_display;
  135. /// Helper to populate a DGUS_VP for a given VP. Return false if not found.
  136. extern bool DGUS_PopulateVP(const DGUS_Addr addr, DGUS_VP * const buffer);