Subversion Repositories group.electronics

Rev

Rev 147 | 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.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; }
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);
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() {
149 pfowler 115
            //this.updateInput(0);
116
            //this.updateInput(1);
139 pfowler 117
            byte[] data = new byte[8];
118
            int transferred;
149 pfowler 119
            base.SendCommand(30, 0, 0, data, out transferred);
120
            this.buttons[0] = data[0];
121
            this.rotary[0, 0] += (sbyte)data[1];
122
            this.buttons[1] = data[2];
123
            this.rotary[1, 0] += (sbyte)data[3];
139 pfowler 124
        }
125
 
126
        public bool isSwapSet(byte dis) {
127
            if ((this.buttons[dis] & 0x01)>0)
128
                return true;
129
            return false;
130
        }
131
 
132
        public bool isFlipSet(byte dis) {
133
            if ((this.buttons[dis] & 0x02) > 0)
134
                return true;
135
            return false;
136
        }
137
 
149 pfowler 138
        public sbyte getRotary(byte dis, byte rotary) {
139 pfowler 139
            return this.rotary[dis, rotary];
140
        }
141
 
149 pfowler 142
        public void resetRotary(byte display, byte rotary) {
143
            this.rotary[display, rotary] = 0x00;
144
        }
145
 
139 pfowler 146
        public void updateDisplay() {
147
            this.updateDisplay(0);
148
            this.updateDisplay(1);
149
        }
150
 
151
        public void updateDisplay(byte dis) {
152
            for (byte i = 0; i < 10; i++) {
153
                this.sendDigit(dis, i, display[dis,i], 0);
154
            }
155
            this.sendLatch(dis);
156
        }
157
 
158
        public void swapFreq(byte display) {
159
            if (display == 0) {
160
                byte[] left = this.getFreq(0);
161
                byte[] right = this.getFreq(1);
162
                this.setFreq(0, ref right);
163
                this.setFreq(1, ref left);
164
            } else if (display == 1) {
165
                byte[] left = this.getFreq(2);
166
                byte[] right = this.getFreq(3);
167
                this.setFreq(2, ref right);
168
                this.setFreq(3, ref left);
169
            }
170
        }
171
    }
172
}