Subversion Repositories group.electronics

Rev

Rev 162 | 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 interface IPanel {
        int Init();
        int Refresh();
        int Input();
    }

    public abstract class Panel : IPanel {
        protected mcp2221 mcp;
        public int id { get; set; }
        public String name { get; set; }
        public String serialno { get; set; }

        public List<Control> controls = new List<Control>();

        public Boolean inputChanged { get; set; }


        private Boolean enabled;
        public Boolean Enabled {
            get {
                return enabled;
            }
            set {
                this.enabled = value;
            }
        }

        public Panel(mcp2221 mcp) {
            this.mcp = mcp;
            this.mcp.usbi2c.Settings.GetConnectionStatus();
            this.id = this.mcp.usbi2c.Management.GetSelectedDevNum();
            this.name = this.mcp.usbi2c.Settings.GetUsbStringDescriptor();

            this.inputChanged = false;
        }

        public virtual int Init() {
            

            return 1;
        }
        public abstract int Refresh();
        public abstract int Input();

        public void addControl(Control control) {
            this.controls.Add(control);
        }
    }


    public class Panel_AHFS : Panel {
        Utils.InputPair switchInput;
        Utils.InputPair adcInput;

        public uint adc_max;
        public uint adc_threshold;

        private mcp23017[] chips;

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

            switchInput.curr = 0;
            switchInput.prev = 0;
            adcInput.curr = 0;
            adcInput.prev = 0;
            this.adc_max = 930;
            this.adc_threshold = 15;

            this.Init();
        }

        public override int Init() {
            //AHCP
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_MASTER_ARM"), 8, 9));   // Train - Safe - Arm
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_GUNPAC"), 10, 11));     // Gunarm - Safe - Arm
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_LASER_ARM"), 12, 13));  // Train - Safe - Arm
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_TGP"), 14));            // Off - On
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_ALT_SCE"), 0, 1));      // Radar  - Delta - Baro
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_DAYNIGHT"), 2));    // Night - Day
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_HUD_MODE"), 15));       // Stby - Norm
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_CICU"), 3));            // Off - On
            this.addControl(new Switch2Pos(new CommandDCS("AHCP_JTRS"), 4));            // Off - On
            this.addControl(new Switch3Pos(new CommandDCS("AHCP_IFFCC"), 6, 5));        // Off - Test - On
            //this.addControl(new Switch2Pos(new CommandDCS("HARS_FAST_ERECT"), 7));      // Off - On
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 7));
            
            // Fuel System
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_AMPL"), 16));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_L"), 24));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_MAIN_R"), 25));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_WING_L"), 26));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_BOOST_WING_R"),27 ));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_CROSSFEED"), 28));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_EXT_TANKS_FUS"), 30));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_EXT_TANKS_WING"), 31));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_MAIN_L"), 20, true));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_MAIN_R"), 21, true));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_WING_L"), 18, true));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_FD_WING_R"), 19, true));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_LINE_CHECK"), 17));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_RCVR_LEVER"), 23));
            this.addControl(new Switch2Pos(new CommandDCS("FSCP_TK_GATE"), 29));
             
            mcp.WriteGpio(3, 0);
            Utils.delayms(10);
            // Enable the mcp23017
            mcp.WriteGpio(3, 1);
            
            // Set io dir, pullups and rev polarity
            chips[0].SetIODirection(0xff, 0xff);
            chips[0].SetIOPolarity(0xf0, 0xff);
            chips[0].SetIOPullups(0xff, 0xff);

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

            // Get the initial ADC value
            adcInput.prev = adcInput.curr = mcp.ReadADC(1);
            this.Refresh();
            switchInput.prev = switchInput.curr;

            return 0;
        }

        public override int Refresh() {

            byte[] data;
            int rslt = 0;
            rslt = chips[0].GetIO(out data);
            
            switchInput.curr = (uint)data[0] << 24;
            switchInput.curr |= (uint)data[1] << 16;

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

            /*
            // Do the Refueling light adc
            devices[devid].cur_adc = mcp.ReadADC(1);

            if (devices[devid].cur_adc > devices[devid].max_adc)
                devices[devid].max_adc = devices[devid].cur_adc;

            ushort lowerval = 0;
            if (devices[devid].prev_adc >= devices[devid].adc_thres)
                lowerval = (ushort)(devices[devid].prev_adc - devices[devid].adc_thres);

            ushort upperval = devices[devid].max_adc;
            if (devices[devid].prev_adc <= upperval - devices[devid].adc_thres)
                upperval = (ushort)(devices[devid].prev_adc + devices[devid].adc_thres);

            //Console.WriteLine("ADC: " + devices[devid].cur_adc + " Max: " + devices[devid].max_adc + " Low: " + lowerval + " High: " + upperval);

            if ((devices[devid].cur_adc < lowerval) || (devices[devid].cur_adc >= upperval)) {
                // Cover our min/max ranges within threshold
                if (devices[devid].cur_adc < devices[devid].adc_thres)
                    devices[devid].cur_adc = 0;
                if (devices[devid].cur_adc > devices[devid].max_adc - devices[devid].adc_thres)
                    devices[devid].cur_adc = devices[devid].max_adc;

                devices[devid].prev_adc = devices[devid].cur_adc;

                ushort refuellight = map_ushort(devices[devid].cur_adc, 0, devices[devid].max_adc, 0, 0xffff);
                Globals.bios.SendData("ALCP_RCVR_LTS " + refuellight.ToString() + "\n");
                Console.WriteLine("ALCP_RCVR_LTS " + ":" + refuellight.ToString() + "(" + devices[devid].cur_adc + ")");
            }
             * */


            if ((switchInput.curr != switchInput.prev) || (adcInput.prev != adcInput.curr))
                this.inputChanged = true;
            else
                this.inputChanged = false;

            return 1;
        }

        public override int Input() {
            
            foreach (Control control in this.controls) {
                control.data = this.switchInput;
                control.Tick();
            }
            switchInput.prev = switchInput.curr;
            adcInput.prev = adcInput.curr;
            this.inputChanged = false;
            return 1;
        }
    }

    public class Panel_AAP : Panel {

        Utils.InputPair input;
        private mcp23017 chip0;

        public Panel_AAP(mcp2221 mcp) : base(mcp) {
            input.curr = 0;
            input.prev = 0;

            chip0 = new mcp23017(mcp, 0x20);

            this.Init();
        }

        public override int Init() {
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F9), 16));
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F11), 17));
            this.addControl(new Switch2Pos(new CommandTrackIRKey(VirtualKeyCode.F7), 12));
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.SPACE), 13));
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.RETURN), 14));
            this.addControl(new Switch2Pos(new CommandVKey(VirtualKeyCode.VOLUME_MUTE), 15));
            this.addControl(new Switch2Pos(new CommandDCS("AAP_CDUPWR"), 11));
            this.addControl(new Switch2Pos(new CommandDCS("AAP_EGIPWR"), 10));
            this.addControl(new Switch3Pos(new CommandDCS("AAP_STEER"), 9, 8));
            this.addControl(new Selector(new CommandDCS("AAP_STEERPT"), new int[] { 4, 5, 6 }));
            this.addControl(new Selector(new CommandDCS("AAP_PAGE"), new int[] { 0, 1, 2, 3 }));

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

            // Set io dir, pullups and rev polarity
            chip0.SetIODirection(0xff, 0xff);
            chip0.SetIOPolarity(0xff, 0xff);
            chip0.SetIOPullups(0xff, 0xff);

            this.Refresh();
            input.prev = input.curr;

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

            // Join all our buttons into a single inputs
            uint gpio = (uint)((1 - mcp.ReadGpio(0)) | ((1 - mcp.ReadGpio(1)) << 1));
            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;
        }
    }
}