Subversion Repositories group.electronics

Rev

Rev 165 | Rev 167 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
162 pfowler 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using WindowsInput.Native;
7
 
8
namespace nitdcscore {
9
    public class NitMultiPanel : Panel {
10
 
11
        Utils.InputPair input;
165 pfowler 12
 
13
        Utils.InputPair js_x;
14
        Utils.InputPair js_y;
15
 
166 pfowler 16
        private mcp23017[] chips;
162 pfowler 17
 
166 pfowler 18
        public NitMultiPanel(mcp2221 mcp) : base(mcp) {
19
            chips = new mcp23017[2] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x24) };
162 pfowler 20
            input.curr = 0;
21
            input.prev = 0;
22
 
165 pfowler 23
            js_x.curr = js_x.prev = 0;
24
            js_y.curr = js_y.prev = 0;
25
 
162 pfowler 26
            this.Init();
27
        }
28
 
29
        public override int Init() {
30
            // Choose what function the number pad shall carry out:
31
            // 0 = UFC #, 1 = F1-F12 Keys, 2 = Misc (+ TrackIR)
32
            Switch3Pos selSwitch = new Switch3Pos(null, 2, 3);
33
 
34
            // Row 1
35
            Dictionary<uint, Command> pad01 = new Dictionary<uint, Command>();
164 pfowler 36
            pad01.Add(0, new CommandDCS("UFC_1"));
162 pfowler 37
            pad01.Add(1, new CommandVKey(VirtualKeyCode.F1));
38
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad01), 7));
39
 
40
            Dictionary<uint, Command> pad02 = new Dictionary<uint, Command>();
164 pfowler 41
            pad02.Add(0, new CommandDCS("UFC_2"));
162 pfowler 42
            pad02.Add(1, new CommandVKey(VirtualKeyCode.F2));
43
            pad02.Add(2, new CommandTrackIRKey(VirtualKeyCode.F7));
44
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad02), 6));
45
 
46
            Dictionary<uint, Command> pad03 = new Dictionary<uint, Command>();
164 pfowler 47
            pad03.Add(0, new CommandDCS("UFC_3"));
162 pfowler 48
            pad03.Add(1, new CommandVKey(VirtualKeyCode.F3));
49
            pad03.Add(2, new CommandTrackIRKey(VirtualKeyCode.F11));
50
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad03), 5));
51
 
52
            Dictionary<uint, Command> pad04 = new Dictionary<uint,Command>();
53
            pad04.Add(0, new CommandDCS("UFC_ENT"));
54
            pad04.Add(1, new CommandVKey(VirtualKeyCode.F4));
55
            pad04.Add(2, new CommandTrackIRKey(VirtualKeyCode.F9));
56
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad04), 4));
57
 
58
            // Row 2
59
            Dictionary<uint, Command> pad05 = new Dictionary<uint, Command>();
164 pfowler 60
            pad05.Add(0, new CommandDCS("UFC_4"));
162 pfowler 61
            pad05.Add(1, new CommandVKey(VirtualKeyCode.F5));
62
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad05), 11));
63
 
64
            Dictionary<uint, Command> pad06 = new Dictionary<uint, Command>();
164 pfowler 65
            pad06.Add(0, new CommandDCS("UFC_5"));
162 pfowler 66
            pad06.Add(1, new CommandVKey(VirtualKeyCode.F6));
67
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad06), 10));
68
 
69
            Dictionary<uint, Command> pad07 = new Dictionary<uint, Command>();
164 pfowler 70
            pad07.Add(0, new CommandDCS("UFC_6"));
162 pfowler 71
            pad07.Add(1, new CommandVKey(VirtualKeyCode.F7));
72
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad07), 9));
73
 
74
            Dictionary<uint, Command> pad08 = new Dictionary<uint, Command>();
75
            pad08.Add(0, new CommandDCS("UFC_10"));
76
            pad08.Add(1, new CommandVKey(VirtualKeyCode.F8));
77
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad08), 8));
78
 
79
            // Row 3
80
            Dictionary<uint, Command> pad09 = new Dictionary<uint, Command>();
81
            pad09.Add(0, new CommandDCS("UFC_7"));
82
            pad09.Add(1, new CommandVKey(VirtualKeyCode.F9));
83
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad09), 15));
84
 
85
            Dictionary<uint, Command> pad10 = new Dictionary<uint, Command>();
164 pfowler 86
            pad10.Add(0, new CommandDCS("UFC_8"));
162 pfowler 87
            pad10.Add(1, new CommandVKey(VirtualKeyCode.F10));
88
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad10), 14));
89
 
90
            Dictionary<uint, Command> pad11 = new Dictionary<uint, Command>();
164 pfowler 91
            pad11.Add(0, new CommandDCS("UFC_9"));
162 pfowler 92
            pad11.Add(1, new CommandVKey(VirtualKeyCode.F11));
93
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad11), 13));
94
 
95
            Dictionary<uint, Command> pad12 = new Dictionary<uint, Command>();
96
            pad12.Add(0, new CommandDCS("UFC_CLR"));
97
            pad12.Add(1, new CommandVKey(VirtualKeyCode.F12));
98
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad12), 12));
99
 
165 pfowler 100
            // Joystick Press - Mapped to mouse button 1
166 pfowler 101
            this.addControl(new Switch2Pos(new CommandMouseButton(CommandMouseButton.MB_LEFT), 32));
162 pfowler 102
 
165 pfowler 103
 
166 pfowler 104
            // NMSP
105
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_EGI_BTN"), 16));
106
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_HARS_BTN"), 17));
107
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ILS_BTN"), 18));
108
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_TCN_BTN"), 19));
109
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ANCHR_BTN"), 20));
110
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_STEERPT_BTN"), 21));
111
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ABLE_STOW"), 22, true));
112
 
113
 
162 pfowler 114
            // Enable the mcp23017
115
            mcp.WriteGpio(3, 0);
164 pfowler 116
            Utils.delayms(50);
162 pfowler 117
            mcp.WriteGpio(3, 1);
166 pfowler 118
            Utils.delayms(50);
162 pfowler 119
 
166 pfowler 120
            chips[0].SetIODirection(0xff, 0xff);
121
            chips[0].SetIOPolarity(0xff, 0xff);
122
            chips[0].SetIOPullups(0xff, 0xff);
162 pfowler 123
 
166 pfowler 124
            chips[1].SetIODirection(0x00, 0xff);
125
            chips[1].SetIOPolarity(0x00, 0xff);
126
            chips[1].SetIOPullups(0x00, 0xff);
127
 
162 pfowler 128
            this.Refresh();
129
            input.prev = input.curr;
130
 
131
            return 1;
132
        }
133
        public override int Refresh() {
134
            byte[] data;
163 pfowler 135
            int rslt = 0;
162 pfowler 136
 
137
            // Join all our buttons into a single inputs
166 pfowler 138
            UInt64 gpio = (UInt64)(1 - mcp.ReadGpio(0));
139
            input.curr = (UInt64)gpio << 32;
140
 
141
            rslt = chips[1].GetIO(out data);
142
            input.curr |= (UInt64)data[0] << 24;
143
            input.curr |= (UInt64)data[1] << 16;
162 pfowler 144
 
166 pfowler 145
            rslt = chips[0].GetIO(out data);
146
            input.curr |= (UInt64)data[0] << 8;
147
            input.curr |= (UInt64)data[1];
148
 
165 pfowler 149
            js_y.curr = mcp.ReadADC(0);
150
            js_x.curr = mcp.ReadADC(1);
166 pfowler 151
 
165 pfowler 152
 
153
            //Console.WriteLine(js_x.curr.ToString() + ":" + js_y.curr.ToString());
154
 
162 pfowler 155
            if (input.curr != input.prev)
156
                this.inputChanged = true;
157
            else
158
                this.inputChanged = false;
159
 
160
            return rslt;
161
        }
162
 
163
        public override int Input() {
166 pfowler 164
            //Console.WriteLine(input.curr.ToString("X"));
162 pfowler 165
            foreach (Control control in this.controls) {
166
                control.data = this.input;
167
                control.Tick();
168
            }
163 pfowler 169
            this.inputChanged = false;
170
 
162 pfowler 171
            input.prev = input.curr;
172
            return 1;
173
        }
174
    }
175
}