Blame | Last modification | View Log | RSS feed
using MCP2221;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using WindowsInput;
using WindowsInput.Native;
namespace nitdcscore{
class NITAAPDevice {
DcsBios _bios;
Bridge_mcp2221 _mcp;
Timer refreshTimer = new Timer();
InputSimulator sendkey = new InputSimulator();
private readonly Object _lockObject = new object();
uint _prev_inputs = 0;
uint _prev_keys = 0;
public uint inputs { get; set; }
public NITAAPDevice(ref DcsBios bios, Bridge_mcp2221 mcp) {
_bios = bios;
_mcp = mcp;
refreshTimer.Enabled = false;
refreshTimer.AutoReset = true;
refreshTimer.Interval = 50;
refreshTimer.Elapsed += refreshTimer_Elapsed;
}
void refreshTimer_Elapsed(object sender, ElapsedEventArgs e) {
refreshTimer.Enabled = false;
this.GetInputs();
if (_prev_inputs != inputs) {
_prev_inputs = inputs;
uint aap_cdupwr = (uint)(this.inputs & 0x0080) >> 7;
_bios.SendData("AAP_CDUPWR " + aap_cdupwr + "\n");
uint aap_egipwr = (uint)(this.inputs & 0x0040) >> 6;
_bios.SendData("AAP_EGIPWR " + aap_egipwr + "\n");
uint steer = (uint)(this.inputs & 0x0300) >> 8;
_bios.SendData("AAP_STEER " + steer + "\n");
uint steerpt = (uint)(this.inputs & 0xC000) >> 14;
_bios.SendData("AAP_STEERPT " + steerpt + "\n");
uint page = (uint)(this.inputs & 0xC00) >> 10;
_bios.SendData("AAP_PAGE " + page + "\n");
uint keys = (uint)(this.inputs & 0x003f);
if (_prev_keys != keys) {
uint key00 = (uint)(this.inputs & 0x0020) >> 5;
if (key00 == 1) {
this.send_trackir(VirtualKeyCode.F9);
Console.WriteLine("F9");
}
uint key01 = (uint)(this.inputs & 0x0010) >> 4;
if (key01 == 1) {
this.send_trackir(VirtualKeyCode.F11);
Console.WriteLine("F11");
}
uint key02 = (uint)(this.inputs & 0x0008) >> 3;
if (key02 == 1) {
this.send_trackir(VirtualKeyCode.F7);
Console.WriteLine("F7");
}
uint key03 = (uint)(this.inputs & 0x0004) >> 2;
if (key03 == 1) {
sendkey.Keyboard.KeyPress(VirtualKeyCode.VOLUME_MUTE);
}
uint key04 = (uint)(this.inputs & 0x0002) >> 1;
if (key04 == 1) {
sendkey.Keyboard.KeyPress(VirtualKeyCode.VOLUME_DOWN);
}
uint key05 = (uint)(this.inputs & 0x0001);
if (key05 == 1) {
sendkey.Keyboard.KeyPress(VirtualKeyCode.VOLUME_UP);
}
}
Console.WriteLine("Input: " + (inputs).ToString("X"));
}
refreshTimer.Enabled = true;
}
public void send_trackir(VirtualKeyCode key) {
sendkey.Keyboard.KeyDown(VirtualKeyCode.CONTROL);
System.Threading.Thread.Sleep(10);
sendkey.Keyboard.KeyPress(key);
sendkey.Keyboard.KeyUp(VirtualKeyCode.CONTROL);
}
public int init() {
// Select our CDU power module
int rslt = _mcp.SelectDevice("AAP Panel");
if (rslt != 0) {
return rslt;
}
// Enable the mcp23017
_mcp.WriteGpio(3, 1);
// Set io dir, pullups and rev polarity
byte[] data;
data = new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff }; // All inputs & polarity
_mcp.WriteI2cData(0x40, data, 5);
data = new byte[] { 0x0c, 0xff, 0xff }; // All pullups = on
_mcp.WriteI2cData(0x40, data, 3);
return 0;
}
public void Enable() {
this.refreshTimer.Enabled = true;
}
public void Disable() {
this.refreshTimer.Enabled = false;
}
public void GetInputs() {
byte[] data;
data = new byte[] { 0x12 }; // Select GPIOA register
_mcp.WriteI2cData(0x40, data, 1);
data = new byte[] { 0x00, 0x00 }; // Read two bytes
int rslt = _mcp.ReadI2CData(0x41, ref data, 2);
// Join all our buttons into a single inputs
uint gpio = (uint)((1 - _mcp.ReadGpio(0)) | ((1 - _mcp.ReadGpio(1)) << 1));
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));
uint but_pwr = (uint)(((data[0] & 0x04) >> 2) | ((data[0] & 0x08) >> 2));
uint but_steer = 1;
if ((uint)(data[0] & 0x02) >> 1 == 1)
but_steer = 0;
if ((uint)(data[0] & 0x01) == 1)
but_steer = 2;
uint steerpt = 0;
if (((uint)(data[1] & 0x40) >> 6 == 1))
steerpt = 0;
if (((uint)(data[1] & 0x20) >> 5 == 1))
steerpt = 1;
if (((uint)(data[1] & 0x10) >> 4 == 1))
steerpt = 2;
uint page = 0;
if (((uint)(data[1] & 0x01) == 1))
page = 0;
if (((uint)(data[1] & 0x02) >> 1 == 1))
page = 1;
if (((uint)(data[1] & 0x04) >> 2 == 1))
page = 2;
if (((uint)(data[1] & 0x08) >> 3 == 1))
page = 3;
//uint but_steer = (uint)(((data[0] & 0x01) << 1) | ((data[0] & 0x02)));
//uint page = (uint)(data[1] & 0x0f);
//uint sel_steert = (uint)(((data[1] & 0x40) >> 6) | ((data[1] & 0x20) >> 4) | ((data[1] & 0x10) >> 2));
lock (_lockObject) {
this.inputs = (uint)(but_trackir) | (but_pwr << 6) | (but_steer << 8) | (page << 10) | (steerpt << 14);
}
/*Console.WriteLine("SelPage: " + (sel_page).ToString("X"));
Console.WriteLine("Sel_SteerRt: " + (sel_steert).ToString("X"));
Console.WriteLine("TrackIR: " + (but_trackir).ToString("X"));
Console.WriteLine("Power: " + (but_pwr).ToString("X"));
Console.WriteLine("Steer: " + (but_steer).ToString("X"));
Console.WriteLine("Inputs: " + (inputs).ToString("X"));*/
}
}
}