Subversion Repositories group.electronics

Rev

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