Skip to content

Instantly share code, notes, and snippets.

Last active May 9, 2016 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekygravity/a4617bf46eec06bd40f4c95e6776d6f5 to your computer and use it in GitHub Desktop.
Save geekygravity/a4617bf46eec06bd40f4c95e6776d6f5 to your computer and use it in GitHub Desktop.
Lego.Ev3.WPF example
<Window x:Class="Lego.Ev3.WPF.MainWindow"
Title="MainWindow" Height="350" Width="525"
<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" />
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()
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
System.Diagnostics.Debug.WriteLine("MainWindow Loaded!");
_brick = new Brick(new UsbCommunication());
// _brick = new Brick(new DummyCommunication());
// _brick = new Brick(new BluetoothCommunication("COM5"));
// _brick = new Brick(new NetworkCommunication(""));
_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";
private void _brick_BrickChanged(object sender, BrickChangedEventArgs e)
// 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