private Guid AddMyoArmbandFromDevice(BluetoothLEDevice _device) { MyoArmband newMyo = new MyoArmband(); newMyo.Id = Guid.NewGuid(); newMyo.Name = _device.Name; newMyo.Device = _device; newMyo.emgCharacs = new GattCharacteristic[4]; newMyo.EmgConnStat = new GattCommunicationStatus[4]; newMyo.Device.ConnectionStatusChanged += deviceConnChanged; newMyo.myDataHandler = new DataHandler(); bool alreadyFound = false; foreach (MyoArmband m in connectedMyos) { if (m.Name == _device.Name) { alreadyFound = true; } } if (connectedMyos.Count <= 2 && !alreadyFound) { connectedMyos.Add(newMyo); } Console.WriteLine(newMyo.Name + " created."); return(newMyo.Id); }
public void vibrate_armband(MyoArmband myo) { if (myo.cmdCharac != null) { byte[] vibeShort = new byte[] { 0x03, 0x01, 0x01 }; Write(myo.cmdCharac, vibeShort); } }
public void ConnectToArmband(Guid armbandGuid) { // identify myo MyoArmband myo = connectedMyos.Where(g => (g.Id == armbandGuid)).FirstOrDefault(); int myoIndex = connectedMyos.IndexOf(myo); if (myo == null) { Console.WriteLine("myo object was null"); return; } // hook control service, establishing a connection Task <GattDeviceServicesResult> grabIt = Task.Run(() => GetServiceAsync(myo.Device, myoGuids["MYO_SERVICE_GCS"])); grabIt.Wait(); var controlserv = grabIt.Result; myo.controlService = controlserv.Services.FirstOrDefault(); // ensure the control service is ready if (myo.controlService != null) { GattCharacteristicsResult fwCh = GetCharac(myo.controlService, myoGuids["MYO_FIRMWARE_CH"]).Result; myo.FW_charac = fwCh.Characteristics.FirstOrDefault(); GattCharacteristicsResult cmdCharac = GetCharac(myo.controlService, myoGuids["COMMAND_CHARACT"]).Result; myo.cmdCharac = cmdCharac.Characteristics.FirstOrDefault(); // read firmware characteristic to establish a connection if (myo.FW_charac != null) { GattReadResult readData = Read(myo.FW_charac).Result; if (readData != null) { ushort[] fwData = new UInt16[readData.Value.Length / 2]; System.Buffer.BlockCopy(readData.Value.ToArray(), 0, fwData, 0, (int)(readData.Value.Length)); myo.myFirmwareVersion = ($"{fwData[0]}.{fwData[1]}.{fwData[2]} rev.{fwData[3]}"); vibrate_armband(myo); // update data object connectedMyos[myoIndex] = myo; int errCode = SetupMyo(myo.Name); Console.WriteLine("Setup of " + myo.Name + "(" + myo.myFirmwareVersion + ") returned code: " + errCode); } } } }
private void deviceConnChanged(BluetoothLEDevice sender, object args) { if (sender == null || sender.Name == "<null>") { return; } MyoArmband modifiedMyo = null; int indexOfModifiedMyo = 0; foreach (MyoArmband myo in connectedMyos.Where(a => a.Name == sender.Name).ToList()) { modifiedMyo = myo; indexOfModifiedMyo = connectedMyos.IndexOf(myo); } if (modifiedMyo == null) { return; } if (sender.ConnectionStatus == BluetoothConnectionStatus.Disconnected) { connectedMyos[indexOfModifiedMyo].IsReady = false; connectedMyos[indexOfModifiedMyo].IsConnected = false; foreach (string s in bondedMyos.ToList()) { if (s == modifiedMyo.Name) { bondedMyos.Remove(s); } } connectedMyos.Remove(modifiedMyo); Disconnect_Myo(modifiedMyo.Id); currentDevice = null; isConnecting = false; } else if (sender.ConnectionStatus == BluetoothConnectionStatus.Connected) { connectedMyos[indexOfModifiedMyo].IsConnected = true; } }
private void RefreshDeviceStatus() { MyoArmband foundLeft = connectedMyos.Where(g => (g.Name == LeftMyoName)).FirstOrDefault(); MyoArmband foundRight = connectedMyos.Where(g => (g.Name == RightMyoName)).FirstOrDefault(); readyMyos = 0; Dispatcher.Invoke(() => { if (foundLeft != null) { txtDevConnStatLt.Text = foundLeft.DevConnStat.ToString(); txtDevLeftRecords.Text = foundLeft.myDataHandler.totalEMGRecords + ", " + foundLeft.myDataHandler.totalIMURecords; if (foundLeft.DevConnStat == BluetoothConnectionStatus.Connected) { txtDeviceLt.Background = UpdateMyoConnectionImage(BluetoothConnectionStatus.Connected); readyMyos++; btnStartStream.IsEnabled = true; } } if (foundRight != null) { txtDevConnStatRt.Text = foundRight.DevConnStat.ToString(); txtDevRightRecords.Text = foundRight.myDataHandler.totalEMGRecords + ", " + foundRight.myDataHandler.totalIMURecords; if (foundRight.DevConnStat == BluetoothConnectionStatus.Connected) { txtDeviceRt.Background = UpdateMyoConnectionImage(BluetoothConnectionStatus.Connected); readyMyos++; btnStartStream.IsEnabled = true; } } if (readyMyos == 2) { Console.WriteLine($"Both Myos are connected"); btnStartStream.IsEnabled = true; } }); }
public int SetupMyo(string myoName) { MyoArmband myo = connectedMyos.Where(g => (g.Name == myoName)).FirstOrDefault(); int myoIndex = connectedMyos.IndexOf(myo); if (myo.Device == null) { return(1); } try { // Establishing an IMU data connection var myServices = Task.Run(() => GetServiceAsync(myo.Device, myoGuids["IMU_DATA_SERVIC"])).Result; myo.imuService = myServices.Services.FirstOrDefault(); if (myo.imuService == null) { return(2); } GattCharacteristicsResult imuDataChar = Task.Run(() => GetCharac(myo.imuService, myoGuids["IMU_DATA_CHARAC"])).Result; myo.imuCharac = imuDataChar.Characteristics.FirstOrDefault(); if (myo.imuCharac == null) { return(3); } Notify(myo.imuCharac, charDesVal_notify); // Establishing an EMG data connection var myservs = Task.Run(() => GetServiceAsync(myo.Device, myoGuids["MYO_EMG_SERVICE"])).Result; myo.emgService = myservs.Services.FirstOrDefault(); if (myo.emgService == null) { return(4); } Task <GattCommunicationStatus>[] EmgNotificationTasks = new Task <GattCommunicationStatus> [4]; for (int t = 0; t < 4; t++) { string currEMGChar = "EMG_DATA_CHAR_" + t.ToString(); var tempCharac = Task.Run(() => GetCharac(myo.emgService, myoGuids[currEMGChar])).Result; myo.emgCharacs[t] = tempCharac.Characteristics.FirstOrDefault(); EmgNotificationTasks[t] = Notify(myo.emgCharacs[t], charDesVal_notify); myo.EmgConnStat[t] = EmgNotificationTasks[t].Result; } Task.WaitAll(EmgNotificationTasks); int errhandCode = myo.TryConnectEventHandlers(); if (errhandCode > 0) { Console.WriteLine("error attached event handlers, code " + errhandCode); } int emgErrCode = (int)myo.EmgConnStat[0] + (int)myo.EmgConnStat[1] + (int)myo.EmgConnStat[2] + (int)myo.EmgConnStat[3]; if (emgErrCode != 0) { return(5); } // signify readiness vibrate_armband(myo); myo.IsReady = true; myo.DevConnStat = BluetoothConnectionStatus.Connected; // prepare files for data collection myo.myDataHandler.Prep_EMG_Datastream(myo.Name, SessionId); myo.myDataHandler.Prep_IMU_Datastream(myo.Name, SessionId); if (!bondedMyos.Contains(myo.Name)) { bondedMyos.Add(myo.Name); } // update data objects connectedMyos[myoIndex] = myo; currentDevice = null; isConnecting = false; } catch { return(9); } return(0); }
private void Disconnect_Myo(Guid armbandGuid) { if (btnStopStream.Visibility == System.Windows.Visibility.Visible) { StopDataStream(true); } MyoArmband myo = connectedMyos.Where(g => (g.Id == armbandGuid)).FirstOrDefault(); if (myo == null) { return; } if (myo.controlService != null) { myo.controlService.Dispose(); } if (myo.imuService != null) { myo.imuService.Dispose(); } if (myo.emgService != null) { myo.emgService.Dispose(); } if (myo.Device != null) { myo.Device.Dispose(); } myo.FW_charac = null; myo.cmdCharac = null; myo.imuCharac = null; myo.emgCharacs = null; connectedMyos.Remove(myo); deviceList.Clear(); addressBook.Clear(); captureDuration = 0; GC.Collect(); Dispatcher.Invoke(() => { btnStartStream.Visibility = System.Windows.Visibility.Visible; btnStopStream.Visibility = System.Windows.Visibility.Hidden; txtBTPairLt.Text = "Disconnected"; txtBTPairRt.Text = "Disconnected"; txtBTAddrLt.Text = "Disconnected"; txtBTAddrRt.Text = "Disconnected"; txtDevConnStatLt.Text = "Disconnected"; txtDevConnStatRt.Text = "Disconnected"; txtTimer.Text = "0"; RadialGradientBrush newBrush = new RadialGradientBrush(); newBrush.GradientOrigin = new System.Windows.Point(0.5, 0.5); newBrush.Center = new System.Windows.Point(0.5, 0.5); GradientStop whiteGS = new GradientStop(); whiteGS.Color = Colors.White; whiteGS.Offset = 0.0; newBrush.GradientStops.Add(whiteGS); GradientStop seethruGS = new GradientStop(); seethruGS.Color = Colors.Transparent; seethruGS.Offset = 1.0; newBrush.GradientStops.Add(seethruGS); txtDeviceLt.Background = newBrush; txtDeviceRt.Background = newBrush; }); Setup_Watchers(); Start_Watchers(); }