Subversion Repositories group.electronics

Rev

Rev 169 | Rev 171 | 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
 
170 pfowler 33
            //Console.WriteLine("JS " + ":" + this.prev + "," + this.value + "," + this.max + "," + lowerval + "," + upperval);
34
 
35
            if ((this.value < lowerval) || (this.value > upperval)) {
167 pfowler 36
                // Cover our min/max ranges within threshold
37
                if (this.value < this.thres)
38
                    this.value = 0;
39
                if (this.value > this.max - this.thres)
40
                    this.value = this.max;
41
 
170 pfowler 42
 
167 pfowler 43
                this.prev = this.value;
44
 
45
                position = Globals.map_uint16(this.value, 0, this.max, 0, this.mapsize);
170 pfowler 46
 
47
                return true;               
167 pfowler 48
            }
170 pfowler 49
 
50
            return false;
167 pfowler 51
        }
52
 
53
 
54
    }
55
 
170 pfowler 56
    public class jsdata {
57
        public bool changed { get; set; }
58
        public UInt64 buttons { get; set; }
59
        public UInt64 prev { get; set; }
167 pfowler 60
 
170 pfowler 61
        public jsaxis[] axis = new jsaxis[6];
62
 
63
        public jsdata() {
64
            changed = false;
65
            prev = buttons = 0;
66
 
67
            for (int i = 0; i < 6; i++) {
68
                axis[i] = new jsaxis();
69
                axis[i].thres = Globals.ADC_THRESHOLD;
70
                axis[i].max = Globals.ADC_DEFMAX;
71
                axis[i].prev = axis[i].value = 0;
72
                axis[i].mapsize = 0xffff;
73
            }            
74
        }
167 pfowler 75
    }
76
 
77
 
160 pfowler 78
    public interface IPanel {
79
        int Init();
80
        int Refresh();
81
        int Input();
82
    }
83
 
84
    public abstract class Panel : IPanel {
167 pfowler 85
        public jsdata data = new jsdata();
160 pfowler 86
        protected mcp2221 mcp;
87
        public int id { get; set; }
88
        public String name { get; set; }
89
        public String serialno { get; set; }
90
 
161 pfowler 91
        public List<Control> controls = new List<Control>();
92
 
160 pfowler 93
        private Boolean enabled;
94
        public Boolean Enabled {
95
            get {
96
                return enabled;
97
            }
98
            set {
161 pfowler 99
                this.enabled = value;
160 pfowler 100
            }
101
        }
102
 
161 pfowler 103
        public Panel(mcp2221 mcp) {
160 pfowler 104
            this.mcp = mcp;
161 pfowler 105
            this.mcp.usbi2c.Settings.GetConnectionStatus();
160 pfowler 106
            this.id = this.mcp.usbi2c.Management.GetSelectedDevNum();
107
            this.name = this.mcp.usbi2c.Settings.GetUsbStringDescriptor();
161 pfowler 108
 
160 pfowler 109
        }
110
 
111
        public virtual int Init() {
112
 
113
 
114
            return 1;
115
        }
116
        public abstract int Refresh();
117
        public abstract int Input();
161 pfowler 118
 
119
        public void addControl(Control control) {
120
            this.controls.Add(control);
121
        }
160 pfowler 122
    }
123
 
161 pfowler 124
 
125
    public class Panel_AHFS : Panel {
126
 
164 pfowler 127
        private mcp23017[] chips;
128
 
161 pfowler 129
        public Panel_AHFS(mcp2221 mcp) : base(mcp) {
164 pfowler 130
            chips = new mcp23017[2] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x21) };
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;
169 pfowler 183
            data.axis[0].prev = data.axis[0].value;
184
            data.changed = false;
161 pfowler 185
 
164 pfowler 186
            return 0;
161 pfowler 187
        }
188
 
189
        public override int Refresh() {
190
 
167 pfowler 191
            byte[] bytes;
164 pfowler 192
            int rslt = 0;
167 pfowler 193
            rslt = chips[0].GetIO(out bytes);
164 pfowler 194
 
167 pfowler 195
            data.buttons = (uint)bytes[0] << 24;
196
            data.buttons |= (uint)bytes[1] << 16;
161 pfowler 197
 
167 pfowler 198
            rslt = chips[1].GetIO(out bytes);
161 pfowler 199
 
167 pfowler 200
            data.buttons |= (uint)bytes[0] << 8;
201
            data.buttons |= (uint)bytes[1];
161 pfowler 202
 
167 pfowler 203
            data.axis[0].value = mcp.ReadADC(1);
161 pfowler 204
 
167 pfowler 205
            if ((data.buttons != data.prev) || (data.axis[0].prev != data.axis[0].value))
206
                data.changed = true;
161 pfowler 207
            else
167 pfowler 208
                data.changed = false;
161 pfowler 209
 
210
            return 1;
211
        }
212
 
213
        public override int Input() {
164 pfowler 214
 
161 pfowler 215
            foreach (Control control in this.controls) {
167 pfowler 216
                control.data = this.data;
161 pfowler 217
                control.Tick();
218
            }
167 pfowler 219
            data.prev = data.buttons;
220
            data.axis[0].prev = data.axis[0].value;
221
            data.changed = false;
161 pfowler 222
            return 1;
223
        }
160 pfowler 224
    }
225
 
226
    public class Panel_AAP : Panel {
227
 
164 pfowler 228
        private mcp23017 chip0;
160 pfowler 229
 
161 pfowler 230
        public Panel_AAP(mcp2221 mcp) : base(mcp) {
167 pfowler 231
            data.buttons = 0;
232
            data.prev = 0;
160 pfowler 233
 
164 pfowler 234
            chip0 = new mcp23017(mcp, 0x20);
235
 
161 pfowler 236
            this.Init();
160 pfowler 237
        }
238
 
239
        public override int Init() {
162 pfowler 240
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 16));
241
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F11), 17));
242
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F7), 12));
243
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.SPACE), 13));
244
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.RETURN), 14));
245
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.VOLUME_MUTE), 15));
161 pfowler 246
            this.addControl(new Switch2Pos(new CommandDCS("AAP_CDUPWR"), 11));
247
            this.addControl(new Switch2Pos(new CommandDCS("AAP_EGIPWR"), 10));
248
            this.addControl(new Switch3Pos(new CommandDCS("AAP_STEER"), 9, 8));
249
            this.addControl(new Selector(new CommandDCS("AAP_STEERPT"), new int[] { 4, 5, 6 }));
250
            this.addControl(new Selector(new CommandDCS("AAP_PAGE"), new int[] { 0, 1, 2, 3 }));
251
 
160 pfowler 252
            // Enable the mcp23017
253
            mcp.WriteGpio(3, 0);
164 pfowler 254
            Utils.delayms(10);
160 pfowler 255
            mcp.WriteGpio(3, 1);
256
 
257
            // Set io dir, pullups and rev polarity
164 pfowler 258
            chip0.SetIODirection(0xff, 0xff);
259
            chip0.SetIOPolarity(0xff, 0xff);
260
            chip0.SetIOPullups(0xff, 0xff);
160 pfowler 261
 
161 pfowler 262
            this.Refresh();
167 pfowler 263
            data.prev = data.buttons;
160 pfowler 264
 
265
            return 1;
266
        }
267
        public override int Refresh() {
167 pfowler 268
            byte[] bytes;
164 pfowler 269
            int rslt = 0;
167 pfowler 270
            rslt = chip0.GetIO(out bytes);
160 pfowler 271
 
161 pfowler 272
            // Join all our buttons into a single inputs
273
            uint gpio = (uint)((1 - mcp.ReadGpio(0)) | ((1 - mcp.ReadGpio(1)) << 1));
167 pfowler 274
            data.buttons = (uint)gpio << 16;
275
            data.buttons |= (uint)bytes[0] << 8;
276
            data.buttons |= (uint)bytes[1];
161 pfowler 277
 
167 pfowler 278
            if (data.buttons != data.prev)
279
                data.changed = true;
161 pfowler 280
            else
167 pfowler 281
                data.changed = false;
161 pfowler 282
 
283
            return rslt;
160 pfowler 284
        }
285
 
286
        public override int Input() {
162 pfowler 287
            //Console.WriteLine(input.curr.ToString("X"));
161 pfowler 288
            foreach (Control control in this.controls) {
167 pfowler 289
                control.data = this.data;
161 pfowler 290
                control.Tick();
291
            }
167 pfowler 292
            data.prev = data.buttons;
293
            data.changed = false;
160 pfowler 294
            return 1;
295
        }
296
    }
297
}