From 464c3eb0edb2b9b3d31ba8c2ace2038760531a03 Mon Sep 17 00:00:00 2001 From: Sebastian M Date: Mon, 13 Feb 2023 00:10:52 +0100 Subject: [PATCH] Long floats are displayed correctly on 7 segment (#1143) --- MobiFlight/MobiFlightLedModule.cs | 59 ++++++++++++++++++- .../MobiFlight/MobiFlightLedModuleTests.cs | 19 ++++-- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/MobiFlight/MobiFlightLedModule.cs b/MobiFlight/MobiFlightLedModule.cs index 4e8e8aef9..46ccaeb79 100644 --- a/MobiFlight/MobiFlightLedModule.cs +++ b/MobiFlight/MobiFlightLedModule.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; +using System.Windows.Forms.Design; using CommandMessenger; using Newtonsoft.Json.Linq; using SharpDX.DirectInput; @@ -114,12 +115,64 @@ private string CalculateCorrectLeadingSpaces(string value) return result; } + static public byte CalculatePositionInString(string value, byte mask) + { + // we have to figure it, how many digits are in use + var maskLength = Convert.ToString(mask, 2).Count(c => c == '1'); + + // we have to find the n-th character + // where "n" is the maskLength + // but we have to treat decimal points correctly + // because they don't occupy a digit + // unless two are following each other + // we start with the last character in the value string + byte positionInValue = 0; + + // we set the last character to . + // so that in case we have the first one being a "." + // then we allocate a digit for it correctly + var lastCharacter = '.'; + + // out inital length is 0, because we start on the left + var length = 0; + + for (byte i = 0; i != value.Length; i++) + { + // in case we see a "." but the last character + // was something different, + // then we don't have to account for this + if (value[i] == '.' && lastCharacter != '.') + { + // we store the last character + lastCharacter = value[i]; + continue; + } + + // in all other cases now we count this + // character, length can be different from "i" + length++; + + // we store the last character + lastCharacter = value[i]; + + // once we reach the length + // we update the positionInValue + if (length == maskLength) + { + positionInValue = i; + if (i + 1 < value.Length && value[i + 1] == '.') + positionInValue += 1; + break; + } + } + + return positionInValue; + } + static public byte CalculateCorrectPoints(string value, byte mask) { byte points = 0; - - // we start with the last character in the value string - int positionInValue = value.Length-1; + byte positionInValue = CalculatePositionInString(value, mask); // we go over all 8 potential digits for (byte digit=0; digit<8; digit++) diff --git a/MobiFlightUnitTests/MobiFlight/MobiFlightLedModuleTests.cs b/MobiFlightUnitTests/MobiFlight/MobiFlightLedModuleTests.cs index 718c3027c..39ba32997 100644 --- a/MobiFlightUnitTests/MobiFlight/MobiFlightLedModuleTests.cs +++ b/MobiFlightUnitTests/MobiFlight/MobiFlightLedModuleTests.cs @@ -171,7 +171,7 @@ public void DisplayTest() value = "1.234"; points = 0; - mask = 8+4+2+1; + mask = 8 + 4 + 2 + 1; mockTransport.Clear(); module.Display(0, value, points, mask); WaitForQueueUpdate(); @@ -180,7 +180,7 @@ public void DisplayTest() value = "1.234"; points = 0; - mask = 128+64+32+16; + mask = 128 + 64 + 32 + 16; mockTransport.Clear(); module.Display(0, value, points, mask); WaitForQueueUpdate(); @@ -189,7 +189,7 @@ public void DisplayTest() value = "1.2"; points = 0; - mask = 0+4+0+1; + mask = 0 + 4 + 0 + 1; mockTransport.Clear(); module.Display(0, value, points, mask); WaitForQueueUpdate(); @@ -205,7 +205,7 @@ public void DisplayTest() DataExpected = $"{CommandId},{ModuleIndex},{SubModuleIndex}, 123,8,{mask};"; Assert.AreEqual(DataExpected, mockTransport.DataWrite, "Decimal point not set correctly"); - + value = "........"; points = 0; mask = 255; @@ -214,7 +214,7 @@ public void DisplayTest() WaitForQueueUpdate(); DataExpected = $"{CommandId},{ModuleIndex},{SubModuleIndex}, ,255,{mask};"; Assert.AreEqual(DataExpected, mockTransport.DataWrite, "Decimal point not set correctly"); - + value = "1..2"; points = 0; mask = 0 + 4 + 2 + 1; @@ -233,6 +233,15 @@ public void DisplayTest() WaitForQueueUpdate(); DataExpected = $"{CommandId},{ModuleIndex},{SubModuleIndex},12350,4,{mask};"; Assert.AreEqual(DataExpected, mockTransport.DataWrite, "Decimal point not set correctly"); + + value = "118.805000305176"; + points = 0; + mask = 255; + mockTransport.Clear(); + module.Display(0, value, points, mask); + WaitForQueueUpdate(); + DataExpected = $"{CommandId},{ModuleIndex},{SubModuleIndex},11880500,32,{mask};"; + Assert.AreEqual(DataExpected, mockTransport.DataWrite, "Decimal point not set correctly"); } [TestMethod()]