Subversion Repositories group.electronics

Rev

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

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