Subversion Repositories group.electronics

Rev

Rev 165 | Rev 167 | 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;

        Utils.InputPair js_x;
        Utils.InputPair js_y;

        private mcp23017[] chips;

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

            js_x.curr = js_x.prev = 0;
            js_y.curr = js_y.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_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;

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

            // Join all our buttons into a single inputs
            UInt64 gpio = (UInt64)(1 - mcp.ReadGpio(0));
            input.curr = (UInt64)gpio << 32;
            
            rslt = chips[1].GetIO(out data);
            input.curr |= (UInt64)data[0] << 24;
            input.curr |= (UInt64)data[1] << 16;

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

            js_y.curr = mcp.ReadADC(0);
            js_x.curr = mcp.ReadADC(1);
        

            //Console.WriteLine(js_x.curr.ToString() + ":" + js_y.curr.ToString());

            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();
            }
            this.inputChanged = false;

            input.prev = input.curr;
            return 1;
        }
    }
}