Subversion Repositories group.electronics

Rev

Rev 164 | Rev 168 | 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 jsaxis {
        public UInt16 prev { get; set; }        // The previous ADC value
        public UInt16 value { get; set; }       // The current ADC value
        public UInt16 thres { get; set; }       // Threshold to report a new position
        public UInt16 max { get; set; }              // The maximum value we've seen
        public UInt16 mapsize { get; set; }     // The mapping maximum

        public jsaxis() {

        }

        public int getPosition(ref UInt16 position) {
            if (this.value > this.max)
                this.max = this.value;

            UInt16 lowerval = 0;
            if (this.prev >= this.thres)
                lowerval = (UInt16)(this.prev - this.thres);

            ushort upperval = this.max;
            if (this.prev <= upperval - this.thres)
                upperval = (ushort)(this.prev + this.thres);

            if ((this.value < lowerval) || (this.value >= upperval)) {
                // Cover our min/max ranges within threshold
                if (this.value < this.thres)
                    this.value = 0;
                if (this.value > this.max - this.thres)
                    this.value = this.max;

                this.prev = this.value;

                position = Globals.map_uint16(this.value, 0, this.max, 0, this.mapsize);
                return 1;
                //Globals.bios.SendData("ALCP_RCVR_LTS " + refuellight.ToString() + "\n");
                //Console.WriteLine("ALCP_RCVR_LTS " + ":" + refuellight.ToString() + "(" + devices[devid].cur_adc + ")");
            } else {
                position = 0;
                return 1;
            }
        }


    }

    public struct jsdata {
        public bool changed;
        public UInt64 buttons;
        public UInt64 prev;

        public jsaxis[] axis;
    }


    public interface IPanel {
        int Init();
        int Refresh();
        int Input();
    }

    public abstract class Panel : IPanel {
        public jsdata data = new jsdata();
        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.data.axis[0] = new jsaxis();
            this.data.axis[1] = new jsaxis();


            data.changed = 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) };

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

            data.axis[0].prev = data.axis[0].value = 0;
            data.axis[0].thres = 15;
            data.axis[0].max = 930;
            data.axis[0].mapsize = 0xffff;

            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));
            this.addControl(new Potentiometer(new CommandDCS("ALCP_RCVR_LTS"), 0));     // 0x00 - 0xFFFF
            
            // 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 values
            this.Refresh();
            data.prev = data.buttons;
            data.axis[0].prev = data.axis[0].value = mcp.ReadADC(1);

            return 0;
        }

        public override int Refresh() {

            byte[] bytes;
            int rslt = 0;
            rslt = chips[0].GetIO(out bytes);

            data.buttons = (uint)bytes[0] << 24;
            data.buttons |= (uint)bytes[1] << 16;

            rslt = chips[1].GetIO(out bytes);

            data.buttons |= (uint)bytes[0] << 8;
            data.buttons |= (uint)bytes[1];

            data.axis[0].value = mcp.ReadADC(1);

            if ((data.buttons != data.prev) || (data.axis[0].prev != data.axis[0].value))
                data.changed = true;
            else
                data.changed = false;

            return 1;
        }

        public override int Input() {
            
            foreach (Control control in this.controls) {
                control.data = this.data;
                control.Tick();
            }
            data.prev = data.buttons;
            data.axis[0].prev = data.axis[0].value;
            data.changed = false;
            return 1;
        }
    }

    public class Panel_AAP : Panel {

        private mcp23017 chip0;

        public Panel_AAP(mcp2221 mcp) : base(mcp) {
            data.buttons = 0;
            data.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();
            data.prev = data.buttons;

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

            // Join all our buttons into a single inputs
            uint gpio = (uint)((1 - mcp.ReadGpio(0)) | ((1 - mcp.ReadGpio(1)) << 1));
            data.buttons = (uint)gpio << 16;
            data.buttons |= (uint)bytes[0] << 8;
            data.buttons |= (uint)bytes[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.prev = data.buttons;
            data.changed = false;
            return 1;
        }
    }
}