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"); } }
/// <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 }); }
public async void Initialize(byte address = 0x40, I2cBusSpeed speed = I2cBusSpeed.StandardMode) { //MainPage rootPage = MainPage.Current; DeviceInformationCollection devices = null; Address = address; Speed = speed; //await rootPage.uart.SendText("I2C Device at Address " + address.ToString() + " Initializing\n\r"); var settings = new I2cConnectionSettings(Address) { BusSpeed = Speed }; settings.SharingMode = I2cSharingMode.Shared; // Get a selector string that will return all I2C controllers on the system string aqs = I2cDevice.GetDeviceSelector(); // Find the I2C bus controller device with our selector string devices = await DeviceInformation.FindAllAsync(aqs); //search for the controller if (!devices.Any()) { throw new IOException("No I2C controllers were found on the system"); } //see if we can find the hat device = await I2cDevice.FromIdAsync(devices[0].Id, settings); //await rootPage.uart.SendText("I2C Device at Address " + address.ToString() + " Initialized\n\r"); }
/// <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); }
protected BaseI2CDevice(I2cBusSpeed busSpeed, I2cSharingMode sharingMode, int slaveAddress) { ConnectionSettings = new I2cConnectionSettings(slaveAddress) { BusSpeed = busSpeed, SharingMode = sharingMode }; }
/// <summary> /// This is used to write the settings of the I2C speed bits per the formatting of the /// Configuration register. /// </summary> /// <param name="speed">I²C Speed</param> public void SetConfiguration(I2cBusSpeed busSpeed) { ResetOneWireAndMatchDeviceRomAddress(); Master.EnableStrongPullup(); Master.OneWireWriteByte((byte)DS28E17.DeviceCommand.WriteConfiguration); Master.OneWireWriteByte((byte)busSpeed); //Also 900 kHz are supported (set value = 0b00000010) }
public static void Initiate(int slaveAddress, I2cBusSpeed speedMode) { var settings = new I2cConnectionSettings(slaveAddress); settings.BusSpeed = speedMode; var controller = I2cController.GetDefaultAsync().GetResults(); device = controller.GetDevice(settings); }
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); }
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); }
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 */ }
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 the MCP23017. Has to be called when creating a new instance. /// </summary> /// <param name="bus">I2C bus controller identifier</param> /// <param name="speed">I2C bus speed</param> /// <param name="address">MCP23017 hardware address (0 - 7)</param> public void init(string bus, I2cBusSpeed speed, byte address) { if (address > 7) { throw new ArgumentOutOfRangeException("Address has to be between 0 and 7"); } // Create I2C device instance _i2c = I2cDevice.FromId(bus, new I2cConnectionSettings(address | 0b0100000) { BusSpeed = speed });
/// <summary> /// Gets the device from the I2C bus at the specified address. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="deviceAddress">The address of the device.</param> /// <param name="busSpeed">The desired bus speed.</param> /// <returns>Returns a device that implements II2CSensor.</returns> public static Task <T> GetI2CDevice <T>(byte deviceAddress, I2cBusSpeed busSpeed) where T : II2CSensor { T returnValue = default(T); if (typeof(T) == typeof(IMcp9808)) { IMcp9808 device = new Mcp9808(deviceAddress, busSpeed); returnValue = (T)device; } return(Task <T> .FromResult(returnValue)); }
/// <summary> /// Constructor /// </summary> /// <param name="busSelector">Which I2C bus to use</param> /// <param name="deviceAddr">The I2C device address (default is 0x40, the 7-bit, shifted address)</param> /// <param name="speed">The I2C bus speed (by default, 100KHz)</param> /// <param name="scale">How many decimal places to look for in the temperature and humidity values</param> public HTU21D(string busSelector = "I2C1", int deviceAddr = 0x40, I2cBusSpeed speed = I2cBusSpeed.StandardMode, uint scale = 2) { _i2CDevice = I2cDevice.FromId(busSelector, new I2cConnectionSettings(deviceAddr) { BusSpeed = speed, SharingMode = I2cSharingMode.Shared }); RelativeHumidity = ERROR_HUMIDITY; TemperatureInCelcius = ERROR_TEMPERATURE; Resolution = 0x81; _scale = scale; }
/// <summary> /// Constructor /// </summary> /// <param name="a0addrSelect">A0 pin connection for address selection</param> /// <param name="clockRateKhz">I2C clock rate in KHz</param> public TMP102(A0AddressSelect a0addrSelect = A0AddressSelect.GND, int clockRateKhz = CLOCK_RATE_KHZ_DEFAULT) { // create buffers for one and two bytes for I2C communications this.regAddress = new byte[REG_ADDRESS_SIZE]; this.regData = new byte[REG_DATA_SIZE]; // configure and create I2C reference device this.i2cConfig = new I2cConnectionSettings((int)(TMP102_ADDRESS_BASE + a0addrSelect)); I2cBusSpeed i2cBusSpeed = (clockRateKhz == CLOCK_RATE_KHZ_DEFAULT) ? I2cBusSpeed.StandardMode : I2cBusSpeed.FastMode; this.i2cConfig.BusSpeed = i2cBusSpeed; this.i2cConfig.SharingMode = I2cSharingMode.Exclusive; }
public static async Task <I2cDevice> GetAsync(int slaveAddr, I2cBusSpeed speed = I2cBusSpeed.StandardMode, I2cSharingMode sharing = I2cSharingMode.Exclusive) { var aqs = I2cDevice.GetDeviceSelector(); var infos = await DeviceInformation.FindAllAsync(aqs); var settings = new I2cConnectionSettings(slaveAddr) { BusSpeed = speed, SharingMode = sharing }; return(await I2cDevice.FromIdAsync(infos[0].Id, settings)); }
/// <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); }
/// <summary> /// Constructor /// </summary> /// <param name="busSelector">Which I2C bus to use</param> /// <param name="deviceAddr">The I2C device address (default is 0x76, the 7-bit, shifted address)</param> /// <param name="speed">The I2C bus speed (by default, 100KHz)</param> /// <param name="scale">How many decimal places to look for in the temperature and humidity values</param> public Bmp280(string busSelector = "I2C1", int deviceAddr = 0x76, I2cBusSpeed speed = I2cBusSpeed.StandardMode, uint scale = 2) { _device = I2cDevice.FromId(busSelector, new I2cConnectionSettings(deviceAddr) { BusSpeed = speed, SharingMode = I2cSharingMode.Shared }); _scale = scale; _wBuf = new byte[1] { 0x00 }; _rBuf = new byte[1] { 0x00 }; PressureInPa = ERROR_PRESSURE; TemperatureInCelcius = ERROR_TEMPERATURE; }
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)); }
/// <summary> /// Add a new device to the I2C bus... /// </summary> /// <param name="addr">The device address</param> /// <param name="speed">The clock speed in hertz</param> public void AddI2CDevice(int addr, I2cBusSpeed speed) { if (_i2CDevices.Count >= MAX_DEVICES_ON_BUS) { throw new IndexOutOfRangeException("Cannot add more devices on the shared I2C bus. Limit exhausted"); } if (DeviceExists(addr)) { throw new Exception("Device exists"); } I2cDevice device = I2cDevice.FromId(_deviceSelector, new I2cConnectionSettings(addr) { BusSpeed = speed, SharingMode = I2cSharingMode.Shared }); _i2CDevices.Add(addr, device); }
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(); }
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)); } }
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(); }
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."); }
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; } }
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; } }
public I2CDevice(byte baseAddress, I2cBusSpeed busSpeed) { BaseAddress = baseAddress; _busSpeed = busSpeed; }
/// <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); }
/// <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; }
/// <summary> /// Creates an instance of IMcp9808 with the given /// device address and bus speed. /// </summary> /// <param name="deviceAddress">The address of the device.</param> /// <param name="busSpeed">The desired bus speed.</param> public Mcp9808(byte deviceAddress, I2cBusSpeed busSpeed) { this.DeviceAddress = deviceAddress; this.BusSpeed = busSpeed; }
/// <summary> /// Creates an instance of IMcp9808 with the given /// device address and bus speed. /// </summary> /// <param name="deviceAddress">The address of the device.</param> /// <param name="busSpeed">The desired bus speed.</param> public Mcp9808(byte deviceAddress, I2cBusSpeed busSpeed) { this.DeviceAddress = deviceAddress; this.BusSpeed = busSpeed; }
/// <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; }
/// <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; }
public static Mb85rcvDeviceId?GetDeviceId(int busNumber, I2cBusSpeed speed = I2cBusSpeed.FastMode, I2cSharingMode sharingMode = I2cSharingMode.Exclusive) { // Call overloaded method return(GetDeviceId(busNumber, DeviceIdI2cAddress, DataI2cAddress, speed, sharingMode)); }
public Ds1307(I2cBusSpeed busSpeed) : base(I2C_ADDRESS, busSpeed) { }
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); } }
public Ds1307(I2cBusSpeed busSpeed) : base(I2C_ADDRESS, busSpeed) { }