Subversion Repositories group.electronics

Rev

Rev 145 | Rev 149 | 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,
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
 
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;
42
            this.rotary = new byte[2, 2];
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);
53
            this.updateDisplay();
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
 
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
}