From 1c94033f7c0ecae200174f251d7cfceab5b7048f Mon Sep 17 00:00:00 2001 From: yufanyufan Date: Sun, 19 Jul 2020 15:34:36 -0700 Subject: [PATCH] Fix endian DGUS WriteVariable (#18703) --- Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.cpp | 21 +++++++++++++++++++ Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.h | 3 +++ .../lcd/extui/lib/dgus/DGUSScreenHandler.cpp | 18 +++++++--------- .../lcd/extui/lib/dgus/DGUSScreenHandler.h | 18 ++-------------- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.cpp b/Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.cpp index 989a06623..fec06290b 100644 --- a/Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.cpp +++ b/Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.cpp @@ -94,9 +94,30 @@ void DGUSDisplay::WriteVariable(uint16_t adr, const void* values, uint8_t values } void DGUSDisplay::WriteVariable(uint16_t adr, uint16_t value) { + value = (value & 0xffU) << 8U | (value >> 8U); WriteVariable(adr, static_cast(&value), sizeof(uint16_t)); } +void DGUSDisplay::WriteVariable(uint16_t adr, int16_t value) { + value = (value & 0xffU) << 8U | (value >> 8U); + WriteVariable(adr, static_cast(&value), sizeof(uint16_t)); +} + +void DGUSDisplay::WriteVariable(uint16_t adr, uint8_t value) { + WriteVariable(adr, static_cast(&value), sizeof(uint8_t)); +} + +void DGUSDisplay::WriteVariable(uint16_t adr, long value) { + union { long l; char lb[4]; } endian; + char tmp[4]; + endian.l = value; + tmp[0] = endian.lb[3]; + tmp[1] = endian.lb[2]; + tmp[2] = endian.lb[1]; + tmp[3] = endian.lb[0]; + WriteVariable(adr, static_cast(&tmp), sizeof(long)); +} + void DGUSDisplay::WriteVariablePGM(uint16_t adr, const void* values, uint8_t valueslen, bool isstr) { const char* myvalues = static_cast(values); bool strend = !myvalues; diff --git a/Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.h b/Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.h index fd8345122..54d68eacd 100644 --- a/Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.h +++ b/Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.h @@ -54,7 +54,10 @@ public: // Variable access. static void WriteVariable(uint16_t adr, const void* values, uint8_t valueslen, bool isstr=false); static void WriteVariablePGM(uint16_t adr, const void* values, uint8_t valueslen, bool isstr=false); + static void WriteVariable(uint16_t adr, int16_t value); static void WriteVariable(uint16_t adr, uint16_t value); + static void WriteVariable(uint16_t adr, uint8_t value); + static void WriteVariable(uint16_t adr, long value); // Until now I did not need to actively read from the display. That's why there is no ReadVariable // (I extensively use the auto upload of the display) diff --git a/Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.cpp b/Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.cpp index 6ff578832..d5958d2b2 100644 --- a/Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.cpp +++ b/Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.cpp @@ -118,10 +118,10 @@ void DGUSScreenHandler::DGUSLCD_SendWordValueToDisplay(DGUS_VP_Variable &var) { if (var.memadr) { //DEBUG_ECHOPAIR(" DGUS_LCD_SendWordValueToDisplay ", var.VP); //DEBUG_ECHOLNPAIR(" data ", *(uint16_t *)var.memadr); - uint8_t *tmp = (uint8_t *) var.memadr; - uint16_t data_to_send = (tmp[0] << 8); - if (var.size >= 1) data_to_send |= tmp[1]; - dgusdisplay.WriteVariable(var.VP, data_to_send); + if (var.size > 1) + dgusdisplay.WriteVariable(var.VP, *(int16_t*)var.memadr); + else + dgusdisplay.WriteVariable(var.VP, *(int8_t*)var.memadr); } } @@ -132,8 +132,7 @@ void DGUSScreenHandler::DGUSLCD_SendPercentageToDisplay(DGUS_VP_Variable &var) { //DEBUG_ECHOLNPAIR(" data ", *(uint16_t *)var.memadr); uint16_t tmp = *(uint8_t *) var.memadr +1 ; // +1 -> avoid rounding issues for the display. tmp = map(tmp, 0, 255, 0, 100); - uint16_t data_to_send = swap16(tmp); - dgusdisplay.WriteVariable(var.VP, data_to_send); + dgusdisplay.WriteVariable(var.VP, tmp); } } @@ -142,8 +141,7 @@ void DGUSScreenHandler::DGUSLCD_SendPrintProgressToDisplay(DGUS_VP_Variable &var //DEBUG_ECHOPAIR(" DGUSLCD_SendPrintProgressToDisplay ", var.VP); uint16_t tmp = ExtUI::getProgress_percent(); //DEBUG_ECHOLNPAIR(" data ", tmp); - uint16_t data_to_send = swap16(tmp); - dgusdisplay.WriteVariable(var.VP, data_to_send); + dgusdisplay.WriteVariable(var.VP, tmp); } // Send the current print time to the display. @@ -242,7 +240,6 @@ void DGUSScreenHandler::DGUSLCD_SendStringToDisplayPGM(DGUS_VP_Variable &var) { DEBUG_ECHOLNPAIR(" data ", *(uint8_t *)var.memadr); uint16_t data_to_send = 0; if (*(uint8_t *) var.memadr) data_to_send = 1; - data_to_send = swap16(data_to_send); dgusdisplay.WriteVariable(var.VP, data_to_send); } } @@ -255,7 +252,6 @@ void DGUSScreenHandler::DGUSLCD_SendHeaterStatusToDisplay(DGUS_VP_Variable &var) DEBUG_ECHOLNPAIR(" data ", *(int16_t *)var.memadr); uint16_t data_to_send = 0; if (*(int16_t *) var.memadr) data_to_send = 1; - data_to_send = swap16(data_to_send); dgusdisplay.WriteVariable(var.VP, data_to_send); } } @@ -268,7 +264,7 @@ void DGUSScreenHandler::DGUSLCD_SendHeaterStatusToDisplay(DGUS_VP_Variable &var) //DEBUG_ECHOPAIR(" DGUSLCD_SendWaitingStatusToDisplay ", var.VP); //DEBUG_ECHOLNPAIR(" data ", swap16(index)); if (period++ > DGUS_UI_WAITING_STATUS_PERIOD) { - dgusdisplay.WriteVariable(var.VP, swap16(index)); + dgusdisplay.WriteVariable(var.VP, index); //DEBUG_ECHOLNPAIR(" data ", swap16(index)); if (++index >= DGUS_UI_WAITING_STATUS) index = 0; period = 0; diff --git a/Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.h b/Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.h index d5b1450e2..a7274fc2f 100644 --- a/Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.h +++ b/Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.h @@ -184,15 +184,7 @@ public: if (var.memadr) { float f = *(float *)var.memadr; f *= cpow(10, decimals); - union { long l; char lb[4]; } endian; - - char tmp[4]; - endian.l = f; - tmp[0] = endian.lb[3]; - tmp[1] = endian.lb[2]; - tmp[2] = endian.lb[1]; - tmp[3] = endian.lb[0]; - dgusdisplay.WriteVariable(var.VP, tmp, 4); + dgusdisplay.WriteVariable(var.VP, (long)f); } } @@ -205,13 +197,7 @@ public: float f = *(float *)var.memadr; DEBUG_ECHOLNPAIR_F(" >> ", f, 6); f *= cpow(10, decimals); - union { int16_t i; char lb[2]; } endian; - - char tmp[2]; - endian.i = f; - tmp[0] = endian.lb[1]; - tmp[1] = endian.lb[0]; - dgusdisplay.WriteVariable(var.VP, tmp, 2); + dgusdisplay.WriteVariable(var.VP, (int16_t)f); } }