Subversion Repositories group.electronics

Rev

Rev 168 | Rev 172 | 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 {
171 pfowler 8
 
9
    public class jsaxis {
10
        public UInt16 prev { get; set; }        // The previous ADC value
11
        public UInt16 value { get; set; }       // The current ADC value
12
        public UInt16 thres { get; set; }       // Threshold to report a new position
13
        public UInt16 max { get; set; }              // The maximum value we've seen
14
        public UInt16 mapsize { get; set; }     // The mapping maximum
15
 
16
        public jsaxis() {
17
 
18
        }
19
 
20
        public bool getPosition(ref UInt16 position) {
21
            if (this.value > this.max)
22
                this.max = this.value;
23
 
24
            UInt16 lowerval = 0;
25
            if (this.prev >= this.thres)
26
                lowerval = (UInt16)(this.prev - this.thres);
27
 
28
            ushort upperval = this.max;
29
            if (this.prev <= upperval - this.thres)
30
                upperval = (ushort)(this.prev + this.thres);
31
 
32
            //Console.WriteLine("JS " + ":" + this.prev + "," + this.value + "," + this.max + "," + lowerval + "," + upperval);
33
 
34
            if ((this.value < lowerval) || (this.value > upperval)) {
35
                // Cover our min/max ranges within threshold
36
                if (this.value < this.thres)
37
                    this.value = 0;
38
                if (this.value > this.max - this.thres)
39
                    this.value = this.max;
40
 
41
 
42
                this.prev = this.value;
43
 
44
                position = Globals.map_uint16(this.value, 0, this.max, 0, this.mapsize);
45
 
46
                return true;
47
            }
48
 
49
            return false;
50
        }
51
    }
52
 
53
    public class jsdata {
54
        public bool changed { get; set; }
55
        public UInt64 buttons { get; set; }
56
        public UInt64 prev { get; set; }
57
 
58
        public jsaxis[] axis = new jsaxis[6];
59
 
60
        public jsdata() {
61
            changed = false;
62
            prev = buttons = 0;
63
 
64
            for (int i = 0; i < 6; i++) {
65
                axis[i] = new jsaxis();
66
                axis[i].thres = Globals.ADC_THRESHOLD;
67
                axis[i].max = Globals.ADC_DEFMAX;
68
                axis[i].prev = axis[i].value = 0;
69
                axis[i].mapsize = 0xffff;
70
            }
71
        }
72
    }
73
 
74
    public class Led {
75
        protected mcp23017 dev;
76
        private int bank { get; set; }
77
        private int pin { get; set; }
78
        private UInt16 address { get; set; }
79
        private UInt16 mask { get; set; }
80
        private int shift { get; set; }
81
 
82
        public uint value { get; set; }
83
 
84
        public Led(mcp23017 dev, int bank, int pin, UInt16 address, UInt16 mask, int shift) {
85
            this.dev = dev;
86
            this.pin = pin;
87
            this.address = address;
88
            this.mask = mask;
89
            this.shift = shift;
90
            this.value = 0;
91
 
92
            Globals.outputs.Add(this.address, this);
93
        }
94
 
95
        public bool Tick(uint data) {
96
            uint newval = (data & mask) >> shift;
97
 
98
            if (newval != this.value) {
99
                byte curr = 0;
100
                dev.GetBank(this.bank, (byte)this.bank, out curr);
101
 
102
                Globals.setBit(curr, pin);
103
                dev.SetBank(this.bank, curr);
104
            }
105
 
106
            return true;
107
        }
108
 
109
 
110
    }
111
 
112
 
161 pfowler 113
    public abstract class Control {
166 pfowler 114
        public UInt64 value { get; set; }
162 pfowler 115
 
161 pfowler 116
        public Control(Command command) {
117
            commands.Add(command);
118
        }
119
 
120
        public List<Command> commands = new List<Command>();
167 pfowler 121
        public jsdata data { get; set; }
161 pfowler 122
        public Boolean updated { get; set; }
123
 
124
        public abstract void Tick();
125
    }
126
 
127
    public class Switch2Pos : Control {
128
        public int pin { get; set; }
162 pfowler 129
 
161 pfowler 130
        public Boolean invert { get; set; }
131
 
132
        public Switch2Pos(Command command, int pin, Boolean invert = false) : base(command) {
133
            this.invert = invert;
134
            this.pin = pin;
135
        }
136
 
137
        public override void Tick() {
166 pfowler 138
            UInt64 chg = (UInt64)(data.prev >> pin) & 0x01;
167 pfowler 139
            UInt64 norm = (UInt64)(data.buttons >> pin) & 0x01;
161 pfowler 140
            this.value = 0;
166 pfowler 141
 
142
            if ((UInt64)(norm) == 1) {
143
                value = (UInt64)(invert ? 0 : 1);
161 pfowler 144
            } else {
166 pfowler 145
                value = (UInt64)(invert ? 1 : 0);
161 pfowler 146
            }
147
 
162 pfowler 148
            if (this.commands != null && norm != chg) {
161 pfowler 149
                foreach (Command cmd in this.commands) {
162 pfowler 150
                    cmd.data = this.data;
164 pfowler 151
                    if (cmd.Send(this.value) == 0)
152
                        Console.Write(cmd.ToString());
161 pfowler 153
                }
154
            }
155
        }
156
    }
157
 
158
    public class Switch3Pos : Control {
159
        public int pin0 { get; set; }
160
        public int pin1 { get; set; }
162 pfowler 161
 
161 pfowler 162
        public Boolean invert { get; set; }
163
 
164
        public Switch3Pos(Command command, int pin0, int pin1, Boolean invert = false) : base(command) {
165
            this.pin0 = pin0;
166
            this.pin1 = pin1;
167
            this.invert = invert;
168
 
169
        }
170
 
171
        public override void Tick() {
166 pfowler 172
            UInt64 chg0 = (UInt64)(data.prev >> pin0) & 0x01;
173
            UInt64 chg1 = (UInt64)(data.prev >> pin1) & 0x01;
167 pfowler 174
            UInt64 nrm0 = (UInt64)(data.buttons >> pin0) & 0x01;
175
            UInt64 nrm1 = (UInt64)(data.buttons >> pin1) & 0x01;
161 pfowler 176
            this.value = 1;
166 pfowler 177
 
178
            if ((UInt64)nrm0 == 1)
179
                this.value = (UInt64)(invert ? 2 : 0);
161 pfowler 180
            else if ((uint)nrm1 == 1)
166 pfowler 181
                this.value = (UInt64)(invert ? 0 : 2);
162 pfowler 182
 
183
            if (this.commands != null &&  ((nrm0 != chg0) || (nrm1 != chg1))) {
161 pfowler 184
                foreach (Command cmd in this.commands) {
162 pfowler 185
                    cmd.data = this.data;
164 pfowler 186
                    if (cmd.Send(this.value) == 0)
187
                        Console.Write(cmd.ToString());
161 pfowler 188
                }
189
            }
190
        }
191
    }
192
 
167 pfowler 193
    public class Potentiometer : Control {
194
        public int channel { get; set; }
195
 
196
        public Boolean invert { get; set; }
197
 
198
        public Potentiometer(Command command, int channel) : base(command) {
199
            this.channel = channel;
200
        }
201
 
202
        public override void Tick() {
203
            UInt16 tmpval = 0;
168 pfowler 204
            bool changed = data.axis[channel].getPosition(ref tmpval);
167 pfowler 205
            this.value = tmpval;
206
 
207
 
168 pfowler 208
            if (changed) {
167 pfowler 209
                foreach (Command cmd in this.commands) {
210
                    cmd.data = this.data;
211
                    if (cmd.Send(this.value) == 0)
212
                        Console.Write(cmd.ToString());
213
                }
214
            }
215
        }
216
    }
217
 
161 pfowler 218
    public class Selector : Control {
219
        int[] pins;
220
 
162 pfowler 221
 
161 pfowler 222
        public Selector(Command command, int[] pins) : base(command) {
223
            this.pins = pins;
224
            this.value = 0;
225
        }
226
 
227
        public override void Tick() {
162 pfowler 228
            // This is a rather complicated way  to check what the previous select
229
            //  was, and was the current selection is. Not really needed, but hoping it 
230
            //  might come in handy for something else down the line.
166 pfowler 231
            UInt64 chg = 0;
232
            UInt64 norm = 0;
233
            UInt64 mask = 0;
162 pfowler 234
            for (int i = 0; i < pins.Length; i++) // Create a mask of how many selection points there are
166 pfowler 235
                mask |= (UInt64)1 << i;
161 pfowler 236
 
237
            for (int i = 0; i < pins.Length; i++) {
166 pfowler 238
                chg |= (UInt64)(data.prev >> (pins[i] - i)); // Narrow down to the previsouly selected bit
167 pfowler 239
                norm |= (UInt64)(data.buttons >> (pins[i] - i)); // Narrow down to the currently selected bit
161 pfowler 240
 
167 pfowler 241
                if ((UInt64)((data.buttons >> pins[i]) & 0x01) == 1) { // Decode the value we need to use
166 pfowler 242
                    value = (UInt64)i;
161 pfowler 243
                }
244
            }
162 pfowler 245
 
246
            norm &= mask; // Remove any bits from above
161 pfowler 247
            chg &= mask;
162 pfowler 248
 
249
            if (norm == 0) // Can sometimes be in the inbetween selector switch, ignore
161 pfowler 250
                return;
251
 
162 pfowler 252
            if (norm != chg) { // Send the update if needed
161 pfowler 253
                foreach (Command cmd in this.commands) {
162 pfowler 254
                    cmd.data = this.data;
164 pfowler 255
                    if (cmd.Send(this.value) == 0)
256
                        Console.Write(cmd.ToString());
161 pfowler 257
                }
258
            }
259
        }
162 pfowler 260
 
161 pfowler 261
    }
262
}