Subversion Repositories group.electronics

Rev

Rev 188 | 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
 
186 pfowler 93
            flap_background = new BitImage(new Bitmap(nitdcscore.Properties.Resources.flap_bg));
94
            flap_needle = new BitImage(new Bitmap(nitdcscore.Properties.Resources.flap_needle));
95
 
184 pfowler 96
            oled = new Oled(Oled.SWITCHCAPVCC, 0x3d, mcp);
183 pfowler 97
            oled.init();
189 pfowler 98
            //oled.display();
181 pfowler 99
 
183 pfowler 100
            // Register the flap gauge position address
101
            UInt16 din; // Addr 10A0, Mask FFFF, Max Val FFFF, Shift 0
102
            if (!Globals.BiosOutput.TryGetValue(0x10A0, out din)) {
103
                Globals.BiosOutput.Add(0x10A0, 0x0000);
104
            }
105
 
181 pfowler 106
            data.axis[0].mapsize = 0xFFFF;
189 pfowler 107
            data.axis[0].thres = 8;
181 pfowler 108
 
109
            this.Refresh();
110
            data.prev = data.buttons;
111
            data.axis[0].prev = data.axis[0].value;
112
            data.changed = false;
113
 
114
            return 1;
115
        }
116
        public override int Refresh() {
117
            byte[] bytes;
118
            int rslt = 0;
119
 
120
            // Join all our buttons into a single inputs
121
 
122
            rslt = chips[1].GetIO(out bytes);
123
            data.buttons = (UInt64)bytes[0] << 24;
124
            data.buttons |= (UInt64)bytes[1] << 16;
125
 
126
            rslt = chips[0].GetIO(out bytes);
127
            data.buttons |= (UInt64)bytes[0] << 8;
128
            data.buttons |= (UInt64)bytes[1];
129
 
130
            data.axis[0].value = mcp.ReadADC(0); // Y Axis
131
 
189 pfowler 132
            if (data.buttons != data.prev || data.axis[0].value != data.axis[0].prev) {
133
                foreach (Control control in this.controls) {
134
                    control.data = this.data;
135
                    control.Tick();
136
                }
137
                data.prev = data.buttons;
138
            }
139
 
140
 
141
            // Update the leds if they have changed
181 pfowler 142
            UInt16 tmpShadow = this.ledShadowA;
143
            foreach (Led led in this.leds) {
144
                led.Tick(ref ledShadowA);
145
            }
146
 
147
            if (tmpShadow != this.ledShadowA) {
148
                byte gpa = (byte)ledShadowA;
149
                byte gpb = (byte)(ledShadowA >> 8);
150
 
151
                chips[2].SetBank(0, (byte)gpa);
152
                chips[2].SetBank(1, (byte)gpb);
153
            }
154
 
189 pfowler 155
            // Check if the flaps need to be updated
156
            Globals.BiosOutput.TryGetValue(0x10A0, out this.flap_value);
186 pfowler 157
            if (this.flap_value != this.flap_shadow) {
158
 
159
                BitImage flap_rotate = new BitImage(flap_needle);
160
                BitImage flap_display = new BitImage(flap_background);
161
 
162
                UInt16 flap_set = Globals.map_uint16(flap_value, 0, 0xffff, 0, 90);
163
                flap_rotate.rotate(flap_set);
164
                flap_display.blit(flap_rotate, 60, 4);
165
                oled.buffer = flap_display.toByteArray();
188 pfowler 166
                oled.i2c.SetGpio(1, 1);
186 pfowler 167
                oled.display();
188 pfowler 168
                oled.i2c.SetGpio(1, 0);
186 pfowler 169
 
170
                this.flap_shadow = this.flap_value;
189 pfowler 171
 
172
                Console.WriteLine("Oled Flap Position: {0}", flap_value);
186 pfowler 173
            }
174
 
189 pfowler 175
            return rslt;
176
        }
177
 
178
        public override int Input() {
179
            data.changed = false;
181 pfowler 180
            return 1;
181
        }
182
    }
183
}
184