private void TryToConnect(object sender, RoutedEventArgs e) { Overlay.Show("Connecting"); ConnControl.Visibility = Visibility.Visible; var conType = CreateConnection(); Dispatcher.Invoke(new Action(async () => { if (conType != null) { _brick = new Brick(conType, true); _brick.BrickChanged += _brick_BrickChanged; try { await _brick.ConnectAsync(); ConnControl.Visibility = Visibility.Collapsed; ConnTypeRun.Text = ConnControl.GetConnectionType().ToString(); } catch (Exception) { MessageBox.Show("Could not connect", "Error", MessageBoxButton.OK); } } else { MessageBox.Show("Invalid connection type for this device", "Error", MessageBoxButton.OK); } Overlay.Hide(); })); }
private async void Window_Loaded(object sender, RoutedEventArgs e) { _brick = new Brick(new BluetoothCommunication("COM6")); _brick.BrickChanged += _brick_BrickChanged; await _brick.ConnectAsync(); await _brick.DirectCommand.PlayToneAsync(100, 3, 300); }
private async void Window_Loaded(object sender, RoutedEventArgs e) { // TODO: Update port value with the connected port on your machine _brick = new Brick(new BluetoothCommunication("COM6")); _brick.BrickChanged += _brick_BrickChanged; await _brick.ConnectAsync(); await _brick.DirectCommand.SetMotorPolarity(OutputPort.B | OutputPort.C, Polarity.Backward); await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false); }
// Stuff that happens when the program is loaded private async void Grid_Loaded(object sender, RoutedEventArgs e) { //connect to brick via bluetooth _brick = new Brick(new BluetoothCommunication("COM3")); _brick.BrickChanged += _brick_BrickChanged; await _brick.ConnectAsync(); await _brick.DirectCommand.PlayToneAsync(50, 1000, 300); // Add comma seperated titles to list LogList.Add("buttonpressed,xcoordinate,ycoordinate"); }
private async void Connect_Click(object sender, RoutedEventArgs e) { try { _brick = new Brick(new BluetoothCommunication()); //_brick = new Brick(new NetworkCommunication("192.168.2.237")); _brick.BrickChanged += brick_BrickChanged; await _brick.ConnectAsync(); Output.Text = "Connected"; } catch(Exception ex) { MessageBox.Show("Failed to connect: " + ex); } }
static async Task Test() { _brick = new Brick(new UsbCommunication()); //_brick = new Brick(new BluetoothCommunication("COM5")); //_brick = new Brick(new NetworkCommunication("192.168.2.237")); _brick.BrickChanged += _brick_BrickChanged; System.Console.WriteLine("Connecting..."); await _brick.ConnectAsync(); System.Console.WriteLine("Connected...Turning motor..."); await _brick.DirectCommand.TurnMotorAtSpeedForTimeAsync(OutputPort.A, 0x50, 1000, false); System.Console.WriteLine("Motor turned...beeping..."); await _brick.DirectCommand.PlayToneAsync(0x50, 5000, 500); System.Console.WriteLine("Beeped...done!"); }
private async void TryToConnect(object sender, RoutedEventArgs e) { Overlay.Show("Connecting"); //ApplicationBar.IsVisible = false; ConnControl.Visibility = Visibility.Visible; var conType = CreateConnection(); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { if (conType != null) { _brick = new Brick(conType, true); _brick.BrickChanged += _brick_BrickChanged; try { await _brick.ConnectAsync(); ConnControl.Visibility = Visibility.Collapsed; ConnTypeRun.Text = ConnControl.GetConnectionType().ToString(); //ApplicationBar.IsVisible = true; } catch (Exception) { new MessageDialog("Could not connect", "Error").ShowAsync(); } } else { MessageDialog dialog = new MessageDialog("Invalid connection type for this device", "Error"); await dialog.ShowAsync(); } Overlay.Hide(); }); }
/************************* * METHODS *************************/ /* * SetUpBrick prepares the Lego brick to send and reveive signals. */ private async void SetUpBrick() { //System.Console.WriteLine("\nIn SetUpBrick()"); _brick = new Brick(new BluetoothCommunication("COM4")); _brick.BrickChanged += _brick_BrickChanged; await _brick.ConnectAsync(); await _brick.DirectCommand.PlayToneAsync(100, 1000, 300); await _brick.DirectCommand.SetMotorPolarity(OutputPort.B | OutputPort.C, Polarity.Forward); await _brick.DirectCommand.StopMotorAsync(OutputPort.All, false); _brick.Ports[InputPort.One].SetMode(ColorMode.Reflective); //System.Console.WriteLine("Leaving SetUpBrick()\n"); }
private async void ConnectClick(object sender, RoutedEventArgs e) { if (!_connected) { _brick = new Brick(new BluetoothCommunication("COM3")); _brick.BrickChanged += _brick_BrickChanged; try { await _brick.ConnectAsync(); await _brick.DirectCommand.PlayToneAsync(5, 1000, 300); buttonConnect.Background = Brushes.LightGreen; _connected = true; stackControls.Visibility = Visibility.Visible; buttonConnect.Content = "Disconnect"; //setmode } catch (Exception) { buttonConnect.Background = Brushes.OrangeRed; MessageBox.Show("No connection :("); _connected = false; stackControls.Visibility = Visibility.Hidden; } } else { _brick.DirectCommand.PlayToneAsync(7, 200, 300); _brick.Disconnect(); buttonConnect.Background = Brushes.OrangeRed; buttonConnect.Content = "Reconnect"; _connected = false; stackControls.Visibility = Visibility.Hidden; } }