Skip to content

Instantly share code, notes, and snippets.

@crpietschmann
Last active August 1, 2018 00:05
Show Gist options
  • Save crpietschmann/cd7e86b620afa07bcd6d3472ba1ceee4 to your computer and use it in GitHub Desktop.
Save crpietschmann/cd7e86b620afa07bcd6d3472ba1ceee4 to your computer and use it in GitHub Desktop.
Sample code for the simple #IoT Police lights demo using Windows 10 IoT Core and a Raspberry Pi 3 with a couple LEDs https://twitter.com/BuildIoTCore/status/756542703786229764
/// License: Public Domain
/// Written by: Chris Pietschmann
/// http://pietschsoft.com
/// http://BuildAzure.com
/// http://BuildIoTCore.com
///
/// This is the code behind the simple "#IoT police lights" shown
/// in the video at the following URL:
/// https://twitter.com/BuildIoTCore/status/756542703786229764
///
using System;
using Windows.Devices.Gpio;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace PiIoTPolice
{
public sealed partial class MainPage : Page
{
DispatcherTimer timer;
GpioPin pin;
GpioPinValue pinValue;
GpioPin pin2;
GpioPinValue pin2Value;
Brush onBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(0, 0, 0, 255));
Brush offBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(0, 0, 0, 0));
const int LED_PIN = 5;
const int LED_PIN2 = 4;
public MainPage()
{
//this.InitializeComponent();
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Tick += Timer_Tick;
InitGPIO();
if (pin != null)
{
timer.Start();
}
}
private void Timer_Tick(object sender, object e)
{
if (pinValue == GpioPinValue.High)
{
pinValue = GpioPinValue.Low;
pin.Write(pinValue);
//LED.Background = onBrush;
}
else
{
pinValue = GpioPinValue.High;
pin.Write(pinValue);
//LED.Background = offBrush;
}
if (pin2Value == GpioPinValue.High)
{
pin2Value = GpioPinValue.Low;
pin2.Write(pin2Value);
}
else
{
pin2Value = GpioPinValue.High;
pin2.Write(pin2Value);
}
}
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);
pin2 = gpio.OpenPin(LED_PIN2);
pin2Value = GpioPinValue.Low;
pin2.Write(pin2Value);
pin2.SetDriveMode(GpioPinDriveMode.Output);
//GpioStatus.Text = "GPIO pin initialized correctly.";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment