‘Hello, blinky!’ background service with Providers.Lightning

This sample demonstrates how to communicate with GPIO using the Micorosot.IoT.Providers.Lightning.I2cProvider

It’s based on the BlinkyHeadless sample. And shares the same setup steps as well as most of the code. The only difference is setting the Lightning provider as the default controllers provider.

Using Lightning Provider

The original code of the Blinky Headless sample used the default GPIO provider for obtaining the GPIO controller:

// Original Blinky Headless sample code

using Windows.Devices.Gpio;

private async void InitGPIO()
{
    var gpio = GpioController.GetDefault();

    if (gpio == null)
    {
        pin = null;
        return;
    }

    pin = gpio.OpenPin(LED_PIN);

    if (pin == null)
    {
        return;
    }

    pin.Write(GpioPinValue.High);
    pin.SetDriveMode(GpioPinDriveMode.Output);
}

To enable the use of the Lightning provider, the GPIO controller initialization code needs to be modified to set the Lightning provider as the default provider. In the initialization code below, if the Lightning driver is enabled on the target device, the Lightning provider will be used. Otherwise, the app falls back to using the default provider.

using Microsoft.IoT.Lightning.Providers;
using Windows.Devices.Gpio;

private async void InitGPIO()
{
    // Set the Lightning Provider as the default if Lightning driver is enabled on the target device
    if (LightningProvider.IsLightningEnabled)
    {
        LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
    }

    // Remaining initialization code works the same regardless of the current default provider

    var gpio = GpioController.GetDefault();

    if (gpio == null)
    {
        pin = null;
        return;
    }

    pin = gpio.OpenPin(LED_PIN);

    if (pin == null)
    {
        return;
    }

    pin.SetDriveMode(GpioPinDriveMode.Output);
    pin.Write(GpioPinValue.High);
}

Building and deploying the sample

  1. Enable Lightning on your Raspberry Pi2 or MBM device using the the steps outlined in the Lightning Setup Guide.

  2. Ensure the correct version of the Windows SDK is installed on your development machine. The required Windows SDK can be installed from here.

  3. Clone the source for the Blinky Background (Headless) sample for Lightning from Github to a local folder.

  4. To setup the LED connectons, refer to the original BlinkyHeadless sample.

  5. Load the solution in Visual Studio.

  6. Build the app.

  7. Deploy and run the app on your device.
    You can use the Deploying an App with Visual Studio for guidance.


Was this page helpful?
Additional feedback?
Thank you! We appreciate your feedback.
Follow us