Here's the final version of MainPage.xaml.cs
There are other source code files in the BlinkyHeaded project, but they are automatically generated by Visual Studio, and we don't need to be concerned with them now.
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Devices.Gpio; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace BlinkyHeaded { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> /// public sealed partial class MainPage : Page { private DispatcherTimer timer; private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red); private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray); private const int LED_PIN = 5; private GpioPin pin; private GpioPinValue pinValue; public MainPage() { this.InitializeComponent(); timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(500); timer.Tick += Timer_Tick; InitGPIO(); if (pin != null) { timer.Start(); } } private void InitGPIO() { GpioController gpio = GpioController.GetDefault(); // Show an error if there is no GPIO controller if (gpio == null) { pin = null; GpioStatus.Text = "No GPIO controller on device."; return; } pin = gpio.OpenPin(LED_PIN); pinValue = GpioPinValue.High; pin.Write(pinValue); pin.SetDriveMode(GpioPinDriveMode.Output); GpioStatus.Text = "GPIO pin initialized."; } private void Timer_Tick(object sender, object e) { if (pinValue == GpioPinValue.High) { pinValue = GpioPinValue.Low; pin.Write(pinValue); LED.Fill = grayBrush; } else { pinValue = GpioPinValue.High; pin.Write(pinValue); LED.Fill = redBrush; } } } }
Page last edited July 15, 2016
Text editor powered by tinymce.