Subversion Repositories group.electronics

Rev

Rev 167 | Rev 171 | 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 jsdata data { get; set; }
161 pfowler 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;
167 pfowler 34
            UInt64 norm = (UInt64)(data.buttons >> 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;
167 pfowler 69
            UInt64 nrm0 = (UInt64)(data.buttons >> pin0) & 0x01;
70
            UInt64 nrm1 = (UInt64)(data.buttons >> 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
 
167 pfowler 88
    public class Potentiometer : Control {
89
        public int channel { get; set; }
90
 
91
        public Boolean invert { get; set; }
92
 
93
        public Potentiometer(Command command, int channel) : base(command) {
94
            this.channel = channel;
95
        }
96
 
97
        public override void Tick() {
98
            UInt16 tmpval = 0;
168 pfowler 99
            bool changed = data.axis[channel].getPosition(ref tmpval);
167 pfowler 100
            this.value = tmpval;
101
 
102
 
168 pfowler 103
            if (changed) {
167 pfowler 104
                foreach (Command cmd in this.commands) {
105
                    cmd.data = this.data;
106
                    if (cmd.Send(this.value) == 0)
107
                        Console.Write(cmd.ToString());
108
                }
109
            }
110
        }
111
    }
112
 
161 pfowler 113
    public class Selector : Control {
114
        int[] pins;
115
 
162 pfowler 116
 
161 pfowler 117
        public Selector(Command command, int[] pins) : base(command) {
118
            this.pins = pins;
119
            this.value = 0;
120
        }
121
 
122
        public override void Tick() {
162 pfowler 123
            // This is a rather complicated way  to check what the previous select
124
            //  was, and was the current selection is. Not really needed, but hoping it 
125
            //  might come in handy for something else down the line.
166 pfowler 126
            UInt64 chg = 0;
127
            UInt64 norm = 0;
128
            UInt64 mask = 0;
162 pfowler 129
            for (int i = 0; i < pins.Length; i++) // Create a mask of how many selection points there are
166 pfowler 130
                mask |= (UInt64)1 << i;
161 pfowler 131
 
132
            for (int i = 0; i < pins.Length; i++) {
166 pfowler 133
                chg |= (UInt64)(data.prev >> (pins[i] - i)); // Narrow down to the previsouly selected bit
167 pfowler 134
                norm |= (UInt64)(data.buttons >> (pins[i] - i)); // Narrow down to the currently selected bit
161 pfowler 135
 
167 pfowler 136
                if ((UInt64)((data.buttons >> pins[i]) & 0x01) == 1) { // Decode the value we need to use
166 pfowler 137
                    value = (UInt64)i;
161 pfowler 138
                }
139
            }
162 pfowler 140
 
141
            norm &= mask; // Remove any bits from above
161 pfowler 142
            chg &= mask;
162 pfowler 143
 
144
            if (norm == 0) // Can sometimes be in the inbetween selector switch, ignore
161 pfowler 145
                return;
146
 
162 pfowler 147
            if (norm != chg) { // Send the update if needed
161 pfowler 148
                foreach (Command cmd in this.commands) {
162 pfowler 149
                    cmd.data = this.data;
164 pfowler 150
                    if (cmd.Send(this.value) == 0)
151
                        Console.Write(cmd.ToString());
161 pfowler 152
                }
153
            }
154
        }
162 pfowler 155
 
161 pfowler 156
    }
157
}