Advertisement
Guest User

Untitled

a guest
May 21st, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.01 KB | None | 0 0
  1. /********************************************************************************************************************************
  2.  *
  3.  *                                              EMBEDDED SYSTEMS LABORATORY 2016
  4.  *
  5.  *  Sample LED control program for students laboratory purposes.
  6.  *  Presents the Raspberry Pi 2/3 IoT ability to control
  7.  *  GPIO output pins blinking LEDs.
  8.  *
  9.  *
  10.  *        Raspberry Pi GPIO Layout - Model B+
  11.  *  1 - 3V3     Power                      2 - 5V Power
  12.  *  3 - GPIO2   SDA1 I2C                   4 - 5V Power
  13.  *  5 - GPIO3   SCL1 I2C                   6 - GND
  14.  *  7 - GPIO4                              8 - GPIO14   UART0_TXD
  15.  *  9 - GND                               10 - GPIO15   UART0_RXD
  16.  * 11 - GPIO17                            12 - GPIO18   PCM_CLK
  17.  * 13 - GPIO27                            14 - GND
  18.  * 15 - GPIO22                            16 - GPIO23
  19.  * 17 - 3V3     Power                     18 - GPIO24
  20.  * 19 - GPIO10  SPI0_MOSI                 20 - GND
  21.  * 21 - GPIO9   SPI0_SCLK                 22 - GPIO25
  22.  * 23 - GPIO11                            24 - GPIO8    SPI0_CE0_N
  23.  * 25 - GND                               26 - GPIO7    SPI0_CE1_N
  24.  * 27 - ID_SD   I2C ID EEPROM             28 - ID_SC    I2C ID EEPROM
  25.  * 29 - GPIO5                             30 - GND
  26.  * 31 - GPIO6                             32 - GPIO12
  27.  * 33 - GPIO13                            34 - GND
  28.  * 35 - GPIO19                            36 - GPIO16
  29.  * 37 - GPIO26                            38 - GPIO20
  30.  * 39 - GND                               40 - GPIO21
  31.  *
  32.  ********************************************************************************************************************************/
  33.  
  34. using System;
  35. using Windows.Devices.Gpio;
  36. using Windows.UI.Xaml;
  37. using Windows.UI.Xaml.Controls;
  38. using Windows.UI.Xaml.Media;
  39.  
  40. namespace LEDTest
  41. {
  42.   public sealed partial class MainPage : Page
  43.   {
  44.     // Raspberry Pi GPIO pins used for LEDs coontrol
  45.     private const int LED_PIN1 = 4;   // GPIO04
  46.     private const int LED_PIN2 = 23;  // GPIO23
  47.     private const int LED_PIN3 = 24;  // GPIO24
  48.     private const int LED_PIN4 = 12;  // GPIO12
  49.  
  50.     private GpioPin pin1;
  51.     private GpioPin pin2;
  52.     private GpioPin pin3;
  53.     private GpioPin pin4;
  54.  
  55.     // Raspberry Pi GPIO Pin variables for blinking effect
  56.     private GpioPinValue[] pinValue1 = new GpioPinValue[10];
  57.     private GpioPinValue[] pinValue2 = new GpioPinValue[10];
  58.     private GpioPinValue[] pinValue3 = new GpioPinValue[10];
  59.     private GpioPinValue[] pinValue4 = new GpioPinValue[10];
  60.  
  61.     private DispatcherTimer timer;
  62.     private SolidColorBrush redBrush  = new SolidColorBrush(Windows.UI.Colors.Red);
  63.     private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);
  64.     private int Counter = 0;
  65.  
  66.     public MainPage() {
  67.       InitLEDs();                                           // Initialize LEDs blinking procedure
  68.       InitializeComponent();                                // Initialize XAML Display view
  69.  
  70.       timer = new DispatcherTimer();                        // define the timer
  71.       timer.Interval = TimeSpan.FromMilliseconds(500);      // set callback timer to execute each 500 ms
  72.       timer.Tick += Timer_Tick;                             // register callback procedure
  73.       InitGPIO();                                           // Initialze GPIO pins
  74.  
  75.       if ((pin1 != null) && (pin2 != null) && (pin3 != null) && (pin4 != null)) {
  76.         timer.Start();                                      // start the timer
  77.       }        
  78.     }
  79.     // Initial LEDs state 8 stages
  80.     void InitLEDs()
  81.     {
  82.       pinValue1[0] = GpioPinValue.High;                     // pins value state 0
  83.       pinValue2[0] = GpioPinValue.Low;
  84.       pinValue3[0] = GpioPinValue.Low;
  85.       pinValue4[0] = GpioPinValue.Low;
  86.  
  87.       pinValue1[1] = GpioPinValue.Low;                     // pins value state 1
  88.       pinValue2[1] = GpioPinValue.High;
  89.       pinValue3[1] = GpioPinValue.Low;
  90.       pinValue4[1] = GpioPinValue.Low;
  91.  
  92.       pinValue1[2] = GpioPinValue.Low;                     // pins value state 2
  93.       pinValue2[2] = GpioPinValue.Low;
  94.       pinValue3[2] = GpioPinValue.High;
  95.       pinValue4[2] = GpioPinValue.Low;
  96.  
  97.       pinValue1[3] = GpioPinValue.Low;                     // pins value state 3
  98.       pinValue2[3] = GpioPinValue.Low;
  99.       pinValue3[3] = GpioPinValue.Low;
  100.       pinValue4[3] = GpioPinValue.High;
  101.  
  102.       pinValue1[4] = GpioPinValue.Low;                      // pins value state 4
  103.       pinValue2[4] = GpioPinValue.Low;
  104.       pinValue3[4] = GpioPinValue.Low;
  105.       pinValue4[4] = GpioPinValue.Low;
  106.  
  107.             pinValue1[5] = GpioPinValue.Low;                     // pins value state 3
  108.             pinValue2[5] = GpioPinValue.Low;
  109.             pinValue3[5] = GpioPinValue.Low;
  110.             pinValue4[5] = GpioPinValue.High;
  111.  
  112.             pinValue1[6] = GpioPinValue.Low;                     // pins value state 2
  113.             pinValue2[6] = GpioPinValue.Low;
  114.             pinValue3[6] = GpioPinValue.High;
  115.             pinValue4[6] = GpioPinValue.Low;
  116.  
  117.             pinValue1[7] = GpioPinValue.Low;                     // pins value state 1
  118.             pinValue2[7] = GpioPinValue.High;
  119.             pinValue3[7] = GpioPinValue.Low;
  120.             pinValue4[7] = GpioPinValue.Low;
  121.  
  122.             pinValue1[8] = GpioPinValue.High;                     // pins value state 0
  123.             pinValue2[8] = GpioPinValue.Low;
  124.             pinValue3[8] = GpioPinValue.Low;
  125.             pinValue4[8] = GpioPinValue.Low;
  126.  
  127.             pinValue1[9] = GpioPinValue.Low;                      // pins value state 4
  128.             pinValue2[9] = GpioPinValue.Low;
  129.             pinValue3[9] = GpioPinValue.Low;
  130.             pinValue4[9] = GpioPinValue.Low;
  131.         }
  132.     // Initialize device GPIO. All LEDs control pins set to the output state
  133.     private void InitGPIO()
  134.     {
  135.       var GPIO = GpioController.GetDefault();
  136.  
  137.       // Show an error if there is no GPIO controller
  138.       if (GPIO == null) {
  139.         pin1 = null;
  140.         pin2 = null;
  141.         pin3 = null;
  142.         pin4 = null;
  143.  
  144.         Time.Text = "There is no GPIO controller on this device.";
  145.         return;
  146.       }
  147.       pin1 = GPIO.OpenPin(LED_PIN1);
  148.       pin2 = GPIO.OpenPin(LED_PIN2);
  149.       pin3 = GPIO.OpenPin(LED_PIN3);
  150.       pin4 = GPIO.OpenPin(LED_PIN4);
  151.  
  152.       pin1.Write(pinValue1[0]);                     // set pin1 state LOW/HIGH
  153.       pin1.SetDriveMode(GpioPinDriveMode.Output);
  154.       pin2.Write(pinValue2[0]);                     // set pin2 state LOW/HIGH
  155.       pin2.SetDriveMode(GpioPinDriveMode.Output);
  156.       pin3.Write(pinValue3[0]);                     // set pin3 state LOW/HIGH
  157.       pin3.SetDriveMode(GpioPinDriveMode.Output);
  158.       pin4.Write(pinValue4[0]);                     // set pin4 state LOW/HIGH
  159.       pin4.SetDriveMode(GpioPinDriveMode.Output);
  160.  
  161.       Time.Text = "GPIO pins initialized correctly.";
  162.     }
  163.     // XAML Timer callback function set to be executed each 500 ms
  164.     private void Timer_Tick(object sender, object e) {
  165.       LED1.Fill = pinValue1[Counter] == GpioPinValue.High ? redBrush : grayBrush;   // Draw LED1 elipse Red/Gray = High/Low
  166.       pin1.Write(pinValue1[Counter]);                                               // Set pin1 value High/Low
  167.       LED2.Fill = pinValue2[Counter] == GpioPinValue.High ? redBrush : grayBrush;   // Draw LED2 elipse Red/Gray = High/Low
  168.       pin2.Write(pinValue2[Counter]);                                               // Set pin2 value High/Low
  169.       LED3.Fill = pinValue3[Counter] == GpioPinValue.High ? redBrush : grayBrush;   // Draw LED3 elipse Red/Gray = High/Low
  170.       pin3.Write(pinValue3[Counter]);                                               // Set pin3 value High/Low
  171.       LED4.Fill = pinValue4[Counter] == GpioPinValue.High ? redBrush : grayBrush;   // Draw LED4 elipse Red/Gray = High/Low
  172.       pin4.Write(pinValue4[Counter]);                                               // Set pin4 value High/Low
  173.       Counter++;
  174.       if (Counter > 9)
  175.         Counter = 0;
  176.     }
  177.   }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement