Advertisement
aznGiLL

Raspberry Pi 2 IoT LED Demo

Oct 5th, 2015
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1.        private const int LED_PIN = 6;
  2.         private const int PB_PIN = 5;
  3.         private GpioPin pin;
  4.         private GpioPin pushButton;
  5.         private DispatcherTimer timer;
  6.         private GpioPinValue pushButtonValue;
  7.  
  8.         public MainPage()
  9.         {
  10.             InitializeComponent();
  11.  
  12.             timer = new DispatcherTimer();
  13.             timer.Interval = TimeSpan.FromMilliseconds(1);
  14.             timer.Tick += Timer_Tick;
  15.             timer.Start();
  16.  
  17.             Unloaded += MainPage_Unloaded;
  18.  
  19.             InitGPIO();
  20.         }
  21.  
  22.         private void InitGPIO()
  23.         {
  24.             var gpio = GpioController.GetDefault();
  25.  
  26.             if (gpio == null)
  27.             {
  28.                 pin = null;
  29.                 return;
  30.             }
  31.             pushButton = gpio.OpenPin(PB_PIN);
  32.             pin = gpio.OpenPin(LED_PIN);
  33.  
  34.             pushButton.SetDriveMode(GpioPinDriveMode.Input);
  35.             pin.Write(GpioPinValue.Low);
  36.             pin.SetDriveMode(GpioPinDriveMode.Output);
  37.  
  38.         }
  39.  
  40.         private void MainPage_Unloaded(object sender, object args)
  41.         {
  42.             pin.Dispose();
  43.             pushButton.Dispose();
  44.         }
  45.  
  46.         private void FlipLED()
  47.         {
  48.             pushButtonValue = pushButton.Read();
  49.             if (pushButtonValue == GpioPinValue.High)
  50.             {
  51.                 pin.Write(GpioPinValue.High);
  52.             }
  53.             else if (pushButtonValue == GpioPinValue.Low)
  54.             {
  55.                 pin.Write(GpioPinValue.Low);
  56.             }
  57.         }
  58.  
  59.         private void Timer_Tick(object sender, object e)
  60.         {
  61.             FlipLED();
  62.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement