Skip to content

Commit

Permalink
Long floats are displayed correctly on 7 segment (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
DocMoebiuz authored Feb 12, 2023
1 parent a17cf88 commit 464c3eb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
59 changes: 56 additions & 3 deletions MobiFlight/MobiFlightLedModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++)
Expand Down
19 changes: 14 additions & 5 deletions MobiFlightUnitTests/MobiFlight/MobiFlightLedModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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()]
Expand Down

0 comments on commit 464c3eb

Please sign in to comment.