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

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