Subversion Repositories group.electronics

Rev

Rev 162 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nitdcscore {
    public abstract class Control {
        public Control(Command command) {
            commands.Add(command);
        }

        public List<Command> commands = new List<Command>();
        public Utils.InputPair data { get; set; }       // The data from the panel
        public Boolean updated { get; set; }

        public abstract void Tick();
    }

    public class Switch2Pos : Control {
        public int pin { get; set; }
        public uint value { get; set; }
        public Boolean invert { get; set; }

        public Switch2Pos(Command command, int pin, Boolean invert = false) : base(command) {
            this.invert = invert;
            this.pin = pin;
        }

        public override void Tick() {
            uint chg = (uint)(data.prev >> pin) & 0x01;
            uint norm = (uint)(data.curr >> pin) & 0x01;
            this.value = 0;
            
            if ((uint)(norm) == 1) {
                value = (uint)(invert ? 0 : 1);
            } else {
                value = (uint)(invert ? 1 : 0);
            }

            if (norm != chg) {
                foreach (Command cmd in this.commands) {
                    cmd.Send(this.value);
                    Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
                }
            }
        }
    }

    public class Switch3Pos : Control {
        public int pin0 { get; set; }
        public int pin1 { get; set; }
        public uint value { get; set; }
        public Boolean invert { get; set; }

        public Switch3Pos(Command command, int pin0, int pin1, Boolean invert = false) : base(command) {
            this.pin0 = pin0;
            this.pin1 = pin1;
            this.invert = invert;
            
        }

        public override void Tick() {
            uint chg0 = (uint)(data.prev >> pin0) & 0x01;
            uint chg1 = (uint)(data.prev >> pin1) & 0x01;
            uint nrm0 = (uint)(data.curr >> pin0) & 0x01;
            uint nrm1 = (uint)(data.curr >> pin1) & 0x01;
            this.value = 1;
            
            if ((uint)nrm0 == 1)
                value = (uint)(invert ? 2 : 0);
            else if ((uint)nrm1 == 1)
                value = (uint)(invert ? 0 : 2);
            if ((nrm0 != chg0) || (nrm1 != chg1)) {
                foreach (Command cmd in this.commands) {
                    cmd.Send(this.value);
                    Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
                }
            }
        }
    }

    public class Selector : Control {
        int[] pins;
        public uint value;

        public Selector(Command command, int[] pins) : base(command) {
            this.pins = pins;
            this.value = 0;
        }

        public override void Tick() {
            uint chg = 0;
            uint norm = 0;
            uint mask = 0;
            for (int i = 0; i < pins.Length; i++)
                mask |= (uint)1 << i;

            for (int i = 0; i < pins.Length; i++) {
                chg |= (uint)(data.prev >> (pins[i] ));
                norm |= (uint)(data.curr >> (pins[i] ));

                if ((uint)((data.curr >> pins[i]) & 0x01) == 1) {
                    value = (uint)i;
                }
            }
            if (pins.Length==3)
                Console.WriteLine(data.curr.ToString("X") + ":" + mask + ":" + chg + ":" + norm + ":" + value);
            norm &= mask;
            chg &= mask;
            //if (pins.Length == 3)
            //    Console.WriteLine(data.curr.ToString("X") + ":" + mask + ":" + chg + ":" + norm + ":" + value);
            // This happens when between positions of the selector
            if (norm == 0)
                return;

            if (norm != chg) {
                foreach (Command cmd in this.commands) {
                    cmd.Send(this.value);
                    Console.Write(this.GetType().ToString() + " : " + cmd.ToString());
                }
            }
        }
    }
}