HaraldHeide / SunfounderSuperKitRaspburryPiC-

CSharp code for examples in SUnfounder Super Kit 3.0 RaspburryPi 3B+
0 stars 0 forks source link

01_blinkLed_CSharp.MainPage MainPage.xaml #1

Open HaraldHeide opened 6 years ago

HaraldHeide commented 6 years ago

<Page x:Class="_01_blinkLed_CSharp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:_01_blinkLed_CSharp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <Ellipse x:Name="LED" Fill="LightGray" Stroke="White" Width="100" Height="100" Margin="10"/>
        <TextBlock x:Name="DelayText" Text="500ms" Margin="10" TextAlignment="Center" FontSize="26.667" />
        <TextBlock x:Name="GpioStatus" Text="Waiting to initialize GPIO..." Margin="10,50,10,10" TextAlignment="Center" FontSize="26.667" />
    </StackPanel>
</Grid>

HaraldHeide commented 6 years ago

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 https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace _01_blinkLed_CSharp {

public sealed partial class MainPage : Page
{
    private const int LED_PIN = 17;
    private GpioPin pin;
    private GpioPinValue pinValue;
    private DispatcherTimer timer;
    private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);
    private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);

    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()
    {
        var gpio = GpioController.GetDefault();

        // Show an error if there is no GPIO controller
        if (gpio == null)
        {
            pin = null;
            GpioStatus.Text = "There is no GPIO controller on this device.";
            return;
        }

        pin = gpio.OpenPin(LED_PIN);
        pinValue = GpioPinValue.High;
        pin.Write(pinValue);
        pin.SetDriveMode(GpioPinDriveMode.Output);

        GpioStatus.Text = "GPIO pin initialized correctly.";

    }

    private void Timer_Tick(object sender, object e)
    {
        if (pinValue == GpioPinValue.High)
        {
            pinValue = GpioPinValue.Low;
            pin.Write(pinValue);
            LED.Fill = redBrush;
        }
        else
        {
            pinValue = GpioPinValue.High;
            pin.Write(pinValue);
            LED.Fill = grayBrush;
        }
    }
}

}