简体   繁体   中英

ABElecrtonics_Win10IOT_Libraries in C# UWP?

Can someone help me understand how I could light a led and read the status of a button using the IoPiPlus extension board in C# UWP with ABElecrtonics_Win10IOT_Libraries? All the tutorials I've found are for python. Thank you.

The IO Pi Plus is a 32 channel digital expansion board designed for use on the Raspberry Pi. It can be controlled via the Raspberry Pi I2C port. GPIO #2&#3 perform I2C. Here is the Raspberry Pi 2 & 3 Pin Mappings .

Please refer to AB Electronics UK Windows 10 IOT Libraries and Demos. You can set the enable pin to use the output enable functions and the enable and disable the output.

ABElectronics_Win10IOT_Libraries.ServoPi servo = new ABElectronics_Win10IOT_Libraries.ServoPi(0x40);

servo.OutputEnablePin = 17; // set to GPIO pin 17 to control
servo.OutputEnable();       //set Gpio Low
servo.OutputDisable();      //set Gpio High

Meanwhile, I managed to control two LEDs in both software and hardware using two buttons. Below I will put the code, it will probably be useful for someone else

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Threading;
using ABElectronics_Win10IOT_Libraries;
using Windows.UI.Popups;

namespace IoPiPlust_start_led
{
    public sealed partial class MainPage : Page
    {
        int x = 0;
        int y = 0;
        public IOPi bus1 = new IOPi(0x20);
        public IOPi bus2 = new IOPi(0x21);
        int TIME_INTERVAL_IN_MILLISECONDS = 100;
        Timer _timer1;

        public MainPage()
        {
            this.InitializeComponent();
            conect();
            _timer1 = new Timer(Timer1_Tick, null, TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite);
        }

        public async void conect()
        {
            bus1.Connected += Bus1_Connected;
            bus2.Connected += Bus2_Connected;
            await bus1.Connect();
            await bus2.Connect();
            bus2.SetPortDirection(0, 0x00);
            bus1.SetPortDirection(0, 0xFF);
            bus1.SetPortPullups(0, 0xFF);
            bus1.InvertPort(0, 0xFF);
        }

        private void Bus2_Connected(object sender, EventArgs e)
        {
            bus2.SetPortDirection(0, 0x00);
        }

        private void Bus1_Connected(object sender, EventArgs e)
        {
            ReadBus1();
            _timer1.Change(TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite);
        }

        private void Timer1_Tick(Object state)
        {
            if (bus1.IsConnected)
            {
                ReadBus1();
                if (bus2.IsConnected)
                {
                    if (bus1.ReadPin(1))
                    { bus2.WritePin(1, true); }
                    if (bus1.ReadPin(2))
                    { bus2.WritePin(2, true); }
                    if (!bus1.ReadPin(1))
                    { bus2.WritePin(1, false); }
                    if (!bus1.ReadPin(2))
                    { bus2.WritePin(2, false); }
                }
                _timer1.Change(TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite);
            }
        }

        public async void ReadBus1()
        {
            if (bus1.IsConnected)
            {
                try
                { Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        bus1_pin1_chk.IsChecked = bus1.ReadPin(1);
                        bus1_pin2_chk.IsChecked = bus1.ReadPin(2);
                    }
                    );
                }
                catch (Exception e)
                {
                    throw e;
                }   
            }
        }

        private void red_led_btn_Click(object sender, RoutedEventArgs e)
        {

            if(x%2==0)
            { bus2.WritePin(2, true); }
            else
            { bus2.WritePin(2, false); }
            x++;
        }

        private void green_led_btn_Click(object sender, RoutedEventArgs e)
        {

            if (y % 2 == 0)
            {  bus2.WritePin(1, true); }
            else
            { bus2.WritePin(1, false); }
            y++;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            bus2.WritePin(1, false);
            bus2.WritePin(2, false);
            x = 0;
            y = 0;
        }
    }
}

MainPage.xaml:

<Page
    x:Class="IoPiPlust_start_led.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:IoPiPlust_start_led"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button x:Name="red_led_btn" Content="Red" HorizontalAlignment="Left" Margin="170,277,0,0" VerticalAlignment="Top" Click="red_led_btn_Click"/>
        <Button x:Name="green_led_btn" Content="Green" HorizontalAlignment="Left" Margin="278,277,0,0" VerticalAlignment="Top" Click="green_led_btn_Click"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="240,364,0,0" VerticalAlignment="Top" Click="Button_Click"/>
        <CheckBox x:Name="bus1_pin1_chk" Content="bus1_pin1" HorizontalAlignment="Left" Margin="638,277,0,0" VerticalAlignment="Top"/>
        <CheckBox x:Name="bus1_pin2_chk" Content="bus1_pin2" HorizontalAlignment="Left" Margin="638,314,0,0" VerticalAlignment="Top"/>

    </Grid>
</Page>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM