Subversion Repositories group.electronics

Rev

Rev 164 | Rev 168 | 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 {
167 pfowler 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
 
160 pfowler 64
    public interface IPanel {
65
        int Init();
66
        int Refresh();
67
        int Input();
68
    }
69
 
70
    public abstract class Panel : IPanel {
167 pfowler 71
        public jsdata data = new jsdata();
160 pfowler 72
        protected mcp2221 mcp;
73
        public int id { get; set; }
74
        public String name { get; set; }
75
        public String serialno { get; set; }
76
 
161 pfowler 77
        public List<Control> controls = new List<Control>();
78
 
167 pfowler 79
        //public Boolean inputChanged { get; set; }
161 pfowler 80
 
81
 
160 pfowler 82
        private Boolean enabled;
83
        public Boolean Enabled {
84
            get {
85
                return enabled;
86
            }
87
            set {
161 pfowler 88
                this.enabled = value;
160 pfowler 89
            }
90
        }
91
 
161 pfowler 92
        public Panel(mcp2221 mcp) {
160 pfowler 93
            this.mcp = mcp;
161 pfowler 94
            this.mcp.usbi2c.Settings.GetConnectionStatus();
160 pfowler 95
            this.id = this.mcp.usbi2c.Management.GetSelectedDevNum();
96
            this.name = this.mcp.usbi2c.Settings.GetUsbStringDescriptor();
161 pfowler 97
 
167 pfowler 98
            this.data.axis[0] = new jsaxis();
99
            this.data.axis[1] = new jsaxis();
100
 
101
 
102
            data.changed = false;
160 pfowler 103
        }
104
 
105
        public virtual int Init() {
106
 
107
 
108
            return 1;
109
        }
110
        public abstract int Refresh();
111
        public abstract int Input();
161 pfowler 112
 
113
        public void addControl(Control control) {
114
            this.controls.Add(control);
115
        }
160 pfowler 116
    }
117
 
161 pfowler 118
 
119
    public class Panel_AHFS : Panel {
167 pfowler 120
        //Utils.InputPair switchInput;
121
        //Utils.InputPair adcInput;
161 pfowler 122
 
167 pfowler 123
        //public uint adc_max;
124
        //public uint adc_threshold;
161 pfowler 125
 
164 pfowler 126
        private mcp23017[] chips;
127
 
161 pfowler 128
        public Panel_AHFS(mcp2221 mcp) : base(mcp) {
164 pfowler 129
            chips = new mcp23017[2] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x21) };
130
 
167 pfowler 131
            data.buttons = 0;
132
            data.prev = 0;
161 pfowler 133
 
167 pfowler 134
            data.axis[0].prev = data.axis[0].value = 0;
135
            data.axis[0].thres = 15;
136
            data.axis[0].max = 930;
137
            data.axis[0].mapsize = 0xffff;
138
 
161 pfowler 139
            this.Init();
140
        }
141
 
142
        public override int Init() {
162 pfowler 143
            //AHCP
161 pfowler 144
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_MASTER_ARM"), 8, 9));   // Train - Safe - Arm
145
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_GUNPAC"), 10, 11));     // Gunarm - Safe - Arm
146
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_LASER_ARM"), 12, 13));  // Train - Safe - Arm
147
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_TGP"), 14));            // Off - On
148
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_ALT_SCE"), 0, 1));      // Radar  - Delta - Baro
149
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_DAYNIGHT"), 2));    // Night - Day
150
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_MODE"), 15));       // Stby - Norm
151
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_CICU"), 3));            // Off - On
152
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_JTRS"), 4));            // Off - On
153
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_IFFCC"), 6, 5));        // Off - Test - On
167 pfowler 154
            this.addControl(new Switch2Pos(new CommandDCS("HARS_FAST_ERECT"), 7));      // Off - On
155
            //this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 7));
156
            this.addControl(new Potentiometer(new CommandDCS("ALCP_RCVR_LTS"), 0));     // 0x00 - 0xFFFF
161 pfowler 157
 
162 pfowler 158
            // Fuel System
159
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_AMPL"), 16));
160
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_L"), 24));
161
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_R"), 25));
162
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_WING_L"), 26));
163
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_WING_R"),27 ));
164
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_CROSSFEED"), 28));
165
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_EXT_TANKS_FUS"), 30));
166
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_EXT_TANKS_WING"), 31));
167
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_MAIN_L"), 20, true));
168
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_MAIN_R"), 21, true));
169
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_WING_L"), 18, true));
170
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_WING_R"), 19, true));
171
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_LINE_CHECK"), 17));
172
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_RCVR_LEVER"), 23));
173
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_TK_GATE"), 29));
161 pfowler 174
 
175
            mcp.WriteGpio(3, 0);
164 pfowler 176
            Utils.delayms(10);
161 pfowler 177
            // Enable the mcp23017
178
            mcp.WriteGpio(3, 1);
164 pfowler 179
 
161 pfowler 180
            // Set io dir, pullups and rev polarity
164 pfowler 181
            chips[0].SetIODirection(0xff, 0xff);
182
            chips[0].SetIOPolarity(0xf0, 0xff);
183
            chips[0].SetIOPullups(0xff, 0xff);
161 pfowler 184
 
164 pfowler 185
            chips[1].SetIODirection(0xff, 0xff);
186
            chips[1].SetIOPolarity(0xff, 0xff);
187
            chips[1].SetIOPullups(0xff, 0xff);
161 pfowler 188
 
167 pfowler 189
            // Get the initial values
161 pfowler 190
            this.Refresh();
167 pfowler 191
            data.prev = data.buttons;
192
            data.axis[0].prev = data.axis[0].value = mcp.ReadADC(1);
161 pfowler 193
 
164 pfowler 194
            return 0;
161 pfowler 195
        }
196
 
197
        public override int Refresh() {
198
 
167 pfowler 199
            byte[] bytes;
164 pfowler 200
            int rslt = 0;
167 pfowler 201
            rslt = chips[0].GetIO(out bytes);
164 pfowler 202
 
167 pfowler 203
            data.buttons = (uint)bytes[0] << 24;
204
            data.buttons |= (uint)bytes[1] << 16;
161 pfowler 205
 
167 pfowler 206
            rslt = chips[1].GetIO(out bytes);
161 pfowler 207
 
167 pfowler 208
            data.buttons |= (uint)bytes[0] << 8;
209
            data.buttons |= (uint)bytes[1];
161 pfowler 210
 
167 pfowler 211
            data.axis[0].value = mcp.ReadADC(1);
161 pfowler 212
 
167 pfowler 213
            if ((data.buttons != data.prev) || (data.axis[0].prev != data.axis[0].value))
214
                data.changed = true;
161 pfowler 215
            else
167 pfowler 216
                data.changed = false;
161 pfowler 217
 
218
            return 1;
219
        }
220
 
221
        public override int Input() {
164 pfowler 222
 
161 pfowler 223
            foreach (Control control in this.controls) {
167 pfowler 224
                control.data = this.data;
161 pfowler 225
                control.Tick();
226
            }
167 pfowler 227
            data.prev = data.buttons;
228
            data.axis[0].prev = data.axis[0].value;
229
            data.changed = false;
161 pfowler 230
            return 1;
231
        }
160 pfowler 232
    }
233
 
234
    public class Panel_AAP : Panel {
235
 
164 pfowler 236
        private mcp23017 chip0;
160 pfowler 237
 
161 pfowler 238
        public Panel_AAP(mcp2221 mcp) : base(mcp) {
167 pfowler 239
            data.buttons = 0;
240
            data.prev = 0;
160 pfowler 241
 
164 pfowler 242
            chip0 = new mcp23017(mcp, 0x20);
243
 
161 pfowler 244
            this.Init();
160 pfowler 245
        }
246
 
247
        public override int Init() {
162 pfowler 248
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 16));
249
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F11), 17));
250
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F7), 12));
251
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.SPACE), 13));
252
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.RETURN), 14));
253
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.VOLUME_MUTE), 15));
161 pfowler 254
            this.addControl(new Switch2Pos(new CommandDCS("AAP_CDUPWR"), 11));
255
            this.addControl(new Switch2Pos(new CommandDCS("AAP_EGIPWR"), 10));
256
            this.addControl(new Switch3Pos(new CommandDCS("AAP_STEER"), 9, 8));
257
            this.addControl(new Selector(new CommandDCS("AAP_STEERPT"), new int[] { 4, 5, 6 }));
258
            this.addControl(new Selector(new CommandDCS("AAP_PAGE"), new int[] { 0, 1, 2, 3 }));
259
 
160 pfowler 260
            // Enable the mcp23017
261
            mcp.WriteGpio(3, 0);
164 pfowler 262
            Utils.delayms(10);
160 pfowler 263
            mcp.WriteGpio(3, 1);
264
 
265
            // Set io dir, pullups and rev polarity
164 pfowler 266
            chip0.SetIODirection(0xff, 0xff);
267
            chip0.SetIOPolarity(0xff, 0xff);
268
            chip0.SetIOPullups(0xff, 0xff);
160 pfowler 269
 
161 pfowler 270
            this.Refresh();
167 pfowler 271
            data.prev = data.buttons;
160 pfowler 272
 
273
            return 1;
274
        }
275
        public override int Refresh() {
167 pfowler 276
            byte[] bytes;
164 pfowler 277
            int rslt = 0;
167 pfowler 278
            rslt = chip0.GetIO(out bytes);
160 pfowler 279
 
161 pfowler 280
            // Join all our buttons into a single inputs
281
            uint gpio = (uint)((1 - mcp.ReadGpio(0)) | ((1 - mcp.ReadGpio(1)) << 1));
167 pfowler 282
            data.buttons = (uint)gpio << 16;
283
            data.buttons |= (uint)bytes[0] << 8;
284
            data.buttons |= (uint)bytes[1];
161 pfowler 285
 
167 pfowler 286
            if (data.buttons != data.prev)
287
                data.changed = true;
161 pfowler 288
            else
167 pfowler 289
                data.changed = false;
161 pfowler 290
 
291
            return rslt;
160 pfowler 292
        }
293
 
294
        public override int Input() {
162 pfowler 295
            //Console.WriteLine(input.curr.ToString("X"));
161 pfowler 296
            foreach (Control control in this.controls) {
167 pfowler 297
                control.data = this.data;
161 pfowler 298
                control.Tick();
299
            }
167 pfowler 300
            data.prev = data.buttons;
301
            data.changed = false;
160 pfowler 302
            return 1;
303
        }
304
    }
305
}