Subversion Repositories group.electronics

Rev

Rev 153 | Go to most recent revision | 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) {
128
            this.Close();
129
        }
130
 
131
        private void cmdUpdate_Click(object sender, EventArgs e) {
132
            this.UpdateDevice();
133
        }
134
 
135
 
136
        private void cmdSwap0_CheckedChanged(object sender, EventArgs e) {
137
            if (cmdSwap0.Checked)
138
                this.SwapFreq(0);
139
        }
140
 
141
        private void cmdSwap0_Click(object sender, EventArgs e) {
142
            cmdSwap0.Checked = false;
143
        }
144
 
145
 
146
        private void inputTimer_Tick(object sender, EventArgs e) {
147
            this.UpdateInput();
148
        }
149
 
149 pfowler 150
        private void cmdSwap1_CheckedChanged(object sender, EventArgs e) {
151
            if (cmdSwap1.Checked)
152
                this.SwapFreq(1);
153
        }
154
 
155
        private void cmdFlip1_CheckedChanged(object sender, EventArgs e) {
156
 
157
        }
158
 
159
        private void cmdSwap1_Click(object sender, EventArgs e) {
160
            cmdSwap1.Checked = false;
161
        }
162
 
163
        private void txtRot0_Click(object sender, EventArgs e) {
164
            navcomm.resetRotary(0, 0);
165
        }
166
 
167
        private void txtRot1_Click(object sender, EventArgs e) {
168
            navcomm.resetRotary(1, 0);
169
        }
170
 
171
        private void freq0_Click(object sender, EventArgs e) {
172
            byte[] bytes = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
153 pfowler 173
            System.Windows.Forms.MessageBox.Show("Clicked");
149 pfowler 174
            navcomm.setFreq(0, ref bytes);
175
            this.UpdateDisplay(0);
176
        }
177
 
178
        private void freq1_Click(object sender, EventArgs e) {
179
            byte[] bytes = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
180
            navcomm.setFreq(1, ref bytes);
181
            this.UpdateDisplay(0);
182
        }
183
 
184
        private void freq2_Click(object sender, EventArgs e) {
185
            byte[] bytes = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
186
            navcomm.setFreq(2, ref bytes);
187
            this.UpdateDisplay(1);
188
        }
189
 
190
        private void freq3_Click(object sender, EventArgs e) {
191
            byte[] bytes = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
192
            navcomm.setFreq(3, ref bytes);
193
            this.UpdateDisplay(1);
194
        }
195
 
139 pfowler 196
    }
197
}