Subversion Repositories group.electronics

Rev

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

Rev 170 Rev 171
Line 5... Line 5...
5
using System.Threading.Tasks;
5
using System.Threading.Tasks;
6
using WindowsInput.Native;
6
using WindowsInput.Native;
7
 
7
 
8
namespace nitdcscore {
8
namespace nitdcscore {
9
 
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 bool 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
            //Console.WriteLine("JS " + ":" + this.prev + "," + this.value + "," + this.max + "," + lowerval + "," + upperval);
-
 
34
 
-
 
35
            if ((this.value < lowerval) || (this.value > upperval)) {
-
 
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
 
-
 
42
                
-
 
43
                this.prev = this.value;
-
 
44
 
-
 
45
                position = Globals.map_uint16(this.value, 0, this.max, 0, this.mapsize);
-
 
46
                
-
 
47
                return true;               
-
 
48
            }
-
 
49
            
-
 
50
            return false;
-
 
51
        }
-
 
52
 
-
 
53
 
-
 
54
    }
-
 
55
 
-
 
56
    public class jsdata {
-
 
57
        public bool changed { get; set; }
-
 
58
        public UInt64 buttons { get; set; }
-
 
59
        public UInt64 prev { get; set; }
-
 
60
 
-
 
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
        }
-
 
75
    }
-
 
76
 
-
 
77
 
-
 
78
    public interface IPanel {
10
    public interface IPanel {
79
        int Init();
11
        int Init();
80
        int Refresh();
12
        int Refresh();
81
        int Input();
13
        int Input();
82
    }
14
    }
Line 118... Line 50...
118
 
50
 
119
        public void addControl(Control control) {
51
        public void addControl(Control control) {
120
            this.controls.Add(control);
52
            this.controls.Add(control);
121
        }
53
        }
122
    }
54
    }
123
 
-
 
124
 
-
 
125
    public class Panel_AHFS : Panel {
-
 
126
 
-
 
127
        private mcp23017[] chips;
-
 
128
 
-
 
129
        public Panel_AHFS(mcp2221 mcp) : base(mcp) {
-
 
130
            chips = new mcp23017[2] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x21) };
-
 
131
            this.Init();
-
 
132
        }
-
 
133
 
-
 
134
        public override int Init() {
-
 
135
            //AHCP
-
 
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
-
 
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
-
 
148
            
-
 
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));
-
 
165
             
-
 
166
            mcp.WriteGpio(3, 0);
-
 
167
            Utils.delayms(10);
-
 
168
            // Enable the mcp23017
-
 
169
            mcp.WriteGpio(3, 1);
-
 
170
            
-
 
171
            // Set io dir, pullups and rev polarity
-
 
172
            chips[0].SetIODirection(0xff, 0xff);
-
 
173
            chips[0].SetIOPolarity(0xf0, 0xff);
-
 
174
            chips[0].SetIOPullups(0xff, 0xff);
-
 
175
 
-
 
176
            chips[1].SetIODirection(0xff, 0xff);
-
 
177
            chips[1].SetIOPolarity(0xff, 0xff);
-
 
178
            chips[1].SetIOPullups(0xff, 0xff);
-
 
179
 
-
 
180
            // Get the initial values
-
 
181
            this.Refresh();
-
 
182
            data.prev = data.buttons;
-
 
183
            data.axis[0].prev = data.axis[0].value;
-
 
184
            data.changed = false;
-
 
185
 
-
 
186
            return 0;
-
 
187
        }
-
 
188
 
-
 
189
        public override int Refresh() {
-
 
190
 
-
 
191
            byte[] bytes;
-
 
192
            int rslt = 0;
-
 
193
            rslt = chips[0].GetIO(out bytes);
-
 
194
 
-
 
195
            data.buttons = (uint)bytes[0] << 24;
-
 
196
            data.buttons |= (uint)bytes[1] << 16;
-
 
197
 
-
 
198
            rslt = chips[1].GetIO(out bytes);
-
 
199
 
-
 
200
            data.buttons |= (uint)bytes[0] << 8;
-
 
201
            data.buttons |= (uint)bytes[1];
-
 
202
 
-
 
203
            data.axis[0].value = mcp.ReadADC(1);
-
 
204
 
-
 
205
            if ((data.buttons != data.prev) || (data.axis[0].prev != data.axis[0].value))
-
 
206
                data.changed = true;
-
 
207
            else
-
 
208
                data.changed = false;
-
 
209
 
-
 
210
            return 1;
-
 
211
        }
-
 
212
 
-
 
213
        public override int Input() {
-
 
214
            
-
 
215
            foreach (Control control in this.controls) {
-
 
216
                control.data = this.data;
-
 
217
                control.Tick();
-
 
218
            }
-
 
219
            data.prev = data.buttons;
-
 
220
            data.axis[0].prev = data.axis[0].value;
-
 
221
            data.changed = false;
-
 
222
            return 1;
-
 
223
        }
-
 
224
    }
-
 
225
 
-
 
226
    public class Panel_AAP : Panel {
-
 
227
 
-
 
228
        private mcp23017 chip0;
-
 
229
 
-
 
230
        public Panel_AAP(mcp2221 mcp) : base(mcp) {
-
 
231
            data.buttons = 0;
-
 
232
            data.prev = 0;
-
 
233
 
-
 
234
            chip0 = new mcp23017(mcp, 0x20);
-
 
235
 
-
 
236
            this.Init();
-
 
237
        }
-
 
238
 
-
 
239
        public override int Init() {
-
 
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));
-
 
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
 
-
 
252
            // Enable the mcp23017
-
 
253
            mcp.WriteGpio(3, 0);
-
 
254
            Utils.delayms(10);
-
 
255
            mcp.WriteGpio(3, 1);
-
 
256
 
-
 
257
            // Set io dir, pullups and rev polarity
-
 
258
            chip0.SetIODirection(0xff, 0xff);
-
 
259
            chip0.SetIOPolarity(0xff, 0xff);
-
 
260
            chip0.SetIOPullups(0xff, 0xff);
-
 
261
 
-
 
262
            this.Refresh();
-
 
263
            data.prev = data.buttons;
-
 
264
 
-
 
265
            return 1;
-
 
266
        }
-
 
267
        public override int Refresh() {
-
 
268
            byte[] bytes;
-
 
269
            int rslt = 0;
-
 
270
            rslt = chip0.GetIO(out bytes);
-
 
271
 
-
 
272
            // Join all our buttons into a single inputs
-
 
273
            uint gpio = (uint)((1 - mcp.ReadGpio(0)) | ((1 - mcp.ReadGpio(1)) << 1));
-
 
274
            data.buttons = (uint)gpio << 16;
-
 
275
            data.buttons |= (uint)bytes[0] << 8;
-
 
276
            data.buttons |= (uint)bytes[1];
-
 
277
 
-
 
278
            if (data.buttons != data.prev)
-
 
279
                data.changed = true;
-
 
280
            else
-
 
281
                data.changed = false;
-
 
282
            
-
 
283
            return rslt;
-
 
284
        }
-
 
285
 
-
 
286
        public override int Input() {
-
 
287
            //Console.WriteLine(input.curr.ToString("X"));
-
 
288
            foreach (Control control in this.controls) {
-
 
289
                control.data = this.data;
-
 
290
                control.Tick();
-
 
291
            }
-
 
292
            data.prev = data.buttons;
-
 
293
            data.changed = false;
-
 
294
            return 1;
-
 
295
        }
-
 
296
    }
-
 
297
}
55
}