Subversion Repositories group.electronics

Rev

Rev 181 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using MCP2221;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using WindowsInput;
using WindowsInput.Native;

namespace nitdcscore {

    public class mcp2221 : i2cmaster {
        public MchpUsbI2c usbi2c = new MchpUsbI2c();

        public List<Panel> panels = new List<Panel>();
        public uint speed { get; set; }
        Timer refresh = new Timer();
        Timer display = new Timer();
        TimeSpan ts = new TimeSpan();
        UInt64 refreshCount = 0;


        public mcp2221() {
            speed = 400000;

            refresh.Interval = 5;
            refresh.AutoReset = true;
            refresh.Elapsed += refresh_Elapsed;
            refresh.Enabled = false;

            display.Interval = 1000;
            display.AutoReset = true;
            display.Enabled = false;
            display.Elapsed += display_Elapsed;

            usbi2c.Settings.GetConnectionStatus();
            int devcount = usbi2c.Management.GetDevCount();
            Console.WriteLine(devcount.ToString() + " devices found");
            for (int i = 0; i < devcount; i++) {

                if (this.SelectDev(i) != 0) {
                    Console.WriteLine("Could not attach device #: " + i);
                    continue;
                }

                string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
                if (usbDescriptor == "AHCP/FSCP Panel") {
                    panels.Add(new Panel_AHFS(this));
                    Console.WriteLine("Found AHCP/FSCP Panel");
                } else if (usbDescriptor == "AAP Panel") {
                    panels.Add(new Panel_AAP(this));
                    Console.WriteLine("Found AAP Panel");
                } else if (usbDescriptor == "Multi-Panel Combo") {
                    panels.Add(new NitMultiPanel(this));
                    Console.WriteLine("Found Multi-Panel Combo");
                } else if (usbDescriptor == "Aux Panel") {
                    panels.Add(new NitAuxPanel(this));
                    Console.WriteLine("Found Aux Panel");
                }
            }

            
        }

        void display_Elapsed(object sender, ElapsedEventArgs e) {
            Double avg = ts.TotalMilliseconds / refreshCount;
            Console.WriteLine("Average Refresh: " + avg);
        }

        private Boolean enabled;
        public Boolean Enabled {
            get {
                return enabled;
            }
            set {
                if (panels.Count == 0) {
                    this.enabled = false;
                    refresh.Enabled = false;
                    return;
                }

                this.enabled = value;
                refresh.Enabled = value;
            }
        }



        void refresh_Elapsed(object sender, ElapsedEventArgs e) {
            if (!Enabled)
                return;

            Stopwatch sw = new Stopwatch();
            sw.Start();
            refresh.Enabled = false;
            foreach (Panel panel in panels) {
                if (this.SelectDev(panel.id) != 0)
                    continue;

                if (!panel.data.changed)
                    panel.Refresh();

                if (panel.data.changed)
                    panel.Input();

            }
            refresh.Enabled = true;
            sw.Stop();
            ts += sw.Elapsed;
            refreshCount++;
        }

        public int SelectDev(int id) {

            int rslt = usbi2c.Management.SelectDev(id);
            usbi2c.Settings.GetConnectionStatus();

            return rslt;
        }

        public byte ReadGpio(byte pinNum) {
            return Convert.ToByte(usbi2c.Functions.ReadGpioPinValue(pinNum));
        }

        public ushort ReadADC(byte pinNum) {
            ushort[] adcData = { 0, 0, 0, 0, 0, 0 };

            int rslt = usbi2c.Functions.GetAdcData(adcData);

            return adcData[pinNum];
        }

        public void WriteGpio(byte pinNum, byte value) {
            this.usbi2c.Functions.WriteGpioPinValue(pinNum, value);
        }

        public int WriteI2cData(byte address, byte[] data, uint count) {
            int rslt = this.usbi2c.Functions.WriteI2cData(address, data, count, this.speed);
            return rslt;
        }

        public int ReadI2CData(byte address, ref byte[] data, uint count) {
            int rslt = this.usbi2c.Functions.ReadI2cData(address, data, count, this.speed);
            return rslt;
        }

        public void SetGpio(byte pin, byte data) {
            this.usbi2c.Functions.WriteGpioPinValue(pin, data);
        }

    }
}