private async Task I2cConnect(int panel) { try { var settings = new I2cConnectionSettings(I2CAddress[panel]); settings.BusSpeed = I2cBusSpeed.FastMode; // If the system is using the Lightning memory mapped driver ther use the Lightning I2CProvider if (LightningProvider.IsLightningEnabled) { //LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider(); var i2cControllers = await I2cController.GetControllersAsync(LightningI2cProvider.GetI2cProvider()); i2cDevice[panel] = i2cControllers[0].GetDevice(settings); } else { // Otherwise, if the inbox provider will continue to be the default string aqs = I2cDevice.GetDeviceSelector(); /* Find the selector string for the I2C bus controller */ var dis = (await DeviceInformation.FindAllAsync(aqs)).ToArray(); /* Find the I2C bus controller device with our selector string */ i2cDevice[panel] = await I2cDevice.FromIdAsync(dis[0].Id, settings); /* Create an I2cDevice with our selected bus controller and I2C settings */ } } catch (Exception e) { throw new Exception("ht16k33 initisation problem: " + e.Message); } }
private async void Init() { // The code below should work the same with any provider, including Lightning and the default one. //I2cController controller = await I2cController.GetDefaultAsync(); I2cController controller = (await I2cController.GetControllersAsync(LightningI2cProvider.GetI2cProvider()))[0]; sensor = controller.GetDevice(new I2cConnectionSettings(0x90 >> 1)); }
static async Task i2cdump(string[] input) { if (input.Length == 3) { int slave = Convert.ToInt32(input[2], 16); UpBridge.Up upb = new UpBridge.Up(); int index = Int32.Parse(input[1]); if (index > 2) { Console.WriteLine("i2c have 0 & 1"); throw new InvalidOperationException("i2c have 0 & 1"); } I2cController controller = (await I2cController.GetControllersAsync(UpWinApis.UpI2cProvider.GetI2cProvider()))[index]; I2cConnectionSettings Settings = new I2cConnectionSettings(slave); Settings.BusSpeed = speed[index]; Console.WriteLine(" 0 1 2 3 4 5 6 7 8 9 a b c d e f"); byte[] writebuf = new byte[1]; byte[] readbuf = new byte[1]; try { for (uint i = 0; i < 256; i += 16) { if (i == 0) { Console.Write("00: "); } else { Console.Write(Convert.ToString(i, 16) + ": "); } for (uint j = 0; j < 16; j++) { writebuf[0] = (byte)(i + j); controller.GetDevice(Settings).WriteRead(writebuf, readbuf); if (readbuf[0] < 0x10) { Console.Write("0" + Convert.ToString(readbuf[0], 16) + " "); } else { Console.Write(Convert.ToString(readbuf[0], 16) + " "); } } Console.Write("\n"); } } catch (Exception) { Console.Write("NOT to read Data" + "\n"); } } else { Console.WriteLine("command error,plese refer to below example \n" + "i2cdump {bus number} {i2c address}\n"); Console.WriteLine(Usage); } }
/* Initialize GPIO, I2C, and the display * The device may not respond to multiple Init calls without being power cycled * so we allow an optional boolean to excuse failures which is useful while debugging * without power cycling the display */ public async Task <bool> Init(int DeviceAddress) { Debug.WriteLine("SSD1306::Initialize"); try { if (LightningProvider.IsLightningEnabled) { LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider(); I2cController controller = (await I2cController.GetControllersAsync(LightningI2cProvider.GetI2cProvider()))[0]; I2cConnectionSettings settings = new I2cConnectionSettings(DeviceAddress); // settings.SharingMode = I2cSharingMode.Shared; displayI2c = controller.GetDevice(settings); } else { //Instantiate the I2CConnectionSettings using the device address I2cConnectionSettings settings = new I2cConnectionSettings(DeviceAddress); //Set the I2C bus speed of connection to fast mode // settings.BusSpeed = I2cBusSpeed.FastMode; //Use the I2CBus device selector to create an advanced query syntax string string aqs = I2cDevice.GetDeviceSelector(I2CControllerName); //Use the Windows.Devices.Enumeration.DeviceInformation class to create a collection using the advanced query syntax string DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs); //Instantiate the the I2C device using the device id of the I2CBus and the I2CConnectionSettings displayI2c = await I2cDevice.FromIdAsync(dis[0].Id, settings); } //Check if device was found if (displayI2c == null) { Debug.WriteLine("Device not found"); return(false); } else { return(true); // return InitDisplay(); } } catch (Exception e) { Debug.WriteLine("Exception: " + e.Message + "\n" + e.StackTrace); return(false); // throw; } }
public async Task <bool> Initialize() { try { //Instantiate the I2CConnectionSettings using the device address of the BMP280 I2cConnectionSettings settings = new I2cConnectionSettings(deviceAddress); //Set the I2C bus speed of connection to fast mode settings.BusSpeed = I2cBusSpeed.FastMode; if (LightningProvider.IsLightningEnabled) { I2cController controller = (await I2cController.GetControllersAsync(LightningI2cProvider.GetI2cProvider()))[0]; //I2cDevice sensor = controller.GetDevice(new I2cConnectionSettings(0x40)); compass = controller.GetDevice(settings); } else { //Use the I2CBus device selector to create an advanced query syntax string string aqs = I2cDevice.GetDeviceSelector(I2CControllerName); //Use the Windows.Devices.Enumeration.DeviceInformation class to create a collection using the advanced query syntax string DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs); //Instantiate the the BMP280 I2C device using the device id of the I2CBus and the I2CConnectionSettings compass = await I2cDevice.FromIdAsync(dis[0].Id, settings); } //Check if device was found if (compass == null) { return(false); } } catch (Exception e) { Debug.WriteLine("Exception: " + e.Message + "\n" + e.StackTrace); } return(CheckId()); }
/** * Start I2C Communication **/ public async void startI2C(byte deviceAddress, string controllerName) { try { I2cConnectionSettings i2cSettings = new I2cConnectionSettings(deviceAddress); i2cSettings.BusSpeed = I2cBusSpeed.FastMode; string deviceSelector = I2cDevice.GetDeviceSelector(controllerName); var i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector); //this._i2cPortExpander = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings); //Changed to use Lightning Provider I2cController controller = (await I2cController.GetControllersAsync(LightningI2cProvider.GetI2cProvider()))[0]; this._i2cPortExpander = controller.GetDevice(i2cSettings); //this._i2cPortExpander = controller.GetDevice(new I2cConnectionSettings(0x90 >> 1)); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message); return; } }
/// <summary> /// Configures the <see cref="LightningProvider"/> when enabled. /// </summary> /// <remarks> /// Not thread safe, must be called in a thread safe context. /// </remarks> public static void Initialize() { // Do nothing when already configured if (_initialized) { return; } lock (_lock) { // Thread-safe double-check lock if (_initialized) { return; } // Set the Lightning Provider as the default if Lightning driver is enabled on the target device _lightningEnabled = LightningProvider.IsLightningEnabled; if (_lightningEnabled) { LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider(); // Add multiple controllers from new lightning provider Gpio = GpioController.GetControllersAsync(LightningGpioProvider.GetGpioProvider()).AsTask().GetAwaiter().GetResult(); I2c = I2cController.GetControllersAsync(LightningI2cProvider.GetI2cProvider()).AsTask().GetAwaiter().GetResult(); Spi = SpiController.GetControllersAsync(LightningSpiProvider.GetSpiProvider()).AsTask().GetAwaiter().GetResult(); } else { // Add single instance providers from old inbox driver Gpio = new ReadOnlyCollection <GpioController>(new[] { GpioController.GetDefault() }); I2c = new ReadOnlyCollection <I2cController>(new[] { I2cController.GetDefaultAsync().AsTask().GetAwaiter().GetResult() }); Spi = new ReadOnlyCollection <SpiController>(new[] { SpiController.GetDefaultAsync().AsTask().GetAwaiter().GetResult() }); } // Flag initialized _initialized = true; } }
public async Task <bool> Init(bool useLightning) { // Set the I2C address and speed var settings = new I2cConnectionSettings(I2C_ID_SERVO_USM); settings.BusSpeed = I2cBusSpeed.StandardMode; if (useLightning && LightningProvider.IsLightningEnabled) { I2cController controller = (await I2cController.GetControllersAsync(LightningI2cProvider.GetI2cProvider()))[0]; ubI2C = controller.GetDevice(settings); } else { // Try to find the UltraBorg on the I2C bus string aqs = I2cDevice.GetDeviceSelector(); var dis = await DeviceInformation.FindAllAsync(aqs); ubI2C = await I2cDevice.FromIdAsync(dis[0].Id, settings); } return(ubI2C != null); }
static async Task i2cset(string[] input) { if (input.Length == 5) { int slave = Convert.ToInt32(input[2], 16); UpBridge.Up upb = new UpBridge.Up(); int index = Int32.Parse(input[1]); if (index > 2) { Console.WriteLine("i2c have 0 & 1"); throw new InvalidOperationException("i2c have 0 & 1"); } I2cController controller = (await I2cController.GetControllersAsync(UpWinApis.UpI2cProvider.GetI2cProvider()))[index]; I2cConnectionSettings Settings = new I2cConnectionSettings(slave); Settings.BusSpeed = speed[index]; byte[] writebuf = new byte[2]; writebuf[0] = Convert.ToByte(input[3], 16); writebuf[1] = Convert.ToByte(input[4], 16); try { controller.GetDevice(Settings).Write(writebuf); Console.WriteLine("Sucess to set data,\n"); } catch (Exception e) { Console.WriteLine("error to set data\n"); } } else { Console.WriteLine("command error,plese refer to below example \n" + "i2cset {bus number} {i2c address} {i2c register} {i2cdata}\n"); Console.WriteLine(Usage); } }
const string I2C_CONTROLLER_NAME = "I2C1"; // For Raspberry Pi 2, use I2C1 public I2cDevice GetI2cDevice(byte address) { // Check if Lightning is enabled and set the Lightning provider as the default provider if (Microsoft.IoT.Lightning.Providers.LightningProvider.IsLightningEnabled) { Windows.Devices.LowLevelDevicesController.DefaultProvider = Microsoft.IoT.Lightning.Providers.LightningProvider.GetAggregateProvider(); var i2CProvider = LightningI2cProvider.GetI2cProvider(); var i2CControllers = I2cController.GetControllersAsync(i2CProvider).AsTask().Result; var i2CController = i2CControllers[0]; var i2CDevice = i2CController.GetDevice(new I2cConnectionSettings(address)); if (i2CDevice != null) { i2CDevice.ConnectionSettings.BusSpeed = I2cBusSpeed.FastMode; return(i2CDevice); } throw new InvalidOperationException("No I2C controllers were found on the system"); } string aqs = I2cDevice.GetDeviceSelector(I2C_CONTROLLER_NAME); /* Get a selector string that will return all I2C controllers on the system */ var deviceInformation = DeviceInformation.FindAllAsync(aqs).AsTask().Result; /* Find the I2C bus controller device with our selector string */ if (deviceInformation.Count == 0) { throw new InvalidOperationException("No I2C controllers were found on the system"); } var settings = new I2cConnectionSettings(address); settings.BusSpeed = I2cBusSpeed.FastMode; var count = deviceInformation.Count; var devAdd = deviceInformation[0]; return(I2cDevice.FromIdAsync(devAdd.Id, settings).AsTask().Result); /* Create an I2cDevice with our selected bus controller and I2C settings */ }
static async Task i2cdetect(string[] input) { try { if (input.Length == 2) { UpBridge.Up upb = new UpBridge.Up(); int index = Int32.Parse(input[1]); if (index > 2) { Console.WriteLine("i2c have 0 & 1"); throw new InvalidOperationException("i2c have 0 & 1"); } I2cController controller = (await I2cController.GetControllersAsync(UpWinApis.UpI2cProvider.GetI2cProvider()))[index]; I2cConnectionSettings Settings = new I2cConnectionSettings(0x00); Settings.BusSpeed = speed[index]; Console.WriteLine(" 0 1 2 3 4 5 6 7 8 9 a b c d e f"); byte[] writebuf = { 0x00 }; byte[] readbuf = new byte[1]; for (uint i = 0; i < 256; i += 16) { if (i == 0) { Console.Write("00: "); } else { Console.Write(Convert.ToString(i, 16) + ": "); } for (uint j = 0; j < 16; j++) { Settings.SlaveAddress = (int)(i + j); try { controller.GetDevice(Settings).WriteRead(writebuf, readbuf); if (i + j < 0x10) { Console.Write("0" + Convert.ToString(i + j, 16) + " "); } else { Console.Write(Convert.ToString(i + j, 16) + " "); } } catch (Exception e) { Console.Write("--" + " "); } } Console.Write("\n"); } } else { Console.WriteLine("command error,plese refer to below example \n" + "i2cdetect {busnumber}\n"); Console.WriteLine(Usage); } } catch (Exception e) { Console.WriteLine(e.Message); } }
static async void i2cdetect(string[] input) { try { if (input.Length == 2) { UpBridge.Up upb = new UpBridge.Up(); // I2cController controller = await I2cController.GetDefaultAsync(); Console.WriteLine("step 1 add controller"); I2cController controller = (await I2cController.GetControllersAsync(UpWinApis.UpI2cProvider.GetI2cProvider()))[0]; Console.WriteLine("step 2 setting"); I2cConnectionSettings Settings = new I2cConnectionSettings(0x00); Console.WriteLine("step 3"); Console.WriteLine(controller.GetDevice(Settings)); Console.WriteLine(controller.GetDevice(Settings).DeviceId); Console.WriteLine(" 0 1 2 3 4 5 6 7 8 9 a b c d e f"); byte[] writebuf = { 0x00 }; byte[] readbuf = new byte[1]; for (uint i = 0; i < 256; i += 16) { if (i == 0) { Console.Write("00: "); } else { Console.Write(Convert.ToString(i, 16) + ": "); } for (uint j = 0; j < 16; j++) { Settings.SlaveAddress = (int)(i + j); try { controller.GetDevice(Settings).WriteRead(writebuf, readbuf); if (i + j < 0x10) { Console.Write("0" + Convert.ToString(i + j, 16) + " "); } else { Console.Write(Convert.ToString(i + j, 16) + " "); } } catch (Exception e) { Console.Write("--" + " "); } } Console.Write("\n"); } } else { Console.WriteLine("command error,plese refer to below example \n" + "i2cdetect\n"); Console.WriteLine(Usage); } } catch (Exception e) { Console.WriteLine(e.Message); } }