Subversion Repositories group.electronics

Rev

Rev 162 | Go to most recent revision | Details | 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 {
9
        public Control(Command command) {
10
            commands.Add(command);
11
        }
12
 
13
        public List<Command> commands = new List<Command>();
14
        public Utils.InputPair data { get; set; }       // The data from the panel
15
        public Boolean updated { get; set; }
16
 
17
        public abstract void Tick();
18
    }
19
 
20
    public class Switch2Pos : Control {
21
        public int pin { get; set; }
22
        public uint value { get; set; }
23
        public Boolean invert { get; set; }
24
 
25
        public Switch2Pos(Command command, int pin, Boolean invert = false) : base(command) {
26
            this.invert = invert;
27
            this.pin = pin;
28
        }
29
 
30
        public override void Tick() {
31
            uint chg = (uint)(data.prev >> pin) & 0x01;
32
            uint norm = (uint)(data.curr >> pin) & 0x01;
33
            this.value = 0;
34
 
35
            if ((uint)(norm) == 1) {
36
                value = (uint)(invert ? 0 : 1);
37
            } else {
38
                value = (uint)(invert ? 1 : 0);
39
            }
40
 
41
            if (norm != chg) {
42
                foreach (Command cmd in this.commands) {
43
                    cmd.Send(this.value);
44
                    Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
45
                }
46
            }
47
        }
48
    }
49
 
50
    public class Switch3Pos : Control {
51
        public int pin0 { get; set; }
52
        public int pin1 { get; set; }
53
        public uint value { get; set; }
54
        public Boolean invert { get; set; }
55
 
56
        public Switch3Pos(Command command, int pin0, int pin1, Boolean invert = false) : base(command) {
57
            this.pin0 = pin0;
58
            this.pin1 = pin1;
59
            this.invert = invert;
60
 
61
        }
62
 
63
        public override void Tick() {
64
            uint chg0 = (uint)(data.prev >> pin0) & 0x01;
65
            uint chg1 = (uint)(data.prev >> pin1) & 0x01;
66
            uint nrm0 = (uint)(data.curr >> pin0) & 0x01;
67
            uint nrm1 = (uint)(data.curr >> pin1) & 0x01;
68
            this.value = 1;
69
 
70
            if ((uint)nrm0 == 1)
71
                value = (uint)(invert ? 2 : 0);
72
            else if ((uint)nrm1 == 1)
73
                value = (uint)(invert ? 0 : 2);
74
            if ((nrm0 != chg0) || (nrm1 != chg1)) {
75
                foreach (Command cmd in this.commands) {
76
                    cmd.Send(this.value);
77
                    Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
78
                }
79
            }
80
        }
81
    }
82
 
83
    public class Selector : Control {
84
        int[] pins;
85
        public uint value;
86
 
87
        public Selector(Command command, int[] pins) : base(command) {
88
            this.pins = pins;
89
            this.value = 0;
90
        }
91
 
92
        public override void Tick() {
93
            uint chg = 0;
94
            uint norm = 0;
95
            uint mask = 0;
96
            for (int i = 0; i < pins.Length; i++)
97
                mask |= (uint)1 << i;
98
 
99
            for (int i = 0; i < pins.Length; i++) {
100
                chg |= (uint)(data.prev >> (pins[i] ));
101
                norm |= (uint)(data.curr >> (pins[i] ));
102
 
103
                if ((uint)((data.curr >> pins[i]) & 0x01) == 1) {
104
                    value = (uint)i;
105
                }
106
            }
107
            if (pins.Length==3)
108
                Console.WriteLine(data.curr.ToString("X") + ":" + mask + ":" + chg + ":" + norm + ":" + value);
109
            norm &= mask;
110
            chg &= mask;
111
            //if (pins.Length == 3)
112
            //    Console.WriteLine(data.curr.ToString("X") + ":" + mask + ":" + chg + ":" + norm + ":" + value);
113
            // This happens when between positions of the selector
114
            if (norm == 0)
115
                return;
116
 
117
            if (norm != chg) {
118
                foreach (Command cmd in this.commands) {
119
                    cmd.Send(this.value);
120
                    Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
121
                }
122
            }
123
        }
124
    }
125
}