Example #1
0
        /// <summary>
        /// Constructs SenseHat instance
        /// </summary>
        public SenseHat(I2cBus?i2cBus = null, bool shouldDispose = false)
        {
            _shouldDispose = shouldDispose || i2cBus == null;
            _i2cBus        = i2cBus ?? I2cBus.Create(DefaultI2cBusId);

            Debug.Assert(SenseHatLedMatrixI2c.I2cAddress == SenseHatJoystick.I2cAddress, $"Default addresses for {nameof(SenseHatLedMatrixI2c)} and {nameof(SenseHatJoystick)} were expected to be the same");
            I2cDevice joystickAndLedMatrixI2cDevice = _i2cBus.CreateDevice(SenseHatLedMatrixI2c.I2cAddress);

            _ledMatrix = new SenseHatLedMatrixI2c(joystickAndLedMatrixI2cDevice);
            _joystick  = new SenseHatJoystick(joystickAndLedMatrixI2cDevice);
            _gyro      = new SenseHatAccelerometerAndGyroscope(_i2cBus.CreateDevice(SenseHatAccelerometerAndGyroscope.I2cAddress));
            _mag       = new SenseHatMagnetometer(_i2cBus.CreateDevice(SenseHatMagnetometer.I2cAddress));
            _temp      = new SenseHatTemperatureAndHumidity(_i2cBus.CreateDevice(SenseHatTemperatureAndHumidity.I2cAddress));
            _press     = new SenseHatPressureAndTemperature(_i2cBus.CreateDevice(SenseHatPressureAndTemperature.I2cAddress));
        }
Example #2
0
    public static Bme280 CreateBme280(I2cBus i2cBus)
    {
        var bme280 = new Bme280(i2cBus.CreateDevice(Bme280.DefaultI2cAddress));

        SetupBme280(bme280);
        return(bme280);
    }
Example #3
0
        /// <summary>
        /// Creates a device on this bus
        /// </summary>
        /// <param name="deviceAddress">The device address</param>
        /// <returns>An I2C device</returns>
        /// <remarks>No test is performed whether the given device exists and is usable</remarks>
        public override I2cDevice CreateDevice(int deviceAddress)
        {
            if (_devices.TryGetValue(deviceAddress, out var device))
            {
                return(device);
            }

            var newDevice = _busInstance.CreateDevice(deviceAddress);

            _devices.Add(deviceAddress, newDevice);
            return(newDevice);
        }
Example #4
0
        public static GpioExpander GetGpioExpander(this I2cBus bus, int address = GpioExpander.DefaultAddress)
        {
            if (bus is null)
            {
                throw ThrowHelper.ArgumentNullException(nameof(bus));
            }

            if (address is < 0 or > 127)
            {
                throw ThrowHelper.ArgumentOutOfRangeException(nameof(address), address);
            }

            return(new RaspberryPiExpander(bus.CreateDevice(address)));
        }
Example #5
0
        /// <summary>
        /// Create an I2C device instance on a default bus.
        /// </summary>
        /// <param name="connectionSettings">Connection parameters (contains I2C address and bus number)</param>
        /// <returns>An I2C device instance</returns>
        /// <remarks>This method can only be used for bus numbers where the corresponding pins are hardwired
        /// (i.e. bus 0 and 1 on the Raspi always use pins 0/1 and 2/3)</remarks>
        public I2cDevice CreateI2cDevice(I2cConnectionSettings connectionSettings)
        {
            Initialize();
            // Returns logical pin numbers for the selected bus (or an exception if using a bus number > 1, because that
            // requires specifying the pins)
            if (_i2cBuses.TryGetValue(connectionSettings.BusId, out var bus))
            {
                return(bus.CreateDevice(connectionSettings.DeviceAddress));
            }

            int[]  pinAssignment = GetDefaultPinAssignmentForI2c(connectionSettings.BusId);
            I2cBus newBus        = CreateOrGetI2cBus(connectionSettings.BusId, pinAssignment);

            return(newBus.CreateDevice(connectionSettings.DeviceAddress));
        }
Example #6
0
        /// <summary>
        /// Performs a scan on the I2C bus, returning the addresses for all connected devices.
        /// </summary>
        /// <param name="bus">The bus to scan</param>
        /// <param name="lowest">The lowest address to scan. Default 0x03</param>
        /// <param name="highest">The highest address to scan. Default 0x77</param>
        /// <returns>A list of bus addresses that are in use, an empty list if no device was found</returns>
        /// <remarks>This method should never throw an exception. Bus scanning may interfere with normal device operation,
        /// so this should not be done while devices are being used.</remarks>
        public static List <int> PerformBusScan(this I2cBus bus, int lowest = 0x3, int highest = 0x77)
        {
            List <int> ret = new List <int>();

            for (int addr = lowest; addr <= highest; addr++)
            {
                try
                {
                    using (var device = bus.CreateDevice(addr))
                    {
                        device.ReadByte(); // Success means that this does not throw an exception
                        ret.Add(addr);
                    }
                }
                catch (Exception x) when(x is IOException || x is UnauthorizedAccessException)
                {
                    // Ignore and continue
                }
            }

            return(ret);
        }