Yazılım

C# UWP – Raspberry Pi Real Time GPIO

Merhabalar;
Bu yazımda C# Universal Windows Application ile Windows 10 IOT kurulu bir Raspberry Pi cihazında GPIO pinlerinin durumlarını gerçek zamanlı olarak dinleyen ve ekranda gösteren bir uygulama nasıl yapılır bunu anlatmaya çalışacağım.

Bu yazıda yaptığımız projeyi Github sayfandan indirebilirsiniz.

Raspberry Pi pinlerini IoT projelerinde giriş-çıkışlarda devamlı olarak kullanıyoruz, bu nedenle bu pinleri dinlemeyi ve her platformda kullanabilmeyi öğrenmek çok önemli.

UWP Projesi Oluşturma;

Öncelikle Visual Studio ile bir Windows Universal Application projesi oluşturarak işleme başlıyoruz;

UWP Tasarım;

Projemiz oluşturulduktan sonra MainPage.xaml dosyamızda <grid> tagları arasında aşağıdaki kodları yazıyoruz, öncesinde tasarım ekranımızın boyutunu da ayarlamamız lazım ben Raspberry Pi orjinal 7″ ekranı kullandığım için buna en yakın çözünürlük olan 10″ IoT Device (1024*768) çözünürlüğünü seçtim.

 <Grid x:Name="MainGrid" Background="#FF0C0B0B" Margin="10,0,0,0">
        <Ellipse x:Name="OGPIO04" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="40,211,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO05" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="145,211,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO06" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="250,211,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO12" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="355,211,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO13" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="460,211,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO16" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="565,211,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO17" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="670,211,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO18" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="775,211,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO19" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="880,211,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO20" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="91,466,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO21" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="196,466,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO22" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="301,466,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO23" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="406,466,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO24" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="511,466,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO25" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="616,466,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO26" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="721,466,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <Ellipse x:Name="OGPIO27" Fill="LightGray" HorizontalAlignment="Left" Height="100" Margin="826,466,0,0" Stroke="Black" VerticalAlignment="Top" Width="100"/>
        <TextBlock x:Name="Gpio04Status" Text="Boşta" Margin="40,316,879,411" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio05Status" Text="Boşta" Margin="150,316,769,411" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio06Status" Text="Boşta" Margin="255,316,664,411" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio12Status" Text="Boşta" Margin="360,316,559,411" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio13Status" Text="Boşta" Margin="465,316,454,411" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio16Status" Text="Boşta" Margin="570,316,349,411" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio17Status" Text="Boşta" Margin="675,316,244,411" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio18Status" Text="Boşta" Margin="780,316,139,411" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio19Status" Text="Boşta" Margin="885,316,34,411" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio20Status" Text="Boşta" Margin="91,571,828,156" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio21Status" Text="Boşta" Margin="196,571,723,156" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio22Status" Text="Boşta" Margin="301,571,618,156" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio23Status" Text="Boşta" Margin="406,571,513,156" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio24Status" Text="Boşta" Margin="511,571,408,156" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio25Status" Text="Boşta" Margin="616,571,303,156" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio26Status" Text="Boşta" Margin="721,571,198,156" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Gpio27Status" Text="Boşta" Margin="826,571,93,156" TextAlignment="Center" FontSize="26.667" Foreground="#FFCEC7EC" />
        <TextBlock x:Name="Baslik" Text="GPIO Control" Margin="73,73,69,602" TextAlignment="Center" FontSize="72" Foreground="#FFCEC7EC" FontFamily="Calibri" />
        <TextBlock x:Name="text04" HorizontalAlignment="Left" Margin="64,229,0,0" TextWrapping="Wrap" Text="04" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text05" HorizontalAlignment="Left" Margin="171,229,0,0" TextWrapping="Wrap" Text="05" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text06" HorizontalAlignment="Left" Margin="276,229,0,0" TextWrapping="Wrap" Text="06" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text12" HorizontalAlignment="Left" Margin="378,229,0,0" TextWrapping="Wrap" Text="12" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text13" HorizontalAlignment="Left" Margin="483,229,0,0" TextWrapping="Wrap" Text="13" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text16" HorizontalAlignment="Left" Margin="588,229,0,0" TextWrapping="Wrap" Text="16" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text17" HorizontalAlignment="Left" Margin="693,229,0,0" TextWrapping="Wrap" Text="17" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text18" HorizontalAlignment="Left" Margin="798,229,0,0" TextWrapping="Wrap" Text="18" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text19" HorizontalAlignment="Left" Margin="903,229,0,0" TextWrapping="Wrap" Text="19" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text20" HorizontalAlignment="Left" Margin="115,486,0,0" TextWrapping="Wrap" Text="20" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text21" HorizontalAlignment="Left" Margin="220,486,0,0" TextWrapping="Wrap" Text="21" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text22" HorizontalAlignment="Left" Margin="325,486,0,0" TextWrapping="Wrap" Text="22" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text23" HorizontalAlignment="Left" Margin="433,486,0,0" TextWrapping="Wrap" Text="23" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text24" HorizontalAlignment="Left" Margin="535,486,0,0" TextWrapping="Wrap" Text="24" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text25" HorizontalAlignment="Left" Margin="640,486,0,0" TextWrapping="Wrap" Text="25" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text26" HorizontalAlignment="Left" Margin="745,486,0,0" TextWrapping="Wrap" Text="26" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="text27" HorizontalAlignment="Left" Margin="850,486,0,0" TextWrapping="Wrap" Text="27" VerticalAlignment="Top" Height="71" Width="62" FontSize="48" SelectionChanged="textBlock_SelectionChanged" FontFamily="Calibri"/>
        <TextBlock x:Name="GpioStatus" HorizontalAlignment="Left" Margin="413,666,0,0" TextWrapping="Wrap" Text="DURUM" VerticalAlignment="Top" Height="65" Width="190" FontSize="48" Foreground="#FF0C0B0B"/>


    </Grid>

Yukarıda ki kodda grid içerisin de her bir GPIO pini için bir elips oluşturduk ve fill ve yazı renklerini vs. tasarımsal ayarlamalarını yaptık ayrıca TextBlock oluşturduk ve “Boşta” yazarak tasarımımızı tamamladık.

Programımız içerisinde herhangi bir pin high olduğunda veya tersi durumda low olduğun da elip kırmızı olacak ve çekildi yazacak.

Kodluyoruz;

Şimdi programımızı yazalım, aşağıda kodları Main.xaml.cs dosyamıza ekliyoruz, yazının devamın da neyi nasıl yaptığımızı açıklayacağım.

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();
            InitGPIO();

        }
        private void InitGPIO()
        {
            var gpio = GpioController.GetDefault();

            // GPIO denetleyici yoksa hata uyarı göster
            if (gpio == null)
            {
                GpioStatus.Text = "Denetleyici Yok";
                GpioStatus.Foreground = redBrush;
                return;
            }

            // GPIO Pinleri aç
            //     GPIO02pin = gpio.OpenPin(GPIO02);
            //     GPIO03pin = gpio.OpenPin(GPIO03);
            GPIO04pin = gpio.OpenPin(GPIO04);
            GPIO05pin = gpio.OpenPin(GPIO05);
            GPIO06pin = gpio.OpenPin(GPIO06);
            //      GPIO07pin = gpio.OpenPin(GPIO07);
            //      GPIO08pin = gpio.OpenPin(GPIO08);
            //      GPIO09pin = gpio.OpenPin(GPIO09);
            //      GPIO10pin = gpio.OpenPin(GPIO10);
            //       GPIO11pin = gpio.OpenPin(GPIO11);
            GPIO12pin = gpio.OpenPin(GPIO12);
            GPIO13pin = gpio.OpenPin(GPIO13);
            //       GPIO14pin = gpio.OpenPin(GPIO14);
            //       GPIO15pin = gpio.OpenPin(GPIO15);
            GPIO16pin = gpio.OpenPin(GPIO16);
            GPIO17pin = gpio.OpenPin(GPIO17);
            GPIO18pin = gpio.OpenPin(GPIO18);
            GPIO19pin = gpio.OpenPin(GPIO19);
            GPIO20pin = gpio.OpenPin(GPIO20);
            GPIO21pin = gpio.OpenPin(GPIO21);
            GPIO22pin = gpio.OpenPin(GPIO22);
            GPIO23pin = gpio.OpenPin(GPIO23);
            GPIO24pin = gpio.OpenPin(GPIO24);
            GPIO25pin = gpio.OpenPin(GPIO25);
            GPIO26pin = gpio.OpenPin(GPIO26);
            GPIO27pin = gpio.OpenPin(GPIO27);




            // GPIO04 PULL CONTROL
            if (GPIO04pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO04pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO04pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO04pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO04pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO05 PULL CONTROL
            if (GPIO05pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO05pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO05pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO05pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO05pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";



            // GPIO06 PULL CONTROL
            if (GPIO06pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO06pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO06pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO06pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO06pin.ValueChanged += Pin_ValueChanged;

            //       GpioStatus.Text = "GPIO hatasız başlatıldı";




            // GPIO12 PULL CONTROL
            if (GPIO12pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO12pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO12pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO12pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO12pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO13 PULL CONTROL
            if (GPIO13pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO13pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO13pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO13pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO13pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";




            // GPIO16 PULL CONTROL
            if (GPIO16pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO16pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO16pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO16pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO16pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO17 PULL CONTROL
            if (GPIO17pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO17pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO17pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO17pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO17pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";




            // GPIO18 PULL CONTROL
            if (GPIO18pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO18pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO18pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO18pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO18pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO19 PULL CONTROL
            if (GPIO19pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO19pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO19pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO19pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO19pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO20 PULL CONTROL
            if (GPIO20pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO20pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO20pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO20pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO20pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO21 PULL CONTROL
            if (GPIO21pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO21pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO21pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO21pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO21pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO22 PULL CONTROL
            if (GPIO22pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO22pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO22pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO22pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO22pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO23 PULL CONTROL
            if (GPIO23pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO23pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO23pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO23pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO23pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO24 PULL CONTROL
            if (GPIO24pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO24pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO24pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO24pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO24pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO25 PULL CONTROL
            if (GPIO25pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO25pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO25pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO25pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO25pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO26 PULL CONTROL
            if (GPIO26pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO26pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO26pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO26pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO26pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";





            // GPIO27 PULL CONTROL
            if (GPIO27pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO27pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO27pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO27pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO27pin.ValueChanged += Pin_ValueChanged;

            //  GpioStatus.Text = "GPIO hatasız başlatıldı";



        }



        private void Pin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
        {
            // need to invoke UI updates on the UI thread because this event
            // handler gets invoked on a separate thread.
            var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
                var PinName = GetPinName(sender.PinNumber);
                var PinTextName = GetPinTextName(sender.PinNumber);
                var PinIcon = MainGrid.FindName(PinName) as Ellipse;
                var PinText = MainGrid.FindName(PinTextName) as TextBlock;

                var test = Gpio04Status;

                if (e.Edge == GpioPinEdge.FallingEdge)
                {
                    PinIcon.Fill = redBrush;
                    PinText.Text = "Çekiliyor";
                }
                else
                {
                    PinIcon.Fill = grayBrush;
                    PinText.Text = "Boşta";
                }
            });
        }

        private string GetPinName(int pinNumber)
        {
            return $"OGPIO{pinNumber:D2}";
        }

        private string GetPinTextName(int pinNumber)
        {
            return $"Gpio{pinNumber:D2}Status";
        }

        private const int GPIO04 = 4;
        private const int GPIO05 = 5;
        private const int GPIO06 = 6;
        private const int GPIO12 = 12;
        private const int GPIO13 = 13;
        private const int GPIO16 = 16;
        private const int GPIO17 = 17;
        private const int GPIO18 = 18;
        private const int GPIO19 = 19;
        private const int GPIO20 = 20;
        private const int GPIO21 = 21;
        private const int GPIO22 = 22;
        private const int GPIO23 = 23;
        private const int GPIO24 = 24;
        private const int GPIO25 = 25;
        private const int GPIO26 = 26;
        private const int GPIO27 = 27;
        private GpioPin GPIO04pin;
        private GpioPin GPIO05pin;
        private GpioPin GPIO06pin;
        private GpioPin GPIO12pin;
        private GpioPin GPIO13pin;
        private GpioPin GPIO16pin;
        private GpioPin GPIO17pin;
        private GpioPin GPIO18pin;
        private GpioPin GPIO19pin;
        private GpioPin GPIO20pin;
        private GpioPin GPIO21pin;
        private GpioPin GPIO22pin;
        private GpioPin GPIO23pin;
        private GpioPin GPIO24pin;
        private GpioPin GPIO25pin;
        private GpioPin GPIO26pin;
        private GpioPin GPIO27pin;
        private GpioPinValue PinValue = GpioPinValue.High;

        //    private DispatcherTimer timer;
        private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);
        private SolidColorBrush yellowBrush = new SolidColorBrush(Windows.UI.Colors.Yellow);
        private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);

        private void textBlock_SelectionChanged(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {

        }
    }

Programı çalıştırdığımız da aşağıdaki gibi açılacağını görebilirsiniz.

App Oluşturma;

Projemizde Sağ tuş Store ve Create Packages diyerek Windows IoT yüklü Raspberry Pi cihazımıza kurabileceğim paketi alabilir veya Raspberry Pi cihazımızın IP tanımlamarını vs. yaptıysak Debug Config den tanımlayarak direk cihazımızın üzerinde Debug ederek de çalıştırabiliriz.

Öncelikle Paket oluşturmayla ilerleyelim.

Paket oluşturması bittiğinde bize oluşturmayı yaptığı dizini gösterecektir, bu dizine gittiğimizde bir takım dosyalardan oluşan bir klasör bulacağız.

Şimdi Raspberry Pi cihazımızın Windows Device Portal ekranına giderek (http://Raspberry_Ip:8080) Apps manager alanında Add diyerek gerekli dosyaları ekliyor ve programımızı kuruyoruz.
Daha sonra listeden programımız yanında ki Actions seçeneği ile Start diyerek programımızı başlatabiliriz.

Install App

Yazdığımız programın başlangıçta otomatik olarak açılmasını istiyorsanız aynı ekranda Startup düğmesini seçili hale getirmeniz yeterli, böylece Raspberry Pi her açıldığında programımız otomatik olarak başlayacaktır.

Şimdi programımızı Raspberry Pi’de başlatalım ve pinleri çekelim çektiğiniz pine göre ekranda hangi pinin çekildiği anlık olarak gösterilecektir.

Kodlarda neler yaptığımız daha yakından inceleyelim;

Aşağıda ki kodda bir GPIO denetleyicisi bulamadıysa bunu kırmızı yazı ile tasarım sayfasında oluşturduğumuz GpioStatus textblock’un Textine yazıyoruz.

if (gpio == null)
            {
                GpioStatus.Text = "Denetleyici Yok";
                GpioStatus.Foreground = redBrush;
                return;
            }

Yine GPIO pinlerimizi açık hale getiriyoruz

GPIO04pin = gpio.OpenPin(GPIO04);
GPIO05pin = gpio.OpenPin(GPIO05);
GPIO06pin = gpio.OpenPin(GPIO06);
...

Bütün pinler için Pull kontrolleri yapıyoruz; böylece pinin çekilip çekilmediğini kontrol ediyoruz;

 if (GPIO04pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                GPIO04pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                GPIO04pin.SetDriveMode(GpioPinDriveMode.Input);

            // Butona basıldığında gürültüyü filitrelemek için zaman aşımı ayarla
            GPIO04pin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            GPIO04pin.ValueChanged += Pin_ValueChanged;

Daha sonra Pin_ValueChanged fonksiyonu ile çekildiğini anladığımız pinler için elipslerin rengini değiştiriyor, ve başka bir textblock textine çekiliyor yazdırıyoruz.

Burada yapılan projeye Github sayfamdan ulaşabilirsiniz.

Ali UYSAL

IT alanında 16 sene önce donanım ile başlayan tecrübem, network ve sonrasında iş analizi, yazılım geliştirme ve proje yöneticiliği alanlarında devam etmiştir. Endüstriyel yazılımlar, sahadan veri toplama ve analizleri, otomatik etiketleme ve barkod sistemleri, otomatik tartım ve robotik konularında tecrübe sahibiyim. Sanayi 4.0 kapsamında imalat sanayinin dijital dönüşümünde çok fazla projenin hayata geçmesini sağladım.Open Source projelerle uzun süre ilgilendim, analiz ve implementasyonu konularında tecrübe edindim. Bunlar dışında hobi amacıyla başlasam da sonradan ürüne dönüşen, geliştirme kartları ile farklı çalışmalarım olmuştur.Raspberry Pi üzerinde yaptığım donanımsal ve yazılımsal işler ile çok farklı ürünler ortaya çıkartarak tecrübe edindim.

İlgili Makaleler

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Başa dön tuşu