Subversion Repositories group.electronics

Rev

Rev 160 | Rev 162 | 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;
6
 
7
namespace nitdcscore {
8
    public interface IPanel {
9
        int Init();
10
        int Refresh();
11
        int Input();
12
    }
13
 
14
    public abstract class Panel : IPanel {
15
        protected mcp2221 mcp;
16
        public int id { get; set; }
17
        public String name { get; set; }
18
        public String serialno { get; set; }
19
 
161 pfowler 20
        public List<Control> controls = new List<Control>();
21
 
22
        public Boolean inputChanged { get; set; }
23
 
24
 
160 pfowler 25
        private Boolean enabled;
26
        public Boolean Enabled {
27
            get {
28
                return enabled;
29
            }
30
            set {
161 pfowler 31
                this.enabled = value;
160 pfowler 32
            }
33
        }
34
 
161 pfowler 35
        public Panel(mcp2221 mcp) {
160 pfowler 36
            this.mcp = mcp;
161 pfowler 37
            this.mcp.usbi2c.Settings.GetConnectionStatus();
160 pfowler 38
            this.id = this.mcp.usbi2c.Management.GetSelectedDevNum();
39
            this.name = this.mcp.usbi2c.Settings.GetUsbStringDescriptor();
161 pfowler 40
 
41
            this.inputChanged = false;
160 pfowler 42
        }
43
 
44
        public virtual int Init() {
45
 
46
 
47
            return 1;
48
        }
49
        public abstract int Refresh();
50
        public abstract int Input();
161 pfowler 51
 
52
        public void addControl(Control control) {
53
            this.controls.Add(control);
54
        }
160 pfowler 55
    }
56
 
161 pfowler 57
 
58
    public class Panel_AHFS : Panel {
59
        Utils.InputPair switchInput;
60
        Utils.InputPair adcInput;
61
 
62
        public uint adc_max;
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
        }
160 pfowler 190
    }
191
 
192
    public class Panel_AAP : Panel {
193
 
161 pfowler 194
        Utils.InputPair input;
160 pfowler 195
 
161 pfowler 196
        public Panel_AAP(mcp2221 mcp) : base(mcp) {
160 pfowler 197
            input.curr = 0;
198
            input.prev = 0;
199
 
161 pfowler 200
            this.Init();
160 pfowler 201
        }
202
 
203
        public override int Init() {
161 pfowler 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
 
160 pfowler 210
            // Enable the mcp23017
211
            mcp.WriteGpio(3, 0);
212
            Utils.delayms(500);
213
            mcp.WriteGpio(3, 1);
214
            //this.delayms();
215
 
216
            // Set io dir, pullups and rev polarity
217
            byte[] data;
218
            data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
219
            mcp.WriteI2cData(0x40, data, 5);
220
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
221
            mcp.WriteI2cData(0x40, data, 3);
222
 
161 pfowler 223
            this.Refresh();
224
            input.prev = input.curr;
160 pfowler 225
 
226
            return 1;
227
        }
228
        public override int Refresh() {
161 pfowler 229
            byte[] data;
230
            data = new byte[] { 0x12 }; // Select GPIOA register
231
            mcp.WriteI2cData(0x40, data, 1);
160 pfowler 232
 
161 pfowler 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
 
247
            return rslt;
160 pfowler 248
        }
249
 
250
        public override int Input() {
161 pfowler 251
            foreach (Control control in this.controls) {
252
                control.data = this.input;
253
                control.Tick();
254
            }
255
            input.prev = input.curr;
256
            this.inputChanged = false;
160 pfowler 257
            return 1;
258
        }
259
    }
260
}