Skip to content

Commit

Permalink
Fix endian DGUS WriteVariable (#18703)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufanyufan authored Jul 19, 2020
1 parent 12ebb71 commit 1c94033
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
21 changes: 21 additions & 0 deletions Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const void*>(&value), sizeof(uint16_t));
}

void DGUSDisplay::WriteVariable(uint16_t adr, int16_t value) {
value = (value & 0xffU) << 8U | (value >> 8U);
WriteVariable(adr, static_cast<const void*>(&value), sizeof(uint16_t));
}

void DGUSDisplay::WriteVariable(uint16_t adr, uint8_t value) {
WriteVariable(adr, static_cast<const void*>(&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<const void*>(&tmp), sizeof(long));
}

void DGUSDisplay::WriteVariablePGM(uint16_t adr, const void* values, uint8_t valueslen, bool isstr) {
const char* myvalues = static_cast<const char*>(values);
bool strend = !myvalues;
Expand Down
3 changes: 3 additions & 0 deletions Marlin/src/lcd/extui/lib/dgus/DGUSDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class DGUSDisplay {
// 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)
Expand Down
18 changes: 7 additions & 11 deletions Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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;
Expand Down
18 changes: 2 additions & 16 deletions Marlin/src/lcd/extui/lib/dgus/DGUSScreenHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,7 @@ class DGUSScreenHandler {
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);
}
}

Expand All @@ -205,13 +197,7 @@ class DGUSScreenHandler {
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);
}
}

Expand Down

0 comments on commit 1c94033

Please sign in to comment.