zaguoba

simple ValueChanged example

Mar 9th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using Windows.Foundation;
  7. using Windows.Foundation.Collections;
  8. using Windows.UI.Core;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Controls;
  11. using Windows.UI.Xaml.Controls.Primitives;
  12. using Windows.UI.Xaml.Data;
  13. using Windows.UI.Xaml.Input;
  14. using Windows.UI.Xaml.Media;
  15. using Windows.UI.Xaml.Navigation;
  16.  
  17. using Windows.Devices.Gpio;
  18.  
  19. //Szablon elementu Pusta strona jest udokumentowany na stronie https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x415
  20.  
  21. namespace valueChangedTest
  22. {
  23.     /// <summary>
  24.     /// Pusta strona, która może być używana samodzielnie lub do której można nawigować wewnątrz ramki.
  25.     /// </summary>
  26.     public sealed partial class MainPage : Page
  27.     {
  28.         // Use GPIO pin 5 to set values
  29.         private const int SET_PIN = 5;
  30.         private GpioPin setPin;
  31.  
  32.         // Use GPIO pin 6 to listen for value changes
  33.         private const int LISTEN_PIN = 27;
  34.         private GpioPin listenPin;
  35.  
  36.         private GpioPinValue currentValue = GpioPinValue.High;
  37.         private DispatcherTimer timer;
  38.  
  39.         public MainPage()
  40.         {
  41.             this.InitializeComponent();
  42.         }
  43.  
  44.         private void Grid_Loaded(object sender, RoutedEventArgs e)
  45.         {
  46.             // Initialize the GPIO objects.
  47.             var gpio = GpioController.GetDefault();
  48.  
  49.             // Set up our GPIO pin for setting values.
  50.             // If this next line crashes with a NullReferenceException,
  51.             // then the problem is that there is no GPIO controller on the device.
  52.             setPin = gpio.OpenPin(SET_PIN);
  53.  
  54.             // Establish initial value and configure pin for output.
  55.             setPin.Write(currentValue);
  56.             setPin.SetDriveMode(GpioPinDriveMode.Output);
  57.  
  58.             // Set up our GPIO pin for listening for value changes.
  59.             listenPin = gpio.OpenPin(LISTEN_PIN);
  60.  
  61.             // Configure pin for input and add ValueChanged listener.
  62.             listenPin.SetDriveMode(GpioPinDriveMode.Input);
  63.             listenPin.ValueChanged += Pin_ValueChanged;
  64.  
  65.             // Start toggling the pin value every 500ms.
  66.             timer = new DispatcherTimer();
  67.             timer.Interval = TimeSpan.FromMilliseconds(500);
  68.             timer.Tick += Timer_Tick;
  69.             timer.Start();
  70.         }
  71.  
  72.         private void Pin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
  73.         {
  74.             // Report the change in pin value.
  75.             var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  76.             {
  77.                 CurrentPinValue.Text = listenPin.Read().ToString();
  78.             });
  79.         }
  80.  
  81.         private void Timer_Tick(object sender, object e)
  82.         {
  83.             // Toggle the existing pin value.
  84.             currentValue = (currentValue == GpioPinValue.High) ? GpioPinValue.Low : GpioPinValue.High;
  85.             setPin.Write(currentValue);
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment