Subversion Repositories group.electronics

Rev

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.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);
20
        }
21
 
22
        private void controlStatus(bool status) {
23
            cmdSend0.Enabled = status;
24
            cmdSend1.Enabled = status;
25
            cmdUpdate.Enabled = status;
26
            inputTimer.Enabled = status;
27
        }
28
 
29
        public void UpdateDevice() {
30
            this.UpdateDisplay();
31
            this.UpdateInput();
32
        }
33
 
34
        public void UpdateDisplay(byte display) {
35
            byte[] data;
36
            if (display == 0) {
37
                data = navcomm.getFreq(0);
38
                freq0.Value = this.byteArraytoString(ref data, 5);
39
                data = navcomm.getFreq(1);
40
                freq1.Value = this.byteArraytoString(ref data, 5);
41
            } else {
42
                data = navcomm.getFreq(2);
43
                freq2.Value = this.byteArraytoString(ref data, 5);
44
                data = navcomm.getFreq(3);
45
                freq3.Value = this.byteArraytoString(ref data, 5);
46
            }
47
            navcomm.updateDisplay(display);
48
 
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");
65
        }
66
 
67
        private string byteArraytoString(ref byte[] data, byte len) {
68
            StringBuilder sb = new StringBuilder();
69
            for (byte i = 0; i < len; i++) {
70
                if (data[i] == 0x0a)
71
                    sb.Append(' ');
72
                else
73
                    sb.Append((byte)(data[i]));
74
            }
75
            return sb.ToString();
76
        }
77
 
78
        public void setDevice(NITNavCommDevice navcomm) {
79
            this.navcomm = navcomm;
80
 
81
            if (!navcomm.isOpen())
82
                navcomm.Open();
83
 
84
            if (navcomm.isOpen())
85
                this.controlStatus(true);
86
        }
87
 
88
        public void SwapFreq(byte display) {
89
            navcomm.swapFreq(display);
90
            this.UpdateDisplay(display);
91
        }
92
 
93
        private void cmdSend0_Click(object sender, EventArgs e) {          
94
            char[] data = txtSend0.Text.ToArray();
95
            byte[] bytes = new byte[5];
96
            for (byte i = 0; i < 5; i++) {
97
                if (data[i] == 'a' || data[i] == ' ')
98
                    bytes[i] = (byte)0x0a;
99
                else
100
                    bytes[i] = (byte)(data[i] - '0');
101
            }
102
            navcomm.setFreq(1, ref bytes);
103
            this.UpdateDisplay(0);
104
        }
105
 
106
        private void cmdSend1_Click(object sender, EventArgs e) {
107
            char[] data = txtSend1.Text.ToArray();
108
            byte[] bytes = new byte[5];
109
            for (byte i = 0; i < 5; i++) {
110
                if (data[i] == 'a' || data[i] == ' ')
111
                    bytes[i] = (byte)0x0a;
112
                else
113
                    bytes[i] = (byte)(data[i] - '0');
114
            }
115
            navcomm.setFreq(3, ref bytes);
116
            this.UpdateDisplay(1);
117
        }
118
 
119
        private void cmdClose_Click(object sender, EventArgs e) {
120
            if (this.navcomm != null && this.navcomm.isOpen())
121
                navcomm.Close();
122
            this.Close();
123
        }
124
 
125
        private void cmdUpdate_Click(object sender, EventArgs e) {
126
            this.UpdateDevice();
127
        }
128
 
129
 
130
        private void cmdSwap0_CheckedChanged(object sender, EventArgs e) {
131
            if (cmdSwap0.Checked)
132
                this.SwapFreq(0);
133
        }
134
 
135
        private void cmdSwap0_Click(object sender, EventArgs e) {
136
            cmdSwap0.Checked = false;
137
            this.SwapFreq(0);
138
        }
139
 
140
 
141
        private void inputTimer_Tick(object sender, EventArgs e) {
142
            this.UpdateInput();
143
        }
144
 
145
    }
146
}