Beispiel #1
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 = I2cController.FindController(controllerId);

            if (controller == null)
            {
                // this controller doesn't exist yet, create it...
                controller = new I2cController(i2cBus);
            }

            // check if this device ID already exists
            var device = FindDevice(controller, deviceId);

            if (device == null)
            {
                // 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(this);

                _syncLock = new object();
            }
            else
            {
                // this device already exists, throw an exception
                throw new I2cDeviceAlreadyInUseException();
            }
        }
Beispiel #2
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;
            }
        }