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.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,
16
            CMD_SET_DISPLAY0 = 21
17
        };
18
 
19
        private byte[,] display { get; set; }
20
        private byte[] buttons {get; set; }
21
        private byte[,] rotary;
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
 
33
        new public bool Close() {
34
            byte[] blank = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
35
            for (byte i = 0; i < 4; i++)
36
                setFreq(i, ref blank);
37
            this.updateDisplay();
38
            return base.Close();
39
        }
40
 
41
        public byte[] getFreq(byte freq) {
42
            byte[] ret = new byte[5];
43
            switch (freq) {
44
                case 0:
45
                    for (byte i = 0; i < 5; i++)
46
                        ret[i] = display[0, i];
47
                    break;
48
                case 1:
49
                    for (byte i = 0; i < 5; i++)
50
                        ret[i] = display[0, i + 5];
51
                    break;
52
                case 2:
53
                    for (byte i = 0; i < 5; i++)
54
                        ret[i] = display[1, i];
55
                    break;
56
                case 3:
57
                    for (byte i = 0; i < 5; i++)
58
                        ret[i] = display[1, i + 5];
59
                    break;
60
            }
61
            return ret;
62
        }
63
 
64
        public void setFreq(byte freq, ref byte[] data) {
65
            switch (freq) {
66
                case 0:
67
                    for (byte i = 0; i < 5; i++)
68
                        display[0, i] = data[i];
69
                    break;
70
                case 1:
71
                    for (byte i = 0; i < 5; i++)
72
                        display[0, i+5] = data[i];
73
                    break;
74
                case 2:
75
                    for (byte i = 0; i < 5; i++)
76
                        display[1, i] = data[i];
77
                    break;
78
                case 3:
79
                    for (byte i = 0; i < 5; i++)
80
                        display[1, i + 5] = data[i];
81
                    break;
82
            }
83
        }
84
 
85
 
86
        private void init() {
87
            this.display = new byte[2,10];
88
            for (byte j=0; j<2; j++)
89
                for (byte i = 0; i < 10; i++)
90
                    this.display[j,i] = 0x0a;
91
            this.buttons = new byte[2];
92
            this.buttons[0] = 0x00;
93
            this.buttons[1] = 0x00;
94
            this.rotary = new byte[2,2];
95
            this.rotary[0,0] = 0x00;
96
            this.rotary[0,1] = 0x00;
97
            this.rotary[1,0] = 0x00;
98
            this.rotary[1,1] = 0x00;
99
        }
100
 
101
        private void sendDigit(byte dis, byte dig, byte val, byte dp) {
102
            ushort wxValue = (ushort)(dis <<8);
103
            wxValue |= dig;
104
            ushort wxIndex = (ushort)(dp << 8);
105
            wxIndex |= val;
106
 
107
            base.SendCommand(21, (short)wxValue, (short)wxIndex);
108
        }
109
 
110
        private void sendLatch(byte dis) {
111
            base.SendCommand(20, (short)dis, 0);
112
        }
113
 
114
        public void updateInput() {
115
            this.updateInput(0);
116
            this.updateInput(1);
117
        }
118
 
119
        public void updateInput(byte dis) {
120
            byte[] data = new byte[8];
121
            int transferred;
122
            base.SendCommand(30, (short)dis, 0, data, out transferred);
123
            this.buttons[dis] = data[0];
124
            this.rotary[dis,0] = data[1];
125
            this.rotary[dis,1] = data[2];
126
        }
127
 
128
        public bool isSwapSet(byte dis) {
129
            if ((this.buttons[dis] & 0x01)>0)
130
                return true;
131
            return false;
132
        }
133
 
134
        public bool isFlipSet(byte dis) {
135
            if ((this.buttons[dis] & 0x02) > 0)
136
                return true;
137
            return false;
138
        }
139
 
140
        public byte getRotary(byte dis, byte rotary) {
141
            return this.rotary[dis, rotary];
142
        }
143
 
144
        public void updateDisplay() {
145
            this.updateDisplay(0);
146
            this.updateDisplay(1);
147
        }
148
 
149
        public void updateDisplay(byte dis) {
150
            for (byte i = 0; i < 10; i++) {
151
                this.sendDigit(dis, i, display[dis,i], 0);
152
            }
153
            this.sendLatch(dis);
154
        }
155
 
156
        public void swapFreq(byte display) {
157
            if (display == 0) {
158
                byte[] left = this.getFreq(0);
159
                byte[] right = this.getFreq(1);
160
                this.setFreq(0, ref right);
161
                this.setFreq(1, ref left);
162
            } else if (display == 1) {
163
                byte[] left = this.getFreq(2);
164
                byte[] right = this.getFreq(3);
165
                this.setFreq(2, ref right);
166
                this.setFreq(3, ref left);
167
            }
168
        }
169
    }
170
}