Subversion Repositories group.electronics

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
139 pfowler 1
using System;
2
using System.Collections.Generic;
3
using System.Drawing;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Windows.Forms;
8
 
9
namespace NITNavComm {
10
    public class SevenSegmentArray : UserControl {
11
        public SevenSegmentArray() {
12
            this.SuspendLayout();
13
            this.Name = "SevenSegmentArray";
14
            this.Size = new System.Drawing.Size(100, 25);
15
            this.Resize += new System.EventHandler(this.SevenSegmentArray_Resize);
16
            this.ResumeLayout(false);
17
 
18
            this.TabStop = false;
19
            elementPadding = new Padding(4, 4, 4, 4);
20
            RecreateSegments(4);
21
        }
22
 
23
 
24
        /// <summary>
25
        /// Array of segment controls that are currently children of this control.
26
        /// </summary>
27
        private SevenSegment[] segments = null;
28
 
29
        /// <summary>
30
        /// Change the number of elements in our LED array. This destroys
31
        /// the previous elements, and creates new ones in their place, applying
32
        /// all the current options to the new ones.
33
        /// </summary>
34
        /// <param name="count">Number of elements to create.</param>
35
        private void RecreateSegments(int count) {
36
            if (segments != null)
37
                for (int i = 0; i < segments.Length; i++) { segments[i].Parent = null; segments[i].Dispose(); }
38
 
39
            if (count <= 0) return;
40
            segments = new SevenSegment[count];
41
 
42
            for (int i = 0; i < count; i++) {
43
                segments[i] = new SevenSegment();
44
                segments[i].Parent = this;
45
                segments[i].Top = 0;
46
                segments[i].Height = this.Height;
47
                segments[i].Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
48
                segments[i].Visible = true;
49
            }
50
 
51
            ResizeSegments();
52
            UpdateSegments();
53
            this.Value = theValue;
54
        }
55
 
56
        /// <summary>
57
        /// Align the elements of the array to fit neatly within the
58
        /// width of the parent control.
59
        /// </summary>
60
        private void ResizeSegments() {
61
            int segWidth = this.Width / segments.Length;
62
            for (int i = 0; i < segments.Length; i++) {
63
                segments[i].Left = this.Width * (segments.Length - 1 - i) / segments.Length;
64
                segments[i].Width = segWidth;
65
            }
66
        }
67
 
68
        /// <summary>
69
        /// Update the properties of each element with the properties
70
        /// we have stored.
71
        /// </summary>
72
        private void UpdateSegments() {
73
            for (int i = 0; i < segments.Length; i++) {
74
                segments[i].ColorBackground = colorBackground;
75
                segments[i].ColorDark = colorDark;
76
                segments[i].ColorLight = colorLight;
77
                segments[i].ElementWidth = elementWidth;
78
                segments[i].ItalicFactor = italicFactor;
79
                segments[i].DecimalShow = showDot;
80
                segments[i].Padding = elementPadding;
81
            }
82
        }
83
 
84
        private void SevenSegmentArray_Resize(object sender, EventArgs e) { ResizeSegments(); }
85
 
86
        protected override void OnPaintBackground(PaintEventArgs e) { e.Graphics.Clear(colorBackground); }
87
 
88
 
89
        private int elementWidth = 10;
90
        private float italicFactor = 0.0F;
91
        private Color colorBackground = Color.DarkGray;
92
        private Color colorDark = Color.DimGray;
93
        private Color colorLight = Color.Red;
94
        private bool showDot = true;
95
        private Padding elementPadding;
96
 
97
        /// <summary>
98
        /// Background color of the LED array.
99
        /// </summary>
100
        public Color ColorBackground { get { return colorBackground; } set { colorBackground = value; UpdateSegments(); } }
101
        /// <summary>
102
        /// Color of inactive LED segments.
103
        /// </summary>
104
        public Color ColorDark { get { return colorDark; } set { colorDark = value; UpdateSegments(); } }
105
        /// <summary>
106
        /// Color of active LED segments.
107
        /// </summary>
108
        public Color ColorLight { get { return colorLight; } set { colorLight = value; UpdateSegments(); } }
109
 
110
        /// <summary>
111
        /// Width of LED segments.
112
        /// </summary>
113
        public int ElementWidth { get { return elementWidth; } set { elementWidth = value; UpdateSegments(); } }
114
        /// <summary>
115
        /// Shear coefficient for italicizing the displays. Try a value like -0.1.
116
        /// </summary>
117
        public float ItalicFactor { get { return italicFactor; } set { italicFactor = value; UpdateSegments(); } }
118
        /// <summary>
119
        /// Specifies if the decimal point LED is displayed.
120
        /// </summary>
121
        public bool DecimalShow { get { return showDot; } set { showDot = value; UpdateSegments(); } }
122
 
123
        /// <summary>
124
        /// Number of seven-segment elements in this array.
125
        /// </summary>
126
        public int ArrayCount { get { return segments.Length; } set { if ((value > 0) && (value <= 100)) RecreateSegments(value); } }
127
        /// <summary>
128
        /// Padding that applies to each seven-segment element in the array.
129
        /// Tweak these numbers to get the perfect appearance for the array of your size.
130
        /// </summary>
131
        public Padding ElementPadding { get { return elementPadding; } set { elementPadding = value; UpdateSegments(); } }
132
 
133
 
134
        private string theValue = null;
135
        /// <summary>
136
        /// The value to be displayed on the LED array. This can contain numbers,
137
        /// certain letters, and decimal points.
138
        /// </summary>
139
        public string Value {
140
            get { return theValue; }
141
            set {
142
                theValue = value;
143
                for (int i = 0; i < segments.Length; i++) { segments[i].CustomPattern = 0; segments[i].DecimalOn = false; }
144
                if (theValue != null) {
145
                    int segmentIndex = 0;
146
                    for (int i = theValue.Length - 1; i >= 0; i--) {
147
                        if (segmentIndex >= segments.Length) break;
148
                        if (theValue[i] == '.') segments[segmentIndex].DecimalOn = true;
149
                        else segments[segmentIndex++].Value = theValue[i].ToString();
150
                    }
151
                }
152
            }
153
        }
154
 
155
    }
156
}