Subversion Repositories group.electronics

Rev

Rev 189 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsInput.Native;

namespace nitdcscore {
    public class NitAuxPanel : Panel {

        private mcp23017[] chips;
        private Oled oled;
        public UInt16 ledShadowA = 0x0000;

        public UInt16 flap_value = 0;
        public UInt16 flap_shadow = 1;

        BitImage flap_background;
        BitImage flap_needle;

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

            this.Init();
        }

        public override int Init() {

            // The strings to send for magnetic hold switches
            Dictionary<UInt64, String> magSwitch = new Dictionary<ulong,string>();
            magSwitch.Add(0, "OFF");
            magSwitch.Add(1, "PUSH");

            this.addControl(new Switch3Pos(new CommandDCS("FIRE_EXT_DISCH"), 0, 1));
            this.addControl(new Switch2Pos(new CommandDCS("FIRE_RENG_PULL"), 2));
            this.addControl(new Switch2Pos(new CommandDCS("EXT_STORES_JETTISON"), 3));
            this.addControl(new Switch2Pos(new CommandDCS("FIRE_APU_PULL"), 4));
            this.addControl(new Switch2Pos(new CommandDCS("FIRE_LENG_PULL"), 5));
            this.addControl(new Switch3Pos(new CommandDCS("SEAT_ADJUST"), 6, 7));

            this.addControl(new Switch2Pos(new CommandDCSMap("ANTI_SKID_SWITCH", magSwitch), 8));
            this.addControl(new Switch3Pos(new CommandDCS("LANDING_LIGHTS"), 9, 10));
            this.addControl(new Switch2Pos(new CommandDCS("DOWNLOCK_OVERRIDE"), 11));
            this.addControl(new Switch2Pos(new CommandDCS("ENGINE_TEMS_DATA"), 12));
            this.addControl(new Switch2Pos(new CommandDCS("GEAR_LEVER"), 14));

            this.addControl(new Switch2Pos(new CommandDCSMap("SASP_PITCH_SAS_L", magSwitch), 17));
            this.addControl(new Switch2Pos(new CommandDCSMap("SASP_PITCH_SAS_R", magSwitch), 18));
            this.addControl(new Switch2Pos(new CommandDCS("SASP_TO_TRIM"), 19));
            this.addControl(new Switch3Pos(new CommandDCS("SASP_MONITOR_TEST"), 20, 21));
            this.addControl(new Switch2Pos(new CommandDCSMap("SASP_YAW_SAS_R", magSwitch), 22));
            this.addControl(new Switch2Pos(new CommandDCSMap("SASP_YAW_SAS_L", magSwitch), 23));
            this.addControl(new Potentiometer(new CommandDCS("SASP_YAW_TRIM"), 0));

            this.addControl(new Switch2Pos(new CommandDCS("EFCP_FLAPS_EMER_RETR"), 24));
            this.addControl(new Switch3Pos(new CommandDCS("EFCP_AILERON_EMER_DISENGAGE"), 26, 25));
            this.addControl(new Switch3Pos(new CommandDCS("EFCP_ELEVATOR_EMER_DISENGAGE"), 31, 30));
            this.addControl(new Switch2Pos(new CommandDCS("EFCP_SPDBK_EMER_RETR"), 27));
            this.addControl(new Switch2Pos(new CommandDCS("EFCP_TRIM_OVERRIDE"), 28));
            this.addControl(new Switch2Pos(new CommandDCS("EFCP_MRFCS"), 29));

            // 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(0xff, 0xff);
            chips[1].SetIOPolarity(0xff, 0xff);
            chips[1].SetIOPullups(0xff, 0xff);

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

            this.addLed(new Led(1, 6, 0x1026, 0x1000, 12));     // Left
            this.addLed(new Led(1, 4, 0x1026, 0x0800, 11));     // Nose
            this.addLed(new Led(1, 2, 0x1026, 0x2000, 13));     // Right
            this.addLed(new Led(1, 1, 0x1026, 0x4000, 14));     // Gear Handle

            this.addLed(new Led(0, 12, 0x10da, 0x0040, 6));     // L_AILERON_EMER_DISENGAGE
            this.addLed(new Led(0, 11, 0x10da, 0x0080, 7));     // R_AILERON_EMER_DISENGAGE
            this.addLed(new Led(0, 10, 0x10da, 0x0100, 8));     // L_ELEVATOR_EMER_DISENGAGE 
            this.addLed(new Led(0, 9, 0x10da, 0x0200, 9));     // R_ELEVATOR_EMER_DISENGAGE 
            this.addLed(new Led(0, 8, 0x1026, 0x0400, 10));    // TAKE_OFF_TRIM

            
            flap_background = new BitImage(new Bitmap(nitdcscore.Properties.Resources.flap_bg));
            flap_needle = new BitImage(new Bitmap(nitdcscore.Properties.Resources.flap_needle));

            oled = new Oled(Oled.SWITCHCAPVCC, 0x3d, mcp);
            oled.init();
            //oled.display();

            // Register the flap gauge position address
            UInt16 din; // Addr 10A0, Mask FFFF, Max Val FFFF, Shift 0
            if (!Globals.BiosOutput.TryGetValue(0x10A0, out din)) {
                Globals.BiosOutput.Add(0x10A0, 0x0000);
            }

            data.axis[0].mapsize = 0xFFFF;
            data.axis[0].thres = 8;

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

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

            // Join all our buttons into a single inputs

            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

            if (data.buttons != data.prev || data.axis[0].value != data.axis[0].prev) {
                foreach (Control control in this.controls) {
                    control.data = this.data;
                    control.Tick();
                }
                data.prev = data.buttons;
            }
            
            
            // Update the leds if they have changed
            UInt16 tmpShadow = this.ledShadowA;
            foreach (Led led in this.leds) {
                led.Tick(ref ledShadowA);
            }

            if (tmpShadow != this.ledShadowA) {
                byte gpa = (byte)ledShadowA;
                byte gpb = (byte)(ledShadowA >> 8);
                
                chips[2].SetBank(0, (byte)gpa);
                chips[2].SetBank(1, (byte)gpb);
            }

            // Check if the flaps need to be updated
            Globals.BiosOutput.TryGetValue(0x10A0, out this.flap_value);
            if (this.flap_value != this.flap_shadow) {

                BitImage flap_rotate = new BitImage(flap_needle);
                BitImage flap_display = new BitImage(flap_background);

                UInt16 flap_set = Globals.map_uint16(flap_value, 0, 0xffff, 0, 60);
                flap_rotate.rotate(flap_set);
                flap_display.blit(flap_rotate, 60, 4);
                oled.buffer = flap_display.toByteArray();
                oled.i2c.SetGpio(1, 1);
                oled.display();
                oled.i2c.SetGpio(1, 0);

                this.flap_shadow = this.flap_value;

                Console.WriteLine("Oled Flap Position: {0}", flap_value);
            }

            return rslt;
        }

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