Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices.WindowsRuntime;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.UI.Core;
- 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;
- using Windows.Devices.Gpio;
- //Szablon elementu Pusta strona jest udokumentowany na stronie https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x415
- namespace valueChangedTest
- {
- /// <summary>
- /// Pusta strona, która może być używana samodzielnie lub do której można nawigować wewnątrz ramki.
- /// </summary>
- public sealed partial class MainPage : Page
- {
- // Use GPIO pin 5 to set values
- private const int SET_PIN = 5;
- private GpioPin setPin;
- // Use GPIO pin 6 to listen for value changes
- private const int LISTEN_PIN = 27;
- private GpioPin listenPin;
- private GpioPinValue currentValue = GpioPinValue.High;
- private DispatcherTimer timer;
- public MainPage()
- {
- this.InitializeComponent();
- }
- private void Grid_Loaded(object sender, RoutedEventArgs e)
- {
- // Initialize the GPIO objects.
- var gpio = GpioController.GetDefault();
- // Set up our GPIO pin for setting values.
- // If this next line crashes with a NullReferenceException,
- // then the problem is that there is no GPIO controller on the device.
- setPin = gpio.OpenPin(SET_PIN);
- // Establish initial value and configure pin for output.
- setPin.Write(currentValue);
- setPin.SetDriveMode(GpioPinDriveMode.Output);
- // Set up our GPIO pin for listening for value changes.
- listenPin = gpio.OpenPin(LISTEN_PIN);
- // Configure pin for input and add ValueChanged listener.
- listenPin.SetDriveMode(GpioPinDriveMode.Input);
- listenPin.ValueChanged += Pin_ValueChanged;
- // Start toggling the pin value every 500ms.
- timer = new DispatcherTimer();
- timer.Interval = TimeSpan.FromMilliseconds(500);
- timer.Tick += Timer_Tick;
- timer.Start();
- }
- private void Pin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
- {
- // Report the change in pin value.
- var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
- {
- CurrentPinValue.Text = listenPin.Read().ToString();
- });
- }
- private void Timer_Tick(object sender, object e)
- {
- // Toggle the existing pin value.
- currentValue = (currentValue == GpioPinValue.High) ? GpioPinValue.Low : GpioPinValue.High;
- setPin.Write(currentValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment