Fix endian DGUS WriteVariable (#18703)

This commit is contained in:
yufanyufan 2020-07-19 15:34:36 -07:00 committed by GitHub
parent 12ebb71155
commit 1c94033f7c
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 27 deletions

View file

@ -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;

View file

@ -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)

View file

@ -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;

View file

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