Subversion Repositories group.electronics

Rev

Rev 163 | Go to most recent revision | Details | 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;
12
 
13
        public NitMultiPanel(mcp2221 mcp)
14
            : base(mcp) {
15
            input.curr = 0;
16
            input.prev = 0;
17
 
18
            this.Init();
19
        }
20
 
21
        public override int Init() {
22
            // Choose what function the number pad shall carry out:
23
            // 0 = UFC #, 1 = F1-F12 Keys, 2 = Misc (+ TrackIR)
24
            Switch3Pos selSwitch = new Switch3Pos(null, 2, 3);
25
 
26
            // Row 1
27
            Dictionary<uint, Command> pad01 = new Dictionary<uint, Command>();
28
            pad01.Add(0, new CommandDCS("UFC_01"));
29
            pad01.Add(1, new CommandVKey(VirtualKeyCode.F1));
30
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad01), 7));
31
 
32
            Dictionary<uint, Command> pad02 = new Dictionary<uint, Command>();
33
            pad02.Add(0, new CommandDCS("UFC_02"));
34
            pad02.Add(1, new CommandVKey(VirtualKeyCode.F2));
35
            pad02.Add(2, new CommandTrackIRKey(VirtualKeyCode.F7));
36
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad02), 6));
37
 
38
            Dictionary<uint, Command> pad03 = new Dictionary<uint, Command>();
39
            pad03.Add(0, new CommandDCS("UFC_03"));
40
            pad03.Add(1, new CommandVKey(VirtualKeyCode.F3));
41
            pad03.Add(2, new CommandTrackIRKey(VirtualKeyCode.F11));
42
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad03), 5));
43
 
44
            Dictionary<uint, Command> pad04 = new Dictionary<uint,Command>();
45
            pad04.Add(0, new CommandDCS("UFC_ENT"));
46
            pad04.Add(1, new CommandVKey(VirtualKeyCode.F4));
47
            pad04.Add(2, new CommandTrackIRKey(VirtualKeyCode.F9));
48
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad04), 4));
49
 
50
            // Row 2
51
            Dictionary<uint, Command> pad05 = new Dictionary<uint, Command>();
52
            pad05.Add(0, new CommandDCS("UFC_04"));
53
            pad05.Add(1, new CommandVKey(VirtualKeyCode.F5));
54
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad05), 11));
55
 
56
            Dictionary<uint, Command> pad06 = new Dictionary<uint, Command>();
57
            pad06.Add(0, new CommandDCS("UFC_05"));
58
            pad06.Add(1, new CommandVKey(VirtualKeyCode.F6));
59
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad06), 10));
60
 
61
            Dictionary<uint, Command> pad07 = new Dictionary<uint, Command>();
62
            pad07.Add(0, new CommandDCS("UFC_06"));
63
            pad07.Add(1, new CommandVKey(VirtualKeyCode.F7));
64
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad07), 9));
65
 
66
            Dictionary<uint, Command> pad08 = new Dictionary<uint, Command>();
67
            pad08.Add(0, new CommandDCS("UFC_10"));
68
            pad08.Add(1, new CommandVKey(VirtualKeyCode.F8));
69
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad08), 8));
70
 
71
            // Row 3
72
            Dictionary<uint, Command> pad09 = new Dictionary<uint, Command>();
73
            pad09.Add(0, new CommandDCS("UFC_7"));
74
            pad09.Add(1, new CommandVKey(VirtualKeyCode.F9));
75
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad09), 15));
76
 
77
            Dictionary<uint, Command> pad10 = new Dictionary<uint, Command>();
78
            pad10.Add(0, new CommandDCS("UFC_08"));
79
            pad10.Add(1, new CommandVKey(VirtualKeyCode.F10));
80
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad10), 14));
81
 
82
            Dictionary<uint, Command> pad11 = new Dictionary<uint, Command>();
83
            pad11.Add(0, new CommandDCS("UFC_09"));
84
            pad11.Add(1, new CommandVKey(VirtualKeyCode.F11));
85
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad11), 13));
86
 
87
            Dictionary<uint, Command> pad12 = new Dictionary<uint, Command>();
88
            pad12.Add(0, new CommandDCS("UFC_CLR"));
89
            pad12.Add(1, new CommandVKey(VirtualKeyCode.F12));
90
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad12), 12));
91
 
92
 
93
 
94
            // Enable the mcp23017
95
            mcp.WriteGpio(3, 0);
96
            Utils.delayms(500);
97
            mcp.WriteGpio(3, 1);
98
            //this.delayms();
99
 
100
            // Set io dir, pullups and rev polarity
101
            byte[] data;
102
            data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
103
            mcp.WriteI2cData(0x40, data, 5);
104
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
105
            mcp.WriteI2cData(0x40, data, 3);
106
 
107
            this.Refresh();
108
            input.prev = input.curr;
109
 
110
            return 1;
111
        }
112
        public override int Refresh() {
113
            byte[] data;
114
            data = new byte[] { 0x12 }; // Select GPIOA register
115
            mcp.WriteI2cData(0x40, data, 1);
116
 
117
            data = new byte[] { 0x00, 0x00 }; // Read two bytes
118
            int rslt = mcp.ReadI2CData(0x41, ref data, 2);
119
 
120
            // Join all our buttons into a single inputs
121
            uint gpio = (uint)(1 - mcp.ReadGpio(0));
122
            input.curr = (uint)gpio << 16;
123
            input.curr |= (uint)data[0] << 8;
124
            input.curr |= (uint)data[1];
125
 
126
            if (input.curr != input.prev)
127
                this.inputChanged = true;
128
            else
129
                this.inputChanged = false;
130
 
131
            return rslt;
132
        }
133
 
134
        public override int Input() {
135
            Console.WriteLine(input.curr.ToString("X"));
136
            foreach (Control control in this.controls) {
137
                control.data = this.input;
138
                control.Tick();
139
            }
140
            input.prev = input.curr;
141
            this.inputChanged = false;
142
            return 1;
143
        }
144
    }
145
}