Subversion Repositories group.electronics

Rev

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

Rev 164 Rev 167
Line 4... Line 4...
4
using System.Text;
4
using System.Text;
5
using System.Threading.Tasks;
5
using System.Threading.Tasks;
6
using WindowsInput.Native;
6
using WindowsInput.Native;
7
 
7
 
8
namespace nitdcscore {
8
namespace nitdcscore {
-
 
9
 
-
 
10
    public class jsaxis {
-
 
11
        public UInt16 prev { get; set; }        // The previous ADC value
-
 
12
        public UInt16 value { get; set; }       // The current ADC value
-
 
13
        public UInt16 thres { get; set; }       // Threshold to report a new position
-
 
14
        public UInt16 max { get; set; }              // The maximum value we've seen
-
 
15
        public UInt16 mapsize { get; set; }     // The mapping maximum
-
 
16
 
-
 
17
        public jsaxis() {
-
 
18
 
-
 
19
        }
-
 
20
 
-
 
21
        public int getPosition(ref UInt16 position) {
-
 
22
            if (this.value > this.max)
-
 
23
                this.max = this.value;
-
 
24
 
-
 
25
            UInt16 lowerval = 0;
-
 
26
            if (this.prev >= this.thres)
-
 
27
                lowerval = (UInt16)(this.prev - this.thres);
-
 
28
 
-
 
29
            ushort upperval = this.max;
-
 
30
            if (this.prev <= upperval - this.thres)
-
 
31
                upperval = (ushort)(this.prev + this.thres);
-
 
32
 
-
 
33
            if ((this.value < lowerval) || (this.value >= upperval)) {
-
 
34
                // Cover our min/max ranges within threshold
-
 
35
                if (this.value < this.thres)
-
 
36
                    this.value = 0;
-
 
37
                if (this.value > this.max - this.thres)
-
 
38
                    this.value = this.max;
-
 
39
 
-
 
40
                this.prev = this.value;
-
 
41
 
-
 
42
                position = Globals.map_uint16(this.value, 0, this.max, 0, this.mapsize);
-
 
43
                return 1;
-
 
44
                //Globals.bios.SendData("ALCP_RCVR_LTS " + refuellight.ToString() + "\n");
-
 
45
                //Console.WriteLine("ALCP_RCVR_LTS " + ":" + refuellight.ToString() + "(" + devices[devid].cur_adc + ")");
-
 
46
            } else {
-
 
47
                position = 0;
-
 
48
                return 1;
-
 
49
            }
-
 
50
        }
-
 
51
 
-
 
52
 
-
 
53
    }
-
 
54
 
-
 
55
    public struct jsdata {
-
 
56
        public bool changed;
-
 
57
        public UInt64 buttons;
-
 
58
        public UInt64 prev;
-
 
59
 
-
 
60
        public jsaxis[] axis;
-
 
61
    }
-
 
62
 
-
 
63
 
9
    public interface IPanel {
64
    public interface IPanel {
10
        int Init();
65
        int Init();
11
        int Refresh();
66
        int Refresh();
12
        int Input();
67
        int Input();
13
    }
68
    }
14
 
69
 
15
    public abstract class Panel : IPanel {
70
    public abstract class Panel : IPanel {
-
 
71
        public jsdata data = new jsdata();
16
        protected mcp2221 mcp;
72
        protected mcp2221 mcp;
17
        public int id { get; set; }
73
        public int id { get; set; }
18
        public String name { get; set; }
74
        public String name { get; set; }
19
        public String serialno { get; set; }
75
        public String serialno { get; set; }
20
 
76
 
21
        public List<Control> controls = new List<Control>();
77
        public List<Control> controls = new List<Control>();
22
 
78
 
23
        public Boolean inputChanged { get; set; }
79
        //public Boolean inputChanged { get; set; }
24
 
80
 
25
 
81
 
26
        private Boolean enabled;
82
        private Boolean enabled;
27
        public Boolean Enabled {
83
        public Boolean Enabled {
28
            get {
84
            get {
Line 37... Line 93...
37
            this.mcp = mcp;
93
            this.mcp = mcp;
38
            this.mcp.usbi2c.Settings.GetConnectionStatus();
94
            this.mcp.usbi2c.Settings.GetConnectionStatus();
39
            this.id = this.mcp.usbi2c.Management.GetSelectedDevNum();
95
            this.id = this.mcp.usbi2c.Management.GetSelectedDevNum();
40
            this.name = this.mcp.usbi2c.Settings.GetUsbStringDescriptor();
96
            this.name = this.mcp.usbi2c.Settings.GetUsbStringDescriptor();
41
 
97
 
-
 
98
            this.data.axis[0] = new jsaxis();
-
 
99
            this.data.axis[1] = new jsaxis();
-
 
100
 
-
 
101
 
42
            this.inputChanged = false;
102
            data.changed = false;
43
        }
103
        }
44
 
104
 
45
        public virtual int Init() {
105
        public virtual int Init() {
46
            
106
            
47
 
107
 
Line 55... Line 115...
55
        }
115
        }
56
    }
116
    }
57
 
117
 
58
 
118
 
59
    public class Panel_AHFS : Panel {
119
    public class Panel_AHFS : Panel {
60
        Utils.InputPair switchInput;
120
        //Utils.InputPair switchInput;
61
        Utils.InputPair adcInput;
121
        //Utils.InputPair adcInput;
62
 
122
 
63
        public uint adc_max;
123
        //public uint adc_max;
64
        public uint adc_threshold;
124
        //public uint adc_threshold;
65
 
125
 
66
        private mcp23017[] chips;
126
        private mcp23017[] chips;
67
 
127
 
68
        public Panel_AHFS(mcp2221 mcp) : base(mcp) {
128
        public Panel_AHFS(mcp2221 mcp) : base(mcp) {
69
            chips = new mcp23017[2] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x21) };
129
            chips = new mcp23017[2] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x21) };
70
 
130
 
71
            switchInput.curr = 0;
131
            data.buttons = 0;
72
            switchInput.prev = 0;
132
            data.prev = 0;
-
 
133
 
73
            adcInput.curr = 0;
134
            data.axis[0].prev = data.axis[0].value = 0;
74
            adcInput.prev = 0;
135
            data.axis[0].thres = 15;
75
            this.adc_max = 930;
136
            data.axis[0].max = 930;
76
            this.adc_threshold = 15;
137
            data.axis[0].mapsize = 0xffff;
77
 
138
 
78
            this.Init();
139
            this.Init();
79
        }
140
        }
80
 
141
 
81
        public override int Init() {
142
        public override int Init() {
Line 88... Line 149...
88
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_DAYNIGHT"), 2));    // Night - Day
149
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_DAYNIGHT"), 2));    // Night - Day
89
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_MODE"), 15));       // Stby - Norm
150
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_MODE"), 15));       // Stby - Norm
90
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_CICU"), 3));            // Off - On
151
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_CICU"), 3));            // Off - On
91
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_JTRS"), 4));            // Off - On
152
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_JTRS"), 4));            // Off - On
92
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_IFFCC"), 6, 5));        // Off - Test - On
153
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_IFFCC"), 6, 5));        // Off - Test - On
93
            //this.addControl(new Switch2Pos(new CommandDCS("HARS_FAST_ERECT"), 7));      // Off - On
154
            this.addControl(new Switch2Pos(new CommandDCS("HARS_FAST_ERECT"), 7));      // Off - On
94
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 7));
155
            //this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 7));
-
 
156
            this.addControl(new Potentiometer(new CommandDCS("ALCP_RCVR_LTS"), 0));     // 0x00 - 0xFFFF
95
            
157
            
96
            // Fuel System
158
            // Fuel System
97
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_AMPL"), 16));
159
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_AMPL"), 16));
98
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_L"), 24));
160
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_L"), 24));
99
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_R"), 25));
161
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_R"), 25));
Line 122... Line 184...
122
 
184
 
123
            chips[1].SetIODirection(0xff, 0xff);
185
            chips[1].SetIODirection(0xff, 0xff);
124
            chips[1].SetIOPolarity(0xff, 0xff);
186
            chips[1].SetIOPolarity(0xff, 0xff);
125
            chips[1].SetIOPullups(0xff, 0xff);
187
            chips[1].SetIOPullups(0xff, 0xff);
126
 
188
 
127
            // Get the initial ADC value
189
            // Get the initial values
128
            adcInput.prev = adcInput.curr = mcp.ReadADC(1);
-
 
129
            this.Refresh();
190
            this.Refresh();
130
            switchInput.prev = switchInput.curr;
191
            data.prev = data.buttons;
-
 
192
            data.axis[0].prev = data.axis[0].value = mcp.ReadADC(1);
131
 
193
 
132
            return 0;
194
            return 0;
133
        }
195
        }
134
 
196
 
135
        public override int Refresh() {
197
        public override int Refresh() {
136
 
198
 
137
            byte[] data;
199
            byte[] bytes;
138
            int rslt = 0;
200
            int rslt = 0;
139
            rslt = chips[0].GetIO(out data);
201
            rslt = chips[0].GetIO(out bytes);
140
            
-
 
141
            switchInput.curr = (uint)data[0] << 24;
-
 
142
            switchInput.curr |= (uint)data[1] << 16;
-
 
143
 
202
 
144
            rslt = chips[1].GetIO(out data);
-
 
145
            
-
 
146
            switchInput.curr |= (uint)data[0] << 8;
203
            data.buttons = (uint)bytes[0] << 24;
147
            switchInput.curr |= (uint)data[1];
204
            data.buttons |= (uint)bytes[1] << 16;
148
 
-
 
149
            /*
-
 
150
            // Do the Refueling light adc
-
 
151
            devices[devid].cur_adc = mcp.ReadADC(1);
-
 
152
 
-
 
153
            if (devices[devid].cur_adc > devices[devid].max_adc)
-
 
154
                devices[devid].max_adc = devices[devid].cur_adc;
-
 
155
 
-
 
156
            ushort lowerval = 0;
-
 
157
            if (devices[devid].prev_adc >= devices[devid].adc_thres)
-
 
158
                lowerval = (ushort)(devices[devid].prev_adc - devices[devid].adc_thres);
-
 
159
 
205
 
160
            ushort upperval = devices[devid].max_adc;
206
            rslt = chips[1].GetIO(out bytes);
161
            if (devices[devid].prev_adc <= upperval - devices[devid].adc_thres)
-
 
162
                upperval = (ushort)(devices[devid].prev_adc + devices[devid].adc_thres);
-
 
163
 
207
 
164
            //Console.WriteLine("ADC: " + devices[devid].cur_adc + " Max: " + devices[devid].max_adc + " Low: " + lowerval + " High: " + upperval);
-
 
165
 
-
 
166
            if ((devices[devid].cur_adc < lowerval) || (devices[devid].cur_adc >= upperval)) {
-
 
167
                // Cover our min/max ranges within threshold
208
            data.buttons |= (uint)bytes[0] << 8;
168
                if (devices[devid].cur_adc < devices[devid].adc_thres)
-
 
169
                    devices[devid].cur_adc = 0;
209
            data.buttons |= (uint)bytes[1];
170
                if (devices[devid].cur_adc > devices[devid].max_adc - devices[devid].adc_thres)
-
 
171
                    devices[devid].cur_adc = devices[devid].max_adc;
-
 
172
 
-
 
173
                devices[devid].prev_adc = devices[devid].cur_adc;
-
 
174
 
-
 
175
                ushort refuellight = map_ushort(devices[devid].cur_adc, 0, devices[devid].max_adc, 0, 0xffff);
-
 
176
                Globals.bios.SendData("ALCP_RCVR_LTS " + refuellight.ToString() + "\n");
-
 
177
                Console.WriteLine("ALCP_RCVR_LTS " + ":" + refuellight.ToString() + "(" + devices[devid].cur_adc + ")");
-
 
178
            }
-
 
179
             * */
-
 
180
 
210
 
-
 
211
            data.axis[0].value = mcp.ReadADC(1);
181
 
212
 
182
            if ((switchInput.curr != switchInput.prev) || (adcInput.prev != adcInput.curr))
213
            if ((data.buttons != data.prev) || (data.axis[0].prev != data.axis[0].value))
183
                this.inputChanged = true;
214
                data.changed = true;
184
            else
215
            else
185
                this.inputChanged = false;
216
                data.changed = false;
186
 
217
 
187
            return 1;
218
            return 1;
188
        }
219
        }
189
 
220
 
190
        public override int Input() {
221
        public override int Input() {
191
            
222
            
192
            foreach (Control control in this.controls) {
223
            foreach (Control control in this.controls) {
193
                control.data = this.switchInput;
224
                control.data = this.data;
194
                control.Tick();
225
                control.Tick();
195
            }
226
            }
196
            switchInput.prev = switchInput.curr;
227
            data.prev = data.buttons;
197
            adcInput.prev = adcInput.curr;
228
            data.axis[0].prev = data.axis[0].value;
198
            this.inputChanged = false;
229
            data.changed = false;
199
            return 1;
230
            return 1;
200
        }
231
        }
201
    }
232
    }
202
 
233
 
203
    public class Panel_AAP : Panel {
234
    public class Panel_AAP : Panel {
204
 
235
 
205
        Utils.InputPair input;
-
 
206
        private mcp23017 chip0;
236
        private mcp23017 chip0;
207
 
237
 
208
        public Panel_AAP(mcp2221 mcp) : base(mcp) {
238
        public Panel_AAP(mcp2221 mcp) : base(mcp) {
209
            input.curr = 0;
239
            data.buttons = 0;
210
            input.prev = 0;
240
            data.prev = 0;
211
 
241
 
212
            chip0 = new mcp23017(mcp, 0x20);
242
            chip0 = new mcp23017(mcp, 0x20);
213
 
243
 
214
            this.Init();
244
            this.Init();
215
        }
245
        }
Line 236... Line 266...
236
            chip0.SetIODirection(0xff, 0xff);
266
            chip0.SetIODirection(0xff, 0xff);
237
            chip0.SetIOPolarity(0xff, 0xff);
267
            chip0.SetIOPolarity(0xff, 0xff);
238
            chip0.SetIOPullups(0xff, 0xff);
268
            chip0.SetIOPullups(0xff, 0xff);
239
 
269
 
240
            this.Refresh();
270
            this.Refresh();
241
            input.prev = input.curr;
271
            data.prev = data.buttons;
242
 
272
 
243
            return 1;
273
            return 1;
244
        }
274
        }
245
        public override int Refresh() {
275
        public override int Refresh() {
246
            byte[] data;
276
            byte[] bytes;
247
            int rslt = 0;
277
            int rslt = 0;
248
            rslt = chip0.GetIO(out data);
278
            rslt = chip0.GetIO(out bytes);
249
 
279
 
250
            // Join all our buttons into a single inputs
280
            // Join all our buttons into a single inputs
251
            uint gpio = (uint)((1 - mcp.ReadGpio(0)) | ((1 - mcp.ReadGpio(1)) << 1));
281
            uint gpio = (uint)((1 - mcp.ReadGpio(0)) | ((1 - mcp.ReadGpio(1)) << 1));
252
            input.curr = (uint)gpio << 16;
282
            data.buttons = (uint)gpio << 16;
253
            input.curr |= (uint)data[0] << 8;
283
            data.buttons |= (uint)bytes[0] << 8;
254
            input.curr |= (uint)data[1];
284
            data.buttons |= (uint)bytes[1];
255
 
285
 
256
            if (input.curr != input.prev)
286
            if (data.buttons != data.prev)
257
                this.inputChanged = true;
287
                data.changed = true;
258
            else
288
            else
259
                this.inputChanged = false;
289
                data.changed = false;
260
            
290
            
261
            return rslt;
291
            return rslt;
262
        }
292
        }
263
 
293
 
264
        public override int Input() {
294
        public override int Input() {
265
            //Console.WriteLine(input.curr.ToString("X"));
295
            //Console.WriteLine(input.curr.ToString("X"));
266
            foreach (Control control in this.controls) {
296
            foreach (Control control in this.controls) {
267
                control.data = this.input;
297
                control.data = this.data;
268
                control.Tick();
298
                control.Tick();
269
            }
299
            }
270
            input.prev = input.curr;
300
            data.prev = data.buttons;
271
            this.inputChanged = false;
301
            data.changed = false;
272
            return 1;
302
            return 1;
273
        }
303
        }
274
    }
304
    }
275
}
305
}