Subversion Repositories group.electronics

Rev

Rev 149 | Rev 155 | 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.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
 
7
using LibUsbDotNet;
8
using LibUsbDotNet.Main;
9
using LibUsbDotNet.DeviceNotify;
10
 
11
namespace NITNavComm {
12
    public class NITNavCommDevice : NITDevice  {
13
 
14
        enum UsbCommands : byte {
15
            CMD_LATCH_DISPLAY = 20,
153 pfowler 16
            CMD_SET_DISPLAY = 21,
139 pfowler 17
        };
18
 
19
        private byte[,] display { get; set; }
20
        private byte[] buttons {get; set; }
149 pfowler 21
        private sbyte[,] rotary;
139 pfowler 22
 
23
        public NITNavCommDevice(NITDevice nitDevice) :
24
            base(nitDevice.usbRegistry, "NITNavComm", nitDevice.vid, nitDevice.pid) {
25
            init();
26
        }
27
 
28
        public NITNavCommDevice(UsbRegistry usbRegistry, string type, int vid, int pid) :
29
            base(usbRegistry, "NITNavComm", 0x20a0, 0x4236) {
30
            init();
31
        }
32
 
147 pfowler 33
        private void init()
34
        {
35
            this.display = new byte[2, 10];
36
            for (byte j = 0; j < 2; j++)
37
                for (byte i = 0; i < 10; i++)
38
                    this.display[j, i] = 0x0a;
39
            this.buttons = new byte[2];
40
            this.buttons[0] = 0x00;
41
            this.buttons[1] = 0x00;
149 pfowler 42
            this.rotary = new sbyte[2, 2];
147 pfowler 43
            this.rotary[0, 0] = 0x00;
44
            this.rotary[0, 1] = 0x00;
45
            this.rotary[1, 0] = 0x00;
46
            this.rotary[1, 1] = 0x00;
47
        }
48
 
139 pfowler 49
        new public bool Close() {
50
            byte[] blank = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
51
            for (byte i = 0; i < 4; i++)
52
                setFreq(i, ref blank);
153 pfowler 53
            this.UpdateDisplay();
139 pfowler 54
            return base.Close();
55
        }
56
 
57
        public byte[] getFreq(byte freq) {
58
            byte[] ret = new byte[5];
59
            switch (freq) {
60
                case 0:
61
                    for (byte i = 0; i < 5; i++)
62
                        ret[i] = display[0, i];
63
                    break;
64
                case 1:
65
                    for (byte i = 0; i < 5; i++)
66
                        ret[i] = display[0, i + 5];
67
                    break;
68
                case 2:
69
                    for (byte i = 0; i < 5; i++)
70
                        ret[i] = display[1, i];
71
                    break;
72
                case 3:
73
                    for (byte i = 0; i < 5; i++)
74
                        ret[i] = display[1, i + 5];
75
                    break;
76
            }
77
            return ret;
78
        }
79
 
80
        public void setFreq(byte freq, ref byte[] data) {
81
            switch (freq) {
82
                case 0:
83
                    for (byte i = 0; i < 5; i++)
84
                        display[0, i] = data[i];
85
                    break;
86
                case 1:
87
                    for (byte i = 0; i < 5; i++)
88
                        display[0, i+5] = data[i];
89
                    break;
90
                case 2:
91
                    for (byte i = 0; i < 5; i++)
92
                        display[1, i] = data[i];
93
                    break;
94
                case 3:
95
                    for (byte i = 0; i < 5; i++)
96
                        display[1, i + 5] = data[i];
97
                    break;
98
            }
99
        }
100
 
153 pfowler 101
        public void setNavComFreqs(MainForm.NavCom_Data data) {
102
            byte[] freq0 = this.charArrayToBytes(data.Freq0.ToString().ToCharArray());
103
            byte[] freq1 = this.charArrayToBytes(data.Freq1.ToString().ToCharArray());
104
            byte[] freq2 = this.charArrayToBytes(data.Freq2.ToString().ToCharArray());
105
            byte[] freq3 = this.charArrayToBytes(data.Freq3.ToString().ToCharArray());
106
 
107
            this.setFreq(0, ref freq0);
108
            this.setFreq(1, ref freq1);
109
            this.setFreq(2, ref freq2);
110
            this.setFreq(3, ref freq3);
111
 
112
            this.UpdateDisplay();
113
        }
114
 
115
        private byte[] charArrayToBytes(char[] digits) {
116
            byte[] bytes = new byte[5];
117
            for (byte i = 0; i < 5; i++) {
118
                bytes[i] = (byte)(digits[i] - '0');
119
            }
120
            return bytes;
121
        }
122
 
123
        public void setDisplay(byte dis, ref byte[] data) {
124
            for (byte i = 0; i < data.Length; i++) {
125
                display[dis, i] = data[i];
126
            }
127
        }
128
 
139 pfowler 129
        private void sendDigit(byte dis, byte dig, byte val, byte dp) {
130
            ushort wxValue = (ushort)(dis <<8);
131
            wxValue |= dig;
132
            ushort wxIndex = (ushort)(dp << 8);
133
            wxIndex |= val;
134
 
135
            base.SendCommand(21, (short)wxValue, (short)wxIndex);
136
        }
137
 
138
        private void sendLatch(byte dis) {
153 pfowler 139
            if (!this.isOpen())
140
                return;
141
 
139 pfowler 142
            base.SendCommand(20, (short)dis, 0);
143
        }
144
 
145
        public void updateInput() {
146
            byte[] data = new byte[8];
147
            int transferred;
149 pfowler 148
            base.SendCommand(30, 0, 0, data, out transferred);
149
            this.buttons[0] = data[0];
150
            this.rotary[0, 0] += (sbyte)data[1];
151
            this.buttons[1] = data[2];
152
            this.rotary[1, 0] += (sbyte)data[3];
139 pfowler 153
        }
154
 
155
        public bool isSwapSet(byte dis) {
156
            if ((this.buttons[dis] & 0x01)>0)
157
                return true;
158
            return false;
159
        }
160
 
161
        public bool isFlipSet(byte dis) {
162
            if ((this.buttons[dis] & 0x02) > 0)
163
                return true;
164
            return false;
165
        }
166
 
149 pfowler 167
        public sbyte getRotary(byte dis, byte rotary) {
139 pfowler 168
            return this.rotary[dis, rotary];
169
        }
170
 
149 pfowler 171
        public void resetRotary(byte display, byte rotary) {
172
            this.rotary[display, rotary] = 0x00;
173
        }
174
 
153 pfowler 175
        public void UpdateDisplay() {
176
            this.sendData(0);
177
            this.sendData(1);
178
            this.latchDisplay(0);
179
            this.latchDisplay(1);
139 pfowler 180
        }
181
 
153 pfowler 182
        public void UpdateDisplay(byte dis) {
183
            this.sendData(dis);
184
            this.latchDisplay(dis);
185
        }
186
 
187
        private void sendData(byte dis) {
139 pfowler 188
            for (byte i = 0; i < 10; i++) {
153 pfowler 189
                this.sendDigit(dis, i, display[dis, i], 0);
190
            }            
191
        }
192
 
193
        private void latchDisplay(byte dis) {
139 pfowler 194
            this.sendLatch(dis);
195
        }
196
 
197
        public void swapFreq(byte display) {
198
            if (display == 0) {
199
                byte[] left = this.getFreq(0);
200
                byte[] right = this.getFreq(1);
201
                this.setFreq(0, ref right);
202
                this.setFreq(1, ref left);
203
            } else if (display == 1) {
204
                byte[] left = this.getFreq(2);
205
                byte[] right = this.getFreq(3);
206
                this.setFreq(2, ref right);
207
                this.setFreq(3, ref left);
208
            }
209
        }
210
    }
211
}