Subversion Repositories group.electronics

Rev

Rev 153 | Rev 156 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 153 Rev 155
Line 1... Line 1...
1
using System;
1
using System;
2
using System.Collections.Generic;
2
using System.Collections.Generic;
3
using System.Linq;
3
using System.Linq;
4
using System.Text;
4
using System.Text;
5
using System.Threading.Tasks;
5
using System.Threading.Tasks;
-
 
6
using System.Timers;
-
 
7
 
-
 
8
using Microsoft.FlightSimulator.SimConnect;
6
 
9
 
7
using LibUsbDotNet;
10
using LibUsbDotNet;
8
using LibUsbDotNet.Main;
11
using LibUsbDotNet.Main;
9
using LibUsbDotNet.DeviceNotify;
12
using LibUsbDotNet.DeviceNotify;
10
 
13
 
Line 17... Line 20...
17
        };
20
        };
18
 
21
 
19
        private byte[,] display { get; set; }
22
        private byte[,] display { get; set; }
20
        private byte[] buttons {get; set; }
23
        private byte[] buttons {get; set; }
21
        private sbyte[,] rotary;
24
        private sbyte[,] rotary;
-
 
25
        private ushort[] points { get; set; }
-
 
26
 
-
 
27
        public int flipcomm { get; set; }
-
 
28
        public int flipnav { get; set; }
-
 
29
 
-
 
30
        public uint simStatus { get; set; }
-
 
31
 
-
 
32
        
-
 
33
 
-
 
34
        private const int TIMER_INTERVAL = 600;
-
 
35
        private const int TIMER_COUNT = 4;
-
 
36
        private const int TIMER_COMMSWAP = 0;
-
 
37
        private const int TIMER_COMMFLIP = 1;
-
 
38
        private const int TIMER_NAVSWAP = 2;
-
 
39
        private const int TIMER_NAVFLIP = 3;
-
 
40
        Timer[] timers = new Timer[TIMER_COUNT];
22
 
41
 
23
        public NITNavCommDevice(NITDevice nitDevice) :
42
        public NITNavCommDevice(NITDevice nitDevice) :
24
            base(nitDevice.usbRegistry, "NITNavComm", nitDevice.vid, nitDevice.pid) {
43
            base(nitDevice.usbRegistry, "NITNavComm", nitDevice.vid, nitDevice.pid) {
25
            init();
44
            this.init();
26
        }
45
        }
27
 
46
 
28
        public NITNavCommDevice(UsbRegistry usbRegistry, string type, int vid, int pid) :
47
        public NITNavCommDevice(UsbRegistry usbRegistry, string type, int vid, int pid) :
29
            base(usbRegistry, "NITNavComm", 0x20a0, 0x4236) {
48
            base(usbRegistry, "NITNavComm", 0x20a0, 0x4236) {
30
            init();
49
            this.init();
31
        }
50
        }
32
 
51
 
33
        private void init()
52
        private void init()
34
        {
53
        {
-
 
54
            base.Open();
-
 
55
            
35
            this.display = new byte[2, 10];
56
            this.display = new byte[2, 10];
36
            for (byte j = 0; j < 2; j++)
57
            this.points = new ushort[2];
37
                for (byte i = 0; i < 10; i++)
58
            this.blankDisplay();
38
                    this.display[j, i] = 0x0a;
59
            
39
            this.buttons = new byte[2];
60
            this.buttons = new byte[2];
40
            this.buttons[0] = 0x00;
61
            this.buttons[0] = 0x00;
41
            this.buttons[1] = 0x00;
62
            this.buttons[1] = 0x00;
-
 
63
 
42
            this.rotary = new sbyte[2, 2];
64
            this.rotary = new sbyte[2, 2];
43
            this.rotary[0, 0] = 0x00;
65
            this.rotary[0, 0] = 0x00;
44
            this.rotary[0, 1] = 0x00;
66
            this.rotary[0, 1] = 0x00;
45
            this.rotary[1, 0] = 0x00;
67
            this.rotary[1, 0] = 0x00;
46
            this.rotary[1, 1] = 0x00;
68
            this.rotary[1, 1] = 0x00;
-
 
69
 
-
 
70
            this.flipcomm = 0;
-
 
71
            this.flipnav = 0;
-
 
72
 
-
 
73
            this.simStatus = 0;
-
 
74
            this.assigned = 1;
-
 
75
 
-
 
76
            for (int i = 0; i < TIMER_COUNT; i++) {
-
 
77
                Timer timer = new Timer();
-
 
78
                timer.Enabled = false;
-
 
79
                timer.AutoReset = false;
-
 
80
                timer.Interval = TIMER_INTERVAL;
-
 
81
                timer.Elapsed += OnDebounceTimer;
-
 
82
                timers[i] = timer;
-
 
83
            }
-
 
84
            
-
 
85
        }
-
 
86
 
-
 
87
        private static void OnDebounceTimer(Object source, ElapsedEventArgs e) {
-
 
88
            Timer timer = (Timer)source;
-
 
89
            timer.Enabled = false;
47
        }
90
        }
48
 
91
 
49
        new public bool Close() {
92
        new public bool Close() {
-
 
93
            this.blankDisplay();
-
 
94
            return base.Close();
-
 
95
        }
-
 
96
 
-
 
97
        public void powerDown() {
-
 
98
            this.blankDisplay();
-
 
99
            this.simStatus = 2;
-
 
100
        }
-
 
101
 
-
 
102
        public void powerUp() {
-
 
103
            this.simStatus = 0;
-
 
104
        }
-
 
105
 
-
 
106
        public void blankDisplay() {
50
            byte[] blank = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
107
            byte[] blank = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a };
51
            for (byte i = 0; i < 4; i++)
108
            for (byte i = 0; i < 4; i++)
52
                setFreq(i, ref blank);
109
                setFreq(i, ref blank);
-
 
110
            this.points[0] = 0x0000
-
 
111
                ;
-
 
112
            this.points[1] = 0x0000;
53
            this.UpdateDisplay();
113
            this.UpdateDisplay();
54
            return base.Close();
-
 
55
        }
114
        }
56
 
115
 
57
        public byte[] getFreq(byte freq) {
116
        public byte[] getFreq(byte freq) {
58
            byte[] ret = new byte[5];
117
            byte[] ret = new byte[5];
59
            switch (freq) {
118
            switch (freq) {
Line 96... Line 155...
96
                        display[1, i + 5] = data[i];
155
                        display[1, i + 5] = data[i];
97
                    break;
156
                    break;
98
            }
157
            }
99
        }
158
        }
100
 
159
 
101
        public void setNavComFreqs(MainForm.NavCom_Data data) {
160
        public void setNavComFreqs(FSXObject.NavCom_Data data) {
102
            byte[] freq0 = this.charArrayToBytes(data.Freq0.ToString().ToCharArray());
161
            byte[] freq0 = this.charArrayToBytes(data.Freq0.ToString().ToCharArray());
103
            byte[] freq1 = this.charArrayToBytes(data.Freq1.ToString().ToCharArray());
162
            byte[] freq1 = this.charArrayToBytes(data.Freq1.ToString().ToCharArray());
104
            byte[] freq2 = this.charArrayToBytes(data.Freq2.ToString().ToCharArray());
163
            byte[] freq2 = this.charArrayToBytes(data.Freq2.ToString().ToCharArray());
105
            byte[] freq3 = this.charArrayToBytes(data.Freq3.ToString().ToCharArray());
164
            byte[] freq3 = this.charArrayToBytes(data.Freq3.ToString().ToCharArray());
106
 
165
 
Line 170... Line 229...
170
 
229
 
171
        public void resetRotary(byte display, byte rotary) {
230
        public void resetRotary(byte display, byte rotary) {
172
            this.rotary[display, rotary] = 0x00;
231
            this.rotary[display, rotary] = 0x00;
173
        }
232
        }
174
 
233
 
-
 
234
        private void sendData(byte dis) {
-
 
235
            for (byte i = 0; i < 10; i++) {
-
 
236
                //this.sendDigit(dis, i, display[dis, i], this.GetBit(this.points[dis], i));
-
 
237
                this.sendDigit(dis, i, display[dis, i], 0);
-
 
238
            }            
-
 
239
        }
-
 
240
 
175
        public void UpdateDisplay() {
241
        public void UpdateDisplay() {
176
            this.sendData(0);
242
            this.sendData(0);
177
            this.sendData(1);
243
            this.sendData(1);
178
            this.latchDisplay(0);
244
            this.latchDisplay(0);
179
            this.latchDisplay(1);
245
            this.latchDisplay(1);
Line 182... Line 248...
182
        public void UpdateDisplay(byte dis) {
248
        public void UpdateDisplay(byte dis) {
183
            this.sendData(dis);
249
            this.sendData(dis);
184
            this.latchDisplay(dis);
250
            this.latchDisplay(dis);
185
        }
251
        }
186
 
252
 
187
        private void sendData(byte dis) {
253
        private byte GetBit(ushort b, byte bitNumber) {
188
            for (byte i = 0; i < 10; i++) {
254
            if ((b & (1 << bitNumber)) != 0)
189
                this.sendDigit(dis, i, display[dis, i], 0);
255
                return 1;
190
            }            
256
            return 0;
191
        }
257
        }
192
 
258
 
193
        private void latchDisplay(byte dis) {
259
        private void latchDisplay(byte dis) {
194
            this.sendLatch(dis);
260
            this.sendLatch(dis);
195
        }
261
        }
Line 205... Line 271...
205
                byte[] right = this.getFreq(3);
271
                byte[] right = this.getFreq(3);
206
                this.setFreq(2, ref right);
272
                this.setFreq(2, ref right);
207
                this.setFreq(3, ref left);
273
                this.setFreq(3, ref left);
208
            }
274
            }
209
        }
275
        }
-
 
276
        // @TODO: Make this work with COMM1/COMM2
-
 
277
        public void simButtons(FSXObject fsx) {
-
 
278
            
-
 
279
            if (!timers[TIMER_COMMSWAP].Enabled && this.isSwapSet(0)) {
-
 
280
                if (this.assigned == 1)
-
 
281
                    fsx.Comm1SwapFreq();
-
 
282
                else
-
 
283
                    fsx.Comm2SwapFreq();
-
 
284
                timers[TIMER_COMMSWAP].Enabled = true;
-
 
285
            }
-
 
286
 
-
 
287
            if (!timers[TIMER_NAVSWAP].Enabled && this.isSwapSet(1)) {
-
 
288
                if (this.assigned == 1)
-
 
289
                    fsx.Nav1SwapFreq();
-
 
290
                else
-
 
291
                    fsx.Nav2SwapFreq();
-
 
292
 
-
 
293
                timers[TIMER_NAVSWAP].Enabled = true;
-
 
294
            }
-
 
295
 
-
 
296
            // Check if the MHz/KHz has been pressed
-
 
297
            if (!timers[TIMER_COMMFLIP].Enabled && this.isFlipSet(0)) {
-
 
298
                this.flipcomm = 1 - this.flipcomm;
-
 
299
                timers[TIMER_COMMFLIP].Enabled = true;
-
 
300
            }
-
 
301
 
-
 
302
            if (!timers[TIMER_NAVFLIP].Enabled && this.isFlipSet(1)) {
-
 
303
                this.flipnav = 1 - this.flipnav;
-
 
304
                timers[TIMER_NAVFLIP].Enabled = true;
-
 
305
            }
-
 
306
 
-
 
307
            // Process the rotary encoders COMM1
-
 
308
            sbyte delta = this.getRotary(0, 0);
-
 
309
            this.resetRotary(0, 0);
-
 
310
            if (delta != 0) {
-
 
311
                if (delta < 0) {
-
 
312
                    delta = (sbyte)-delta;
-
 
313
 
-
 
314
                    if (this.flipcomm == 1)
-
 
315
                        if (this.assigned == 1)
-
 
316
                            fsx.Comm1DecFract();
-
 
317
                        else
-
 
318
                            fsx.Comm2DecFract();
-
 
319
                    else
-
 
320
                        if (this.assigned == 1)
-
 
321
                            fsx.Comm1DecWhole();
-
 
322
                        else
-
 
323
                            fsx.Comm2DecWhole();
-
 
324
                } else {
-
 
325
 
-
 
326
                    if (this.flipcomm == 1)
-
 
327
                        if (this.assigned == 1)
-
 
328
                            fsx.Comm1IncFract();
-
 
329
                        else
-
 
330
                            fsx.Comm2IncFract();
-
 
331
                    else
-
 
332
                        if (this.assigned == 1)
-
 
333
                            fsx.Comm1IncWhole();
-
 
334
                        else
-
 
335
                            fsx.Comm2IncWhole();
-
 
336
                }
-
 
337
            }
-
 
338
 
-
 
339
            // Process the rotary encoders NAV1
-
 
340
            delta = this.getRotary(1, 0);
-
 
341
            this.resetRotary(1, 0);
-
 
342
            if (delta != 0) {
-
 
343
                if (delta < 0) {
-
 
344
                    delta = (sbyte)-delta;
-
 
345
 
-
 
346
                    if (this.flipnav == 1)
-
 
347
                        if (this.assigned == 1)
-
 
348
                            fsx.Nav1DecFract();
-
 
349
                        else
-
 
350
                            fsx.Nav2DecFract();
-
 
351
                    else
-
 
352
                        if (this.assigned == 1)
-
 
353
                            fsx.Nav1DecWhole();
-
 
354
                        else
-
 
355
                            fsx.Nav2DecWhole();
-
 
356
                } else {
-
 
357
 
-
 
358
                    if (this.flipcomm == 1)
-
 
359
                        if (this.assigned == 1)
-
 
360
                            fsx.Nav1IncFract();
-
 
361
                        else
-
 
362
                            fsx.Nav2IncFract();
-
 
363
                    else
-
 
364
                        if (this.assigned == 1)
-
 
365
                            fsx.Nav1IncWhole();
-
 
366
                        else
-
 
367
                            fsx.Nav2IncWhole();
-
 
368
                }
-
 
369
            }
-
 
370
 
-
 
371
        }
210
    }
372
    }
211
}
373
}