Subversion Repositories group.electronics

Rev

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