Subversion Repositories group.electronics

Rev

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