Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lego.Ev3.WPF example
<Window x:Class="Lego.Ev3.WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Lego.Ev3.WPF"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
Loaded="MainWindow_Loaded">
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock x:Name="myStatus" HorizontalAlignment="Left" Margin="18,0,0,0" TextWrapping="Wrap" Text="Wait for beep" VerticalAlignment="Top"/>
<Button ClickMode="Press" Click="Start_Program" Content="Start Program" Width="150" HorizontalAlignment="Left" Margin="171,275,0,0" VerticalAlignment="Top" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Lego.Ev3.Core;
using Lego.Ev3.Desktop;
namespace Lego.Ev3.WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Brick _brick;
bool _not_connected = true;
public MainWindow()
{
InitializeComponent();
}
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("MainWindow Loaded!");
try
{
_brick = new Brick(new UsbCommunication());
// _brick = new Brick(new DummyCommunication());
// _brick = new Brick(new BluetoothCommunication("COM5"));
// _brick = new Brick(new NetworkCommunication("192.168.2.237"));
_brick.BrickChanged += _brick_BrickChanged;
await _brick.ConnectAsync(); _not_connected = false;
await _brick.DirectCommand.PlayToneAsync(100, 1000, 300);
await _brick.DirectCommand.SetMotorPolarityAsync(OutputPort.B | OutputPort.C, Polarity.Backward);
await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false);
System.Diagnostics.Debug.WriteLine("Loop check");
// System.Diagnostics.Debug.WriteLine("Turn Motor");
// await _brick.DirectCommand.TurnMotorAtSpeedForTimeAsync(OutputPort.B, 0x50, 500, false);
}
catch (System.Exception ex) // (Exception ex)
{
// MessageBox.Show("Could not connect", "Error", MessageBoxButton.OK);
// myStatus.Text = "Could not connect";
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
private void _brick_BrickChanged(object sender, BrickChangedEventArgs e)
{
System.Console.WriteLine(e.Ports[InputPort.One].SIValue);
// myStatus.Text = e.Ports[InputPort.Four].SIValue.ToString();
// System.Diagnostics.Debug.WriteLine("Brick Changed!");
}
private async void Start_Program(object sender, RoutedEventArgs e)
{
if (_not_connected) { return; };
await _brick.DirectCommand.StartProgramAsync("../prjs/Barn_Door_Tracker/Program-01.rbf");
System.Diagnostics.Debug.WriteLine("Start program!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.