Subversion Repositories group.electronics

Rev

Rev 164 | Rev 167 | 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 {
166 pfowler 9
        public UInt64 value { get; set; }
162 pfowler 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() {
166 pfowler 33
            UInt64 chg = (UInt64)(data.prev >> pin) & 0x01;
34
            UInt64 norm = (UInt64)(data.curr >> pin) & 0x01;
161 pfowler 35
            this.value = 0;
166 pfowler 36
 
37
            if ((UInt64)(norm) == 1) {
38
                value = (UInt64)(invert ? 0 : 1);
161 pfowler 39
            } else {
166 pfowler 40
                value = (UInt64)(invert ? 1 : 0);
161 pfowler 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;
164 pfowler 46
                    if (cmd.Send(this.value) == 0)
47
                        Console.Write(cmd.ToString());
161 pfowler 48
                }
49
            }
50
        }
51
    }
52
 
53
    public class Switch3Pos : Control {
54
        public int pin0 { get; set; }
55
        public int pin1 { get; set; }
162 pfowler 56
 
161 pfowler 57
        public Boolean invert { get; set; }
58
 
59
        public Switch3Pos(Command command, int pin0, int pin1, Boolean invert = false) : base(command) {
60
            this.pin0 = pin0;
61
            this.pin1 = pin1;
62
            this.invert = invert;
63
 
64
        }
65
 
66
        public override void Tick() {
166 pfowler 67
            UInt64 chg0 = (UInt64)(data.prev >> pin0) & 0x01;
68
            UInt64 chg1 = (UInt64)(data.prev >> pin1) & 0x01;
69
            UInt64 nrm0 = (UInt64)(data.curr >> pin0) & 0x01;
70
            UInt64 nrm1 = (UInt64)(data.curr >> pin1) & 0x01;
161 pfowler 71
            this.value = 1;
166 pfowler 72
 
73
            if ((UInt64)nrm0 == 1)
74
                this.value = (UInt64)(invert ? 2 : 0);
161 pfowler 75
            else if ((uint)nrm1 == 1)
166 pfowler 76
                this.value = (UInt64)(invert ? 0 : 2);
162 pfowler 77
 
78
            if (this.commands != null &&  ((nrm0 != chg0) || (nrm1 != chg1))) {
161 pfowler 79
                foreach (Command cmd in this.commands) {
162 pfowler 80
                    cmd.data = this.data;
164 pfowler 81
                    if (cmd.Send(this.value) == 0)
82
                        Console.Write(cmd.ToString());
161 pfowler 83
                }
84
            }
85
        }
86
    }
87
 
88
    public class Selector : Control {
89
        int[] pins;
90
 
162 pfowler 91
 
161 pfowler 92
        public Selector(Command command, int[] pins) : base(command) {
93
            this.pins = pins;
94
            this.value = 0;
95
        }
96
 
97
        public override void Tick() {
162 pfowler 98
            // This is a rather complicated way  to check what the previous select
99
            //  was, and was the current selection is. Not really needed, but hoping it 
100
            //  might come in handy for something else down the line.
166 pfowler 101
            UInt64 chg = 0;
102
            UInt64 norm = 0;
103
            UInt64 mask = 0;
162 pfowler 104
            for (int i = 0; i < pins.Length; i++) // Create a mask of how many selection points there are
166 pfowler 105
                mask |= (UInt64)1 << i;
161 pfowler 106
 
107
            for (int i = 0; i < pins.Length; i++) {
166 pfowler 108
                chg |= (UInt64)(data.prev >> (pins[i] - i)); // Narrow down to the previsouly selected bit
109
                norm |= (UInt64)(data.curr >> (pins[i] - i)); // Narrow down to the currently selected bit
161 pfowler 110
 
166 pfowler 111
                if ((UInt64)((data.curr >> pins[i]) & 0x01) == 1) { // Decode the value we need to use
112
                    value = (UInt64)i;
161 pfowler 113
                }
114
            }
162 pfowler 115
 
116
            norm &= mask; // Remove any bits from above
161 pfowler 117
            chg &= mask;
162 pfowler 118
 
119
            if (norm == 0) // Can sometimes be in the inbetween selector switch, ignore
161 pfowler 120
                return;
121
 
162 pfowler 122
            if (norm != chg) { // Send the update if needed
161 pfowler 123
                foreach (Command cmd in this.commands) {
162 pfowler 124
                    cmd.data = this.data;
164 pfowler 125
                    if (cmd.Send(this.value) == 0)
126
                        Console.Write(cmd.ToString());
161 pfowler 127
                }
128
            }
129
        }
162 pfowler 130
 
161 pfowler 131
    }
132
}