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
No known key found for this signature in database
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) { 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)); 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) { void DGUSDisplay::WriteVariablePGM(uint16_t adr, const void* values, uint8_t valueslen, bool isstr) {
const char* myvalues = static_cast<const char*>(values); const char* myvalues = static_cast<const char*>(values);
bool strend = !myvalues; bool strend = !myvalues;

View File

@ -54,7 +54,10 @@ public:
// Variable access. // Variable access.
static void WriteVariable(uint16_t adr, const void* values, uint8_t valueslen, bool isstr=false); 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 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, 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 // 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) // (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) { if (var.memadr) {
//DEBUG_ECHOPAIR(" DGUS_LCD_SendWordValueToDisplay ", var.VP); //DEBUG_ECHOPAIR(" DGUS_LCD_SendWordValueToDisplay ", var.VP);
//DEBUG_ECHOLNPAIR(" data ", *(uint16_t *)var.memadr); //DEBUG_ECHOLNPAIR(" data ", *(uint16_t *)var.memadr);
uint8_t *tmp = (uint8_t *) var.memadr; if (var.size > 1)
uint16_t data_to_send = (tmp[0] << 8); dgusdisplay.WriteVariable(var.VP, *(int16_t*)var.memadr);
if (var.size >= 1) data_to_send |= tmp[1]; else
dgusdisplay.WriteVariable(var.VP, data_to_send); 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); //DEBUG_ECHOLNPAIR(" data ", *(uint16_t *)var.memadr);
uint16_t tmp = *(uint8_t *) var.memadr +1 ; // +1 -> avoid rounding issues for the display. uint16_t tmp = *(uint8_t *) var.memadr +1 ; // +1 -> avoid rounding issues for the display.
tmp = map(tmp, 0, 255, 0, 100); tmp = map(tmp, 0, 255, 0, 100);
uint16_t data_to_send = swap16(tmp); dgusdisplay.WriteVariable(var.VP, tmp);
dgusdisplay.WriteVariable(var.VP, data_to_send);
} }
} }
@ -142,8 +141,7 @@ void DGUSScreenHandler::DGUSLCD_SendPrintProgressToDisplay(DGUS_VP_Variable &var
//DEBUG_ECHOPAIR(" DGUSLCD_SendPrintProgressToDisplay ", var.VP); //DEBUG_ECHOPAIR(" DGUSLCD_SendPrintProgressToDisplay ", var.VP);
uint16_t tmp = ExtUI::getProgress_percent(); uint16_t tmp = ExtUI::getProgress_percent();
//DEBUG_ECHOLNPAIR(" data ", tmp); //DEBUG_ECHOLNPAIR(" data ", tmp);
uint16_t data_to_send = swap16(tmp); dgusdisplay.WriteVariable(var.VP, tmp);
dgusdisplay.WriteVariable(var.VP, data_to_send);
} }
// Send the current print time to the display. // 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); DEBUG_ECHOLNPAIR(" data ", *(uint8_t *)var.memadr);
uint16_t data_to_send = 0; uint16_t data_to_send = 0;
if (*(uint8_t *) var.memadr) data_to_send = 1; if (*(uint8_t *) var.memadr) data_to_send = 1;
data_to_send = swap16(data_to_send);
dgusdisplay.WriteVariable(var.VP, 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); DEBUG_ECHOLNPAIR(" data ", *(int16_t *)var.memadr);
uint16_t data_to_send = 0; uint16_t data_to_send = 0;
if (*(int16_t *) var.memadr) data_to_send = 1; if (*(int16_t *) var.memadr) data_to_send = 1;
data_to_send = swap16(data_to_send);
dgusdisplay.WriteVariable(var.VP, 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_ECHOPAIR(" DGUSLCD_SendWaitingStatusToDisplay ", var.VP);
//DEBUG_ECHOLNPAIR(" data ", swap16(index)); //DEBUG_ECHOLNPAIR(" data ", swap16(index));
if (period++ > DGUS_UI_WAITING_STATUS_PERIOD) { if (period++ > DGUS_UI_WAITING_STATUS_PERIOD) {
dgusdisplay.WriteVariable(var.VP, swap16(index)); dgusdisplay.WriteVariable(var.VP, index);
//DEBUG_ECHOLNPAIR(" data ", swap16(index)); //DEBUG_ECHOLNPAIR(" data ", swap16(index));
if (++index >= DGUS_UI_WAITING_STATUS) index = 0; if (++index >= DGUS_UI_WAITING_STATUS) index = 0;
period = 0; period = 0;

View File

@ -184,15 +184,7 @@ public:
if (var.memadr) { if (var.memadr) {
float f = *(float *)var.memadr; float f = *(float *)var.memadr;
f *= cpow(10, decimals); f *= cpow(10, decimals);
union { long l; char lb[4]; } endian; dgusdisplay.WriteVariable(var.VP, (long)f);
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);
} }
} }
@ -205,13 +197,7 @@ public:
float f = *(float *)var.memadr; float f = *(float *)var.memadr;
DEBUG_ECHOLNPAIR_F(" >> ", f, 6); DEBUG_ECHOLNPAIR_F(" >> ", f, 6);
f *= cpow(10, decimals); f *= cpow(10, decimals);
union { int16_t i; char lb[2]; } endian; dgusdisplay.WriteVariable(var.VP, (int16_t)f);
char tmp[2];
endian.i = f;
tmp[0] = endian.lb[1];
tmp[1] = endian.lb[0];
dgusdisplay.WriteVariable(var.VP, tmp, 2);
} }
} }