Subversion Repositories group.electronics

Rev

Rev 168 | Rev 172 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 168 Rev 171
Line 3... Line 3...
3
using System.Linq;
3
using System.Linq;
4
using System.Text;
4
using System.Text;
5
using System.Threading.Tasks;
5
using System.Threading.Tasks;
6
 
6
 
7
namespace nitdcscore {
7
namespace nitdcscore {
-
 
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
 
8
    public abstract class Control {
113
    public abstract class Control {
9
        public UInt64 value { get; set; }
114
        public UInt64 value { get; set; }
10
 
115
 
11
        public Control(Command command) {
116
        public Control(Command command) {
12
            commands.Add(command);
117
            commands.Add(command);