Skip to content

Commit c699a12

Browse files
committed
Workaround DWIN touch screen truncating random numbers #217
1 parent 9efb5cf commit c699a12

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

Marlin/src/lcd/extui/lib/dgus_creality/DGUSScreenHandler.cpp

+25-4
Original file line numberDiff line numberDiff line change
@@ -903,12 +903,13 @@ uint16_t CreateRgb(double h, double s, double v) {
903903
void DGUSScreenHandler::UpdateMeshValue(const int8_t x, const int8_t y, const float z) {
904904
SERIAL_ECHOPAIR("X", x);
905905
SERIAL_ECHOPAIR(" Y", y);
906-
SERIAL_ECHOPAIR_F_P(" Z", z);
907-
SERIAL_ECHOLN("");
906+
SERIAL_ECHO(" Z");
907+
SERIAL_ECHO_F(z, 4);
908908

909909
// Determine the screen X and Y value
910910
if (x % SkipMeshPoint != 0 || y % SkipMeshPoint != 0) {
911911
// Skip this point
912+
SERIAL_ECHOLN("");
912913
return;
913914
}
914915

@@ -917,7 +918,26 @@ void DGUSScreenHandler::UpdateMeshValue(const int8_t x, const int8_t y, const fl
917918

918919
// Each Y is a full edge of X values
919920
const uint16_t vpAddr = VP_MESH_LEVEL_X0_Y0 + (scrY * MESH_LEVEL_VP_SIZE) + (scrX * MESH_LEVEL_VP_EDGE_SIZE);
920-
dgusdisplay.WriteVariable(vpAddr, z);
921+
922+
// ... DWIN is inconsistently truncating floats. Examples: 0.1811 becomes 0.181, 0.1810 becomes 0.180. But 0.1800 is not 0.179
923+
// so we need to calculate a good number here that will not overflow
924+
float displayZ = z;
925+
926+
{
927+
constexpr float correctionFactor = 0.0001;
928+
929+
if (round(z * cpow(10,3)) == round((z + correctionFactor) * cpow(10,3))) {
930+
// If we don't accidently overshoot to the next number, trick the display by upping the number 0.0001 💩
931+
displayZ += correctionFactor;
932+
933+
SERIAL_ECHO(" displayZ: ");
934+
SERIAL_ECHO_F(z, 4);
935+
}
936+
}
937+
938+
SERIAL_ECHOLN("");
939+
940+
dgusdisplay.WriteVariable(vpAddr, displayZ);
921941

922942
// Set color
923943
const uint16_t spAddr = SP_MESH_LEVEL_X0_Y0 + (scrY * MESH_LEVEL_SP_SIZE) + (scrX * MESH_LEVEL_SP_EDGE_SIZE);
@@ -958,7 +978,8 @@ void DGUSScreenHandler::HandleMeshPoint(DGUS_VP_Variable &var, void *val_ptr) {
958978

959979
SERIAL_ECHOPAIR("Overriding mesh value. X:", x);
960980
SERIAL_ECHOPAIR(" Y:", y);
961-
SERIAL_ECHOPAIR_F(" Z:", z);
981+
SERIAL_ECHO(" Z:");
982+
SERIAL_ECHO_F(z, 4);
962983
SERIAL_ECHOPAIR(" [raw: ", rawZ);
963984
SERIAL_ECHOPAIR("] [point ", probe_point, "] ");
964985
SERIAL_ECHOPAIR(" [VP: ", var.VP);

0 commit comments

Comments
 (0)