@@ -106,6 +106,27 @@ void DGUSDisplay::writeVariable(uint16_t adr, const void *values, uint8_t values
106106 }
107107}
108108
109+ #if DGUS_LCD_UI_CREALITY_TOUCH
110+
111+ DGUS_ScreenID DGUSDisplay::displayRequest = DGUS_SCREEN_BOOT;
112+
113+ void DGUSDisplay::readCurrentScreen () { readVariable (0x14 /* PIC_NOW*/ ); }
114+
115+ void DGUSDisplay::resetDisplay () {
116+ DEBUG_ECHOLNPGM (" resetDisplay" );
117+ const unsigned char resetCommand[] = { 0x55 , 0xAA , 0x5A , 0xA5 };
118+ writeVariable (0x04 , resetCommand, sizeof (resetCommand));
119+ }
120+
121+ void DGUSDisplay::readVariable (uint16_t adr) {
122+ writeHeader (adr, DGUS_CMD_READVAR, sizeof (uint8_t ));
123+
124+ // Specify to read one byte
125+ LCD_SERIAL.write (static_cast <uint8_t >(1 ));
126+ }
127+
128+ #endif // DWIN_CREALITY_TOUCHLCD
129+
109130void DGUSDisplay::writeVariable (uint16_t adr, uint16_t value) {
110131 value = (value & 0xFFU ) << 8U | (value >> 8U );
111132 writeVariable (adr, static_cast <const void *>(&value), sizeof (uint16_t ));
@@ -204,11 +225,48 @@ void DGUSDisplay::processRx() {
204225 | Command DataLen (in Words) */
205226 if (command == DGUS_CMD_READVAR) {
206227 const uint16_t vp = tmp[0 ] << 8 | tmp[1 ];
207- DGUS_VP_Variable ramcopy;
208- if (populate_VPVar (vp, &ramcopy)) {
209- if (ramcopy.set_by_display_handler )
210- ramcopy.set_by_display_handler (ramcopy, &tmp[3 ]);
211- }
228+
229+ #if DGUS_LCD_UI_CREALITY_TOUCH
230+
231+ if (vp == 0x14 /* PIC_Now*/ ) {
232+ const uint16_t screen_id = tmp[3 ] << 8 | tmp[4 ];
233+ // In the code below DGUS_SCREEN_BOOT acts as a sentinel
234+ if (screen_id == 255 ) {
235+ // DGUS OS sometimes randomly sends 255 back as an answer. Possible buffer overrun?
236+ readCurrentScreen (); // Request again
237+ }
238+ else if (displayRequest != DGUS_SCREEN_BOOT && screen_id != displayRequest) {
239+ // A display was requested. If the screen didn't yet switch to that display, we won't give that value back, otherwise the code gets confused.
240+ // The DWIN display mostly honours the PIC_SET requests from the firmware, so after a while we may want to nudge it to the correct screen
241+ DEBUG_ECHOPGM (" Got a response on the current screen: " , screen_id);
242+ DEBUG_ECHOLNPGM (" - however, we've requested screen " , displayRequest);
243+ }
244+ else {
245+ displayRequest = DGUS_SCREEN_BOOT;
246+ if (current_screen_update_callback)
247+ current_screen_update_callback (static_cast <DGUS_ScreenID>(screen_id));
248+ }
249+ }
250+ else {
251+ DGUS_VP_Variable ramcopy;
252+ if (populate_VPVar (vp, &ramcopy)) {
253+ if (ramcopy.set_by_display_handler )
254+ ramcopy.set_by_display_handler (ramcopy, &tmp[3 ]);
255+ }
256+
257+ // Always ask for a screen update so we can send a screen update earlier, this prevents a flash of unstyled screen
258+ readCurrentScreen ();
259+ }
260+
261+ #else // !DWIN_CREALITY_TOUCHLCD
262+
263+ DGUS_VP_Variable ramcopy;
264+ if (populate_VPVar (vp, &ramcopy)) {
265+ if (ramcopy.set_by_display_handler )
266+ ramcopy.set_by_display_handler (ramcopy, &tmp[3 ]);
267+ }
268+
269+ #endif
212270
213271 rx_datagram_state = DGUS_IDLE;
214272 break ;
0 commit comments