Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
158 pfowler 1
using MCP2221;
2
using System;
3
using System.Collections.Generic;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Timers;
8
using WindowsInput;
9
using WindowsInput.Native;
10
 
11
namespace nitdcscore{    
12
    class NITAAPDevice  {
13
 
14
        DcsBios _bios;
15
        Bridge_mcp2221 _mcp;
16
        Timer refreshTimer = new Timer();
17
        InputSimulator sendkey = new InputSimulator();
18
 
19
        private readonly Object _lockObject = new object();
20
 
21
        uint _prev_inputs = 0;
22
        uint _prev_keys = 0;
23
        public uint inputs { get; set; }
24
 
25
        public NITAAPDevice(ref DcsBios bios, Bridge_mcp2221 mcp) {
26
            _bios = bios;
27
            _mcp = mcp;
28
 
29
            refreshTimer.Enabled = false;
30
            refreshTimer.AutoReset = true;
31
            refreshTimer.Interval = 50;
32
            refreshTimer.Elapsed += refreshTimer_Elapsed;
33
        }
34
 
35
        void refreshTimer_Elapsed(object sender, ElapsedEventArgs e) {
36
            refreshTimer.Enabled = false;
37
            this.GetInputs();
38
 
39
            if (_prev_inputs != inputs) {
40
                _prev_inputs = inputs;
41
 
42
                uint aap_cdupwr = (uint)(this.inputs & 0x0080) >> 7;
43
                _bios.SendData("AAP_CDUPWR " + aap_cdupwr + "\n");
44
 
45
                uint aap_egipwr = (uint)(this.inputs & 0x0040) >> 6;
46
                _bios.SendData("AAP_EGIPWR " + aap_egipwr + "\n");
47
 
48
                uint steer = (uint)(this.inputs & 0x0300) >> 8;
49
                _bios.SendData("AAP_STEER " + steer + "\n");
50
 
51
                uint steerpt = (uint)(this.inputs & 0xC000) >> 14;
52
                _bios.SendData("AAP_STEERPT " + steerpt + "\n");
53
 
54
                uint page = (uint)(this.inputs & 0xC00) >> 10;
55
                _bios.SendData("AAP_PAGE " + page + "\n");
56
 
57
                uint keys = (uint)(this.inputs & 0x003f);
58
                if (_prev_keys != keys) {
59
                    uint key00 = (uint)(this.inputs & 0x0020) >> 5;
60
                    if (key00 == 1) {
61
                        this.send_trackir(VirtualKeyCode.F9);
62
                        Console.WriteLine("F9");
63
                    }
64
                    uint key01 = (uint)(this.inputs & 0x0010) >> 4;
65
                    if (key01 == 1) {
66
                        this.send_trackir(VirtualKeyCode.F11);
67
                        Console.WriteLine("F11");
68
                    }
69
                    uint key02 = (uint)(this.inputs & 0x0008) >> 3;
70
                    if (key02 == 1) {
71
                        this.send_trackir(VirtualKeyCode.F7);
72
                        Console.WriteLine("F7");
73
                    }
74
                    uint key03 = (uint)(this.inputs & 0x0004) >> 2;
75
                    if (key03 == 1) {
76
                        sendkey.Keyboard.KeyPress(VirtualKeyCode.VOLUME_MUTE);
77
                    }
78
                    uint key04 = (uint)(this.inputs & 0x0002) >> 1;
79
                    if (key04 == 1) {
80
                        sendkey.Keyboard.KeyPress(VirtualKeyCode.VOLUME_DOWN);
81
                    }
82
                    uint key05 = (uint)(this.inputs & 0x0001);
83
                    if (key05 == 1) {
84
                        sendkey.Keyboard.KeyPress(VirtualKeyCode.VOLUME_UP);
85
                    }
86
                }
87
 
88
                Console.WriteLine("Input: " + (inputs).ToString("X"));
89
            }
90
 
91
            refreshTimer.Enabled = true;
92
        }
93
 
94
        public void send_trackir(VirtualKeyCode key) {
95
            sendkey.Keyboard.KeyDown(VirtualKeyCode.CONTROL);
96
            System.Threading.Thread.Sleep(10);
97
            sendkey.Keyboard.KeyPress(key);
98
            sendkey.Keyboard.KeyUp(VirtualKeyCode.CONTROL);
99
        }
100
 
101
        public int init() {
102
            // Select our CDU power module
103
            int rslt = _mcp.SelectDevice("AAP Panel");
104
 
105
            if (rslt != 0) {
106
                return rslt;
107
            }
108
 
109
            // Enable the mcp23017
110
            _mcp.WriteGpio(3, 1);
111
 
112
            // Set io dir, pullups and rev polarity
113
            byte[] data;
114
            data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
115
            _mcp.WriteI2cData(0x40, data, 5);
116
            data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
117
            _mcp.WriteI2cData(0x40, data, 3);
118
 
119
            return 0;
120
        }
121
 
122
        public void Enable() {
123
            this.refreshTimer.Enabled = true;
124
        }
125
        public void Disable() {
126
            this.refreshTimer.Enabled = false;
127
        }
128
 
129
        public void GetInputs() {
130
            byte[] data;
131
            data = new byte[] { 0x12 }; // Select GPIOA register
132
            _mcp.WriteI2cData(0x40, data, 1);
133
 
134
            data = new byte[] { 0x00, 0x00 }; // Read two bytes
135
            int rslt = _mcp.ReadI2CData(0x41, ref data, 2);
136
 
137
            // Join all our buttons into a single inputs
138
            uint gpio = (uint)((1 - _mcp.ReadGpio(0)) | ((1 - _mcp.ReadGpio(1)) << 1));
139
            uint but_trackir = (uint)(((gpio & 0x02) << 3) | ((gpio & 0x01) << 5) | (uint)((data[0] & 0x10) >> 1) | (uint)((data[0] & 0x20) >> 3) | (uint)((data[0] & 0x40) >> 5) | (uint)((data[0] & 0x80) >> 7));
140
            uint but_pwr = (uint)(((data[0] & 0x04) >> 2) | ((data[0] & 0x08) >> 2));
141
 
142
 
143
            uint but_steer = 1;
144
            if ((uint)(data[0] & 0x02) >> 1 == 1)
145
                but_steer = 0;
146
            if ((uint)(data[0] & 0x01) == 1)
147
                but_steer = 2;
148
 
149
            uint steerpt = 0;
150
            if (((uint)(data[1] & 0x40) >> 6 == 1))
151
                steerpt = 0;
152
            if (((uint)(data[1] & 0x20) >> 5 == 1))
153
                steerpt = 1;
154
            if (((uint)(data[1] & 0x10) >> 4 == 1))
155
                steerpt = 2;
156
 
157
 
158
            uint page = 0;
159
            if (((uint)(data[1] & 0x01) == 1))
160
                page = 0;
161
            if (((uint)(data[1] & 0x02) >> 1 == 1))
162
                page = 1;
163
            if (((uint)(data[1] & 0x04) >> 2 == 1))
164
                page = 2;
165
            if (((uint)(data[1] & 0x08) >> 3 == 1))
166
                page = 3;
167
 
168
            //uint but_steer = (uint)(((data[0] & 0x01) << 1) | ((data[0] & 0x02)));
169
            //uint page = (uint)(data[1] & 0x0f);
170
            //uint sel_steert = (uint)(((data[1] & 0x40) >> 6) | ((data[1] & 0x20) >> 4) | ((data[1] & 0x10) >> 2));
171
            lock (_lockObject) {
172
                this.inputs = (uint)(but_trackir) | (but_pwr << 6) | (but_steer << 8) | (page << 10) | (steerpt << 14);
173
            }
174
 
175
 
176
            /*Console.WriteLine("SelPage: " + (sel_page).ToString("X"));
177
            Console.WriteLine("Sel_SteerRt: " + (sel_steert).ToString("X"));
178
            Console.WriteLine("TrackIR: " + (but_trackir).ToString("X"));
179
            Console.WriteLine("Power: " + (but_pwr).ToString("X"));
180
            Console.WriteLine("Steer: " + (but_steer).ToString("X"));
181
            Console.WriteLine("Inputs: " + (inputs).ToString("X"));*/
182
        }
183
 
184
    }
185
}