Subversion Repositories group.electronics

Rev

Rev 173 | Rev 177 | 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;
173 pfowler 12
        public byte ledShadow = 0x00;
172 pfowler 13
 
166 pfowler 14
        public NitMultiPanel(mcp2221 mcp) : base(mcp) {
15
            chips = new mcp23017[2] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x24) };
162 pfowler 16
 
17
            this.Init();
18
        }
19
 
20
        public override int Init() {
21
            // Choose what function the number pad shall carry out:
22
            // 0 = UFC #, 1 = F1-F12 Keys, 2 = Misc (+ TrackIR)
23
            Switch3Pos selSwitch = new Switch3Pos(null, 2, 3);
24
 
25
            // Row 1
26
            Dictionary<uint, Command> pad01 = new Dictionary<uint, Command>();
164 pfowler 27
            pad01.Add(0, new CommandDCS("UFC_1"));
176 pfowler 28
            pad01.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F1));
162 pfowler 29
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad01), 7));
30
 
31
            Dictionary<uint, Command> pad02 = new Dictionary<uint, Command>();
164 pfowler 32
            pad02.Add(0, new CommandDCS("UFC_2"));
176 pfowler 33
            pad02.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F2));
34
            pad02.Add(2, new CommandTrackIRKey(OSInput.VirtualKeyCodes.VK_F7));
162 pfowler 35
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad02), 6));
36
 
37
            Dictionary<uint, Command> pad03 = new Dictionary<uint, Command>();
164 pfowler 38
            pad03.Add(0, new CommandDCS("UFC_3"));
176 pfowler 39
            pad03.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F3));
40
            pad03.Add(2, new CommandTrackIRKey(OSInput.VirtualKeyCodes.VK_F11));
162 pfowler 41
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad03), 5));
42
 
43
            Dictionary<uint, Command> pad04 = new Dictionary<uint,Command>();
44
            pad04.Add(0, new CommandDCS("UFC_ENT"));
176 pfowler 45
            pad04.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F4));
46
            pad04.Add(2, new CommandTrackIRKey(OSInput.VirtualKeyCodes.VK_F9));
162 pfowler 47
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad04), 4));
48
 
49
            // Row 2
50
            Dictionary<uint, Command> pad05 = new Dictionary<uint, Command>();
164 pfowler 51
            pad05.Add(0, new CommandDCS("UFC_4"));
176 pfowler 52
            pad05.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F5));
162 pfowler 53
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad05), 11));
54
 
55
            Dictionary<uint, Command> pad06 = new Dictionary<uint, Command>();
164 pfowler 56
            pad06.Add(0, new CommandDCS("UFC_5"));
176 pfowler 57
            pad06.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F6));
162 pfowler 58
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad06), 10));
59
 
60
            Dictionary<uint, Command> pad07 = new Dictionary<uint, Command>();
164 pfowler 61
            pad07.Add(0, new CommandDCS("UFC_6"));
176 pfowler 62
            pad07.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F7));
162 pfowler 63
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad07), 9));
64
 
65
            Dictionary<uint, Command> pad08 = new Dictionary<uint, Command>();
66
            pad08.Add(0, new CommandDCS("UFC_10"));
176 pfowler 67
            pad08.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F8));
162 pfowler 68
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad08), 8));
69
 
70
            // Row 3
71
            Dictionary<uint, Command> pad09 = new Dictionary<uint, Command>();
72
            pad09.Add(0, new CommandDCS("UFC_7"));
176 pfowler 73
            pad09.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F9));
162 pfowler 74
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad09), 15));
75
 
76
            Dictionary<uint, Command> pad10 = new Dictionary<uint, Command>();
164 pfowler 77
            pad10.Add(0, new CommandDCS("UFC_8"));
176 pfowler 78
            pad10.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F10));
162 pfowler 79
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad10), 14));
80
 
81
            Dictionary<uint, Command> pad11 = new Dictionary<uint, Command>();
164 pfowler 82
            pad11.Add(0, new CommandDCS("UFC_9"));
176 pfowler 83
            pad11.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F11));
162 pfowler 84
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad11), 13));
85
 
86
            Dictionary<uint, Command> pad12 = new Dictionary<uint, Command>();
87
            pad12.Add(0, new CommandDCS("UFC_CLR"));
176 pfowler 88
            pad12.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F12));
162 pfowler 89
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad12), 12));
90
 
166 pfowler 91
            // NMSP
92
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_EGI_BTN"), 16));
93
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_HARS_BTN"), 17));
94
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ILS_BTN"), 18));
95
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_TCN_BTN"), 19));
96
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ANCHR_BTN"), 20));
97
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_STEERPT_BTN"), 21));
98
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ABLE_STOW"), 22, true));
99
 
100
 
171 pfowler 101
            // Joystick Press - Mapped to mouse button 1
102
            this.addControl(new Switch2Pos(new CommandMouseButton(CommandMouseButton.MB_LEFT), 32));
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
 
172 pfowler 118
            this.addLed(new Led(0, 1, 0x1112, 0x0002, 1));     // NMSP_ANCHR_LED
119
            this.addLed(new Led(0, 4, 0x1110, 0x0800, 11));    // NMSP_EGI_LED
120
            this.addLed(new Led(0, 6, 0x11bc, 0x0004, 2));     // NMSP_FM_LED
121
            this.addLed(new Led(0, 5, 0x1110, 0x0200, 9));     // NMSP_HARS_LEF
122
            this.addLed(new Led(0, 3, 0x1112, 0x0020, 5));     // NMSP_ILS_LED
123
            this.addLed(new Led(0, 0, 0x1110, 0x8000, 15));    // NMSP_STEERPT_LED
124
            this.addLed(new Led(0, 2, 0x1112, 0x0008, 3));     // NMSP_TCN_LED
125
            this.addLed(new Led(0, 7, 0x11bc, 0x0002, 1));     // NMSP_UHF_LED
171 pfowler 126
 
173 pfowler 127
            //this.addLed(new Led(0, 6, 0x1012, 0x0800, 11));     // MASTER_CAUTION
128
 
162 pfowler 129
            this.Refresh();
167 pfowler 130
            data.prev = data.buttons;
131
            data.axis[0].prev = data.axis[0].value;
132
            data.axis[1].prev = data.axis[1].value;
171 pfowler 133
            data.changed = false;
167 pfowler 134
 
162 pfowler 135
            return 1;
136
        }
137
        public override int Refresh() {
167 pfowler 138
            byte[] bytes;
163 pfowler 139
            int rslt = 0;
162 pfowler 140
 
141
            // Join all our buttons into a single inputs
166 pfowler 142
            UInt64 gpio = (UInt64)(1 - mcp.ReadGpio(0));
167 pfowler 143
            data.buttons = (UInt64)gpio << 32;
162 pfowler 144
 
167 pfowler 145
            rslt = chips[1].GetIO(out bytes);
146
            data.buttons |= (UInt64)bytes[0] << 24;
147
            data.buttons |= (UInt64)bytes[1] << 16;
166 pfowler 148
 
167 pfowler 149
            rslt = chips[0].GetIO(out bytes);
150
            data.buttons |= (UInt64)bytes[0] << 8;
151
            data.buttons |= (UInt64)bytes[1];
165 pfowler 152
 
167 pfowler 153
            data.axis[0].value = mcp.ReadADC(0);
154
            data.axis[1].value = mcp.ReadADC(1);
165 pfowler 155
 
167 pfowler 156
 
157
            if (data.buttons != data.prev)
158
                data.changed = true;
162 pfowler 159
            else
167 pfowler 160
                data.changed = false;
162 pfowler 161
 
173 pfowler 162
            byte tmpShadow = this.ledShadow;
172 pfowler 163
            foreach (Led led in this.leds) {
173 pfowler 164
                led.Tick(ref ledShadow);
172 pfowler 165
            }
166
 
173 pfowler 167
            // Only update if leds have changed
168
            if (tmpShadow != this.ledShadow)
169
                chips[1].SetBank(0, ledShadow);
170
 
171
 
162 pfowler 172
            return rslt;
173
        }
174
 
175
        public override int Input() {
166 pfowler 176
            //Console.WriteLine(input.curr.ToString("X"));
162 pfowler 177
            foreach (Control control in this.controls) {
167 pfowler 178
                control.data = this.data;
162 pfowler 179
                control.Tick();
180
            }
167 pfowler 181
            data.changed = false;
163 pfowler 182
 
167 pfowler 183
            data.prev = data.buttons;
162 pfowler 184
            return 1;
185
        }
186
    }
187
}