首页 > 代码库 > BlinkLED 点亮第一个LED灯(C#)

BlinkLED 点亮第一个LED灯(C#)

界面:

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBox Name="infoText" Text="Ready" FontSize="50" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBox>
            <Button Name="submitAction" FontSize="50" HorizontalAlignment="Center" VerticalAlignment="Center" Click="submitAction_Click">Exit</Button>
        </StackPanel>

代码:

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;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace MyIoT
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private GpioPin pin4;
        private GpioPin pin5Input;
        private DispatcherTimer timer1;
        public MainPage()
        {
            this.InitializeComponent();

            GpioController gpio = GpioController.GetDefault();
            pin4 = gpio.OpenPin(4);
            pin5Input = gpio.OpenPin(5);
            pin4.SetDriveMode(GpioPinDriveMode.Output);
            pin5Input.SetDriveMode(GpioPinDriveMode.Input);

            timer1 = new DispatcherTimer();
            timer1.Interval = TimeSpan.FromMilliseconds(500);
            timer1.Tick += Timer1_Tick;
            timer1.Start();

            
        }

        private void Timer1_Tick(object sender, object e)
        {
            GpioPinValue value = pin5Input.Read();

            if (value =http://www.mamicode.com/= GpioPinValue.High)
            {
                pin4.Write(GpioPinValue.High);
                this.infoText.Text = "pin5Input is High";
            }
            else if (value =http://www.mamicode.com/= GpioPinValue.Low)
            {
                pin4.Write(GpioPinValue.Low);
                this.infoText.Text = "pin5Input is Low";
            }
        }

        private void submitAction_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Exit();
        }
    }
}

 

记得要添加:Windows IoT Extensions for the UWP 引用。

 

BlinkLED 点亮第一个LED灯(C#)