Subversion Repositories group.electronics

Rev

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