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);
            }
        }
Пример #2
0
        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));
        }
Пример #3
0
        /**
         * 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;
            }
        }
Пример #5
0
        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);
        }
Пример #6
0
        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 */
        }
Пример #7
0
        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());
        }
Пример #8
0
        /* 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;
            }
        }