Subversion Repositories group.electronics

Rev

Rev 161 | Rev 164 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
160 pfowler 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
162 pfowler 6
using WindowsInput.Native;
160 pfowler 7
 
8
namespace nitdcscore {
9
    public interface IPanel {
10
        int Init();
11
        int Refresh();
12
        int Input();
13
    }
14
 
15
    public abstract class Panel : IPanel {
16
        protected mcp2221 mcp;
17
        public int id { get; set; }
18
        public String name { get; set; }
19
        public String serialno { get; set; }
20
 
161 pfowler 21
        public List<Control> controls = new List<Control>();
22
 
23
        public Boolean inputChanged { get; set; }
24
 
25
 
160 pfowler 26
        private Boolean enabled;
27
        public Boolean Enabled {
28
            get {
29
                return enabled;
30
            }
31
            set {
161 pfowler 32
                this.enabled = value;
160 pfowler 33
            }
34
        }
35
 
161 pfowler 36
        public Panel(mcp2221 mcp) {
160 pfowler 37
            this.mcp = mcp;
161 pfowler 38
            this.mcp.usbi2c.Settings.GetConnectionStatus();
160 pfowler 39
            this.id = this.mcp.usbi2c.Management.GetSelectedDevNum();
40
            this.name = this.mcp.usbi2c.Settings.GetUsbStringDescriptor();
161 pfowler 41
 
42
            this.inputChanged = false;
160 pfowler 43
        }
44
 
45
        public virtual int Init() {
46
 
47
 
48
            return 1;
49
        }
50
        public abstract int Refresh();
51
        public abstract int Input();
161 pfowler 52
 
53
        public void addControl(Control control) {
54
            this.controls.Add(control);
55
        }
160 pfowler 56
    }
57
 
161 pfowler 58
 
59
    public class Panel_AHFS : Panel {
60
        Utils.InputPair switchInput;
61
        Utils.InputPair adcInput;
62
 
63
        public uint adc_max;
64
        public uint adc_threshold;
65
 
66
        public Panel_AHFS(mcp2221 mcp) : base(mcp) {
67
            switchInput.curr = 0;
68
            switchInput.prev = 0;
69
            adcInput.curr = 0;
70
            adcInput.prev = 0;
71
            this.adc_max = 930;
72
            this.adc_threshold = 15;
73
 
74
            this.Init();
75
        }
76
 
77
        public override int Init() {
162 pfowler 78
            //AHCP
161 pfowler 79
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_MASTER_ARM"), 8, 9));   // Train - Safe - Arm
80
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_GUNPAC"), 10, 11));     // Gunarm - Safe - Arm
81
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_LASER_ARM"), 12, 13));  // Train - Safe - Arm
82
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_TGP"), 14));            // Off - On
83
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_ALT_SCE"), 0, 1));      // Radar  - Delta - Baro
84
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_DAYNIGHT"), 2));    // Night - Day
85
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_MODE"), 15));       // Stby - Norm
86
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_CICU"), 3));            // Off - On
87
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_JTRS"), 4));            // Off - On
88
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_IFFCC"), 6, 5));        // Off - Test - On
162 pfowler 89
            //this.addControl(new Switch2Pos(new CommandDCS("HARS_FAST_ERECT"), 7));      // Off - On
90
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 7));
161 pfowler 91
 
162 pfowler 92
            // Fuel System
93
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_AMPL"), 16));
94
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_L"), 24));
95
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_R"), 25));
96
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_WING_L"), 26));
97
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_WING_R"),27 ));
98
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_CROSSFEED"), 28));
99
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_EXT_TANKS_FUS"), 30));
100
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_EXT_TANKS_WING"), 31));
101
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_MAIN_L"), 20, true));
102
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_MAIN_R"), 21, true));
103
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_WING_L"), 18, true));
104
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_WING_R"), 19, true));
105
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_LINE_CHECK"), 17));
106
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_RCVR_LEVER"), 23));
107
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_TK_GATE"), 29));
161 pfowler 108
 
109
            mcp.WriteGpio(3, 0);
110
            Utils.delayms(500);
111
            // Enable the mcp23017
112
            mcp.WriteGpio(3, 1);
113
            Utils.delayms();
114
            // Set io dir, pullups and rev polarity
115
            byte[] data;
116
            // Soldered a couple of inputs wrong, the f0 is to reverse them (fuel boost switches)
117
            data = new byte[] { 0x00, 0xff, 0xff, 0xf0, 0xff }; // All inputs & polarity
118
            mcp.WriteI2cData(0x40, data, 5);
119
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
120
            mcp.WriteI2cData(0x40, data, 3);
121
 
122
            data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
123
            mcp.WriteI2cData(0x42, data, 5);
124
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
125
            int rslt = mcp.WriteI2cData(0x42, data, 3);
126
 
127
            // Get the initial ADC value
128
            adcInput.prev = adcInput.curr = mcp.ReadADC(1);
129
            this.Refresh();
130
            switchInput.prev = switchInput.curr;
131
 
132
            return rslt;
133
        }
134
 
135
        public override int Refresh() {
136
 
137
            byte[] data;
138
            data = new byte[] { 0x12 }; // Select GPIOA register
139
            mcp.WriteI2cData(0x40, data, 1);
140
 
141
            data = new byte[] { 0x00, 0x00 }; // Read two bytes
142
            mcp.ReadI2CData(0x41, ref data, 2);
143
 
144
            switchInput.curr = (uint)data[0] << 24;
145
            switchInput.curr |= (uint)data[1] << 16;
146
 
147
            data = new byte[] { 0x12 }; // Select GPIOA register
148
            mcp.WriteI2cData(0x42, data, 1);
149
 
150
            data = new byte[] { 0x00, 0x00 }; // Read two bytes
151
            mcp.ReadI2CData(0x43, ref data, 2);
152
 
153
            switchInput.curr |= (uint)data[0] << 8;
154
            switchInput.curr |= (uint)data[1];
155
 
156
            /*
157
            // Do the Refueling light adc
158
            devices[devid].cur_adc = mcp.ReadADC(1);
159
 
160
            if (devices[devid].cur_adc > devices[devid].max_adc)
161
                devices[devid].max_adc = devices[devid].cur_adc;
162
 
163
            ushort lowerval = 0;
164
            if (devices[devid].prev_adc >= devices[devid].adc_thres)
165
                lowerval = (ushort)(devices[devid].prev_adc - devices[devid].adc_thres);
166
 
167
            ushort upperval = devices[devid].max_adc;
168
            if (devices[devid].prev_adc <= upperval - devices[devid].adc_thres)
169
                upperval = (ushort)(devices[devid].prev_adc + devices[devid].adc_thres);
170
 
171
            //Console.WriteLine("ADC: " + devices[devid].cur_adc + " Max: " + devices[devid].max_adc + " Low: " + lowerval + " High: " + upperval);
172
 
173
            if ((devices[devid].cur_adc < lowerval) || (devices[devid].cur_adc >= upperval)) {
174
                // Cover our min/max ranges within threshold
175
                if (devices[devid].cur_adc < devices[devid].adc_thres)
176
                    devices[devid].cur_adc = 0;
177
                if (devices[devid].cur_adc > devices[devid].max_adc - devices[devid].adc_thres)
178
                    devices[devid].cur_adc = devices[devid].max_adc;
179
 
180
                devices[devid].prev_adc = devices[devid].cur_adc;
181
 
182
                ushort refuellight = map_ushort(devices[devid].cur_adc, 0, devices[devid].max_adc, 0, 0xffff);
183
                Globals.bios.SendData("ALCP_RCVR_LTS " + refuellight.ToString() + "\n");
184
                Console.WriteLine("ALCP_RCVR_LTS " + ":" + refuellight.ToString() + "(" + devices[devid].cur_adc + ")");
185
            }
186
             * */
187
 
188
 
189
            if ((switchInput.curr != switchInput.prev) || (adcInput.prev != adcInput.curr))
190
                this.inputChanged = true;
191
            else
192
                this.inputChanged = false;
193
 
194
            return 1;
195
        }
196
 
197
        public override int Input() {
198
            foreach (Control control in this.controls) {
199
                control.data = this.switchInput;
200
                control.Tick();
201
            }
202
            switchInput.prev = switchInput.curr;
203
            adcInput.prev = adcInput.curr;
204
            this.inputChanged = false;
205
            return 1;
206
        }
160 pfowler 207
    }
208
 
209
    public class Panel_AAP : Panel {
210
 
161 pfowler 211
        Utils.InputPair input;
160 pfowler 212
 
161 pfowler 213
        public Panel_AAP(mcp2221 mcp) : base(mcp) {
160 pfowler 214
            input.curr = 0;
215
            input.prev = 0;
216
 
161 pfowler 217
            this.Init();
160 pfowler 218
        }
219
 
220
        public override int Init() {
162 pfowler 221
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 16));
222
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F11), 17));
223
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F7), 12));
224
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.SPACE), 13));
225
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.RETURN), 14));
226
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.VOLUME_MUTE), 15));
161 pfowler 227
            this.addControl(new Switch2Pos(new CommandDCS("AAP_CDUPWR"), 11));
228
            this.addControl(new Switch2Pos(new CommandDCS("AAP_EGIPWR"), 10));
229
            this.addControl(new Switch3Pos(new CommandDCS("AAP_STEER"), 9, 8));
230
            this.addControl(new Selector(new CommandDCS("AAP_STEERPT"), new int[] { 4, 5, 6 }));
231
            this.addControl(new Selector(new CommandDCS("AAP_PAGE"), new int[] { 0, 1, 2, 3 }));
232
 
160 pfowler 233
            // Enable the mcp23017
234
            mcp.WriteGpio(3, 0);
235
            Utils.delayms(500);
236
            mcp.WriteGpio(3, 1);
237
            //this.delayms();
238
 
239
            // Set io dir, pullups and rev polarity
240
            byte[] data;
241
            data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
242
            mcp.WriteI2cData(0x40, data, 5);
243
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
244
            mcp.WriteI2cData(0x40, data, 3);
245
 
161 pfowler 246
            this.Refresh();
247
            input.prev = input.curr;
160 pfowler 248
 
249
            return 1;
250
        }
251
        public override int Refresh() {
161 pfowler 252
            byte[] data;
253
            data = new byte[] { 0x12 }; // Select GPIOA register
254
            mcp.WriteI2cData(0x40, data, 1);
160 pfowler 255
 
161 pfowler 256
            data = new byte[] { 0x00, 0x00 }; // Read two bytes
257
            int rslt = mcp.ReadI2CData(0x41, ref data, 2);
258
 
259
            // Join all our buttons into a single inputs
260
            uint gpio = (uint)((1 - mcp.ReadGpio(0)) | ((1 - mcp.ReadGpio(1)) << 1));
261
            input.curr = (uint)gpio << 16;
262
            input.curr |= (uint)data[0] << 8;
263
            input.curr |= (uint)data[1];
264
 
265
            if (input.curr != input.prev)
266
                this.inputChanged = true;
267
            else
268
                this.inputChanged = false;
269
 
270
            return rslt;
160 pfowler 271
        }
272
 
273
        public override int Input() {
162 pfowler 274
            //Console.WriteLine(input.curr.ToString("X"));
161 pfowler 275
            foreach (Control control in this.controls) {
276
                control.data = this.input;
277
                control.Tick();
278
            }
279
            input.prev = input.curr;
280
            this.inputChanged = false;
160 pfowler 281
            return 1;
282
        }
283
    }
284
}