Beispiel #1
0
        internal static I2cDevice FindDevice(I2cController controller, int index)
        {
            for (int i = 0; i < controller.DeviceCollection.Count; i++)
            {
                if (((I2cDevice)controller.DeviceCollection[i])._deviceId == index)
                {
                    return((I2cDevice)controller.DeviceCollection[i]);
                }
            }

            return(null);
        }
Beispiel #2
0
        internal I2cDevice(string i2cBus, I2cConnectionSettings settings)
        {
            // generate a unique ID for the device by joining the I2C bus ID and the slave address, should be pretty unique
            // the encoding is (I2C bus number x 1000 + slave address)
            // i2cBus is an ASCII string with the bus name in format 'I2Cn'
            // need to grab 'n' from the string and convert that to the integer value from the ASCII code (do this by subtracting 48 from the char value)
            var controllerId = i2cBus[3] - 48;
            var deviceId     = (controllerId * deviceUniqueIdMultiplier) + settings.SlaveAddress;

            I2cController controller;

            if (!I2cControllerManager.ControllersCollection.Contains(controllerId))
            {
                // this controller doesn't exist yet, create it...
                controller = new I2cController(i2cBus);
            }
            else
            {
                // get the controller from the collection...
                controller = (I2cController)I2cControllerManager.ControllersCollection[controllerId];
            }

            // check if this device ID already exists
            if (!controller.DeviceCollection.Contains(deviceId))
            {
                // device doesn't exist, create it...
                _connectionSettings = new I2cConnectionSettings(settings.SlaveAddress)
                {
                    BusSpeed    = settings.BusSpeed,
                    SharingMode = settings.SharingMode
                };

                // save device ID
                _deviceId = deviceId;

                // call native init to allow HAL/PAL inits related with I2C hardware
                NativeInit();

                // ... and add this device
                controller.DeviceCollection.Add(deviceId, this);

                _syncLock = new object();
            }
            else
            {
                // this device already exists, throw an exception
                throw new I2cDeviceAlreadyInUseException();
            }
        }
Beispiel #3
0
        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                bool disposeController = false;

                if (disposing)
                {
                    // get the controller
                    var controller = I2cController.FindController(_deviceId / deviceUniqueIdMultiplier);

                    if (controller != null)
                    {
                        // find device
                        var device = FindDevice(controller, _deviceId);

                        if (device != null)
                        {
                            // remove from device collection
                            controller.DeviceCollection.Remove(device);

                            // it's OK to also remove the controller, if there is no other device associated
                            if (controller.DeviceCollection.Count == 0)
                            {
                                I2cControllerManager.ControllersCollection.Remove(controller);

                                // flag this to native dispose
                                disposeController = true;
                            }
                        }
                    }
                }

                NativeDispose(disposeController);

                _disposed = true;
            }
        }
Beispiel #4
0
 /// <summary>
 /// Retrieves an Advanced Query Syntax (AQS) string for all of the inter-integrated circuit (I2C) bus controllers on the system. You can use this string with the DeviceInformation.FindAll
 /// method to get DeviceInformation objects for those bus controllers.
 /// </summary>
 /// <returns>An AQS string for all of the I2C bus controllers on the system, which you can use with the DeviceInformation.FindAllAsync method to get DeviceInformation
 /// objects for those bus controllers.</returns>
 public static string GetDeviceSelector()
 {
     return(I2cController.GetDeviceSelector());
 }