Rev 163 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsInput.Native;
namespace nitdcscore {
public class NitMultiPanel : Panel {
Utils.InputPair input;
public NitMultiPanel(mcp2221 mcp)
: base(mcp) {
input.curr = 0;
input.prev = 0;
this.Init();
}
public override int Init() {
// Choose what function the number pad shall carry out:
// 0 = UFC #, 1 = F1-F12 Keys, 2 = Misc (+ TrackIR)
Switch3Pos selSwitch = new Switch3Pos(null, 2, 3);
// Row 1
Dictionary<uint, Command> pad01 = new Dictionary<uint, Command>();
pad01.Add(0, new CommandDCS("UFC_01"));
pad01.Add(1, new CommandVKey(VirtualKeyCode.F1));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad01), 7));
Dictionary<uint, Command> pad02 = new Dictionary<uint, Command>();
pad02.Add(0, new CommandDCS("UFC_02"));
pad02.Add(1, new CommandVKey(VirtualKeyCode.F2));
pad02.Add(2, new CommandTrackIRKey(VirtualKeyCode.F7));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad02), 6));
Dictionary<uint, Command> pad03 = new Dictionary<uint, Command>();
pad03.Add(0, new CommandDCS("UFC_03"));
pad03.Add(1, new CommandVKey(VirtualKeyCode.F3));
pad03.Add(2, new CommandTrackIRKey(VirtualKeyCode.F11));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad03), 5));
Dictionary<uint, Command> pad04 = new Dictionary<uint,Command>();
pad04.Add(0, new CommandDCS("UFC_ENT"));
pad04.Add(1, new CommandVKey(VirtualKeyCode.F4));
pad04.Add(2, new CommandTrackIRKey(VirtualKeyCode.F9));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad04), 4));
// Row 2
Dictionary<uint, Command> pad05 = new Dictionary<uint, Command>();
pad05.Add(0, new CommandDCS("UFC_04"));
pad05.Add(1, new CommandVKey(VirtualKeyCode.F5));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad05), 11));
Dictionary<uint, Command> pad06 = new Dictionary<uint, Command>();
pad06.Add(0, new CommandDCS("UFC_05"));
pad06.Add(1, new CommandVKey(VirtualKeyCode.F6));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad06), 10));
Dictionary<uint, Command> pad07 = new Dictionary<uint, Command>();
pad07.Add(0, new CommandDCS("UFC_06"));
pad07.Add(1, new CommandVKey(VirtualKeyCode.F7));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad07), 9));
Dictionary<uint, Command> pad08 = new Dictionary<uint, Command>();
pad08.Add(0, new CommandDCS("UFC_10"));
pad08.Add(1, new CommandVKey(VirtualKeyCode.F8));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad08), 8));
// Row 3
Dictionary<uint, Command> pad09 = new Dictionary<uint, Command>();
pad09.Add(0, new CommandDCS("UFC_7"));
pad09.Add(1, new CommandVKey(VirtualKeyCode.F9));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad09), 15));
Dictionary<uint, Command> pad10 = new Dictionary<uint, Command>();
pad10.Add(0, new CommandDCS("UFC_08"));
pad10.Add(1, new CommandVKey(VirtualKeyCode.F10));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad10), 14));
Dictionary<uint, Command> pad11 = new Dictionary<uint, Command>();
pad11.Add(0, new CommandDCS("UFC_09"));
pad11.Add(1, new CommandVKey(VirtualKeyCode.F11));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad11), 13));
Dictionary<uint, Command> pad12 = new Dictionary<uint, Command>();
pad12.Add(0, new CommandDCS("UFC_CLR"));
pad12.Add(1, new CommandVKey(VirtualKeyCode.F12));
this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad12), 12));
// Enable the mcp23017
mcp.WriteGpio(3, 0);
Utils.delayms(500);
mcp.WriteGpio(3, 1);
//this.delayms();
// 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);
this.Refresh();
input.prev = input.curr;
return 1;
}
public override int Refresh() {
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));
input.curr = (uint)gpio << 16;
input.curr |= (uint)data[0] << 8;
input.curr |= (uint)data[1];
if (input.curr != input.prev)
this.inputChanged = true;
else
this.inputChanged = false;
return rslt;
}
public override int Input() {
Console.WriteLine(input.curr.ToString("X"));
foreach (Control control in this.controls) {
control.data = this.input;
control.Tick();
}
input.prev = input.curr;
this.inputChanged = false;
return 1;
}
}
}