Example #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);
        }
Example #2
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);
            }
        }