Subversion Repositories group.electronics

Rev

Rev 187 | 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 {

        private mcp23017[] chips;
        public byte ledShadow = 0x00;
        
        public NitMultiPanel(mcp2221 mcp) : base(mcp) {
            chips = new mcp23017[2] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x24) };

            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_1"));
            pad01.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F1));
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad01), 7));

            Dictionary<uint, Command> pad02 = new Dictionary<uint, Command>();
            pad02.Add(0, new CommandDCS("UFC_2"));
            pad02.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_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_3"));
            pad03.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_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(OSInput.VirtualKeyCodes.VK_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_4"));
            pad05.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F5));
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad05), 11));

            Dictionary<uint, Command> pad06 = new Dictionary<uint, Command>();
            pad06.Add(0, new CommandDCS("UFC_5"));
            pad06.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F6));
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad06), 10));

            Dictionary<uint, Command> pad07 = new Dictionary<uint, Command>();
            pad07.Add(0, new CommandDCS("UFC_6"));
            pad07.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_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(OSInput.VirtualKeyCodes.VK_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(OSInput.VirtualKeyCodes.VK_F9));
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad09), 15));

            Dictionary<uint, Command> pad10 = new Dictionary<uint, Command>();
            pad10.Add(0, new CommandDCS("UFC_8"));
            pad10.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_F10));
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad10), 14));

            Dictionary<uint, Command> pad11 = new Dictionary<uint, Command>();
            pad11.Add(0, new CommandDCS("UFC_9"));
            pad11.Add(1, new CommandVKey(OSInput.VirtualKeyCodes.VK_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(OSInput.VirtualKeyCodes.VK_F12));
            this.addControl(new Switch2Pos(new CommandIf(selSwitch, pad12), 12));

            // NMSP
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_EGI_BTN"), 16));
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_HARS_BTN"), 17));
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ILS_BTN"), 18));
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_TCN_BTN"), 19));
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ANCHR_BTN"), 20));
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_STEERPT_BTN"), 21));
            this.addControl(new Switch2Pos(new CommandDCS("NMSP_ABLE_STOW"), 22, true));


            // Joystick 
            Switch3Pos joySwitch = new Switch3Pos(null, 0, 1);

            Dictionary<uint, Command> joyx = new Dictionary<uint, Command>();
            joyx.Add(0, new CommandVJSAxis(1, HID_USAGES.HID_USAGE_X));
            this.addControl(new Potentiometer(new CommandIf(joySwitch, joyx), 1));

            Dictionary<uint, Command> joyy = new Dictionary<uint, Command>();
            joyy.Add(0, new CommandVJSAxis(1, HID_USAGES.HID_USAGE_Y));
            this.addControl(new Potentiometer(new CommandIf(joySwitch, joyy), 0));

            //this.addControl(new Potentiometer(new CommandVJSAxis(1, HID_USAGES.HID_USAGE_Y), 0));     // X 0x00 - 0x8000
            //this.addControl(new Potentiometer(new CommandVJSAxis(1, HID_USAGES.HID_USAGE_X), 1));     // Y 0x00 - 0x8000
            this.addControl(new Switch2Pos(new CommandMouseButton(CommandMouseButton.MB_LEFT), 32));

            // Enable the mcp23017
            mcp.WriteGpio(3, 0);
            Utils.delayms(50);
            mcp.WriteGpio(3, 1);
            Utils.delayms(50);

            chips[0].SetIODirection(0xff, 0xff);
            chips[0].SetIOPolarity(0xff, 0xff);
            chips[0].SetIOPullups(0xff, 0xff);

            chips[1].SetIODirection(0x00, 0xff);
            chips[1].SetIOPolarity(0x00, 0xff);
            chips[1].SetIOPullups(0x00, 0xff);

            this.addLed(new Led(0, 1, 0x1112, 0x0002, 1));     // NMSP_ANCHR_LED
            this.addLed(new Led(0, 4, 0x1110, 0x0800, 11));    // NMSP_EGI_LED
            this.addLed(new Led(0, 6, 0x11bc, 0x0004, 2));     // NMSP_FM_LED
            this.addLed(new Led(0, 5, 0x1110, 0x0200, 9));     // NMSP_HARS_LEF
            this.addLed(new Led(0, 3, 0x1112, 0x0020, 5));     // NMSP_ILS_LED
            this.addLed(new Led(0, 0, 0x1110, 0x8000, 15));    // NMSP_STEERPT_LED
            this.addLed(new Led(0, 2, 0x1112, 0x0008, 3));     // NMSP_TCN_LED
            this.addLed(new Led(0, 7, 0x11bc, 0x0002, 1));     // NMSP_UHF_LED

            //this.addLed(new Led(0, 6, 0x1012, 0x0800, 11));     // MASTER_CAUTION
            // Center the vjoy axis
            Globals.vjoy.SetAxis(0x4000, 1, HID_USAGES.HID_USAGE_X);
            Globals.vjoy.SetAxis(0x4000, 1, HID_USAGES.HID_USAGE_Y);

            data.axis[0].mapsize = 0x8000;
            data.axis[1].mapsize = 0x8000;
            data.axis[0].thres = 10;
            data.axis[1].thres = 10;

            this.Refresh();
            data.prev = data.buttons;
            data.axis[0].prev = data.axis[0].value;
            data.axis[1].prev = data.axis[1].value;
            data.changed = false;

            return 1;
        }
        public override int Refresh() {
            byte[] bytes;
            int rslt = 0;

            // Join all our buttons into a single inputs
            UInt64 gpio = (UInt64)(1 - mcp.ReadGpio(0));
            data.buttons = (UInt64)gpio << 32;

            rslt = chips[1].GetIO(out bytes);
            data.buttons |= (UInt64)bytes[0] << 24;
            data.buttons |= (UInt64)bytes[1] << 16;

            rslt = chips[0].GetIO(out bytes);
            data.buttons |= (UInt64)bytes[0] << 8;
            data.buttons |= (UInt64)bytes[1];

            data.axis[0].value = mcp.ReadADC(0); // Y Axis
            data.axis[1].value = mcp.ReadADC(1); // X Axis

            if (data.buttons != data.prev
                || (data.axis[0].prev != data.axis[0].value)
                || (data.axis[1].prev != data.axis[1].value)) {
                    foreach (Control control in this.controls) {
                        control.data = this.data;
                        control.Tick();
                    }
                    data.prev = data.buttons;
            }
            
            byte tmpShadow = this.ledShadow;
            foreach (Led led in this.leds) {
                led.Tick(ref ledShadow);
            }

            // Only update if leds have changed
            if (tmpShadow != this.ledShadow)
                chips[1].SetBank(0, ledShadow);

            return rslt;
        }

        public override int Input() {
            data.changed = false;
            return 1;
        }
    }
}