Blinky Headless Sample

The classic LED blinking app running headless.

Blinky Headless Sample

We’ll create a simple Blinky app and connect a LED to your MinnowBoard Max (or MBM for short).

Headless mode

This application will run in either headed or headless mode, but if you are interested in the headless mode, you can modify the headless state of your device by manipulating a registry key.

  1. Telnet to your device and log in using the local system account (no username and no password)

  2. Modify the registry to configure the headless state of your device.

    • To configure your device to be headless, run this command and reboot:

        reg add hklm\system\currentcontrolset\control\wininit /v headless /t REG_DWORD /f /d 1
        shutdown /r /t 0
    • To return your device to headed mode, you can run this command and reboot:

        reg add hklm\system\currentcontrolset\control\wininit /v headless /t REG_DWORD /f /d 0
        shutdown /r /t 0

Load the project in Visual Studio

You can find this sample in the Samples\BlinkyHeadless folder. Choose either the C++ version or C# version.

Make sure you set the ‘Remote Debugging’ setting to point to your MBM. Go back to the basic ‘Hello World’ sample if you need guidance.

Make sure you connect the LED to your MBM. Go back to the basic ‘Blinky’ sample if you need guidance.

Let’s look at the code

The code for this sample is pretty simple. We use a timer, and each time the ‘Tick’ event is called, we flip the state of the LED.

If you want to recreate the headless application from scratch, you’ll need to start from the project template ‘Athens StartupTask (UAP)’.

Timer code

Here is how you set up the timer in C#:

using Windows.System.Threading;

BackgroundTaskDeferral _deferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
    _deferral = taskInstance.GetDeferral();

    this.timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(500));
    . . .
}
    
private void Timer_Tick(ThreadPoolTimer timer)
{
    . . .
}

Initialize the GPIO pin

To drive the GPIO pin, first we need to initialize it. Here is the C# code (notice how we leverage the new WinRT classes in the Windows.Devices.Gpio namespace):

using Windows.Devices.Gpio;
    
private async void InitGPIO()
{
    var deviceId = GpioController.GetDeviceSelector("GPIO_S5");
    var deviceInfos = await DeviceInformation.FindAllAsync(deviceId, null);
    var controller = await GpioController.FromIdAsync(deviceInfos[0].Id);
    GpioPinInfo pinInfo;
    controller.Pins.TryGetValue(0, out pinInfo);
    pinInfo.TryOpenOutput(GpioPinValue.High, GpioSharingMode.Exclusive, out this.outPin);
}

Let’s break this down a little:

Modify the state of the GPIO pin

Once we have access to the GpioOutputPin instance, it’s trivial to change the state of the pin to turn the LED on or off. You can modify ‘Timer_Tick’ to do this.

To turn the LED on, simply set the value of the pin to GpioPinValue.Low:

this.outPin.Value = GpioPinValue.Low;

and of course, set it to GpioPinValue.High to turn the LED off:

this.outPin.Value = GpioPinValue.High;

Remember that we connected the other end of the LED to the 3.3 Volts power supply, so we need to drive the pin to low to have current flow into the LED.

Questions/Suggestions

As always, for questions and feedback, contact us.