Subversion Repositories group.electronics

Rev

Rev 167 | Rev 170 | 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 {

        private mcp23017[] chips;

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

            data.buttons = 0;
            data.prev = 0;

            data.axis[0].mapsize = 0xffff;
            data.axis[0].thres = 15;

            data.axis[1].mapsize = 0xffff;
            data.axis[1].thres = 15;

            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(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_2"));
            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_3"));
            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_4"));
            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_5"));
            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_6"));
            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_8"));
            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_9"));
            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));

            // Joystick Press - Mapped to mouse button 1
            this.addControl(new Switch2Pos(new CommandMouseButton(CommandMouseButton.MB_LEFT), 32));


            // 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));


            // 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.Refresh();
            //input.prev = input.curr;

            data.prev = data.buttons;
            data.axis[0].prev = data.axis[0].value;
            data.axis[1].prev = data.axis[1].value;


            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);
            data.axis[1].value = mcp.ReadADC(1);


            if (data.buttons != data.prev)
                data.changed = true;
            else
                data.changed = false;

            return rslt;
        }

        public override int Input() {
            //Console.WriteLine(input.curr.ToString("X"));
            foreach (Control control in this.controls) {
                control.data = this.data;
                control.Tick();
            }
            data.changed = false;

            data.prev = data.buttons;
            return 1;
        }
    }
}