Subversion Repositories group.electronics

Rev

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

Rev 158 Rev 159
Line 4... Line 4...
4
using System.Linq;
4
using System.Linq;
5
using System.Reflection;
5
using System.Reflection;
6
using System.Text;
6
using System.Text;
7
using System.Threading.Tasks;
7
using System.Threading.Tasks;
8
using System.Timers;
8
using System.Timers;
-
 
9
using WindowsInput;
-
 
10
using WindowsInput.Native;
9
 
11
 
10
namespace nitdcscore {
12
namespace nitdcscore {
11
    public struct devdata {
13
    public struct devdata {
12
        public uint prev_input;
14
        public uint prev_input;
13
        public uint cur_input;
15
        public uint cur_input;
-
 
16
        public ushort cur_adc;
-
 
17
        public ushort prev_adc;
-
 
18
        public ushort max_adc;
-
 
19
        public ushort adc_thres;
14
 
20
 
15
        public int id;
21
        public int id;
16
        public string name;
22
        public string name;
17
        public string function;
23
        public string function;
18
    }
24
    }
19
 
25
 
20
 
26
 
21
 
27
 
-
 
28
 
-
 
29
 
22
    public class mcp2221 {
30
    public class mcp2221 {
23
        DcsBios bios = new DcsBios();
31
        DcsBios bios = new DcsBios();
24
        MchpUsbI2c usbi2c = new MchpUsbI2c();
32
        MchpUsbI2c usbi2c = new MchpUsbI2c();
25
        public Boolean hasAHFS { get; set; }
33
        public Boolean hasAHFS { get; set; }
26
        public Boolean hasAAP { get; set; }
34
        public Boolean hasAAP { get; set; }
27
 
35
 
28
        int idx { get; set; }
36
        int idx { get; set; }
29
        int devcount { get; set; }
37
        int devcount { get; set; }
-
 
38
        long dummy = 0;
30
 
39
 
31
        public uint speed { get; set; }
40
        public uint speed { get; set; }
32
 
41
 
33
        Timer refresh = new Timer();
42
        Timer refresh = new Timer();
-
 
43
        Timer delay = new Timer();
-
 
44
        InputSimulator sendkey = new InputSimulator();
34
 
45
 
35
        private readonly Object lockobject = new object();
46
        private readonly Object lockobject = new object();
36
 
47
 
37
        public devdata[] devices = new devdata[5];
48
        public devdata[] devices = new devdata[5];
38
 
49
 
39
 
50
 
40
        public mcp2221() {
51
        public mcp2221() {
41
            speed = 400000;
52
            speed = 400000;
42
            idx = 0;
53
            idx = 0;
43
 
54
 
-
 
55
            refresh.Interval = 70;
-
 
56
            refresh.AutoReset = true;
-
 
57
            refresh.Elapsed += refresh_Elapsed;
-
 
58
            refresh.Enabled = false;
-
 
59
 
-
 
60
            delay.Interval = 5;
-
 
61
            delay.AutoReset = false;
-
 
62
            delay.Elapsed += delay_Elapsed;
-
 
63
            delay.Enabled = false;
-
 
64
 
44
            bios.InitUDP();
65
            bios.InitUDP();
45
 
66
 
46
            usbi2c.Settings.GetConnectionStatus();
67
            usbi2c.Settings.GetConnectionStatus();
47
            devcount = usbi2c.Management.GetDevCount();
68
            devcount = usbi2c.Management.GetDevCount();
48
            Console.WriteLine(devcount.ToString() + " devices found");
69
            Console.WriteLine(devcount.ToString() + " devices found");
Line 52... Line 73...
52
                string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
73
                string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
53
                if (usbDescriptor == "AHCP/FSCP Panel") {
74
                if (usbDescriptor == "AHCP/FSCP Panel") {
54
                    this.hasAHFS = true;
75
                    this.hasAHFS = true;
55
                    devices[i].cur_input = 0;
76
                    devices[i].cur_input = 0;
56
                    devices[i].prev_input = 0;
77
                    devices[i].prev_input = 0;
-
 
78
                    devices[i].cur_adc = 0;
-
 
79
                    devices[i].max_adc = 930;
-
 
80
                    devices[i].adc_thres = 15;
57
                    devices[i].id = i;
81
                    devices[i].id = i;
58
                    devices[i].name = "AHCP/FSCP Panel";
82
                    devices[i].name = "AHCP/FSCP Panel";
59
                    devices[i].function = "refresh_ahfs";
83
                    devices[i].function = "refresh_ahfs";
60
 
84
 
61
                    init_ahfs(ref devices[i]);
85
                    init_ahfs(i);
62
                } else if (usbDescriptor == "AAP Panel") {
86
                } else if (usbDescriptor == "AAP Panel") {
63
                    this.hasAAP = true;
87
                    this.hasAAP = true;
64
                    devices[i].cur_input = 0;
88
                    devices[i].cur_input = 0;
65
                    devices[i].prev_input = 0;
89
                    devices[i].prev_input = 0;
-
 
90
                    devices[i].prev_adc = 0;
66
                    devices[i].id = i;
91
                    devices[i].id = i;
67
                    devices[i].name = "AAP Panel";
92
                    devices[i].name = "AAP Panel";
68
                    devices[i].function = "refresh_aap";
93
                    devices[i].function = "refresh_aap";
69
 
94
 
70
                    init_aap(ref devices[i]);
95
                    init_aap(i);
71
                }
96
                }
72
            }
97
            }
73
 
98
 
74
            refresh.Interval = 500;
99
            
75
            refresh.AutoReset = true;
-
 
76
            refresh.Elapsed += refresh_Elapsed;
-
 
77
            refresh.Enabled = false;
-
 
78
        }
100
        }
79
 
101
 
80
        private Boolean enabled;
102
        private Boolean enabled;
81
        public Boolean Enabled {
103
        public Boolean Enabled {
82
            get {
104
            get {
83
                return enabled;
105
                return enabled;
84
            }
106
            }
85
            set {
107
            set {
-
 
108
                if (this.devcount == 0)
-
 
109
                    return;
-
 
110
 
86
                this.enabled = true;
111
                this.enabled = true;
87
                refresh.Enabled = true;
112
                refresh.Enabled = true;
88
            }
113
            }
89
        }
114
        }
90
 
115
 
-
 
116
        void delay_Elapsed(object sender, ElapsedEventArgs e) {
-
 
117
            delay.Enabled = false;
-
 
118
        }
-
 
119
 
-
 
120
        void delayms(double ms = 100) {
-
 
121
            delay.Interval = ms;
-
 
122
            delay.Enabled = true;
-
 
123
            dummy = 0;
-
 
124
            while (delay.Enabled) { dummy++; }
-
 
125
        }
-
 
126
 
91
        void refresh_Elapsed(object sender, ElapsedEventArgs e) {
127
        void refresh_Elapsed(object sender, ElapsedEventArgs e) {
92
            if (!Enabled)
128
            if (!Enabled)
93
                return;
129
                return;
94
            devdata dev = devices[idx];
130
            devdata dev = devices[idx];
95
 
131
 
96
            usbi2c.Management.SelectDev(dev.id);
132
            usbi2c.Management.SelectDev(dev.id);
-
 
133
            usbi2c.Settings.GetConnectionStatus();
97
 
134
 
98
            
135
            
99
            Type type = this.GetType();
136
            Type type = this.GetType();
100
            MethodInfo callfunc = type.GetMethod(dev.function);
137
            MethodInfo callfunc = type.GetMethod(dev.function);
101
            ParameterInfo[] parameters = callfunc.GetParameters();
138
            ParameterInfo[] parameters = callfunc.GetParameters();
102
            object[] parms = { dev };
139
            object[] parms = { idx };
103
            callfunc.Invoke(this, parms);
140
            callfunc.Invoke(this, parms);
104
            
-
 
105
 
-
 
106
 
141
 
107
            idx++;
142
            idx++;
108
            if (idx >= devcount)
143
            if (idx >= devcount)
109
                idx = 0;
144
                idx = 0;
110
        }
145
        }
111
 
146
 
112
        public void init_aap(ref devdata dev) {
147
        public void init_aap(int devid) {
113
            // Enable the mcp23017
148
            // Enable the mcp23017
114
            WriteGpio(3, 1);
149
            WriteGpio(3, 0);
-
 
150
            this.delayms(500);
-
 
151
            this.WriteGpio(3, 1);
-
 
152
            this.delayms();
115
 
153
 
116
            // Set io dir, pullups and rev polarity
154
            // Set io dir, pullups and rev polarity
117
            byte[] data;
155
            byte[] data;
118
            data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
156
            data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
119
            WriteI2cData(0x40, data, 5);
157
            WriteI2cData(0x40, data, 5);
120
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
158
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
121
            WriteI2cData(0x40, data, 3);
159
            WriteI2cData(0x40, data, 3);
122
 
160
 
123
            refresh_aap(ref dev);
161
            refresh_aap(devid);
124
            dev.prev_input = dev.cur_input;
162
            devices[devid].prev_input = devices[devid].cur_input;
125
        }
163
        }
126
 
164
 
127
        public void refresh_aap(ref devdata dev) {
165
        public void refresh_aap(int devid) {
-
 
166
            refresh.Enabled = false;
-
 
167
            lock (lockobject) {
-
 
168
                byte[] data;
-
 
169
                data = new byte[] { 0x12 }; // Select GPIOA register
-
 
170
                WriteI2cData(0x40, data, 1);
-
 
171
 
-
 
172
                data = new byte[] { 0x00, 0x00 }; // Read two bytes
-
 
173
                int rslt = ReadI2CData(0x41, ref data, 2);
-
 
174
 
-
 
175
                // Join all our buttons into a single inputs
-
 
176
                uint gpio = (uint)((1 - ReadGpio(0)) | ((1 - ReadGpio(1)) << 1));
-
 
177
                devices[devid].cur_input = (uint)gpio << 16;
-
 
178
                devices[devid].cur_input |= (uint)data[0] << 8;
-
 
179
                devices[devid].cur_input |= (uint)data[1];
-
 
180
 
-
 
181
                if (devices[devid].cur_input != devices[devid].prev_input) {
-
 
182
                    this.input_aap(devid);
128
            Console.WriteLine("In aap " + usbi2c.Management.GetSelectedDevNum().ToString() + " : " + dev.name + " " + dev.cur_input.ToString("X"));
183
                    devices[devid].prev_input = devices[devid].cur_input;
-
 
184
                }
-
 
185
 
-
 
186
            }
-
 
187
            refresh.Enabled = enabled;
129
        }
188
        }
130
 
189
 
131
        public void init_ahfs(ref devdata dev) {
190
        public void input_aap(int devid) {
-
 
191
            //
132
            // Get the initial ADC value
192
            Switch2Pos(11, "AAP_CDUPWR", devid);
-
 
193
            //
-
 
194
            Switch2Pos(10, "AAP_EGIPWR", devid);
-
 
195
            //
-
 
196
            Switch3Pos(9, 8, "AAP_STEER", devid);
-
 
197
            //
-
 
198
            Selector(new int[] { 6, 5, 4 }, "AAP_STEERPT", devid);
-
 
199
            //
133
            //this._prev_adc = this.adc = _mcp.ReadADC(1);
200
            Selector(new int[] {0,1,2,3}, "AAP_PAGE", devid);
-
 
201
 
134
 
202
 
-
 
203
 
-
 
204
        }
-
 
205
 
-
 
206
        public void init_ahfs(int devid) {
-
 
207
            WriteGpio(3, 0);
-
 
208
            this.delayms(500);
135
            // Enable the mcp23017
209
            // Enable the mcp23017
136
            WriteGpio(3, 1);
210
            WriteGpio(3, 1);
137
 
-
 
-
 
211
            this.delayms();
138
            // Set io dir, pullups and rev polarity
212
            // Set io dir, pullups and rev polarity
139
            byte[] data;
213
            byte[] data;
140
            // Soldered a couple of inputs wrong, the f0 is to reverse them (fuel boost switches)
214
            // Soldered a couple of inputs wrong, the f0 is to reverse them (fuel boost switches)
141
            data = new byte[] { 0x00, 0xff, 0xff, 0xf0, 0xff }; // All inputs & polarity
215
            data = new byte[] { 0x00, 0xff, 0xff, 0xf0, 0xff }; // All inputs & polarity
142
            WriteI2cData(0x40, data, 5);
216
            WriteI2cData(0x40, data, 5);
Line 146... Line 220...
146
            data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
220
            data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
147
            WriteI2cData(0x42, data, 5);
221
            WriteI2cData(0x42, data, 5);
148
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
222
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
149
            WriteI2cData(0x42, data, 3);
223
            WriteI2cData(0x42, data, 3);
150
 
224
 
-
 
225
            // Get the initial ADC value
-
 
226
            devices[devid].prev_adc = devices[devid].cur_adc = ReadADC(1);
151
            refresh_ahfs(ref dev);
227
            refresh_ahfs(devid);
152
            dev.prev_input = dev.cur_input;
228
            devices[devid].prev_input = devices[devid].cur_input;
153
        }
229
        }
154
 
230
 
155
        public void refresh_ahfs(ref devdata dev) {
231
        public void refresh_ahfs(int devid) {
156
            byte[] data;
232
            refresh.Enabled = false;
157
            lock (lockobject) {
233
            lock (lockobject) {
-
 
234
                byte[] data;
158
                data = new byte[] { 0x12 }; // Select GPIOA register
235
                data = new byte[] { 0x12 }; // Select GPIOA register
159
                WriteI2cData(0x40, data, 1);
236
                WriteI2cData(0x40, data, 1);
160
 
237
 
161
                data = new byte[] { 0x00, 0x00 }; // Read two bytes
238
                data = new byte[] { 0x00, 0x00 }; // Read two bytes
162
                int rslt = ReadI2CData(0x41, ref data, 2);
239
                int rslt = ReadI2CData(0x41, ref data, 2);
163
 
240
 
164
                dev.cur_input = (uint)data[0] << 24;
241
                devices[devid].cur_input = (uint)data[0] << 24;
165
                dev.cur_input |= (uint)data[1] << 16;
242
                devices[devid].cur_input |= (uint)data[1] << 16;
166
 
243
 
167
                data = new byte[] { 0x12 }; // Select GPIOA register
244
                data = new byte[] { 0x12 }; // Select GPIOA register
168
                WriteI2cData(0x42, data, 1);
245
                WriteI2cData(0x42, data, 1);
169
 
246
 
170
                data = new byte[] { 0x00, 0x00 }; // Read two bytes
247
                data = new byte[] { 0x00, 0x00 }; // Read two bytes
171
                rslt = ReadI2CData(0x43, ref data, 2);
248
                rslt = ReadI2CData(0x43, ref data, 2);
172
 
249
 
173
                dev.cur_input |= (uint)data[0] << 8;
250
                devices[devid].cur_input |= (uint)data[0] << 8;
174
                dev.cur_input |= (uint)data[1];
251
                devices[devid].cur_input |= (uint)data[1];
-
 
252
 
-
 
253
                
-
 
254
 
-
 
255
                if (devices[devid].cur_input != devices[devid].prev_input) {
-
 
256
                    this.input_ahfs(devid);
-
 
257
                    devices[devid].prev_input = devices[devid].cur_input;
-
 
258
                }
-
 
259
 
-
 
260
 
-
 
261
                // Do the Refueling light adc
-
 
262
                devices[devid].cur_adc = ReadADC(1);
-
 
263
                
-
 
264
                if (devices[devid].cur_adc > devices[devid].max_adc)
-
 
265
                    devices[devid].max_adc = devices[devid].cur_adc;
-
 
266
 
-
 
267
                ushort lowerval = 0;
-
 
268
                if (devices[devid].prev_adc >= devices[devid].adc_thres)
-
 
269
                    lowerval = (ushort)(devices[devid].prev_adc - devices[devid].adc_thres);
-
 
270
 
-
 
271
                ushort upperval = devices[devid].max_adc;
-
 
272
                if (devices[devid].prev_adc <= upperval - devices[devid].adc_thres)
-
 
273
                    upperval = (ushort)(devices[devid].prev_adc + devices[devid].adc_thres);
-
 
274
 
-
 
275
                //Console.WriteLine("ADC: " + devices[devid].cur_adc + " Max: " + devices[devid].max_adc + " Low: " + lowerval + " High: " + upperval);
-
 
276
 
-
 
277
                if ((devices[devid].cur_adc < lowerval) || (devices[devid].cur_adc >= upperval)) {
-
 
278
                    // Cover our min/max ranges within threshold
-
 
279
                    if (devices[devid].cur_adc < devices[devid].adc_thres)
-
 
280
                        devices[devid].cur_adc = 0;
-
 
281
                    if (devices[devid].cur_adc > devices[devid].max_adc - devices[devid].adc_thres)
-
 
282
                        devices[devid].cur_adc = devices[devid].max_adc;
-
 
283
 
-
 
284
                    devices[devid].prev_adc = devices[devid].cur_adc;
-
 
285
                    
-
 
286
                    ushort refuellight = map_ushort(devices[devid].cur_adc, 0, devices[devid].max_adc, 0, 0xffff);
-
 
287
                    bios.SendData("ALCP_RCVR_LTS " + refuellight.ToString() + "\n");
-
 
288
                    Console.WriteLine("ALCP_RCVR_LTS " + ":" + refuellight.ToString() + "(" + devices[devid].cur_adc + ")");
-
 
289
                }
-
 
290
 
175
            }
291
            }
-
 
292
            refresh.Enabled = enabled;
-
 
293
        }
176
 
294
 
-
 
295
        public void input_ahfs(int devid) {
-
 
296
            // AHCP
-
 
297
            
-
 
298
            // Train - Safe - Arm
-
 
299
            Switch3Pos(8, 9, "AHCP_MASTER_ARM", devid);
-
 
300
            // Gunarm - Safe - Arm
-
 
301
            Switch3Pos(10, 11, "AHCP_GUNPAC", devid);
-
 
302
            // Train - Safe - Arm
-
 
303
            Switch3Pos(12, 13, "AHCP_LASER_ARM", devid);
-
 
304
            // Off - On
-
 
305
            Switch2Pos(14, "AHCP_TGP", devid);
-
 
306
            // Radar  - Delta - Baro
-
 
307
            Switch3Pos(0, 1, "AHCP_ALT_SCE", devid);
-
 
308
            // Night - Day
-
 
309
            Switch2Pos(2, "AHCP_HUD_DAYNIGHT", devid);
-
 
310
            //Stby - Norm
-
 
311
            Switch2Pos(15, "AHCP_HUD_MODE", devid);
-
 
312
            // Off - On
-
 
313
            Switch2Pos(3, "AHCP_CICU", devid);
-
 
314
            // Off - On
-
 
315
            Switch2Pos(4, "AHCP_JTRS", devid);
-
 
316
            //Off - Test - On
-
 
317
            Switch3Pos(6, 5, "AHCP_IFFCC", devid);
-
 
318
            //
-
 
319
            Switch2Pos(7, "HARS_FAST_ERECT", devid);
-
 
320
 
-
 
321
            // Fuel System
-
 
322
 
-
 
323
            // 
-
 
324
            Switch2Pos(16, "FSCP_AMPL", devid);
-
 
325
            // 
-
 
326
            Switch2Pos(24, "FSCP_BOOST_MAIN_L", devid);
-
 
327
            // 
-
 
328
            Switch2Pos(25, "FSCP_BOOST_MAIN_R", devid);
-
 
329
            // 
-
 
330
            Switch2Pos(26, "FSCP_BOOST_WING_L", devid);
-
 
331
            // 
-
 
332
            Switch2Pos(27, "FSCP_BOOST_WING_R", devid);
-
 
333
            // 
-
 
334
            Switch2Pos(28, "FSCP_CROSSFEED", devid);
-
 
335
            // 
-
 
336
            Switch2Pos(30, "FSCP_EXT_TANKS_FUS", devid);
-
 
337
            // 
-
 
338
            Switch2Pos(31, "FSCP_EXT_TANKS_WING", devid);
-
 
339
            // 
-
 
340
            Switch2Pos(20, "FSCP_FD_MAIN_L",devid, true);
-
 
341
            // 
-
 
342
            Switch2Pos(21, "FSCP_FD_MAIN_R", devid, true);
-
 
343
            // 
-
 
344
            Switch2Pos(18, "FSCP_FD_WING_L", devid, true);
-
 
345
            // 
-
 
346
            Switch2Pos(19, "FSCP_FD_WING_R", devid, true);
-
 
347
            // 
-
 
348
            Switch2Pos(17, "FSCP_LINE_CHECK", devid);
-
 
349
            // 
177
            Console.WriteLine("In ahfs " + usbi2c.Management.GetSelectedDevNum().ToString() + " : " + dev.name + " " + dev.cur_input.ToString("X"));
350
            Switch2Pos(23, "FSCP_RCVR_LEVER", devid); // Technically a 3pos, but 3rd not needed
-
 
351
            // 
-
 
352
            Switch2Pos(29, "FSCP_TK_GATE", devid);
178
        }
353
        }
179
 
354
 
180
 
355
 
181
        public byte ReadGpio(byte pinNum) {
356
        public byte ReadGpio(byte pinNum) {
182
            return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
357
            return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
Line 202... Line 377...
202
        public int ReadI2CData(byte address, ref byte[] data, uint count) {
377
        public int ReadI2CData(byte address, ref byte[] data, uint count) {
203
            int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
378
            int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
204
            return rslt;
379
            return rslt;
205
        }
380
        }
206
 
381
 
207
        public uint Switch2Pos(int pin, String cmd, devdata data, Boolean invert = false) {
382
        public uint Switch2Pos(int pin, String cmd, int devid, Boolean invert = false) {
208
            uint chg = (uint)(data.prev_input >> pin) & 0x01;
383
            uint chg = (uint)(devices[devid].prev_input >> pin) & 0x01;
209
            uint norm = (uint)(data.cur_input >> pin) & 0x01;
384
            uint norm = (uint)(devices[devid].cur_input >> pin) & 0x01;
210
            uint value = 0;
385
            uint value = 0;
211
 
386
 
212
            if ((uint)(norm) == 1) {
387
            if ((uint)(norm) == 1) {
213
                value = (uint)(invert ? 0 : 1);
388
                value = (uint)(invert ? 0 : 1);
-
 
389
            } else {
-
 
390
                value = (uint)(invert ? 1 : 0);
214
            }
391
            }
215
 
392
 
216
            if (norm != chg) {
393
            if (norm != chg) {
217
                bios.SendData(cmd + " " + value.ToString() + "\n");
394
                bios.SendData(cmd + " " + value.ToString() + "\n");
218
                Console.WriteLine(cmd + ":" + value.ToString());
395
                Console.WriteLine(cmd + ":" + value.ToString());
219
            }
396
            }
220
            return value;
397
            return value;
221
        }
398
        }
222
 
399
 
223
        public uint Switch3Pos(int pin0, int pin1, String cmd, devdata data, Boolean invert = false) {
400
        public uint Switch3Pos(int pin0, int pin1, String cmd, int devid, Boolean invert = false) {
224
            uint value = 1;
401
            uint value = 1;
225
            uint chg0 = (uint)(data.prev_input >> pin0) & 0x01;
402
            uint chg0 = (uint)(devices[devid].prev_input >> pin0) & 0x01;
226
            uint chg1 = (uint)(data.prev_input >> pin1) & 0x01;
403
            uint chg1 = (uint)(devices[devid].prev_input >> pin1) & 0x01;
227
            uint nrm0 = (uint)(data.cur_input >> pin0) & 0x01;
404
            uint nrm0 = (uint)(devices[devid].cur_input >> pin0) & 0x01;
228
            uint nrm1 = (uint)(data.cur_input >> pin1) & 0x01;
405
            uint nrm1 = (uint)(devices[devid].cur_input >> pin1) & 0x01;
229
 
406
 
230
            if ((uint)nrm0 == 1)
407
            if ((uint)nrm0 == 1)
231
                value = (uint)(invert ? 2 : 0);
408
                value = (uint)(invert ? 2 : 0);
232
            else if ((uint)nrm1 == 1)
409
            else if ((uint)nrm1 == 1)
233
                value = (uint)(invert ? 0 : 2);
410
                value = (uint)(invert ? 0 : 2);
Line 238... Line 415...
238
            }
415
            }
239
 
416
 
240
            return value;
417
            return value;
241
        }
418
        }
242
 
419
 
-
 
420
        public uint Selector(int[] pins, String cmd, int devid) {
-
 
421
            uint value = 0;
-
 
422
            uint chg = 0;
-
 
423
            uint norm = 0;
-
 
424
            uint mask = 0;
-
 
425
            for (int i = 0; i < pins.Length; i++)
-
 
426
                mask |= (uint)1 << i;
-
 
427
 
-
 
428
            for (int i = 0; i < pins.Length; i++) {
-
 
429
                chg |= (uint)(devices[devid].prev_input >> (pins[i] - i)) & mask;
-
 
430
                norm |= (uint)(devices[devid].cur_input >> (pins[i] - i)) & mask;
-
 
431
 
-
 
432
                if ((uint)((devices[devid].cur_input >> pins[i]) & 0x01) == 1) {
-
 
433
                    value = (uint)i;
-
 
434
                }
-
 
435
            }
-
 
436
 
-
 
437
            // This happens when between positions of the selector
-
 
438
            if (norm == 0)
-
 
439
                return 0;
-
 
440
 
-
 
441
 
-
 
442
            if (norm != chg) {
-
 
443
                bios.SendData(cmd + " " + value.ToString() + "\n");
-
 
444
                Console.WriteLine(cmd + ":" + value.ToString());
-
 
445
            }
-
 
446
 
-
 
447
            return value;
-
 
448
        }
-
 
449
 
-
 
450
        public void send_trackir(VirtualKeyCode key) {
-
 
451
            sendkey.Keyboard.KeyDown(VirtualKeyCode.CONTROL);
-
 
452
            System.Threading.Thread.Sleep(10);
-
 
453
            sendkey.Keyboard.KeyPress(key);
-
 
454
            sendkey.Keyboard.KeyUp(VirtualKeyCode.CONTROL);
-
 
455
        }
-
 
456
 
243
        public ushort map_ushort(ushort x, ushort in_min, ushort in_max, ushort out_min, ushort out_max) {
457
        public ushort map_ushort(ushort x, ushort in_min, ushort in_max, ushort out_min, ushort out_max) {
244
            return (ushort)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
458
            return (ushort)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
245
        }
459
        }
246
 
460
 
247
 
461