Subversion Repositories group.electronics

Rev

Rev 155 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
139 pfowler 1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Windows.Forms;
10
 
11
namespace NITNavComm {
12
    public partial class NITCommNavForm : Form {
13
 
14
        private NITNavCommDevice navcomm = null;
15
 
16
        public NITCommNavForm() {
17
            InitializeComponent();
18
 
19
            this.controlStatus(false);
153 pfowler 20
            txtCount.Text = "0";
139 pfowler 21
        }
22
 
23
        private void controlStatus(bool status) {
24
            cmdSend0.Enabled = status;
25
            cmdSend1.Enabled = status;
26
            cmdUpdate.Enabled = status;
27
            inputTimer.Enabled = status;
28
        }
29
 
30
        public void UpdateDevice() {
31
            this.UpdateDisplay();
32
            this.UpdateInput();
33
        }
34
 
153 pfowler 35
        public void UpdateDisplay(byte dis) {
139 pfowler 36
            byte[] data;
153 pfowler 37
            if (dis == 0) {
139 pfowler 38
                data = navcomm.getFreq(0);
39
                freq0.Value = this.byteArraytoString(ref data, 5);
40
                data = navcomm.getFreq(1);
41
                freq1.Value = this.byteArraytoString(ref data, 5);
42
            } else {
43
                data = navcomm.getFreq(2);
44
                freq2.Value = this.byteArraytoString(ref data, 5);
45
                data = navcomm.getFreq(3);
46
                freq3.Value = this.byteArraytoString(ref data, 5);
47
            }
153 pfowler 48
            navcomm.UpdateDisplay(dis);
139 pfowler 49
        }
50
 
51
        public void UpdateDisplay() {
52
            this.UpdateDisplay(0);
53
            this.UpdateDisplay(1);
54
        }
55
 
56
        public void UpdateInput() {
57
            this.UpdateInput(0);
58
        }
59
 
60
        public void UpdateInput(byte display) {
61
            navcomm.updateInput();
62
            cmdSwap0.Checked = navcomm.isSwapSet(0);
63
            cmdFlip0.Checked = navcomm.isFlipSet(0);
64
            txtRot0.Text = navcomm.getRotary(0, 0).ToString("X");
149 pfowler 65
 
66
            cmdSwap1.Checked = navcomm.isSwapSet(1);
67
            cmdFlip1.Checked = navcomm.isFlipSet(1);
68
            txtRot1.Text = navcomm.getRotary(1, 0).ToString("X");
139 pfowler 69
        }
70
 
71
        private string byteArraytoString(ref byte[] data, byte len) {
72
            StringBuilder sb = new StringBuilder();
73
            for (byte i = 0; i < len; i++) {
74
                if (data[i] == 0x0a)
75
                    sb.Append(' ');
76
                else
77
                    sb.Append((byte)(data[i]));
78
            }
79
            return sb.ToString();
80
        }
81
 
82
        public void setDevice(NITNavCommDevice navcomm) {
83
            this.navcomm = navcomm;
84
 
85
            if (!navcomm.isOpen())
86
                navcomm.Open();
87
 
88
            if (navcomm.isOpen())
89
                this.controlStatus(true);
90
        }
91
 
92
        public void SwapFreq(byte display) {
93
            navcomm.swapFreq(display);
94
            this.UpdateDisplay(display);
95
        }
96
 
97
        private void cmdSend0_Click(object sender, EventArgs e) {          
98
            char[] data = txtSend0.Text.ToArray();
99
            byte[] bytes = new byte[5];
100
            for (byte i = 0; i < 5; i++) {
155 pfowler 101
                if (data.Length <= i)                           // Check if there was enough data to fill all chars
139 pfowler 102
                    bytes[i] = (byte)0x0a;
155 pfowler 103
                else if (data[i] == 'a' || data[i] == ' ')      // Is its an 'a' or space, send a blank digit (0x0a)
104
                    bytes[i] = (byte)0x0a;
139 pfowler 105
                else
155 pfowler 106
                    bytes[i] = (byte)(data[i] - '0');           // Other wise, send the non-ascii digit (char - '0')
139 pfowler 107
            }
155 pfowler 108
            navcomm.setFreq(1, ref bytes);                      // Send all the bytes to the display board
139 pfowler 109
            this.UpdateDisplay(0);
110
        }
111
 
112
        private void cmdSend1_Click(object sender, EventArgs e) {
113
            char[] data = txtSend1.Text.ToArray();
114
            byte[] bytes = new byte[5];
115
            for (byte i = 0; i < 5; i++) {
155 pfowler 116
                if (data.Length <= i)
139 pfowler 117
                    bytes[i] = (byte)0x0a;
155 pfowler 118
                else if (data[i] == 'a' || data[i] == ' ')
119
                    bytes[i] = (byte)0x0a;
139 pfowler 120
                else
121
                    bytes[i] = (byte)(data[i] - '0');
122
            }
123
            navcomm.setFreq(3, ref bytes);
124
            this.UpdateDisplay(1);
125
        }
126
 
127
        private void cmdClose_Click(object sender, EventArgs e) {
157 pfowler 128
            if (this.navcomm != null && this.navcomm.isOpen()) {
129
                navcomm.resetAllRotary();
130
                navcomm.blankDisplay();
131
            }
139 pfowler 132
            this.Close();
133
        }
134
 
135
        private void cmdUpdate_Click(object sender, EventArgs e) {
136
            this.UpdateDevice();
137
        }
138
 
139
 
140
        private void cmdSwap0_CheckedChanged(object sender, EventArgs e) {
141
            if (cmdSwap0.Checked)
142
                this.SwapFreq(0);
143
        }
144
 
145
        private void cmdSwap0_Click(object sender, EventArgs e) {
146
            cmdSwap0.Checked = false;
147
        }
148
 
149
 
150
        private void inputTimer_Tick(object sender, EventArgs e) {
151
            this.UpdateInput();
152
        }
153
 
149 pfowler 154
        private void cmdSwap1_CheckedChanged(object sender, EventArgs e) {
155
            if (cmdSwap1.Checked)
156
                this.SwapFreq(1);
157
        }
158
 
159
        private void cmdFlip1_CheckedChanged(object sender, EventArgs e) {
160
 
161
        }
162
 
163
        private void cmdSwap1_Click(object sender, EventArgs e) {
164
            cmdSwap1.Checked = false;
165
        }
166
 
167
        private void txtRot0_Click(object sender, EventArgs e) {
168
            navcomm.resetRotary(0, 0);
169
        }
170
 
171
        private void txtRot1_Click(object sender, EventArgs e) {
172
            navcomm.resetRotary(1, 0);
173
        }
174
 
175
        private void freq0_Click(object sender, EventArgs e) {
176
            byte[] bytes = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
153 pfowler 177
            System.Windows.Forms.MessageBox.Show("Clicked");
149 pfowler 178
            navcomm.setFreq(0, ref bytes);
179
            this.UpdateDisplay(0);
180
        }
181
 
182
        private void freq1_Click(object sender, EventArgs e) {
183
            byte[] bytes = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
184
            navcomm.setFreq(1, ref bytes);
185
            this.UpdateDisplay(0);
186
        }
187
 
188
        private void freq2_Click(object sender, EventArgs e) {
189
            byte[] bytes = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
190
            navcomm.setFreq(2, ref bytes);
191
            this.UpdateDisplay(1);
192
        }
193
 
194
        private void freq3_Click(object sender, EventArgs e) {
195
            byte[] bytes = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
196
            navcomm.setFreq(3, ref bytes);
197
            this.UpdateDisplay(1);
198
        }
199
 
139 pfowler 200
    }
201
}