Subversion Repositories group.electronics

Rev

Rev 181 | Rev 184 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
181 pfowler 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using WindowsInput.Native;
7
 
8
namespace nitdcscore {
9
    public class NitAuxPanel : Panel {
10
 
11
        private mcp23017[] chips;
183 pfowler 12
        private Oled oled;
181 pfowler 13
        public UInt16 ledShadowA = 0x0000;
14
 
15
        public NitAuxPanel(mcp2221 mcp) : base(mcp) {
16
            chips = new mcp23017[3] { new mcp23017(mcp, 0x20), new mcp23017(mcp, 0x21), new mcp23017(mcp, 0x22) };
17
 
18
            this.Init();
19
        }
20
 
21
        public override int Init() {
22
 
23
            // The strings to send for magnetic hold switches
24
            Dictionary<UInt64, String> magSwitch = new Dictionary<ulong,string>();
25
            magSwitch.Add(0, "OFF");
26
            magSwitch.Add(1, "PUSH");
27
 
28
            this.addControl(new Switch3Pos(new CommandDCS("FIRE_EXT_DISCH"), 0, 1));
29
            this.addControl(new Switch2Pos(new CommandDCS("FIRE_RENG_PULL"), 2));
30
            this.addControl(new Switch2Pos(new CommandDCS("EXT_STORES_JETTISON"), 3));
31
            this.addControl(new Switch2Pos(new CommandDCS("FIRE_APU_PULL"), 4));
32
            this.addControl(new Switch2Pos(new CommandDCS("FIRE_LENG_PULL"), 5));
33
            this.addControl(new Switch3Pos(new CommandDCS("SEAT_ADJUST"), 6, 7));
34
 
35
            this.addControl(new Switch2Pos(new CommandDCSMap("ANTI_SKID_SWITCH", magSwitch), 8));
36
            this.addControl(new Switch3Pos(new CommandDCS("LANDING_LIGHTS"), 9, 10));
37
            this.addControl(new Switch2Pos(new CommandDCS("DOWNLOCK_OVERRIDE"), 11));
38
            this.addControl(new Switch2Pos(new CommandDCS("ENGINE_TEMS_DATA"), 12));
39
            this.addControl(new Switch2Pos(new CommandDCS("GEAR_LEVER"), 14));
40
 
41
            this.addControl(new Switch2Pos(new CommandDCSMap("SASP_PITCH_SAS_L", magSwitch), 17));
42
            this.addControl(new Switch2Pos(new CommandDCSMap("SASP_PITCH_SAS_R", magSwitch), 18));
43
            this.addControl(new Switch2Pos(new CommandDCS("SASP_TO_TRIM"), 19));
44
            this.addControl(new Switch3Pos(new CommandDCS("SASP_MONITOR_TEST"), 20, 21));
45
            this.addControl(new Switch2Pos(new CommandDCSMap("SASP_YAW_SAS_R", magSwitch), 22));
46
            this.addControl(new Switch2Pos(new CommandDCSMap("SASP_YAW_SAS_L", magSwitch), 23));
47
            this.addControl(new Potentiometer(new CommandDCS("SASP_YAW_TRIM"), 0));
48
 
49
            this.addControl(new Switch2Pos(new CommandDCS("EFCP_FLAPS_EMER_RETR"), 24));
50
            this.addControl(new Switch3Pos(new CommandDCS("EFCP_AILERON_EMER_DISENGAGE"), 26, 25));
51
            this.addControl(new Switch3Pos(new CommandDCS("EFCP_ELEVATOR_EMER_DISENGAGE"), 31, 30));
52
            this.addControl(new Switch2Pos(new CommandDCS("EFCP_SPDBK_EMER_RETR"), 27));
53
            this.addControl(new Switch2Pos(new CommandDCS("EFCP_TRIM_OVERRIDE"), 28));
54
            this.addControl(new Switch2Pos(new CommandDCS("EFCP_MRFCS"), 29));
55
 
56
            // Enable the mcp23017
57
            mcp.WriteGpio(3, 0);
58
            Utils.delayms(50);
59
            mcp.WriteGpio(3, 1);
60
            Utils.delayms(50);
61
 
62
            chips[0].SetIODirection(0xff, 0xff);
63
            chips[0].SetIOPolarity(0xff, 0xff);
64
            chips[0].SetIOPullups(0xff, 0xff);
65
 
66
            chips[1].SetIODirection(0xff, 0xff);
67
            chips[1].SetIOPolarity(0xff, 0xff);
68
            chips[1].SetIOPullups(0xff, 0xff);
69
 
70
            chips[2].SetIODirection(0x00, 0x00);
71
            chips[2].SetIOPolarity(0x00, 0x00);
72
            chips[2].SetIOPullups(0x00, 0x00);
73
 
74
            this.addLed(new Led(1, 6, 0x1026, 0x1000, 12));     // Left
75
            this.addLed(new Led(1, 4, 0x1026, 0x0800, 11));     // Nose
76
            this.addLed(new Led(1, 2, 0x1026, 0x2000, 13));     // Right
77
            this.addLed(new Led(1, 1, 0x1026, 0x4000, 14));     // Gear Handle
78
 
79
            this.addLed(new Led(0, 12, 0x10da, 0x0040, 6));     // L_AILERON_EMER_DISENGAGE
80
            this.addLed(new Led(0, 11, 0x10da, 0x0080, 7));     // R_AILERON_EMER_DISENGAGE
81
            this.addLed(new Led(0, 10, 0x10da, 0x0100, 8));     // L_ELEVATOR_EMER_DISENGAGE 
82
            this.addLed(new Led(0, 9, 0x10da, 0x0200, 9));     // R_ELEVATOR_EMER_DISENGAGE 
83
            this.addLed(new Led(0, 8, 0x1026, 0x0400, 10));    // TAKE_OFF_TRIM
84
 
183 pfowler 85
            oled = new Oled(Oled.EXTERNALVCC, 0x47, mcp);
86
            oled.init();
181 pfowler 87
 
183 pfowler 88
            // Register the flap gauge position address
89
            UInt16 din; // Addr 10A0, Mask FFFF, Max Val FFFF, Shift 0
90
            if (!Globals.BiosOutput.TryGetValue(0x10A0, out din)) {
91
                Globals.BiosOutput.Add(0x10A0, 0x0000);
92
            }
93
 
181 pfowler 94
            data.axis[0].mapsize = 0xFFFF;
95
            data.axis[0].thres = 3;
96
 
97
            this.Refresh();
98
            data.prev = data.buttons;
99
            data.axis[0].prev = data.axis[0].value;
100
            data.changed = false;
101
 
102
            return 1;
103
        }
104
        public override int Refresh() {
105
            byte[] bytes;
106
            int rslt = 0;
107
 
108
            // Join all our buttons into a single inputs
109
 
110
            rslt = chips[1].GetIO(out bytes);
111
            data.buttons = (UInt64)bytes[0] << 24;
112
            data.buttons |= (UInt64)bytes[1] << 16;
113
 
114
            rslt = chips[0].GetIO(out bytes);
115
            data.buttons |= (UInt64)bytes[0] << 8;
116
            data.buttons |= (UInt64)bytes[1];
117
 
118
            data.axis[0].value = mcp.ReadADC(0); // Y Axis
119
 
120
            //ushort tmpval = 0;
121
            //bool changed = data.axis[0].getPosition(ref tmpval);
122
            //tmpval = 0;
123
            if (data.buttons != data.prev || data.axis[0].value != data.axis[0].prev)
124
                data.changed = true;
125
            else
126
                data.changed = false;
127
 
128
            UInt16 tmpShadow = this.ledShadowA;
129
            foreach (Led led in this.leds) {
130
                led.Tick(ref ledShadowA);
131
            }
132
 
133
            // Only update if leds have changed
134
            if (tmpShadow != this.ledShadowA) {
135
                byte gpa = (byte)ledShadowA;
136
                byte gpb = (byte)(ledShadowA >> 8);
137
 
138
                chips[2].SetBank(0, (byte)gpa);
139
                chips[2].SetBank(1, (byte)gpb);
140
            }
141
 
142
 
143
            return rslt;
144
        }
145
 
146
        public override int Input() {
147
            //Console.WriteLine(data.buttons.ToString("X") + " " + data.axis[0].value.ToString());
148
            foreach (Control control in this.controls) {
149
                control.data = this.data;
150
                control.Tick();
151
            }
152
 
153
            data.axis[0].prev = data.axis[0].value;
154
            data.prev = data.buttons;
155
            data.changed = false;
156
            return 1;
157
        }
158
    }
159
}
160