Subversion Repositories group.electronics

Rev

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

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

namespace nitdcscore {
    public struct devdata {
        public uint prev_input;
        public uint cur_input;
        public ushort cur_adc;
        public ushort prev_adc;
        public ushort max_adc;
        public ushort adc_thres;

        public int id;
        public string name;
        public string function;
    }

    public class mcp2221 {
        public MchpUsbI2c usbi2c = new MchpUsbI2c();
        public Boolean hasAHFS { get; set; }
        public Boolean hasAAP { get; set; }

        int idx { get; set; }
        int devcount { get; set; }

        public uint speed { get; set; }

        Timer refresh = new Timer();
        Timer delay = new Timer();

        private readonly Object lockobject = new object();
        public List<Panel> panels = new List<Panel>();


        public mcp2221() {
            speed = 400000;
            idx = 0;

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

            usbi2c.Settings.GetConnectionStatus();
            devcount = usbi2c.Management.GetDevCount();
            Console.WriteLine(devcount.ToString() + " devices found");
            for (int i = 0; i < devcount; i++) {
                int rslt = usbi2c.Management.SelectDev(i);
                usbi2c.Settings.GetConnectionStatus();
                string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
                if (usbDescriptor == "AHCP/FSCP Panel") {
                    this.hasAHFS = true;
                    panels.Add(new Panel_AHFS(this));
                    Console.WriteLine("Found AHCP/FSCP Panel");
                } else if (usbDescriptor == "AAP Panel") {
                    this.hasAAP = true;
                    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");
                }
            }

            
        }

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

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



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

            refresh.Enabled = false;
            foreach (Panel panel in panels) {
                usbi2c.Management.SelectDev(panel.id);
                usbi2c.Settings.GetConnectionStatus();

                if (!panel.inputChanged)
                    panel.Refresh();

                if (panel.inputChanged)
                    panel.Input();
            }
            refresh.Enabled = true;

        }

        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;
        }

    }
}