Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,38 @@
xmlns:oxyPlotView="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
x:Class="MultiShimmerExample.MainPage">
<StackLayout>
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="MultiVerisenseExample" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
<Frame BackgroundColor="#2196F3" Padding="10" CornerRadius="0">
<Label Text="MultiVerisenseExample" HorizontalTextAlignment="Center" TextColor="White" FontSize="24"/>
</Frame>
<ListView
x:Name="deviceList"
HasUnevenRows="True"
HeightRequest="100"
CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" VerticalOptions="Center">
<Label Text="{Binding Uuid, StringFormat='{0}'}" FontSize="Small" />
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Status}" TextColor="Blue" FontSize="Micro"/>
<Label Text="{Binding TransferSpeed}" TextColor="Blue" FontSize="Micro"/>
<Label Text="{Binding PayloadIndex}" TextColor="Blue" FontSize="Micro"/>
</StackLayout>
<Label Text="{Binding BinFilePath}" TextColor="Blue" FontSize="Micro"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Grid>
<ScrollView Orientation="Horizontal">
<StackLayout Orientation="Horizontal">
<Button Clicked="connectDevicesButton_Clicked" x:Name="connectDevicesButton" WidthRequest="150" HeightRequest="50" Text="Connect Devices"></Button>
<Button Clicked="startStreamingButton_Clicked" x:Name="startStreamingButton" WidthRequest="150" HeightRequest="50" Text="Start Streaming"></Button>
<Button Clicked="stopStreamingButton_Clicked" x:Name="stopStreamingButton" WidthRequest="150" HeightRequest="50" Text="Stop Streaming"></Button>
<Button Clicked="disconnectDevicesButton_Clicked" x:Name="disconnectDevicesButton" WidthRequest="150" HeightRequest="50" Text="Disconnect Devices"></Button>
<Button Clicked="syncDevicesButton_Clicked" x:Name="syncDevicesButton" WidthRequest="100" HeightRequest="50" Text="Sync Devices"></Button>
<Button Clicked="connectDevicesButton_Clicked" x:Name="connectDevicesButton" WidthRequest="150" HeightRequest="60" Text="Connect Devices"></Button>
<Button Clicked="startStreamingButton_Clicked" x:Name="startStreamingButton" WidthRequest="150" HeightRequest="60" Text="Start Streaming"></Button>
<Button Clicked="stopStreamingButton_Clicked" x:Name="stopStreamingButton" WidthRequest="150" HeightRequest="60" Text="Stop Streaming"></Button>
<Button Clicked="disconnectDevicesButton_Clicked" x:Name="disconnectDevicesButton" WidthRequest="150" HeightRequest="60" Text="Disconnect Devices"></Button>
<Button Clicked="syncDevicesButton_Clicked" x:Name="syncDevicesButton" WidthRequest="100" HeightRequest="60" Text="Sync Devices"></Button>
</StackLayout>
</ScrollView>
</Grid>
Expand Down
133 changes: 119 additions & 14 deletions ShimmerBLE/MultiVerisenseExample/MultiVerisenseExample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,118 @@
using System.Diagnostics;
using Xamarin.Forms;
using shimmer.Sensors;
using shimmer.Communications;
using shimmer.Models;
using ShimmerAPI;
using System.ComponentModel;
using static shimmer.Models.ShimmerBLEEventData;
using static ShimmerBLEAPI.AbstractPlotManager;

namespace MultiShimmerExample
{
public partial class MainPage : ContentPage
{

public class DeviceInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}

private string _status;
public string Status { get { return _status; } set { _status = value; NotifyPropertyChanged("Status"); } }
private string _uuid { get; set; }
public string Uuid { get { return _uuid; } set { _uuid = value; NotifyPropertyChanged("Uuid"); } }
private string _payloadIndex { get; set; }
public string PayloadIndex { get { return _payloadIndex; } set { _payloadIndex = value; NotifyPropertyChanged("PayloadIndex"); } }
private string _transferSpeed { get; set; }
public string TransferSpeed { get { return _transferSpeed; } set { _transferSpeed = value; NotifyPropertyChanged("TransferSpeed"); } }
private string _binFilePath { get; set; }
public string BinFilePath { get { return _binFilePath; } set { _binFilePath = value; NotifyPropertyChanged("BinFilePath"); } }

public DeviceInfo(string uuid)
{
Uuid = uuid;
}
}

private Dictionary<string, VerisenseBLEDevice> ConnectedDevices = new Dictionary<string, VerisenseBLEDevice>();
private Dictionary<string, VerisenseBLEDevice> Devices = new Dictionary<string, VerisenseBLEDevice>();
private Dictionary<string, bool> IsFirstOjcForDevice = new Dictionary<string, bool>();
private PlotManager PlotManager;
//IVerisenseBLEManager bleManager = DependencyService.Get<IVerisenseBLEManager>();

List<string> uuids = new List<string>()
List<DeviceInfo> deviceInfos = new List<DeviceInfo>();

List<string> uuids = new List<string>() //at some stage this should be retrieved from the OS, any verisense device
{
//"00000000-0000-0000-0000-e1ec063f5c80",
//"00000000-0000-0000-0000-daa56d898b02"
//"00000000-0000-0000-0000-e7452c6d6f14",
//"00000000-0000-0000-0000-c96117537402",
//"def7b570-bb64-5167-aa2c-76f634454258",
//"7b3eba6c-026c-0861-bb0d-45d23d4dad64"
"00000000-0000-0000-0000-daa619f04ad7"
"7b3eba6c-026c-0861-bb0d-45d23d4dad64",
"00000000-0000-0000-0000-daa619f04ad7",
//"00000000-0000-0000-0000-d02b463da2bb",
//"00000000-0000-0000-0000-e7ec37a0d234",
//"04514419-5ab1-6eee-a83d-334220dade3d",
//"ad973fda-127f-d52e-e6c7-0b9dd347e90d"

};

public MainPage()
{
InitializeComponent();
foreach (String uuid in uuids)
{
deviceInfos.Add(new DeviceInfo(uuid));
}
PlotManager = new PlotManager("Data", "Data Point", "Timestamp", true);
plotView.Model = PlotManager.BuildPlotModel();

//var service = DependencyService.Get<IVerisenseBLEManager>();
//bleManager.BLEManagerEvent += BLEManager_BLEEvent;
deviceList.ItemsSource = deviceInfos;

}

public async void ConnectDevices()
{
foreach (string uuid in uuids)
{
VerisenseBLEDevice device = new VerisenseBLEDevice(uuid, "");
if (Devices.ContainsKey(uuid))
{
Devices.Remove(uuid);
Devices.Add(uuid, device);
}
else
{
Devices.Add(uuid, device);
}
device.ShimmerBLEEvent += ShimmerDevice_BLEEvent;
bool result = await device.Connect(true);
if (result)
{
Debug.WriteLine("Device Version: " + device.GetProductionConfig().REV_HW_MAJOR + "." + device.GetProductionConfig().REV_HW_MINOR);
Debug.WriteLine("Firmware Version: " + device.GetProductionConfig().REV_FW_MAJOR + "." + device.GetProductionConfig().REV_FW_MINOR + "." + device.GetProductionConfig().REV_FW_INTERNAL);
if (Devices.ContainsKey(uuid))
if (ConnectedDevices.ContainsKey(uuid))
{
Devices.Remove(uuid);
Devices.Add(uuid, device);
ConnectedDevices.Remove(uuid);
ConnectedDevices.Add(uuid, device);
}
else if (!Devices.ContainsKey(uuid))
else if (!ConnectedDevices.ContainsKey(uuid))
{
Devices.Add(uuid, device);
ConnectedDevices.Add(uuid, device);
}
Debug.WriteLine("\nBT state: " + device.GetVerisenseBLEState() + "\nUUID: " + device.Asm_uuid + "\nBattery: " + device.GetStatus().BatteryPercent + "%");
ConfigureDevice(device);
Expand Down Expand Up @@ -86,7 +149,7 @@ public void ConfigureDevice(VerisenseBLEDevice device)

public async void StartStreamingDevices()
{
foreach (VerisenseBLEDevice device in Devices.Values)
foreach (VerisenseBLEDevice device in ConnectedDevices.Values)
{
var streamResult = await device.ExecuteRequest(RequestType.StartStreaming);
Debug.WriteLine("Stream Status: " + streamResult);
Expand All @@ -105,7 +168,7 @@ public async void StartStreamingDevices()

public async void StartDataSync()
{
foreach (VerisenseBLEDevice device in Devices.Values)
foreach (VerisenseBLEDevice device in ConnectedDevices.Values)
{
var syncResult = device.ExecuteRequest(RequestType.TransferLoggedData);
Debug.WriteLine("Sync Status: " + syncResult);
Expand All @@ -119,7 +182,15 @@ public async void StartDataSync()

private void ShimmerDevice_BLEEvent(object sender, ShimmerBLEEventData e)
{
if (e.CurrentEvent == VerisenseBLEEvent.NewDataPacket)
if (e.CurrentEvent == VerisenseBLEEvent.StateChange)
{
int index = deviceInfos.FindIndex(x => x.Uuid == e.ASMID);
if (index >= 0)
{
deviceInfos[index].Status = Devices[e.ASMID].GetVerisenseBLEState().ToString();
}
}
else if (e.CurrentEvent == VerisenseBLEEvent.NewDataPacket)
{
ObjectCluster ojc = (ObjectCluster)e.ObjMsg;

Expand Down Expand Up @@ -149,12 +220,41 @@ private void ShimmerDevice_BLEEvent(object sender, ShimmerBLEEventData e)
var systemTimestampPlot = ojc.GetData(ShimmerConfiguration.SignalNames.SYSTEM_TIMESTAMP_PLOT, ShimmerConfiguration.SignalFormats.CAL).Data;
//Debug.WriteLine("UUID: " + ojc.GetCOMPort() + " |X : " + Math.Round(a2x.Data, 2) + " Y : " + Math.Round(a2y.Data, 2) + " Z : " + Math.Round(a2z.Data, 2) + "| Systen TS Plot: " + systemTimestampPlot + " ms");
}
else if (e.CurrentEvent == VerisenseBLEEvent.SyncLoggedDataNewPayload)
{
string[] words = e.Message.Split('(');
int index = deviceInfos.FindIndex(x => x.Uuid == e.ASMID);
if (index >= 0)
{
deviceInfos[index].TransferSpeed = words[0];
deviceInfos[index].PayloadIndex = words[1].Remove(words[1].Length - 1);
deviceInfos[index].BinFilePath = Devices[e.ASMID].dataFilePath;
}
}
}

private void BLEManager_BLEEvent(object sender, BLEManagerEvent e)
{

if (e.CurrentEvent == BLEManagerEvent.BLEAdapterEvent.ScanCompleted)
{

}
else if (e.CurrentEvent == BLEManagerEvent.BLEAdapterEvent.DevicePaired)
{

}
else if (e.CurrentEvent == BLEManagerEvent.BLEAdapterEvent.DeviceDiscovered)
{

}
}


public async void StopStreamingDevices()
{
PlotManager.RemoveAllSignalsFromPlot();
foreach (VerisenseBLEDevice device in Devices.Values)
foreach (VerisenseBLEDevice device in ConnectedDevices.Values)
{
var result = await device.ExecuteRequest(RequestType.StopStreaming);
IsFirstOjcForDevice[device.Asm_uuid.ToString()] = true;
Expand All @@ -166,7 +266,7 @@ public async void DisconnectDevices()
{
PlotManager.RemoveAllSignalsFromPlot();
List<string> listOfDisconnectedUuids = new List<string>();
foreach (VerisenseBLEDevice device in Devices.Values)
foreach (VerisenseBLEDevice device in ConnectedDevices.Values)
{
var result = await device.Disconnect();
if (result)
Expand All @@ -180,13 +280,18 @@ public async void DisconnectDevices()
//Remove disconnected devices from the Dictionary
foreach (var uuid in listOfDisconnectedUuids)
{
Devices.Remove(uuid);
ConnectedDevices.Remove(uuid);
}
}

//------------------------------------------------------------------------------------------

//GUI Functionality
//private void scanDevicesButton_Clicked(object sender, EventArgs e)
//{
// bleManager.StartScanForDevices();
//}

private void connectDevicesButton_Clicked(object sender, EventArgs e)
{
ConnectDevices();
Expand Down