The classic LED blinking app running headless.
We’ll create a simple Blinky app and connect a LED to your MinnowBoard Max (or MBM for short).
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.
Telnet to your device and log in using the local system account (no username and no password)
Modify the registry to configure the headless state of your device.
To configure your device to be headless, run this command and reboot:
To return your device to headed mode, you can run this command and reboot:
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.
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)’.
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)
{
. . .
}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:
First, we use GpioController.GetDeviceSelector() to gather the deviceId for the GPIO_S5 set of pins.
Then we gather the device information about this pins with DeviceInformation.FindAllAsync().
Once we have the array of info, we can grab the controller for GPIO_S5 using GpioController.FromIdAsync().
Now that we have the controller, we can access the single GpioPinInfo we are interested in (which is pin 0) accessing the GpioController.Pins dictionary.
From the pinInfo, we call GpioPinInfo.TryOpenOutput() to open the pin in ‘output’ mode. We’ll use pinOut, which is of type GpioOutputPin, to 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.
As always, for questions and feedback, contact us.