Subversion Repositories group.electronics

Rev

Rev 181 | Rev 189 | 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 {
172 pfowler 75
        public int bank { get; set; }
76
        public int pin { get; set; }
77
        public UInt16 address { get; set; }
78
        public UInt16 mask { get; set; }
79
        public int shift { get; set; }
171 pfowler 80
        public uint value { get; set; }
175 pfowler 81
        private UInt16 prevdata;
171 pfowler 82
 
172 pfowler 83
        public Led(int bank, int pin, UInt16 address, UInt16 mask, int shift) {
171 pfowler 84
            this.pin = pin;
85
            this.address = address;
86
            this.mask = mask;
87
            this.shift = shift;
88
            this.value = 0;
89
 
172 pfowler 90
            UInt16 data;
91
            if (!Globals.BiosOutput.TryGetValue(this.address, out data)) {
92
                Globals.BiosOutput.Add(this.address, 0x0000);
93
            }
94
 
171 pfowler 95
        }
96
 
172 pfowler 97
        public bool Tick(ref byte shadow) {
173 pfowler 98
 
172 pfowler 99
            UInt16 data;
100
            byte tmpshadow = shadow;
101
            if (Globals.BiosOutput.TryGetValue(this.address, out data)) {
175 pfowler 102
                // Only continue if the data parm has changed
103
                if (data == this.prevdata)
104
                    return false;
105
                this.prevdata = data;
173 pfowler 106
 
172 pfowler 107
                this.value = (UInt16)((data & mask) >> shift);
171 pfowler 108
 
172 pfowler 109
                if (this.value == 1)
173 pfowler 110
                    Globals.setBit(ref shadow, this.pin);
172 pfowler 111
                else
173 pfowler 112
                    Globals.clearBit(ref shadow, this.pin);
171 pfowler 113
            }
114
 
175 pfowler 115
            // Only update the led if the led value has changed
172 pfowler 116
            if (tmpshadow == shadow)
117
                return false;
118
 
175 pfowler 119
            // Return true to signal the panel to update the led
171 pfowler 120
            return true;
121
        }
122
 
181 pfowler 123
        public bool Tick(ref UInt16 shadow) {
124
 
125
            UInt16 data;
126
            UInt16 tmpshadow = shadow;
127
            if (Globals.BiosOutput.TryGetValue(this.address, out data)) {
128
                // Only continue if the data parm has changed
129
                if (data == this.prevdata)
130
                    return false;
131
                this.prevdata = data;
132
 
133
                this.value = (UInt16)((data & mask) >> shift);
134
 
135
                if (this.value == 1) {
136
                    Globals.setBit(ref shadow, this.pin);
137
                } else {
138
                    Globals.clearBit(ref shadow, this.pin);
139
                }
140
            }
141
 
142
            // Only update the led if the led value has changed
143
            if (tmpshadow == shadow)
144
                return false;
145
 
146
            // Return true to signal the panel to update the led
147
            return true;
148
        }
149
 
171 pfowler 150
    }
151
 
152
 
161 pfowler 153
    public abstract class Control {
166 pfowler 154
        public UInt64 value { get; set; }
162 pfowler 155
 
161 pfowler 156
        public Control(Command command) {
157
            commands.Add(command);
158
        }
159
 
160
        public List<Command> commands = new List<Command>();
167 pfowler 161
        public jsdata data { get; set; }
161 pfowler 162
        public Boolean updated { get; set; }
163
 
164
        public abstract void Tick();
165
    }
166
 
167
    public class Switch2Pos : Control {
168
        public int pin { get; set; }
162 pfowler 169
 
161 pfowler 170
        public Boolean invert { get; set; }
171
 
172
        public Switch2Pos(Command command, int pin, Boolean invert = false) : base(command) {
173
            this.invert = invert;
174
            this.pin = pin;
175
        }
176
 
177
        public override void Tick() {
166 pfowler 178
            UInt64 chg = (UInt64)(data.prev >> pin) & 0x01;
167 pfowler 179
            UInt64 norm = (UInt64)(data.buttons >> pin) & 0x01;
161 pfowler 180
            this.value = 0;
166 pfowler 181
 
182
            if ((UInt64)(norm) == 1) {
183
                value = (UInt64)(invert ? 0 : 1);
161 pfowler 184
            } else {
166 pfowler 185
                value = (UInt64)(invert ? 1 : 0);
161 pfowler 186
            }
187
 
162 pfowler 188
            if (this.commands != null && norm != chg) {
161 pfowler 189
                foreach (Command cmd in this.commands) {
162 pfowler 190
                    cmd.data = this.data;
164 pfowler 191
                    if (cmd.Send(this.value) == 0)
192
                        Console.Write(cmd.ToString());
161 pfowler 193
                }
194
            }
195
        }
196
    }
197
 
198
    public class Switch3Pos : Control {
199
        public int pin0 { get; set; }
200
        public int pin1 { get; set; }
162 pfowler 201
 
161 pfowler 202
        public Boolean invert { get; set; }
203
 
204
        public Switch3Pos(Command command, int pin0, int pin1, Boolean invert = false) : base(command) {
205
            this.pin0 = pin0;
206
            this.pin1 = pin1;
207
            this.invert = invert;
208
 
209
        }
210
 
211
        public override void Tick() {
166 pfowler 212
            UInt64 chg0 = (UInt64)(data.prev >> pin0) & 0x01;
213
            UInt64 chg1 = (UInt64)(data.prev >> pin1) & 0x01;
167 pfowler 214
            UInt64 nrm0 = (UInt64)(data.buttons >> pin0) & 0x01;
215
            UInt64 nrm1 = (UInt64)(data.buttons >> pin1) & 0x01;
161 pfowler 216
            this.value = 1;
166 pfowler 217
 
218
            if ((UInt64)nrm0 == 1)
219
                this.value = (UInt64)(invert ? 2 : 0);
161 pfowler 220
            else if ((uint)nrm1 == 1)
166 pfowler 221
                this.value = (UInt64)(invert ? 0 : 2);
162 pfowler 222
 
223
            if (this.commands != null &&  ((nrm0 != chg0) || (nrm1 != chg1))) {
161 pfowler 224
                foreach (Command cmd in this.commands) {
162 pfowler 225
                    cmd.data = this.data;
164 pfowler 226
                    if (cmd.Send(this.value) == 0)
227
                        Console.Write(cmd.ToString());
161 pfowler 228
                }
229
            }
230
        }
231
    }
232
 
167 pfowler 233
    public class Potentiometer : Control {
234
        public int channel { get; set; }
235
 
236
        public Boolean invert { get; set; }
237
 
238
        public Potentiometer(Command command, int channel) : base(command) {
239
            this.channel = channel;
240
        }
241
 
242
        public override void Tick() {
243
            UInt16 tmpval = 0;
168 pfowler 244
            bool changed = data.axis[channel].getPosition(ref tmpval);
187 pfowler 245
 
180 pfowler 246
 
167 pfowler 247
 
168 pfowler 248
            if (changed) {
187 pfowler 249
                this.value = tmpval;
167 pfowler 250
                foreach (Command cmd in this.commands) {
251
                    cmd.data = this.data;
252
                    if (cmd.Send(this.value) == 0)
253
                        Console.Write(cmd.ToString());
254
                }
255
            }
256
        }
257
    }
258
 
161 pfowler 259
    public class Selector : Control {
260
        int[] pins;
261
 
162 pfowler 262
 
161 pfowler 263
        public Selector(Command command, int[] pins) : base(command) {
264
            this.pins = pins;
265
            this.value = 0;
266
        }
267
 
268
        public override void Tick() {
162 pfowler 269
            // This is a rather complicated way  to check what the previous select
270
            //  was, and was the current selection is. Not really needed, but hoping it 
271
            //  might come in handy for something else down the line.
166 pfowler 272
            UInt64 chg = 0;
273
            UInt64 norm = 0;
274
            UInt64 mask = 0;
162 pfowler 275
            for (int i = 0; i < pins.Length; i++) // Create a mask of how many selection points there are
166 pfowler 276
                mask |= (UInt64)1 << i;
161 pfowler 277
 
278
            for (int i = 0; i < pins.Length; i++) {
166 pfowler 279
                chg |= (UInt64)(data.prev >> (pins[i] - i)); // Narrow down to the previsouly selected bit
167 pfowler 280
                norm |= (UInt64)(data.buttons >> (pins[i] - i)); // Narrow down to the currently selected bit
161 pfowler 281
 
167 pfowler 282
                if ((UInt64)((data.buttons >> pins[i]) & 0x01) == 1) { // Decode the value we need to use
166 pfowler 283
                    value = (UInt64)i;
161 pfowler 284
                }
285
            }
162 pfowler 286
 
287
            norm &= mask; // Remove any bits from above
161 pfowler 288
            chg &= mask;
162 pfowler 289
 
290
            if (norm == 0) // Can sometimes be in the inbetween selector switch, ignore
161 pfowler 291
                return;
292
 
162 pfowler 293
            if (norm != chg) { // Send the update if needed
161 pfowler 294
                foreach (Command cmd in this.commands) {
162 pfowler 295
                    cmd.data = this.data;
164 pfowler 296
                    if (cmd.Send(this.value) == 0)
297
                        Console.Write(cmd.ToString());
161 pfowler 298
                }
299
            }
300
        }
162 pfowler 301
 
161 pfowler 302
    }
303
}