/// <summary> /// Connects to the device /// </summary> /// <returns></returns> public async Task <bool> ConnectAsync() { try { var i2CBus = await I2CBusHelper.GetI2CBusDeviceInformation(); if (i2CBus == null) { return(false); // bus not found } // ADDR HI 0x5C, ADDR LO 0x23 var settings = new I2cConnectionSettings(_address) { BusSpeed = I2cBusSpeed.FastMode }; // Create an I2cDevice with our selected bus controller and I2C settings _device = await I2cDevice.FromIdAsync(i2CBus.Id, settings); var id = new byte[1]; _device.WriteRead(new byte[] { (byte)Registers.ID }, id); if (id[0] != 0x55) { throw new InvalidOperationException("I2C device communication failure"); } Connected = true; } catch (Exception ex) { Debug.WriteLine("Initialization failed: " + ex); } return(Connected); }
/// <summary> /// Connects to the devices /// </summary> /// <returns></returns> public async Task <bool> ConnectAsync() { try { var i2CBus = await I2CBusHelper.GetI2CBusDeviceInformation(); if (i2CBus == null) { return(false); // bus not found } // ADDR HI 0x5C, ADDR LO 0x23 var settings = new I2cConnectionSettings(_address) { BusSpeed = I2cBusSpeed.FastMode }; // Create an I2cDevice with our selected bus controller and I2C settings _device = await I2cDevice.FromIdAsync(i2CBus.Id, settings); Powered = true; WriteMtReg((byte)_mTime); Powered = false; Connected = true; } catch (Exception ex) { Debug.WriteLine("Initialization failed: " + ex); } return(Connected); }
/// <summary> /// Open connection to chip /// </summary> /// <returns></returns> public async Task <bool> ConnectAsync() { try { var i2CBus = await I2CBusHelper.GetI2CBusDeviceInformation(); if (i2CBus == null) { return(false); // bus not found } // ADDR HI 0x5C, ADDR LO 0x23 var settings = new I2cConnectionSettings(Address) { BusSpeed = I2cBusSpeed.FastMode }; // Create an I2cDevice with our selected bus controller and I2C settings _device = await I2cDevice.FromIdAsync(i2CBus.Id, settings); // Self-test before attaching to the DRDY pin await SelfTest().ConfigureAwait(true); if (_drdyPin.HasValue) { var gpc = GpioController.GetDefault(); if (gpc == null) { return(false); } _drdy = gpc.OpenPin(_drdyPin.Value); _drdy.Write(GpioPinValue.High); _drdy.SetDriveMode(GpioPinDriveMode.Input); _drdy.ValueChanged += DataReady; } Connected = true; } catch (Exception ex) { Debug.WriteLine("Initialization failed: " + ex); } return(Connected); }
public async Task <bool> ConnectAsync() { try { DeviceInformation i2CBus = await I2CBusHelper.GetI2CBusDeviceInformation(); if (i2CBus == null) { return(false); // bus not found } // ADDR HI 0x5C, ADDR LO 0x23 var settings = new I2cConnectionSettings(_address) { BusSpeed = I2cBusSpeed.FastMode }; // Create an I2cDevice with our selected bus controller and I2C settings _device = await I2cDevice.FromIdAsync(i2CBus.Id, settings); var gpc = GpioController.GetDefault(); if (gpc == null) { return(false); } _chipSelect = gpc.OpenPin(_csPinNumber); _chipSelect.Write(GpioPinValue.Low); _chipSelect.SetDriveMode(GpioPinDriveMode.Output); await Task.Delay(15); var sbuf = new byte[1]; _device.WriteRead(new [] { (byte)Registers.Status }, sbuf); _chipSelect.Write(GpioPinValue.High); Connected = true; } catch (Exception ex) { Debug.WriteLine("Initialization failed: " + ex); _chipSelect?.Dispose(); _chipSelect = null; } return(Connected); }