Subversion Repositories group.electronics

Rev

Blame | Last modification | View Log | RSS feed

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

namespace nitdcscore {
    public class Bridge_mcp2221 {
        public MchpUsbI2c usbi2c = new MchpUsbI2c();
        public DcsBios bios = new DcsBios();
        public uint speed { get; set; }
        public int deviceid { get; set; }

        Timer timer = new Timer();

        NITAAPDevice aap;
        NITAHFSDevice ahfs;

        public Bridge_mcp2221() {
            this.speed = 400000;

            timer.Enabled = false;
            timer.AutoReset = true;
            timer.Interval = 50;
            timer.Elapsed += timer_Elapsed;

            bios.InitUDP();

            
        }

        public void init() {
            aap = new NITAAPDevice(ref this.bios, this);
            ahfs = new NITAHFSDevice(ref this.bios, this);
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e) {
            
        }

        public int SelectDevice(String devName) {
            if (usbi2c.Settings.GetConnectionStatus() == false)
                return -2;
            int rslt = 0;
            
            int count = usbi2c.Management.GetDevCount();
            for (int i = 0; i < count; i++) {
                rslt = usbi2c.Management.SelectDev(i);
                usbi2c.Settings.GetConnectionStatus();
                string usbDescriptor = this.usbi2c.Settings.GetUsbStringDescriptor();
                if (usbDescriptor == devName) {
                    rslt = usbi2c.Management.SelectDev(i);
                    Console.WriteLine(devName + ":" + usbDescriptor + ":" + i.ToString());
                    this.deviceid = i;
                    return rslt;
                }
            }
            return -5;
        }


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

            //foreach (ushort adc in adcData) {
            //    Console.Write((adc).ToString() + " ");
            //}
            //Console.WriteLine();

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