Subversion Repositories group.electronics

Rev

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

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