Beispiel #1
0
        public static DisplayFrame FromDecimal(decimal value, StatusLed signLed)
        {
            if (value < MinDecimalValue || value > MaxDecimalValue)
            {
                throw new ArgumentOutOfRangeException(nameof(value));
            }

            bool sign = value < 0m;

            value = Math.Abs(value);

            int integerDigits    = value.NumIntegerDigits();
            int fractionalDigits = value.NumFractionalDigits();
            int totalDigits      = integerDigits + fractionalDigits;

            if (totalDigits > CharacterCount)
            {
                value            = Decimal.Round(value, CharacterCount - integerDigits);
                totalDigits      = CharacterCount;
                fractionalDigits = totalDigits - integerDigits;
            }

            var frame = new DisplayFrame();

            for (int i = 0; i < integerDigits; i++)
            {
                bool dot       = (i == integerDigits - 1) && (fractionalDigits > 0);
                var  character = DisplayCharacter.FromDigit(value.GetDigit(integerDigits - i - 1), dot);
                frame = frame.WithCharacter(totalDigits - i - 1, character);
            }

            for (int i = 0; i < fractionalDigits; i++)
            {
                var character = DisplayCharacter.FromDigit(value.GetDigit(-i - 1));
                frame = frame.WithCharacter(fractionalDigits - i - 1, character);
            }

            if (sign)
            {
                if (totalDigits == CharacterCount)
                {
                    frame = frame.WithStatus(signLed, true);
                }
                else
                {
                    var character = DisplayCharacter.FromSymbol('-');
                    frame = frame.WithCharacter(totalDigits, character);
                }
            }

            return(frame);
        }
Beispiel #2
0
        public DisplayFrame WithCharacter(int index, DisplayCharacter character)
        {
            character.ThrowIfNull(nameof(character));
            if (index < 0 || index > CharacterCount)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            var newCharacters = _characters.ToArray();

            newCharacters[index] = character;
            return(new DisplayFrame(newCharacters, _statusLeds));
        }
Beispiel #3
0
        public static IEnumerable <DisplayCharacter> GetString(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                DisplayCharacter current = DisplayCharacter.FromSymbol(str[i]);

                // Special case: lookahead for dot and combine it if necessary
                if (i < str.Length - 1 && str[i + 1] == '.' && !current.HasDot)
                {
                    current = current.WithDot();
                    i++;
                }

                yield return(current);
            }
        }
Beispiel #4
0
        public IEnumerator <DisplayFrame> GetEnumerator()
        {
            int index          = 0;
            int totalPositions = 8 + (DisplayFrame.CharacterCount - 2) * 2;

            for (;;)
            {
                (byte value, int characterIndex) = GetValueAndCharacterIndex(index, totalPositions);
                var character = DisplayCharacter.FromRaw(value);
                yield return(new DisplayFrame().WithCharacter(characterIndex, character));

                index++;
                if (index >= totalPositions)
                {
                    index = 0;
                }
            }
        }
Beispiel #5
0
 /// <summary>
 /// Creates a new frame where one existing character is pushed out to make room for another.
 /// This can be called many times in succession to create a "scrolling" effect.
 /// </summary>
 /// <param name="pushFromLeft">If true, the new character is pushed in from the left and the other two characters will be pushed to the right.</param>
 public DisplayFrame WithPushedCharacter(DisplayCharacter character, bool pushFromLeft = false)
 {
     DisplayCharacter[] newCharacters;
     if (pushFromLeft)
     {
         newCharacters = new DisplayCharacter[]
         {
             _characters[1],
             _characters[2],
             character
         };
     }
     else
     {
         newCharacters = new DisplayCharacter[]
         {
             character,
             _characters[0],
             _characters[1]
         };
     }
     return(new DisplayFrame(newCharacters, _statusLeds));
 }
Beispiel #6
0
 private DisplayFrame(DisplayCharacter defaultCharacter, bool defaultStatus)
     : this(Enumerable.Repeat(defaultCharacter, CharacterCount), defaultStatus)
 {
 }