Subversion Repositories group.electronics

Rev

Rev 161 | Rev 164 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 161 Rev 162
Line 4... Line 4...
4
using System.Text;
4
using System.Text;
5
using System.Threading.Tasks;
5
using System.Threading.Tasks;
6
 
6
 
7
namespace nitdcscore {
7
namespace nitdcscore {
8
    public abstract class Control {
8
    public abstract class Control {
-
 
9
        public uint value { get; set; }
-
 
10
 
9
        public Control(Command command) {
11
        public Control(Command command) {
10
            commands.Add(command);
12
            commands.Add(command);
11
        }
13
        }
12
 
14
 
13
        public List<Command> commands = new List<Command>();
15
        public List<Command> commands = new List<Command>();
Line 17... Line 19...
17
        public abstract void Tick();
19
        public abstract void Tick();
18
    }
20
    }
19
 
21
 
20
    public class Switch2Pos : Control {
22
    public class Switch2Pos : Control {
21
        public int pin { get; set; }
23
        public int pin { get; set; }
22
        public uint value { get; set; }
24
        
23
        public Boolean invert { get; set; }
25
        public Boolean invert { get; set; }
24
 
26
 
25
        public Switch2Pos(Command command, int pin, Boolean invert = false) : base(command) {
27
        public Switch2Pos(Command command, int pin, Boolean invert = false) : base(command) {
26
            this.invert = invert;
28
            this.invert = invert;
27
            this.pin = pin;
29
            this.pin = pin;
Line 36... Line 38...
36
                value = (uint)(invert ? 0 : 1);
38
                value = (uint)(invert ? 0 : 1);
37
            } else {
39
            } else {
38
                value = (uint)(invert ? 1 : 0);
40
                value = (uint)(invert ? 1 : 0);
39
            }
41
            }
40
 
42
 
41
            if (norm != chg) {
43
            if (this.commands != null && norm != chg) {
42
                foreach (Command cmd in this.commands) {
44
                foreach (Command cmd in this.commands) {
-
 
45
                    cmd.data = this.data;
43
                    cmd.Send(this.value);
46
                    cmd.Send(this.value);
44
                    Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
47
                    //Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
-
 
48
                    Console.Write(cmd.ToString());
45
                }
49
                }
46
            }
50
            }
47
        }
51
        }
48
    }
52
    }
49
 
53
 
50
    public class Switch3Pos : Control {
54
    public class Switch3Pos : Control {
51
        public int pin0 { get; set; }
55
        public int pin0 { get; set; }
52
        public int pin1 { get; set; }
56
        public int pin1 { get; set; }
53
        public uint value { get; set; }
-
 
-
 
57
 
54
        public Boolean invert { get; set; }
58
        public Boolean invert { get; set; }
55
 
59
 
56
        public Switch3Pos(Command command, int pin0, int pin1, Boolean invert = false) : base(command) {
60
        public Switch3Pos(Command command, int pin0, int pin1, Boolean invert = false) : base(command) {
57
            this.pin0 = pin0;
61
            this.pin0 = pin0;
58
            this.pin1 = pin1;
62
            this.pin1 = pin1;
Line 66... Line 70...
66
            uint nrm0 = (uint)(data.curr >> pin0) & 0x01;
70
            uint nrm0 = (uint)(data.curr >> pin0) & 0x01;
67
            uint nrm1 = (uint)(data.curr >> pin1) & 0x01;
71
            uint nrm1 = (uint)(data.curr >> pin1) & 0x01;
68
            this.value = 1;
72
            this.value = 1;
69
            
73
            
70
            if ((uint)nrm0 == 1)
74
            if ((uint)nrm0 == 1)
71
                value = (uint)(invert ? 2 : 0);
75
                this.value = (uint)(invert ? 2 : 0);
72
            else if ((uint)nrm1 == 1)
76
            else if ((uint)nrm1 == 1)
73
                value = (uint)(invert ? 0 : 2);
77
                this.value = (uint)(invert ? 0 : 2);
-
 
78
 
74
            if ((nrm0 != chg0) || (nrm1 != chg1)) {
79
            if (this.commands != null &&  ((nrm0 != chg0) || (nrm1 != chg1))) {
75
                foreach (Command cmd in this.commands) {
80
                foreach (Command cmd in this.commands) {
-
 
81
                    cmd.data = this.data;
76
                    cmd.Send(this.value);
82
                    cmd.Send(this.value);
77
                    Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
83
                    Console.Write(cmd.ToString());
78
                }
84
                }
79
            }
85
            }
80
        }
86
        }
81
    }
87
    }
82
 
88
 
83
    public class Selector : Control {
89
    public class Selector : Control {
84
        int[] pins;
90
        int[] pins;
85
        public uint value;
-
 
-
 
91
 
86
 
92
 
87
        public Selector(Command command, int[] pins) : base(command) {
93
        public Selector(Command command, int[] pins) : base(command) {
88
            this.pins = pins;
94
            this.pins = pins;
89
            this.value = 0;
95
            this.value = 0;
90
        }
96
        }
91
 
97
 
92
        public override void Tick() {
98
        public override void Tick() {
-
 
99
            // This is a rather complicated way  to check what the previous select
-
 
100
            //  was, and was the current selection is. Not really needed, but hoping it 
-
 
101
            //  might come in handy for something else down the line.
93
            uint chg = 0;
102
            uint chg = 0;
94
            uint norm = 0;
103
            uint norm = 0;
95
            uint mask = 0;
104
            uint mask = 0;
96
            for (int i = 0; i < pins.Length; i++)
105
            for (int i = 0; i < pins.Length; i++) // Create a mask of how many selection points there are
97
                mask |= (uint)1 << i;
106
                mask |= (uint)1 << i;
98
 
107
 
99
            for (int i = 0; i < pins.Length; i++) {
108
            for (int i = 0; i < pins.Length; i++) {
100
                chg |= (uint)(data.prev >> (pins[i] ));
109
                chg |= (uint)(data.prev >> (pins[i] -i )); // Narrow down to the previsouly selected bit
101
                norm |= (uint)(data.curr >> (pins[i] ));
110
                norm |= (uint)(data.curr >> (pins[i] -i )); // Narrow down to the currently selected bit
102
 
111
 
103
                if ((uint)((data.curr >> pins[i]) & 0x01) == 1) {
112
                if ((uint)((data.curr >> pins[i]) & 0x01) == 1) { // Decode the value we need to use
104
                    value = (uint)i;
113
                    value = (uint)i;
105
                }
114
                }
106
            }
115
            }
107
            if (pins.Length==3)
116
                           
108
                Console.WriteLine(data.curr.ToString("X") + ":" + mask + ":" + chg + ":" + norm + ":" + value);
-
 
109
            norm &= mask;
117
            norm &= mask; // Remove any bits from above
110
            chg &= mask;
118
            chg &= mask;
111
            //if (pins.Length == 3)
-
 
112
            //    Console.WriteLine(data.curr.ToString("X") + ":" + mask + ":" + chg + ":" + norm + ":" + value);
-
 
-
 
119
 
113
            // This happens when between positions of the selector
120
            if (norm == 0) // Can sometimes be in the inbetween selector switch, ignore
114
            if (norm == 0)
-
 
115
                return;
121
                return;
116
 
122
 
117
            if (norm != chg) {
123
            if (norm != chg) { // Send the update if needed
118
                foreach (Command cmd in this.commands) {
124
                foreach (Command cmd in this.commands) {
-
 
125
                    cmd.data = this.data;
119
                    cmd.Send(this.value);
126
                    cmd.Send(this.value);
120
                    Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
127
                    Console.Write(cmd.ToString());
121
                }
128
                }
122
            }
129
            }
123
        }
130
        }
-
 
131
 
124
    }
132
    }
125
}
133
}