protected void OnConnectButtonClicked(object sender, System.EventArgs e) { SpawnThread(delegate() { if (USBRadiobutton.Active) { brick = new MonoBrick.NXT.Brick <MonoBrick.NXT.Sensor, MonoBrick.NXT.Sensor, MonoBrick.NXT.Sensor, MonoBrick.NXT.Sensor>(new USB <MonoBrick.NXT.Command, MonoBrick.NXT.Reply>()); } if (bluetoothRadiobutton.Active) { brick = new MonoBrick.NXT.Brick <MonoBrick.NXT.Sensor, Sensor, Sensor, Sensor>(new Bluetooth <MonoBrick.NXT.Command, MonoBrick.NXT.Reply>(comportCombobox.ActiveText)); } if (networkRadiobutton.Active) { if (waitForConnectioncheckbutton.Active) { brick = new MonoBrick.NXT.Brick <Sensor, Sensor, Sensor, Sensor>(new TunnelConnection <MonoBrick.NXT.Command, MonoBrick.NXT.Reply>(ushort.Parse(portSpinbutton.Text))); Label networkLabel = new Label(); //label.HeightRequest = 160; networkLabel.WidthRequest = 300; string[] addresses = NetworkHelper.GetLocalIPAddress(); for (int i = 0; i < addresses.Length; i++) { networkLabel.Text = networkLabel.Text + "Local IP address: " + addresses[i].PadRight(80) + "\n"; } networkLabel.Text = networkLabel.Text + "External IP Address: "; Dialog networkDialog = new Dialog("Wait for connection", this, Gtk.DialogFlags.DestroyWithParent); networkDialog.TypeHint = WindowTypeHint.Splashscreen; networkDialog.BorderWidth = 4; networkDialog.Modal = true; networkDialog.VBox.Add(networkLabel); //dialog.VBox.Add (progressBar); networkDialog.HasSeparator = false; networkDialog.AddButton("Cancel", ResponseType.Close); networkDialog.Response += delegate(object obj, ResponseArgs args){ brick.Connection.Close(); }; Gtk.Application.Invoke(delegate { networkDialog.ShowAll(); }); networkLabel.Text = networkLabel.Text + Utilities.NetworkHelper.GetExternalIP(); //MonoBrick.ConnectionDelegate onNetworkConnection = delegate(){}; //brick.Connection.Connected += onNetworkConnection; try{ brick.Connection.Open(); //This will block and wait for a network connection } catch (Exception exception) { //brick.Connection.Connected -= onNetworkConnection; Gtk.Application.Invoke(delegate { networkDialog.Destroy(); }); throw(exception); } //brick.Connection.Connected -= onNetworkConnection; Gtk.Application.Invoke(delegate { networkDialog.Destroy(); }); } else { brick = new MonoBrick.NXT.Brick <Sensor, Sensor, Sensor, Sensor>(new TunnelConnection <Command, Reply>(ipEntry.Text, ushort.Parse(portSpinbutton.Text))); } } System.Timers.Timer timer = new System.Timers.Timer(100); ProgressBar progress = new ProgressBar(); //progress.Fraction = 0.0; progress.Orientation = ProgressBarOrientation.LeftToRight; timer.Elapsed += (obj1, obj2) => { progress.Pulse(); }; Label connectLabel = new Label("Opening connection..."); //label.HeightRequest = 160; connectLabel.WidthRequest = 200; Dialog connectDialog = new Dialog("Connecting to the NXT", this, Gtk.DialogFlags.DestroyWithParent); connectDialog.TypeHint = WindowTypeHint.Splashscreen; connectDialog.Modal = true; connectDialog.VBox.Add(connectLabel); connectDialog.HasSeparator = false; connectDialog.VBox.Add(progress); Gtk.Application.Invoke(delegate { connectDialog.ShowAll(); timer.Start(); }); if (!brick.Connection.IsConnected) { try{ brick.Connection.Open(); } catch (Exception ex) { Gtk.Application.Invoke(delegate { connectDialog.Destroy(); timer.Stop(); }); throw ex; } } brick.Connection.CommandSend += OnCommandSend; brick.Connection.ReplyReceived += OnReplyRecieved; //brick.ConnectionOpened += OnConnected; brick.Connection.Disconnected += Disconnected; try{ Gtk.Application.Invoke(delegate { connectLabel.Text = "Retrieving brick information..."; }); DeviceInfo info = brick.GetDeviceInfo(); DeviceFirmware firmware = brick.GetDeviceFirmware(); ushort batteryLevel = brick.GetBatteryLevel(); SetSensor[] setSensor = new SetSensor[] { delegate(Sensor sensor){ brick.Sensor1 = sensor; brick.Sensor1.Initialize(); }, delegate(Sensor sensor){ brick.Sensor2 = sensor; brick.Sensor2.Initialize(); }, delegate(Sensor sensor){ brick.Sensor3 = sensor; brick.Sensor3.Initialize(); }, delegate(Sensor sensor){ brick.Sensor4 = sensor; brick.Sensor4.Initialize(); }, }; ComboBox[] comboArray = new ComboBox[] { sensor1Combobox, sensor2Combobox, sensor3Combobox, sensor4Combobox }; //Check if sensors should be initialized with something other than none for (int i = 0; i < 4; i++) { Console.WriteLine(i); Gtk.Application.Invoke(delegate { connectLabel.Text = "Setting up sensor " + (i + 1); }); if (comboArray[i].Active != 0) { try{ setSensor[i]((Sensor)comboArray[i].GetActiveValue()); } catch (MonoBrickException nxtEx) { if (nxtEx is MonoBrickException) { comboArray[i].Active = 0; //unable to set the sensor value set the comboBox to none setSensor[i](new Sensor(SensorType.NoSensor, SensorMode.Raw)); } else { throw nxtEx; } } } } Gtk.Application.Invoke(delegate { UpdateDeviceInfo(info); UpdateFirmwareInfo(firmware); UpdateBatteryInfo(batteryLevel); }); Connected(); DisableAllTunnelInput(); if (isConnectedViaNetwork()) { Gtk.Application.Invoke(delegate { connectLabel.Text = "Checking tunnel settings..."; }); if (brickType == BrickType.NXT) { var reply = brick.Connection.SendAndReceive(new Command(CommandType.TunnelCommand, CommandByte.GetTunnelCommands, true)); if (!reply.HasError) { for (int i = 0; i < reply.Data.Length - 3; i++) { switch (reply.Data[i + 3]) { case (byte)CommandByte.GetTunnelRTSPSettings: EnableVideoStreamInput(); break; case (byte)CommandByte.GetTunnelGPSPosition: EnableGPSInput(); break; case (byte)CommandByte.TunnelSpeak: EnableSpeakInput(); break; } } } else { //The tunnel does not support any commands } } } } catch (MonoBrickException ex) { Gtk.Application.Invoke(delegate { connectDialog.Destroy(); timer.Stop(); }); brick.Connection.Close(); throw ex; } Gtk.Application.Invoke(delegate { connectDialog.Destroy(); timer.Stop(); }); }); }
public override bool OnOptionsItemSelected (IMenuItem item) { switch (item.ItemId) { case Resource.Id.menuConnection: if(brickController.NXT.Connection.IsConnected){ ProgressDialog progress = null; RunOnUiThread(delegate() { progress = ProgressDialog.Show(this,"Disconnecting From Brick...","Closing connection... "); }); System.Threading.Thread t = new System.Threading.Thread( delegate(object obj){ try{ brickController.NXT.Connection.Close(); } catch{ } RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this,"Disconnected from brick"); }); }); t.IsBackground = true; t.Priority = System.Threading.ThreadPriority.Normal; t.Start(); } else{ ProgressDialog progress = null; System.Threading.Thread t = new System.Threading.Thread(delegate(object obj) { if(settings.DeviceName == ""){ RunOnUiThread(delegate() { AlertDialog dialog = new AlertDialog.Builder(this).Create(); dialog.SetTitle("Select Bluetooth Device"); dialog.SetMessage("No Bluetooth Device has been selected. Please go to settings and select a Bluetooth device"); dialog.SetButton("OK", delegate(object sen, DialogClickEventArgs eventArgs) { }); dialog.SetIcon(Android.Resource.Drawable.IcDialogInfo); dialog.Show(); }); return; } RunOnUiThread(delegate() { progress = ProgressDialog.Show(this,"Connecting To Brick...","Opening connection... ",true,false); }); try{ if(settings.Type == BrickType.NXT){ brickController.NXT = new MonoBrick.NXT.Brick<MonoBrick.NXT.Sensor, MonoBrick.NXT.Sensor, MonoBrick.NXT.Sensor, MonoBrick.NXT.Sensor>( //new MonoBrick.Loopback() new MonoBrick.Bluetooth<MonoBrick.NXT.Command,MonoBrick.NXT.Reply>(MonoBrick.Bluetooth<MonoBrick.NXT.Command,MonoBrick.NXT.Reply>.GetBondDevice(settings.DeviceName)) ); } brickController.NXT.Connection.Open(); } catch(Exception ex){ RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this,"Error connecting: " + ex.Message, ToastLength.Long); }); return; } Dictionary<string,MonoBrick.NXT.Sensor> sensorDictionary = MonoBrick.NXT.Sensor.SensorDictionary; string[] sensorName = new string[]{settings.Sensor1, settings.Sensor2, settings.Sensor3, settings.Sensor4}; MonoBrick.NXT.Sensor sensorType = new MonoBrick.NXT.NoSensor(); try{ SetSensor[] setSensor = new SetSensor[]{ delegate(MonoBrick.NXT.Sensor sensor){brickController.NXT.Sensor1 = sensor; brickController.NXT.Sensor1.Initialize();}, delegate(MonoBrick.NXT.Sensor sensor){brickController.NXT.Sensor2 = sensor; brickController.NXT.Sensor2.Initialize();}, delegate(MonoBrick.NXT.Sensor sensor){brickController.NXT.Sensor3 = sensor; brickController.NXT.Sensor3.Initialize();}, delegate(MonoBrick.NXT.Sensor sensor){brickController.NXT.Sensor4 = sensor; brickController.NXT.Sensor4.Initialize();}, }; //Check if sensors should be initialized with something other than none for(int i = 0; i < 4; i++){ try{ if(!sensorDictionary.TryGetValue(sensorName[i], out sensorType)){ //could not load a value sensorName[i] = "None"; sensorType = new MonoBrick.NXT.NoSensor(); } RunOnUiThread(delegate() { progress.SetMessage("initialize sensor " + (i+1) + " as " + sensorName[i]); }); setSensor[i](sensorType); } catch(MonoBrickException nxtEx){ if(nxtEx is MonoBrick.NXT.BrickException){ RunOnUiThread(delegate() { progress.SetMessage("Sensor " + (i+1) + " initialized as \"no-sensor\""); }); sensorName[i] = "None"; setSensor[i](new MonoBrick.NXT.Sensor(MonoBrick.NXT.SensorType.NoSensor,MonoBrick.NXT.SensorMode.Raw)); } else{ RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this,"Failed to initialize sensors.\n" + nxtEx.Message); OnDisconnected(); }); brickController.NXT.Connection.Close(); return; } } } } catch(MonoBrick.NXT.BrickException ex){ RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this,"Failed to initialize sensors.\n" + ex.Message); }); brickController.NXT.Connection.Close(); return; } RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this,"Connected successfully to brick"); }); }); t.IsBackground = true; t.Priority = System.Threading.ThreadPriority.Normal; t.Start(); } break; case Resource.Id.menuSettings: StartActivity(new Intent(this,typeof(SettingsActivity))); break; case Resource.Id.menuRefresh: fileFragment.ReloadFileList(); break; case Resource.Id.menuUpload: fileFragment.UploadFile(); break; case Resource.Id.menuFormat: fileFragment.Format(); break; default: return base.OnOptionsItemSelected (item); } return true; }
protected void OnConnectButtonClicked (object sender, System.EventArgs e) { SpawnThread(delegate() { if(USBRadiobutton.Active){ brick = new MonoBrick.NXT.Brick<MonoBrick.NXT.Sensor,MonoBrick.NXT.Sensor,MonoBrick.NXT.Sensor,MonoBrick.NXT.Sensor>(new USB<MonoBrick.NXT.Command,MonoBrick.NXT.Reply>()); } if(bluetoothRadiobutton.Active){ brick = new MonoBrick.NXT.Brick<MonoBrick.NXT.Sensor,Sensor,Sensor,Sensor>(new Bluetooth<MonoBrick.NXT.Command,MonoBrick.NXT.Reply>(comportCombobox.ActiveText)); } if(networkRadiobutton.Active){ if(waitForConnectioncheckbutton.Active){ brick = new MonoBrick.NXT.Brick<Sensor,Sensor,Sensor,Sensor>(new TunnelConnection<MonoBrick.NXT.Command,MonoBrick.NXT.Reply>(ushort.Parse(portSpinbutton.Text))); Label networkLabel = new Label(); //label.HeightRequest = 160; networkLabel.WidthRequest = 300; string[] addresses = NetworkHelper.GetLocalIPAddress(); for(int i = 0; i < addresses.Length; i++){ networkLabel.Text = networkLabel.Text + "Local IP address: " + addresses[i].PadRight(80)+"\n"; } networkLabel.Text = networkLabel.Text+ "External IP Address: "; Dialog networkDialog = new Dialog ("Wait for connection", this, Gtk.DialogFlags.DestroyWithParent); networkDialog.TypeHint = WindowTypeHint.Splashscreen; networkDialog.BorderWidth = 4; networkDialog.Modal = true; networkDialog.VBox.Add (networkLabel); //dialog.VBox.Add (progressBar); networkDialog.HasSeparator = false; networkDialog.AddButton ("Cancel", ResponseType.Close); networkDialog.Response += delegate(object obj, ResponseArgs args){ brick.Connection.Close(); }; Gtk.Application.Invoke (delegate { networkDialog.ShowAll(); }); networkLabel.Text = networkLabel.Text + Utilities.NetworkHelper.GetExternalIP(); //MonoBrick.ConnectionDelegate onNetworkConnection = delegate(){}; //brick.Connection.Connected += onNetworkConnection; try{ brick.Connection.Open();//This will block and wait for a network connection } catch(Exception exception){ //brick.Connection.Connected -= onNetworkConnection; Gtk.Application.Invoke (delegate { networkDialog.Destroy(); }); throw(exception); } //brick.Connection.Connected -= onNetworkConnection; Gtk.Application.Invoke (delegate { networkDialog.Destroy(); }); } else{ brick = new MonoBrick.NXT.Brick<Sensor,Sensor,Sensor,Sensor>(new TunnelConnection<Command,Reply>(ipEntry.Text, ushort.Parse(portSpinbutton.Text))); } } System.Timers.Timer timer = new System.Timers.Timer(100); ProgressBar progress = new ProgressBar(); //progress.Fraction = 0.0; progress.Orientation = ProgressBarOrientation.LeftToRight; timer.Elapsed += (obj1,obj2) => { progress.Pulse(); }; Label connectLabel = new Label("Opening connection..."); //label.HeightRequest = 160; connectLabel.WidthRequest = 200; Dialog connectDialog = new Dialog ("Connecting to the NXT", this, Gtk.DialogFlags.DestroyWithParent); connectDialog.TypeHint = WindowTypeHint.Splashscreen; connectDialog.Modal = true; connectDialog.VBox.Add (connectLabel); connectDialog.HasSeparator = false; connectDialog.VBox.Add (progress); Gtk.Application.Invoke (delegate { connectDialog.ShowAll(); timer.Start(); }); if(!brick.Connection.IsConnected){ try{ brick.Connection.Open(); } catch(Exception ex){ Gtk.Application.Invoke (delegate { connectDialog.Destroy(); timer.Stop(); }); throw ex; } } brick.Connection.CommandSend += OnCommandSend; brick.Connection.ReplyReceived += OnReplyRecieved; //brick.ConnectionOpened += OnConnected; brick.Connection.Disconnected += Disconnected; try{ Gtk.Application.Invoke (delegate { connectLabel.Text = "Retrieving brick information..."; }); DeviceInfo info = brick.GetDeviceInfo(); DeviceFirmware firmware = brick.GetDeviceFirmware(); ushort batteryLevel = brick.GetBatteryLevel(); SetSensor[] setSensor = new SetSensor[]{ delegate(Sensor sensor){brick.Sensor1 = sensor; brick.Sensor1.Initialize();}, delegate(Sensor sensor){brick.Sensor2 = sensor; brick.Sensor2.Initialize();}, delegate(Sensor sensor){brick.Sensor3 = sensor; brick.Sensor3.Initialize();}, delegate(Sensor sensor){brick.Sensor4 = sensor; brick.Sensor4.Initialize();}, }; ComboBox[] comboArray = new ComboBox[]{sensor1Combobox, sensor2Combobox,sensor3Combobox, sensor4Combobox}; //Check if sensors should be initialized with something other than none for(int i = 0; i < 4; i++){ Console.WriteLine(i); Gtk.Application.Invoke (delegate { connectLabel.Text = "Setting up sensor " + (i+1); }); if(comboArray[i].Active != 0){ try{ setSensor[i]((Sensor) comboArray[i].GetActiveValue()); } catch(MonoBrickException nxtEx){ if(nxtEx is MonoBrickException){ comboArray[i].Active = 0;//unable to set the sensor value set the comboBox to none setSensor[i](new Sensor(SensorType.NoSensor,SensorMode.Raw)); } else throw nxtEx; } } } Gtk.Application.Invoke (delegate { UpdateDeviceInfo(info); UpdateFirmwareInfo(firmware); UpdateBatteryInfo(batteryLevel); }); Connected(); DisableAllTunnelInput(); if(isConnectedViaNetwork()){ Gtk.Application.Invoke (delegate { connectLabel.Text = "Checking tunnel settings..."; }); if(brickType == BrickType.NXT){ var reply = brick.Connection.SendAndReceive(new Command(CommandType.TunnelCommand,CommandByte.GetTunnelCommands,true)); if(!reply.HasError){ for(int i = 0; i < reply.Data.Length-3; i++){ switch(reply.Data[i+3]){ case (byte)CommandByte.GetTunnelRTSPSettings: EnableVideoStreamInput(); break; case (byte)CommandByte.GetTunnelGPSPosition: EnableGPSInput(); break; case (byte)CommandByte.TunnelSpeak: EnableSpeakInput(); break; } } } else{ //The tunnel does not support any commands } } } } catch(MonoBrickException ex){ Gtk.Application.Invoke (delegate { connectDialog.Destroy(); timer.Stop(); }); brick.Connection.Close(); throw ex; } Gtk.Application.Invoke (delegate { connectDialog.Destroy(); timer.Stop(); }); }); }
public override bool OnOptionsItemSelected(IMenuItem item) { switch (item.ItemId) { case Resource.Id.menuConnection: if (brickController.NXT.Connection.IsConnected) { ProgressDialog progress = null; RunOnUiThread(delegate() { progress = ProgressDialog.Show(this, "Disconnecting From Brick...", "Closing connection... "); }); System.Threading.Thread t = new System.Threading.Thread(delegate(object obj){ try{ brickController.NXT.Connection.Close(); } catch { } RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this, "Disconnected from brick"); }); }); t.IsBackground = true; t.Priority = System.Threading.ThreadPriority.Normal; t.Start(); } else { ProgressDialog progress = null; System.Threading.Thread t = new System.Threading.Thread(delegate(object obj) { if (settings.DeviceName == "") { RunOnUiThread(delegate() { AlertDialog dialog = new AlertDialog.Builder(this).Create(); dialog.SetTitle("Select Bluetooth Device"); dialog.SetMessage("No Bluetooth Device has been selected. Please go to settings and select a Bluetooth device"); dialog.SetButton("OK", delegate(object sen, DialogClickEventArgs eventArgs) { }); dialog.SetIcon(Android.Resource.Drawable.IcDialogInfo); dialog.Show(); }); return; } RunOnUiThread(delegate() { progress = ProgressDialog.Show(this, "Connecting To Brick...", "Opening connection... ", true, false); }); try{ if (settings.Type == BrickType.NXT) { brickController.NXT = new MonoBrick.NXT.Brick <MonoBrick.NXT.Sensor, MonoBrick.NXT.Sensor, MonoBrick.NXT.Sensor, MonoBrick.NXT.Sensor>( //new MonoBrick.Loopback() new MonoBrick.Bluetooth <MonoBrick.NXT.Command, MonoBrick.NXT.Reply>(MonoBrick.Bluetooth <MonoBrick.NXT.Command, MonoBrick.NXT.Reply> .GetBondDevice(settings.DeviceName)) ); } brickController.NXT.Connection.Open(); } catch (Exception ex) { RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this, "Error connecting: " + ex.Message, ToastLength.Long); }); return; } Dictionary <string, MonoBrick.NXT.Sensor> sensorDictionary = MonoBrick.NXT.Sensor.SensorDictionary; string[] sensorName = new string[] { settings.Sensor1, settings.Sensor2, settings.Sensor3, settings.Sensor4 }; MonoBrick.NXT.Sensor sensorType = new MonoBrick.NXT.NoSensor(); try{ SetSensor[] setSensor = new SetSensor[] { delegate(MonoBrick.NXT.Sensor sensor){ brickController.NXT.Sensor1 = sensor; brickController.NXT.Sensor1.Initialize(); }, delegate(MonoBrick.NXT.Sensor sensor){ brickController.NXT.Sensor2 = sensor; brickController.NXT.Sensor2.Initialize(); }, delegate(MonoBrick.NXT.Sensor sensor){ brickController.NXT.Sensor3 = sensor; brickController.NXT.Sensor3.Initialize(); }, delegate(MonoBrick.NXT.Sensor sensor){ brickController.NXT.Sensor4 = sensor; brickController.NXT.Sensor4.Initialize(); }, }; //Check if sensors should be initialized with something other than none for (int i = 0; i < 4; i++) { try{ if (!sensorDictionary.TryGetValue(sensorName[i], out sensorType)) { //could not load a value sensorName[i] = "None"; sensorType = new MonoBrick.NXT.NoSensor(); } RunOnUiThread(delegate() { progress.SetMessage("initialize sensor " + (i + 1) + " as " + sensorName[i]); }); setSensor[i](sensorType); } catch (MonoBrickException nxtEx) { if (nxtEx is MonoBrick.NXT.BrickException) { RunOnUiThread(delegate() { progress.SetMessage("Sensor " + (i + 1) + " initialized as \"no-sensor\""); }); sensorName[i] = "None"; setSensor[i](new MonoBrick.NXT.Sensor(MonoBrick.NXT.SensorType.NoSensor, MonoBrick.NXT.SensorMode.Raw)); } else { RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this, "Failed to initialize sensors.\n" + nxtEx.Message); OnDisconnected(); }); brickController.NXT.Connection.Close(); return; } } } } catch (MonoBrick.NXT.BrickException ex) { RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this, "Failed to initialize sensors.\n" + ex.Message); }); brickController.NXT.Connection.Close(); return; } RunOnUiThread(delegate() { progress.Dismiss(); ShowToast(this, "Connected successfully to brick"); }); }); t.IsBackground = true; t.Priority = System.Threading.ThreadPriority.Normal; t.Start(); } break; case Resource.Id.menuSettings: StartActivity(new Intent(this, typeof(SettingsActivity))); break; case Resource.Id.menuRefresh: fileFragment.ReloadFileList(); break; case Resource.Id.menuUpload: fileFragment.UploadFile(); break; case Resource.Id.menuFormat: fileFragment.Format(); break; default: return(base.OnOptionsItemSelected(item)); } return(true); }