Subversion Repositories group.electronics

Rev

Rev 166 | Rev 168 | 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>();
167 pfowler 16
        //public Utils.InputPair data { get; set; }       // The data from the panel
17
        public jsdata data { get; set; }
161 pfowler 18
        public Boolean updated { get; set; }
19
 
20
        public abstract void Tick();
21
    }
22
 
23
    public class Switch2Pos : Control {
24
        public int pin { get; set; }
162 pfowler 25
 
161 pfowler 26
        public Boolean invert { get; set; }
27
 
28
        public Switch2Pos(Command command, int pin, Boolean invert = false) : base(command) {
29
            this.invert = invert;
30
            this.pin = pin;
31
        }
32
 
33
        public override void Tick() {
166 pfowler 34
            UInt64 chg = (UInt64)(data.prev >> pin) & 0x01;
167 pfowler 35
            UInt64 norm = (UInt64)(data.buttons >> pin) & 0x01;
161 pfowler 36
            this.value = 0;
166 pfowler 37
 
38
            if ((UInt64)(norm) == 1) {
39
                value = (UInt64)(invert ? 0 : 1);
161 pfowler 40
            } else {
166 pfowler 41
                value = (UInt64)(invert ? 1 : 0);
161 pfowler 42
            }
43
 
162 pfowler 44
            if (this.commands != null && norm != chg) {
161 pfowler 45
                foreach (Command cmd in this.commands) {
162 pfowler 46
                    cmd.data = this.data;
164 pfowler 47
                    if (cmd.Send(this.value) == 0)
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() {
166 pfowler 68
            UInt64 chg0 = (UInt64)(data.prev >> pin0) & 0x01;
69
            UInt64 chg1 = (UInt64)(data.prev >> pin1) & 0x01;
167 pfowler 70
            UInt64 nrm0 = (UInt64)(data.buttons >> pin0) & 0x01;
71
            UInt64 nrm1 = (UInt64)(data.buttons >> pin1) & 0x01;
161 pfowler 72
            this.value = 1;
166 pfowler 73
 
74
            if ((UInt64)nrm0 == 1)
75
                this.value = (UInt64)(invert ? 2 : 0);
161 pfowler 76
            else if ((uint)nrm1 == 1)
166 pfowler 77
                this.value = (UInt64)(invert ? 0 : 2);
162 pfowler 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;
164 pfowler 82
                    if (cmd.Send(this.value) == 0)
83
                        Console.Write(cmd.ToString());
161 pfowler 84
                }
85
            }
86
        }
87
    }
88
 
167 pfowler 89
    public class Potentiometer : Control {
90
        public int channel { get; set; }
91
 
92
        public Boolean invert { get; set; }
93
 
94
        public Potentiometer(Command command, int channel) : base(command) {
95
            this.channel = channel;
96
        }
97
 
98
        public override void Tick() {
99
            //this.value = data.axis[channel].
100
            UInt16 tmpval = 0;
101
            int changed = data.axis[channel].getPosition(ref tmpval);
102
            this.value = tmpval;
103
 
104
 
105
            if (changed == 0) {
106
                foreach (Command cmd in this.commands) {
107
                    cmd.data = this.data;
108
                    if (cmd.Send(this.value) == 0)
109
                        Console.Write(cmd.ToString());
110
                }
111
            }
112
        }
113
    }
114
 
161 pfowler 115
    public class Selector : Control {
116
        int[] pins;
117
 
162 pfowler 118
 
161 pfowler 119
        public Selector(Command command, int[] pins) : base(command) {
120
            this.pins = pins;
121
            this.value = 0;
122
        }
123
 
124
        public override void Tick() {
162 pfowler 125
            // This is a rather complicated way  to check what the previous select
126
            //  was, and was the current selection is. Not really needed, but hoping it 
127
            //  might come in handy for something else down the line.
166 pfowler 128
            UInt64 chg = 0;
129
            UInt64 norm = 0;
130
            UInt64 mask = 0;
162 pfowler 131
            for (int i = 0; i < pins.Length; i++) // Create a mask of how many selection points there are
166 pfowler 132
                mask |= (UInt64)1 << i;
161 pfowler 133
 
134
            for (int i = 0; i < pins.Length; i++) {
166 pfowler 135
                chg |= (UInt64)(data.prev >> (pins[i] - i)); // Narrow down to the previsouly selected bit
167 pfowler 136
                norm |= (UInt64)(data.buttons >> (pins[i] - i)); // Narrow down to the currently selected bit
161 pfowler 137
 
167 pfowler 138
                if ((UInt64)((data.buttons >> pins[i]) & 0x01) == 1) { // Decode the value we need to use
166 pfowler 139
                    value = (UInt64)i;
161 pfowler 140
                }
141
            }
162 pfowler 142
 
143
            norm &= mask; // Remove any bits from above
161 pfowler 144
            chg &= mask;
162 pfowler 145
 
146
            if (norm == 0) // Can sometimes be in the inbetween selector switch, ignore
161 pfowler 147
                return;
148
 
162 pfowler 149
            if (norm != chg) { // Send the update if needed
161 pfowler 150
                foreach (Command cmd in this.commands) {
162 pfowler 151
                    cmd.data = this.data;
164 pfowler 152
                    if (cmd.Send(this.value) == 0)
153
                        Console.Write(cmd.ToString());
161 pfowler 154
                }
155
            }
156
        }
162 pfowler 157
 
161 pfowler 158
    }
159
}