Blinky is simple app to blink an LED that is connected to your Windows IoT Core device. The source code and documentation for this app is available in C++ or C# from the link here. But in this tutorial here we'll create this same app in VB.NET. This app does not have a UI and will run as a background service in your Winows IoT device, which can be Raspberry Pi 2 or 3, MinnowBoard Max or DragonBoard.
Imports Windows.ApplicationModel.Background Imports Windows.Devices.Gpio Imports Windows.System.Threading Public NotInheritable Class StartupTask Implements IBackgroundTask Const LED_PIN As Short = 5 'GPIO Pin(5) = Physical Pin(29) = WiringPi Pin (21) Const INTERVAL As Short = 2 'Interval in seconds Private gpio Private pin As GpioPin Private pinValue As GpioPinValue Private blinktimer As ThreadPoolTimer Private cancelled As Boolean = False Private deferral As BackgroundTaskDeferral = Nothing Public Sub Run(taskInstance As IBackgroundTaskInstance) Implements IBackgroundTask.Run 'Handler for cancelled background task AddHandler taskInstance.Canceled, New BackgroundTaskCanceledEventHandler(AddressOf onCanceled) 'Background task continues to run even after IBackgroundTask.Run returns deferral = taskInstance.GetDeferral() 'Initialize GPIO initGPIO() 'Create a periodic timer blinktimer = ThreadPoolTimer.CreatePeriodicTimer(New TimerElapsedHandler(AddressOf blinktimer_tick), TimeSpan.FromSeconds(INTERVAL)) End Sub Private Sub onCanceled(sender As IBackgroundTaskInstance, reason As BackgroundTaskCancellationReason) cancelled = True End Sub Sub blinktimer_tick(state As Object) If Not cancelled Then 'Toggele Pin Value If (pinValue = GpioPinValue.High) Then pinValue = GpioPinValue.Low Else pinValue = GpioPinValue.High End If pin.Write(pinValue) Else 'if the task is cancelled 'Stop the timer And finish execution blinktimer.Cancel() deferral.Complete() End If End Sub Private Sub initGPIO() 'Get the default GPIO controller for the system gpio = GpioController.GetDefault() If Not gpio Is Nothing Then 'Open a connection to the GPIO Pin pin = gpio.openpin(LED_PIN) pinValue = GpioPinValue.Low 'Write Pin value low to turn on LED pin.Write(pinValue) 'Set the pin for output pin.SetDriveMode(GpioPinDriveMode.Output) End If End Sub End Class
The namespace Windows.Devices.gpio contains the necessary classes that allow you to read the current status and switch on/off the GPIO pins from the app.
AddHandler taskInstance.Canceled, New BackgroundTaskCanceledEventHandler(AddressOf onCanceled)
Enable the app to recognize cancellation request that is made against your app.
deferral = taskInstance.GetDeferral()
Toggling the LED on/off is performed inside an timer which runs asynchronously. The above line tells the system not to stop timer task when the Run method is finished.
This is the function that initializes the GPIO. It will get the GPIO controller for the system and opens GPIO pin 5.
blinktimer = ThreadPoolTimer.CreatePeriodicTimer(New TimerElapsedHandler(AddressOf blinktimer_tick), TimeSpan.FromSeconds(INTERVAL))
Create a periodic timer that calls the
blinktimer_tick function every 2 seconds. You can adjust the blink speed by changing the constant value INTERVAL
Sub blinktimer_tick(state As Object)
Inside this function first we check if a cancel request was recieved in which case the timer is stopped and the app execution is completed. Otherwise toggle the pin value to high or low, there by changing the LED off and on respectively.