public void Initialise(I2cBusSpeed i2CBusSpeed = I2cBusSpeed.StandardMode, I2cSharingMode i2CSharingMode = I2cSharingMode.Shared)
        {
            string aqs = I2cDevice.GetDeviceSelector();

            DeviceInformationCollection I2CBusControllers = DeviceInformation.FindAllAsync(aqs).AsTask().Result;

            if (I2CBusControllers.Count != 1)
            {
                throw new IndexOutOfRangeException("I2CBusControllers");
            }

            I2cConnectionSettings settings = new I2cConnectionSettings(I2CAddress)
            {
                BusSpeed    = i2CBusSpeed,
                SharingMode = i2CSharingMode,
            };

            Device = I2cDevice.FromIdAsync(I2CBusControllers[0].Id, settings).AsTask().Result;

            byte[] writeBuffer = new byte[1] {
                RegisterDeviceId
            };
            byte[] readBuffer = new byte[1] {
                0
            };

            Device.WriteRead(writeBuffer, readBuffer);
            byte deviceId = readBuffer[0];

            Debug.WriteLine($"GroveBaseHatRPI DeviceId 0x{deviceId:X}");
            if (deviceId != DeviceId)
            {
                throw new Exception("GroveBaseHatRPI not found");
            }
        }
Example #2
0
        /// <summary>
        /// Instanciate asyncronously a new PCF8591 object.
        /// As System.Threading.Tasks.Task are not valid Windows Runtime type supported, this method has been set to private and is publicly exposed through the IAsyncOperation method "Create".
        /// </summary>
        /// <param name="BusSpeed">The I2C Bus Speed. Default value: StandardMode </param>
        /// <param name="SharingMode">The I2C Sharing Mode. Default value is Exclusive. To use with caution </param>
        /// <returns></returns>
        async static private Task <PCF8591> CreateAsync(I2cBusSpeed BusSpeed, I2cSharingMode SharingMode)
        {
            PCF8591 newADC = new PCF8591();
            /// advanced query syntax used to find devices on the RaspberryPi.
            string AQS         = Windows.Devices.I2c.I2cDevice.GetDeviceSelector();
            var    DevicesInfo = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(AQS);

            if (DevicesInfo.Count == 0)
            {
                throw new Exception("No Device Information were found with query: " + AQS);
            }
            // I2C bus settings
            var settings = new Windows.Devices.I2c.I2cConnectionSettings(addr_PCF8591);

            settings.BusSpeed    = BusSpeed;
            settings.SharingMode = SharingMode;
            // Reteives the device from the I2C bus with the given ID.
            newADC.device = await Windows.Devices.I2c.I2cDevice.FromIdAsync(DevicesInfo[0].Id, settings);

            if (newADC.device == null)
            {
                throw new Exception("No I2C Device were found with ID " + DevicesInfo[0].Id);
            }
            return(newADC);
        }
Example #3
0
 protected BaseI2CDevice(I2cBusSpeed busSpeed, I2cSharingMode sharingMode, int slaveAddress)
 {
     ConnectionSettings = new I2cConnectionSettings(slaveAddress)
     {
         BusSpeed    = busSpeed,
         SharingMode = sharingMode
     };
 }
Example #4
0
        public I2cTimedDevice(int slaveAddress, ReadingMode mode, TimeSpan readInterval,
                              I2cBusSpeed busSpeed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Shared, string i2cControllerName = RaspberryPiI2cControllerName)
            : base(slaveAddress, busSpeed, sharingMode, i2cControllerName)
        {
            this.readInterval = readInterval;
            this.readingMode  = mode;

            timer = new Timer(CheckState, null, Timeout.Infinite, Timeout.Infinite);
        }
Example #5
0
        public Mb85rc256vDevice(int busNumber, byte chipNumber,
                                I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
            : base(chipNumber, MemorySize)
        {
            // Get address
            var address = GetDataI2cAddress(chipNumber);

            // Connect to hardware
            Hardware = I2cExtensions.Connect(busNumber, address, speed, sharingMode);
        }
Example #6
0
        public I2cDeviceBase(int slaveAddress, I2cBusSpeed busSpeed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Shared, string i2cControllerName = RaspberryPiI2cControllerName)
        {
            // Initialize I2C device
            Settings = new I2cConnectionSettings(slaveAddress)
            {
                BusSpeed = busSpeed,
                SharingMode = sharingMode
            };

            deviceSelector = I2cDevice.GetDeviceSelector(i2cControllerName);    /* Find the selector string for the I2C bus controller */
        }
Example #7
0
        public Ms5611Device(int busNumber, bool csb, Ms5611Osr rate,
                            I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            // Get address
            ChipSelectBit = csb;
            Address       = GetI2cAddress(csb);

            // Connect to hardware
            _hardware = I2cExtensions.Connect(busNumber, Address, speed, sharingMode);

            // Initialize members
            Prom = new Ms5611PromData();
            Osr  = rate;

            // Read current calibration data (potentially not stable until next reset)
            // We don't reset automatically so that it is possible for any ongoing tasks to complete
            ReadProm();
        }
Example #8
0
        public Pca9685Device(int busNumber, byte chipNumber, int?clockSpeed,
                             I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            // Validate
            if (clockSpeed.HasValue && (clockSpeed == 0 || clockSpeed.Value > ClockSpeedMaximum))
            {
                throw new ArgumentOutOfRangeException(nameof(clockSpeed));
            }

            // Get address
            var address = GetI2cAddress(chipNumber);

            // Connect to hardware
            _hardware = I2cExtensions.Connect(busNumber, address, speed, sharingMode);

            // Initialize configuration
            ClockIsExternal  = clockSpeed.HasValue;
            ClockSpeed       = clockSpeed ?? InternalClockSpeed;
            FrequencyDefault = CalculateFrequency(PrescaleDefault, ClockSpeed);
            FrequencyMinimum = CalculateFrequency(PrescaleMaximum, ClockSpeed); // Inverse relationship (max = min)
            FrequencyMaximum = CalculateFrequency(PrescaleMinimum, ClockSpeed); // Inverse relationship (min = max)

            // Build channels
            _channels = new Collection <Pca9685ChannelValue>();
            Channels  = new ReadOnlyCollection <Pca9685ChannelValue>(_channels);
            for (var index = 0; index < ChannelCount; index++)
            {
                _channels.Add(new Pca9685ChannelValue(index));
            }

            // Set "all call" address
            I2cExtensions.WriteJoinByte(_hardware, (byte)Pca9685Register.AllCall, I2cAllCallAddress);

            // Enable auto-increment and "all call"
            I2cExtensions.WriteReadWriteBit(_hardware, (byte)Pca9685Register.Mode1,
                                            (byte)(Pca9685Mode1Bits.AutoIncrement | Pca9685Mode1Bits.AllCall), true);

            // Read current values and update properties
            ReadAll();
        }
Example #9
0
        public async Task I2CInit(byte slaveAddress, I2cBusSpeed busSpeed, I2cSharingMode sharingMode)
        {
            // get aqs filter  to find i2c device
            string aqs = I2cDevice.GetDeviceSelector("I2C1");

            // Find the I2C bus controller with our selector string
            var dis = await DeviceInformation.FindAllAsync(aqs);
            if (dis.Count == 0)
                throw new Exception("There is no I2C device"); // bus not found

            I2cConnectionSettings settings = new I2cConnectionSettings(slaveAddress);
            // Create an I2cDevice with our selected bus controller and I2C settings
            _i2CDevice = await I2cDevice.FromIdAsync(dis[0].Id, settings);
            if (_i2CDevice == null) return;
            var status = _i2CDevice.WritePartial(new byte[] { 0x00 });
            if (status.Status == I2cTransferStatus.FullTransfer) _isConnected = true;
            else if (status.Status == I2cTransferStatus.PartialTransfer)
                throw new Exception("There was an error during sending test data.");
            else if (status.Status == I2cTransferStatus.SlaveAddressNotAcknowledged)
              throw new Exception("I2C Device is not connected.");
        
        }
Example #10
0
        public Mb85rc04vDevice(int busNumber, byte chipNumber,
                               I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
            : base(chipNumber, MemorySize)
        {
            // Check device addresses
            var lowerAddress = GetDataI2cAddress(chipNumber, false);
            var upperAddress = GetDataI2cAddress(chipNumber, true);

            // Connect to devices
            try
            {
                Hardware      = I2cExtensions.Connect(busNumber, lowerAddress, speed, sharingMode);
                HardwareUpper = I2cExtensions.Connect(busNumber, upperAddress, speed, sharingMode);
            }
            catch
            {
                // Free resources on initialization error
                Hardware?.Dispose();
                HardwareUpper?.Dispose();

                // Continue error
                throw;
            }
        }
Example #11
0
        public static TSL2591 CreateDevice(string i2cBus, int i2cAddress = HARDWARE_BASE_ADDRESS, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            try
            {
                // Setup our connection settings to the I2C bus
                I2cConnectionSettings i2cSettings = new I2cConnectionSettings(i2cAddress)
                {
                    BusSpeed = busSpeed, SharingMode = sharingMode
                };

                // Get an instance of the i2CDevice.
                var i2cDevice = I2cDevice.FromId(i2cBus, i2cSettings);

                // Create an instance of our device.
                var instance = new TSL2591(i2cDevice);

                // Reset the device (POR) [Low Gain, 100ms Integration Time]
                instance._i2cDevice.Write(new byte[] { TSL2591_CONTROL_RW, TSL2591_DEVICE_RESET_VALUE });

                // Is this device actaully a TSL2591? Simple way to check that we are really working.
                var readBuffer = new byte[1];
                instance._i2cDevice.WriteRead(new byte[] { TSL2591_ID_R }, readBuffer);

                if (readBuffer[0] != TSL2591_DEVICE_ID_VALUE)
                {
                    throw new Exception("Device is not recogonized as a TSL2591");
                }

                // Return the instance to the caller
                return(instance);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #12
0
        public static SI1145 CreateDevice(string i2cBus, int i2cAddress = HARDWARE_BASE_ADDRESS, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            try
            {
                // Setup our connection settings to the I2C bus
                I2cConnectionSettings i2cSettings = new I2cConnectionSettings(i2cAddress)
                {
                    BusSpeed = busSpeed, SharingMode = sharingMode
                };

                // Get an instance of the i2CDevice.
                var i2cDevice = I2cDevice.FromId(i2cBus, i2cSettings);

                // Create an instance of our device.
                var instance = new SI1145(i2cDevice);

                var readBuffer = new byte[1];
                instance._i2cDevice.WriteRead(new byte[] { SI1145_REG_PARTID }, readBuffer);

                if (readBuffer[0] != 0x45)
                {
                    throw new Exception("Device is not recogonized as a SI1145");
                }

                // Power on reset
                instance._i2cDevice.Write(new byte[] { SI1145_REG_MEASRATE0, 0 });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_MEASRATE1, 0 });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_IRQEN, 0 });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_IRQMODE1, 0 });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_IRQMODE2, 0 });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_INTCFG, 0 });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_IRQSTAT, 0xFF });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_COMMAND, SI1145_RESET });
                System.Threading.Thread.Sleep(10);

                instance._i2cDevice.Write(new byte[] { SI1145_REG_HWKEY, 0x17 });
                System.Threading.Thread.Sleep(10);

                // enable UVindex measurement coefficients!
                instance._i2cDevice.Write(new byte[] { SI1145_REG_UCOEFF0, 0x29 });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_UCOEFF1, 0x89 });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_UCOEFF2, 0x02 });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_UCOEFF3, 0x00 });

                // enable UV sensor
                instance.writeParam(SI1145_PARAM_CHLIST, SI1145_PARAM_CHLIST_ENUV | SI1145_PARAM_CHLIST_ENALSIR | SI1145_PARAM_CHLIST_ENALSVIS | SI1145_PARAM_CHLIST_ENPS1);

                // enable interrupt on every sample
                instance._i2cDevice.Write(new byte[] { SI1145_REG_INTCFG, SI1145_REG_INTCFG_INTOE });
                instance._i2cDevice.Write(new byte[] { SI1145_REG_IRQEN, SI1145_REG_IRQEN_ALSEVERYSAMPLE });

                /****************************** Prox Sense 1 */
                // program LED current
                instance._i2cDevice.Write(new byte[] { SI1145_REG_PSLED21, 0x03 }); // 20mA for LED 1 only
                instance.writeParam(SI1145_PARAM_PS1ADCMUX, SI1145_PARAM_ADCMUX_LARGEIR);

                // prox sensor #1 uses LED #1
                instance.writeParam(SI1145_PARAM_PSLED12SEL, SI1145_PARAM_PSLED12SEL_PS1LED1);

                // fastest clocks, clock div 1
                instance.writeParam(SI1145_PARAM_PSADCGAIN, 0);

                // take 511 clocks to measure
                instance.writeParam(SI1145_PARAM_PSADCOUNTER, SI1145_PARAM_ADCCOUNTER_511CLK);

                // in prox mode, high range
                instance.writeParam(SI1145_PARAM_PSADCMISC, SI1145_PARAM_PSADCMISC_RANGE | SI1145_PARAM_PSADCMISC_PSMODE);

                instance.writeParam(SI1145_PARAM_ALSIRADCMUX, SI1145_PARAM_ADCMUX_SMALLIR);

                // fastest clocks, clock div 1
                instance.writeParam(SI1145_PARAM_ALSIRADCGAIN, 0);

                // take 511 clocks to measure
                instance.writeParam(SI1145_PARAM_ALSIRADCOUNTER, SI1145_PARAM_ADCCOUNTER_511CLK);

                // in high range mode
                instance.writeParam(SI1145_PARAM_ALSIRADCMISC, SI1145_PARAM_ALSIRADCMISC_RANGE);

                // fastest clocks, clock div 1
                instance.writeParam(SI1145_PARAM_ALSVISADCGAIN, 0);

                // take 511 clocks to measure
                instance.writeParam(SI1145_PARAM_ALSVISADCOUNTER, SI1145_PARAM_ADCCOUNTER_511CLK);

                // in high range mode (not normal signal)
                instance.writeParam(SI1145_PARAM_ALSVISADCMISC, SI1145_PARAM_ALSVISADCMISC_VISRANGE);

                /************************/
                // measurement rate for auto
                instance._i2cDevice.Write(new byte[] { SI1145_REG_MEASRATE0, 0xFF }); // 255 * 31.25uS = 8ms

                // auto run
                instance._i2cDevice.Write(new byte[] { SI1145_REG_COMMAND, SI1145_PSALS_AUTO });

                // Return the instance to the caller
                return(instance);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #13
0
 public Tcs34725(I2cBusSpeed busSpeed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Shared, SlaveAddress slaveAddress = SlaveAddress.Ox29)
     : base(busSpeed, sharingMode, (int)slaveAddress)
 {
 }
        public I2cDeviceBase(int slaveAddress, I2cBusSpeed busSpeed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Shared, string i2cControllerName = RaspberryPiI2cControllerName)
        {
            // Initialize I2C device
            Settings = new I2cConnectionSettings(slaveAddress)
            {
                BusSpeed    = busSpeed,
                SharingMode = sharingMode
            };

            deviceSelector = I2cDevice.GetDeviceSelector(i2cControllerName);    /* Find the selector string for the I2C bus controller */
        }
 /// <summary>
 /// Initializes a copy of a <see cref="I2cConnectionSettings"/> object.
 /// </summary>
 /// <param name="settings">Object to copy from.</param>
 internal I2cConnectionSettings(I2cConnectionSettings settings)
 {
     _slaveAddress = settings.SlaveAddress;
     _busSpeed     = settings.BusSpeed;
     _sharingMode  = settings.SharingMode;
 }
 /// <summary>
 /// Construct a copy of an I2cConnectionSettings object.
 /// </summary>
 /// <param name="source">Source object to copy from.</param>
 internal I2cConnectionSettings(I2cConnectionSettings source)
 {
     m_slaveAddress = source.m_slaveAddress;
     m_busSpeed     = source.m_busSpeed;
     m_sharingMode  = source.m_sharingMode;
 }
Example #17
0
        public static Mb85rcvDeviceId?GetDeviceId(int busNumber, byte idAddress, byte dataAddress,
                                                  I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            // Connect to ID device
            using (var framIdRegister = I2cExtensions.Connect(busNumber, idAddress, speed, sharingMode))
            {
                // Send ID command sequence, returning device ID bytes
                var dataAddress8bit = (byte)(dataAddress << 1);
                var data            = new byte[Mb85rcvDeviceId.Size];
                var transfer        = framIdRegister.WriteReadPartial(new[] { dataAddress8bit }, data);
                if (transfer.Status == I2cTransferStatus.SlaveAddressNotAcknowledged)       // Any other error must throw
                {
                    // Return null when not found
                    return(null);
                }

                // Decode and return ID when found
                return(new Mb85rcvDeviceId(data));
            }
        }
Example #18
0
        public static DS1307 CreateDevice(string i2cBus, int i2cAddress = HARDWARE_BASE_ADDRESS, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            try
            {
                // Setup our connection settings to the I2C bus
                I2cConnectionSettings i2cSettings = new I2cConnectionSettings(i2cAddress)
                {
                    BusSpeed = busSpeed, SharingMode = sharingMode
                };

                // Get an instance of the i2CDevice.
                var i2cDevice = I2cDevice.FromId(i2cBus, i2cSettings);

                // Create an instance of our device.
                var instance = new DS1307(i2cDevice);

                // Set the defaults for our device

                // Return the instance to the caller
                return(instance);
            }
            catch (Exception)
            {
                return(null);
            }
        }
 /// <summary>
 /// Construct a copy of an I2cConnectionSettings object.
 /// </summary>
 /// <param name="source">Source object to copy from.</param>
 internal I2cConnectionSettings(I2cConnectionSettings source)
 {
     m_slaveAddress = source.m_slaveAddress;
     m_busSpeed = source.m_busSpeed;
     m_sharingMode = source.m_sharingMode;
 }
 public Adxl345Accelerometer(int slaveAddress = DefaultI2cAddress, ReadingMode mode = ReadingMode.Continuous, I2cBusSpeed busSpeed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Shared, string i2cControllerName = RaspberryPiI2cControllerName)
     : base(slaveAddress, mode, DEFAULT_READ_INTERVAL, busSpeed, sharingMode, i2cControllerName)
 {
 }
        public static MCP23017GpioController CreateDevice(string i2cBus, int address = HARDWARE_BASE_ADDRESS, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive, IGpioPin interruptPin = null)
        {
            // Create the I2c connection settings instance.
            I2cConnectionSettings settings = new I2cConnectionSettings(address)
            {
                BusSpeed = busSpeed, SharingMode = sharingMode
            };

            // Create the I2c device instance
            var i2cDevice = I2cDevice.FromId(i2cBus, settings);

            if (i2cDevice == null)
            {
                // No device was created
                throw new Exception("Unable to create I2c instance.");
            }

            try
            {
                var instance = new MCP23017GpioController(i2cDevice, interruptPin);

                // Set the defaults for our device. These are power on defaults
                instance.WriteRegister(MCP23017_IODIRA, 0xFF);
                instance.WriteRegister(MCP23017_IPOLA, 0x00);
                instance.WriteRegister(MCP23017_GPINTENA, 0x00);
                instance.WriteRegister(MCP23017_DEFVALA, 0x00);
                instance.WriteRegister(MCP23017_INTCONA, 0x00);
                instance.WriteRegister(MCP23017_IOCONA, 0x00);
                instance.WriteRegister(MCP23017_GPPUA, 0x00);
                instance.WriteRegister(MCP23017_GPIOA, 0x00);
                instance.WriteRegister(MCP23017_OLATA, 0x00);

                instance.WriteRegister(MCP23017_IODIRB, 0xFF);
                instance.WriteRegister(MCP23017_IPOLB, 0x00);
                instance.WriteRegister(MCP23017_GPINTENB, 0x00);
                instance.WriteRegister(MCP23017_DEFVALB, 0x00);
                instance.WriteRegister(MCP23017_INTCONB, 0x00);
                instance.WriteRegister(MCP23017_IOCONB, 0x00);
                instance.WriteRegister(MCP23017_GPPUB, 0x00);
                instance.WriteRegister(MCP23017_GPIOB, 0x00);
                instance.WriteRegister(MCP23017_OLATB, 0x00);

                return(instance);
            }
            catch (Exception)
            {
                i2cDevice?.Dispose();
                throw;
            }
        }
Example #22
0
        private async Task <I2cDevice> FindI2CDevice(DeviceInformation bus, int slaveAddress, I2cBusSpeed busSpeed, I2cSharingMode sharingMode)
        {
            var settings = new I2cConnectionSettings(slaveAddress)
            {
                BusSpeed    = busSpeed,
                SharingMode = sharingMode,
            };
            var device = await I2cDevice.FromIdAsync(bus.Id, settings);    /* Create an I2cDevice with our selected bus controller and I2C settings */

            if (device != null)
            {
                return(device);
            }


            _logger.LogInfo($"Slave address {settings.SlaveAddress} on I2C Controller {bus.Id} is currently in use by " +
                            "another application, or was not found. Please ensure that no other applications are using I2C and your device is correctly connected to the I2C bus.");
            return(null);
        }
Example #23
0
        public static Mb85rcvDeviceId?GetDeviceId(int busNumber, byte chipNumber,
                                                  I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            // Validate
            if (chipNumber < 0 || chipNumber > MaximumDevices)
            {
                throw new ArgumentOutOfRangeException(nameof(chipNumber));
            }

            // Calculate device ID address
            var idAddress   = GetDeviceIdI2cAddress(chipNumber);
            var dataAddress = GetDataI2cAddress(chipNumber, false);

            // Call overloaded method
            return(GetDeviceId(busNumber, idAddress, dataAddress, speed, sharingMode));
        }
Example #24
0
        /// <summary>
        /// Initialises a new Wii Nunchuk
        /// </summary>
        /// <param name="busId">The identifier of the I²C bus.</param>
        /// <param name="slaveAddress">The I²C address.</param>
        /// <param name="busSpeed">The bus speed, an enumeration that defaults to StandardMode</param>
        /// <param name="sharingMode">The sharing mode, an enumeration that defaults to Shared.</param>
        public WiiNunchuk(string busId, ushort slaveAddress = 0x52, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Shared)
        {
            I2cTransferResult result;

            // This initialisation routine seems to work. I got it at http://wiibrew.org/wiki/Wiimote/Extension_Controllers#The_New_Way
            Device = I2cDevice.FromId(busId, new I2cConnectionSettings(slaveAddress)
            {
                BusSpeed    = busSpeed,
                SharingMode = sharingMode,
            });

            result = Device.WritePartial(new byte[] { 0xf0, 0x55 });
            if (result.Status != I2cTransferStatus.FullTransfer)
            {
                throw new ApplicationException("Something went wrong reading the Nunchuk. Did you use proper pull-up resistors?");
            }

            result = Device.WritePartial(new byte[] { 0xfb, 0x00 });
            if (result.Status != I2cTransferStatus.FullTransfer)
            {
                throw new ApplicationException("Something went wrong reading the Nunchuk. Did you use proper pull-up resistors?");
            }

            this.Device.Write(new byte[] { 0xf0, 0x55 });
            this.Device.Write(new byte[] { 0xfb, 0x00 });
        }
Example #25
0
 public static Mb85rcvDeviceId?GetDeviceId(int busNumber,
                                           I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
 {
     // Call overloaded method
     return(GetDeviceId(busNumber, DeviceIdI2cAddress, DataI2cAddress, speed, sharingMode));
 }
Example #26
0
        public static DS3231M CreateDevice(string i2cBus, byte hardwareAddress = BASE_ADDRESS, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive, GpioPin alarmPin = null)
        {
            // Create the I2c connection settings instance.
            I2cConnectionSettings settings = new I2cConnectionSettings(hardwareAddress)
            {
                BusSpeed = busSpeed, SharingMode = sharingMode
            };

            // Create teh I2c device instance
            var i2cDevice = I2cDevice.FromId(i2cBus, settings);

            if (i2cDevice == null)
            {
                // No device was created
                throw new Exception("Unable to create I2c instance.");
            }

            try
            {
                var instance = new DS3231M(i2cDevice, alarmPin);
                instance._i2cDevice.Write(new byte[] { DS3231_CONTROL_ADDR, DS3231_INTCN });

                return(instance);
            }
            catch (Exception)
            {
                i2cDevice?.Dispose();
                throw;
            }
        }
Example #27
0
        /// <summary>
        /// Try and connect to a specific device address
        /// </summary>
        /// <param name="addr">Address of device</param>
        /// <param name="speed">Bus speed</param>
        /// <param name="sharing">Sharing mode</param>
        /// <returns></returns>
        protected async Task GetDevice(int addr, I2cBusSpeed speed = I2cBusSpeed.StandardMode, I2cSharingMode sharing = I2cSharingMode.Exclusive)
        {
            var aqs   = I2cDevice.GetDeviceSelector();
            var infos = await DeviceInformation.FindAllAsync(aqs);

            var settings = new I2cConnectionSettings(addr)
            {
                BusSpeed    = speed,
                SharingMode = sharing
            };

            device = await I2cDevice.FromIdAsync(infos[0].Id, settings);
        }
Example #28
0
 public Si7021(I2cBusSpeed busSpeed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Shared, SlaveAddress slaveAddress = SlaveAddress.Ox40)
     : base(busSpeed, sharingMode, (int)slaveAddress)
 {
 }
        /// <summary>
        /// Connects to an I2C device if it exists.
        /// </summary>
        /// <param name="controllerIndex">Controller index.</param>
        /// <param name="address">Slave address.</param>
        /// <param name="speed">Bus speed.</param>
        /// <param name="sharingMode">Sharing mode.</param>
        /// <returns>Device when controller and device exist, otherwise null.</returns>
        public static I2cDevice ConnectI2c(int controllerIndex, int address, 
            I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            // Validate
            if (controllerIndex < 0) throw new ArgumentOutOfRangeException(nameof(controllerIndex));

            // Initialize
            Initialize();

            // Get controller (return null when doesn't exist)
            if (I2c.Count < controllerIndex + 1)
                return null;
            var controller = I2c[controllerIndex];

            // Connect to device and return (if exists)
            var settings = new I2cConnectionSettings(address)
            {
                BusSpeed = speed,
                SharingMode = sharingMode
            };
            return controller.GetDevice(settings);
        }
Example #30
0
 /// <summary>
 /// Instanciate asyncronously a new PCF8591 object.
 /// </summary>
 /// <param name="BusSpeed">The I2C Bus Speed. Default value: StandardMode </param>
 /// <param name="SharingMode">The I2C Sharing Mode. Default value is Exclusive. To use with caution </param>
 /// <returns>A new PCF8591 object instance.</returns>
 public static Windows.Foundation.IAsyncOperation <PCF8591> Create(I2cBusSpeed BusSpeed, I2cSharingMode SharingMode)
 {
     return(CreateAsync(BusSpeed, SharingMode).AsAsyncOperation());
 }
Example #31
0
        public static ADS1015 CreateDevice(string i2cBus, byte hardwareAddress = BASE_ADDRESS, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            // Create the I2c connection settings instance.
            I2cConnectionSettings settings = new I2cConnectionSettings(hardwareAddress)
            {
                BusSpeed = busSpeed, SharingMode = sharingMode
            };

            // Create teh I2c device instance
            var i2cDevice = I2cDevice.FromId(i2cBus, settings);

            if (i2cDevice == null)
            {
                // No device was created
                throw new Exception("Unable to create I2c instance.");
            }

            try
            {
                var instance = new ADS1015(i2cDevice);
                return(instance);
            }
            catch (Exception)
            {
                i2cDevice?.Dispose();
                throw;
            }
        }
Example #32
0
 public Bmp280(I2cBusSpeed busSpeed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Shared, SlaveAddress slaveAddress = SlaveAddress.Ox77)
     : base(busSpeed, sharingMode, (int)slaveAddress)
 {
 }
Example #33
0
        public static HTU21DF CreateDeviceAsync(string i2cBus, int i2cAddress = HARDWARE_BASE_ADDRESS, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive)
        {
            try
            {
                // Setup our connection settings to the I2C bus
                I2cConnectionSettings i2cSettings = new I2cConnectionSettings(i2cAddress)
                {
                    BusSpeed = busSpeed, SharingMode = sharingMode
                };

                // Get an instance of the i2CDevice.
                var i2cDevice = I2cDevice.FromId(i2cBus, i2cSettings);

                // Create an instance of our device.
                var instance = new HTU21DF(i2cDevice);

                // Reset the device (POR)
                instance._i2cDevice.Write(new byte[] { HTU21DF_RESET });
                System.Threading.Thread.Sleep(15);

                var readBuffer = new byte[1];
                instance._i2cDevice.WriteRead(new byte[] { HTU21DF_READREG }, readBuffer);

                if (readBuffer[0] != 0x02)
                {
                    throw new Exception("Device is not recogonized as a HTU21DF");
                }

                // Return the instance to the caller
                return(instance);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #34
0
 public Bmp180BarometricSensor(int slaveAddress = DefaultI2cAddress, ReadingMode mode = ReadingMode.Continuous, I2cBusSpeed busSpeed = I2cBusSpeed.StandardMode, I2cSharingMode sharingMode = I2cSharingMode.Shared, string i2cControllerName = RaspberryPiI2cControllerName)
     : base(slaveAddress, mode, DEFAULT_READ_INTERVAL, busSpeed, sharingMode, i2cControllerName)
 {
 }