/// <summary>
 /// Create a new SMBus device
 /// </summary>
 /// <param name="address">The 7-bit address of the device</param>
 /// <param name="I2CModule">The i2C module this device is connected to.</param>
 /// <param name="rateKHz">the rate, in kHz, that should be used to communicate with this device.</param>
 public SMBusDevice(byte address, I2c I2CModule, int rateKHz = 100)
 {
     if (address > 0x7f)
         throw new ArgumentOutOfRangeException("address", "The address parameter expects a 7-bit address that doesn't include a Read/Write bit. The maximum address is 0x7F");
     this.address = address;
     I2c = I2CModule;
     this.rateKhz = rateKHz;
     I2c.Enabled = true;
 }
 /// <summary>
 /// Construct a new AMIS30624 stepper motor controller
 /// </summary>
 /// <param name="module">The I2c module this stepper motor is attached to</param>
 /// <param name="address">The address of the module</param>
 /// <param name="speed">The speed to operate this peripheral at</param>
 public Amis30624(I2c module, byte address, int speed)
 {
     dev = new SMBusDevice(address, module, speed);
     ResetToDefault().ConfigureAwait(false);
     GetFullStatus().ConfigureAwait(false);
 }
        /// <summary>
        /// Construct a new AMIS30624 stepper motor controller
        /// </summary>
        /// <param name="module">The I2c module this stepper motor is attached to</param>
        /// <param name="addressPin">The hardwired address pin state</param>
        /// <param name="speed">The speed to operate this peripheral at</param>
        public Amis30624(I2c module, bool addressPin = false, int speed = 400) : this(module, (byte)(addressPin ? 0x61 : 0x60), speed)
        {

        }
        /// <summary>
        /// Open the TreehopperBoard. The board must be opened before any other methods are called.
        /// </summary>
        public void Open()
        {
            if (usb.Open())
            {
                // If this is a "whole" usb device (libusb-win32, linux libusb)
                // it will have an IUsbDevice interface. If not (WinUSB) the
                // variable will be null indicating this is an interface of a
                // device.
                IUsbDevice wholeUsbDevice = usb as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // This is a "whole" USB device. Before it can be used,
                    // the desired configuration and interface must be selected.

                    // Select config #1
                    wholeUsbDevice.SetConfiguration(1);

                    // Claim interface #0.
                    wholeUsbDevice.ClaimInterface(0);
                }

                pinStateBuffer = new byte[64];

                Pins = new List <Pin>();

                // Initialize Pins
                pin1  = new Pin1(this);
                pin2  = new Pin2(this);
                pin3  = new Pin3(this);
                pin4  = new Pin4(this);
                pin5  = new Pin5(this);
                pin6  = new Pin6(this);
                pin7  = new Pin7(this);
                pin8  = new Pin8(this);
                pin9  = new Pin9(this);
                pin10 = new Pin10(this);
                pin11 = new Pin11(this);
                pin12 = new Pin12(this);
                pin13 = new Pin13(this);
                pin14 = new Pin14(this);

                Pins.Add(pin1);
                Pins.Add(pin2);
                Pins.Add(pin3);
                Pins.Add(pin4);
                Pins.Add(pin5);
                Pins.Add(pin6);
                Pins.Add(pin7);
                Pins.Add(pin8);
                Pins.Add(pin9);
                Pins.Add(pin10);
                Pins.Add(pin11);
                Pins.Add(pin12);
                Pins.Add(pin13);
                Pins.Add(pin14);

                SoftPwmMgr = new SoftPwmManager(this);

                // Comparator
                //Comparator1 = new Comparator(1);
                //Comparator2 = new Comparator(2);

                // Initialize modules
                analogOut = new AnalogOut(this);
                i2c       = new I2c(this);
                spi       = new Spi(this);
                //UART = new UART();

                // Initialize endpoint readers/writers
                PinConfig    = usb.OpenEndpointWriter(WriteEndpointID.Ep01);
                pinState     = usb.OpenEndpointReader(ReadEndpointID.Ep01);
                CommsConfig  = usb.OpenEndpointWriter(WriteEndpointID.Ep02);
                CommsReceive = usb.OpenEndpointReader(ReadEndpointID.Ep02);

                // Start reader events
                pinState.DataReceived       += pinState_DataReceived;
                pinState.DataReceivedEnabled = true;
                this.IsConnected             = true;
            }
            else
            {
                if (usb != null)
                {
                    if (usb.IsOpen)
                    {
                        usb.Close();
                    }
                    usb = null;
                }
            }
        }