Subversion Repositories group.electronics

Rev

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