Subversion Repositories group.electronics

Rev

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