Subversion Repositories group.electronics

Rev

Rev 171 | Rev 173 | 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;
172 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
 
166 pfowler 90
            // NMSP
91
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_EGI_BTN"), 16));
92
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_HARS_BTN"), 17));
93
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ILS_BTN"), 18));
94
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_TCN_BTN"), 19));
95
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ANCHR_BTN"), 20));
96
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_STEERPT_BTN"), 21));
97
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ABLE_STOW"), 22, true));
98
 
99
 
171 pfowler 100
            // Joystick Press - Mapped to mouse button 1
101
            this.addControl(new Switch2Pos(new CommandMouseButton(CommandMouseButton.MB_LEFT), 32));
102
 
162 pfowler 103
            // Enable the mcp23017
104
            mcp.WriteGpio(3, 0);
164 pfowler 105
            Utils.delayms(50);
162 pfowler 106
            mcp.WriteGpio(3, 1);
166 pfowler 107
            Utils.delayms(50);
162 pfowler 108
 
166 pfowler 109
            chips[0].SetIODirection(0xff, 0xff);
110
            chips[0].SetIOPolarity(0xff, 0xff);
111
            chips[0].SetIOPullups(0xff, 0xff);
162 pfowler 112
 
166 pfowler 113
            chips[1].SetIODirection(0x00, 0xff);
114
            chips[1].SetIOPolarity(0x00, 0xff);
115
            chips[1].SetIOPullups(0x00, 0xff);
116
 
172 pfowler 117
            this.addLed(new Led(0, 1, 0x1112, 0x0002, 1));     // NMSP_ANCHR_LED
118
            this.addLed(new Led(0, 4, 0x1110, 0x0800, 11));    // NMSP_EGI_LED
119
            this.addLed(new Led(0, 6, 0x11bc, 0x0004, 2));     // NMSP_FM_LED
120
            this.addLed(new Led(0, 5, 0x1110, 0x0200, 9));     // NMSP_HARS_LEF
121
            this.addLed(new Led(0, 3, 0x1112, 0x0020, 5));     // NMSP_ILS_LED
122
            this.addLed(new Led(0, 0, 0x1110, 0x8000, 15));    // NMSP_STEERPT_LED
123
            this.addLed(new Led(0, 2, 0x1112, 0x0008, 3));     // NMSP_TCN_LED
124
            this.addLed(new Led(0, 7, 0x11bc, 0x0002, 1));     // NMSP_UHF_LED
171 pfowler 125
 
162 pfowler 126
            this.Refresh();
167 pfowler 127
            data.prev = data.buttons;
128
            data.axis[0].prev = data.axis[0].value;
129
            data.axis[1].prev = data.axis[1].value;
171 pfowler 130
            data.changed = false;
167 pfowler 131
 
162 pfowler 132
            return 1;
133
        }
134
        public override int Refresh() {
167 pfowler 135
            byte[] bytes;
163 pfowler 136
            int rslt = 0;
162 pfowler 137
 
138
            // Join all our buttons into a single inputs
166 pfowler 139
            UInt64 gpio = (UInt64)(1 - mcp.ReadGpio(0));
167 pfowler 140
            data.buttons = (UInt64)gpio << 32;
162 pfowler 141
 
167 pfowler 142
            rslt = chips[1].GetIO(out bytes);
143
            data.buttons |= (UInt64)bytes[0] << 24;
144
            data.buttons |= (UInt64)bytes[1] << 16;
166 pfowler 145
 
167 pfowler 146
            rslt = chips[0].GetIO(out bytes);
147
            data.buttons |= (UInt64)bytes[0] << 8;
148
            data.buttons |= (UInt64)bytes[1];
165 pfowler 149
 
167 pfowler 150
            data.axis[0].value = mcp.ReadADC(0);
151
            data.axis[1].value = mcp.ReadADC(1);
165 pfowler 152
 
167 pfowler 153
 
154
            if (data.buttons != data.prev)
155
                data.changed = true;
162 pfowler 156
            else
167 pfowler 157
                data.changed = false;
162 pfowler 158
 
172 pfowler 159
 
160
            byte ledByte = (byte)(data.buttons >> 16);
161
            foreach (Led led in this.leds) {
162
                if (led.Tick(ref ledByte)) {
163
                    Console.WriteLine("Led: " + ledByte);
164
                    chips[1].SetBank(led.bank, ledByte);
165
                }
166
            }
167
 
162 pfowler 168
            return rslt;
169
        }
170
 
171
        public override int Input() {
166 pfowler 172
            //Console.WriteLine(input.curr.ToString("X"));
162 pfowler 173
            foreach (Control control in this.controls) {
167 pfowler 174
                control.data = this.data;
162 pfowler 175
                control.Tick();
176
            }
167 pfowler 177
            data.changed = false;
163 pfowler 178
 
167 pfowler 179
            data.prev = data.buttons;
162 pfowler 180
            return 1;
181
        }
182
    }
183
}