Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright(c) Microsoft Open Technologies, Inc. All rights reserved.
- The MIT License(MIT)
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files(the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions :
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- using System;
- using Windows.Devices.Gpio;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Media;
- namespace Blinky
- {
- public sealed partial class MainPage : Page
- {
- public MainPage()
- {
- InitializeComponent();
- timer = new DispatcherTimer();
- timer.Interval = TimeSpan.FromMilliseconds(500);
- timer.Tick += Timer_Tick;
- timer.Start();
- Unloaded += MainPage_Unloaded;
- InitGPIO();
- }
- 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);
- pin.Write(GpioPinValue.High);
- pin.SetDriveMode(GpioPinDriveMode.Output);
- GpioStatus.Text = "GPIO pin initialized correctly.";
- }
- private void MainPage_Unloaded(object sender, object args)
- {
- // Cleanup
- pin.Dispose();
- }
- private void FlipLED()
- {
- if (LEDStatus == 0)
- {
- LEDStatus = 1;
- if (pin != null)
- {
- // to turn on the LED, we need to push the pin 'low'
- pin.Write(GpioPinValue.Low);
- }
- LED.Fill = redBrush;
- }
- else
- {
- LEDStatus = 0;
- if (pin != null)
- {
- pin.Write(GpioPinValue.High);
- }
- LED.Fill = grayBrush;
- }
- }
- private void TurnOffLED()
- {
- if (LEDStatus == 1)
- {
- FlipLED();
- }
- }
- private void Timer_Tick(object sender, object e)
- {
- FlipLED();
- }
- private void Delay_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
- {
- if (timer == null)
- {
- return;
- }
- if (e.NewValue == Delay.Minimum)
- {
- DelayText.Text = "Stopped";
- timer.Stop();
- TurnOffLED();
- }
- else
- {
- DelayText.Text = e.NewValue + "ms";
- timer.Interval = TimeSpan.FromMilliseconds(e.NewValue);
- timer.Start();
- }
- }
- private int LEDStatus = 0;
- private const int LED_PIN = 5;
- private GpioPin pin;
- private DispatcherTimer timer;
- private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);
- private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement