Skip to content

Commit

Permalink
LSLBridge is auto hidden if no streams active. BlueMuse also polls to…
Browse files Browse the repository at this point in the history
… keep LSL bridge open if not currently streaming, therefore LSLBridge has proper auto closing mechanism that won't prematurely trigger. This process may seem strange and convoluted but it appears to be the only good method to manage this trusted process with the current Windows UWP API.
  • Loading branch information
kowalej committed May 8, 2018
1 parent bd2a442 commit 86bd090
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
9 changes: 8 additions & 1 deletion BlueMuse/Bluetooth/BluetoothManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class BluetoothManager
private volatile bool LSLBridgeLaunched = false;
private static readonly Object syncLock = new object();
Timer pollMuseTimer;
Timer pollBridge;

private static object syncRoot = new Object();
private static volatile BluetoothManager instance;
Expand All @@ -44,6 +45,13 @@ public static BluetoothManager Instance

private BluetoothManager() {
Muses = new ObservableCollection<Muse>();
pollBridge = new Timer(PollBridge, null, 0, 1000);
}

public async void PollBridge(object state)
{
if(LSLBridgeLaunched && !Muses.Any(x => x.IsStreaming))
await AppService.AppServiceManager.SendMessageAsync(Constants.LSL_MESSAGE_TYPE_KEEP_ACTIVE, new Windows.Foundation.Collections.ValueSet());
}

public async void Close()
Expand Down Expand Up @@ -282,7 +290,6 @@ public async Task StopStreamingAll()
{
await muse.ToggleStream(false);
}
//await DeactivateLSLBridge();
}
}

Expand Down
1 change: 1 addition & 0 deletions BlueMuse/Misc/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static class Constants
public const int MUSE_LSL_BUFFER_LENGTH = 360;

public const string LSL_MESSAGE_TYPE = nameof(LSL_MESSAGE_TYPE);
public const string LSL_MESSAGE_TYPE_KEEP_ACTIVE = nameof(LSL_MESSAGE_TYPE_KEEP_ACTIVE);
public const string LSL_MESSAGE_TYPE_OPEN_STREAM = nameof(LSL_MESSAGE_TYPE_OPEN_STREAM);
public const string LSL_MESSAGE_TYPE_CLOSE_STREAM = nameof(LSL_MESSAGE_TYPE_CLOSE_STREAM);
public const string LSL_MESSAGE_TYPE_SEND_CHUNK = nameof(LSL_MESSAGE_TYPE_SEND_CHUNK);
Expand Down
8 changes: 4 additions & 4 deletions BlueMuse/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="BlueMuse.App">
<uap:VisualElements DisplayName="Blue Muse" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="Simple application to connect Muse EEG headsets to Windows system and stream data via LSL." BackgroundColor="transparent">
<uap:VisualElements DisplayName="BlueMuse" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="Simple application to connect Muse EEG headsets to Windows system and stream data via LSL." BackgroundColor="transparent">
<uap:LockScreen Notification="badgeAndTileText" BadgeLogo="Assets\BadgeLogo.png" />
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" Square310x310Logo="Assets\LargeTile.png" Square71x71Logo="Assets\SmallTile.png" ShortName="Blue Muse">
<uap:ShowNameOnTiles>
Expand All @@ -27,12 +27,12 @@
<uap:SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="transparent" />
</uap:VisualElements>
<Extensions>
<uap:Extension Category="windows.appService">
<uap:AppService Name="LSLService" />
</uap:Extension>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="bluemuse" />
</uap:Extension>
<uap:Extension Category="windows.appService">
<uap:AppService Name="LSLService" />
</uap:Extension>
<desktop:Extension Category="windows.fullTrustProcess" Executable="Win32\LSLBridge.exe" />
</Extensions>
</Application>
Expand Down
Binary file modified BlueMuse/Win32/LSLBridge.exe
Binary file not shown.
8 changes: 4 additions & 4 deletions LSLBridge/LSL/MuseLSLStreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MuseLSLStreamManager
private AppServiceConnection lslStreamService;
private Timer keepAliveTimer;
private static readonly Object syncLock = new object();
private DateTime lastMessageTime = DateTime.MinValue;
private DateTime lastMessageTime = DateTime.MinValue;

public MuseLSLStreamManager(ObservableCollection<MuseLSLStream> museStreams, Action<int> museStreamCountSetter)
{
Expand All @@ -31,7 +31,7 @@ public MuseLSLStreamManager(ObservableCollection<MuseLSLStream> museStreams, Act
lslStreamService.AppServiceName = "LSLService";
lslStreamService.RequestReceived += LSLService_RequestReceived;
OpenService();
keepAliveTimer = new Timer(CheckLastMessage, null, 0, 600); // Start the Searching for Muses... animation.
keepAliveTimer = new Timer(CheckLastMessage, null, 0, 1); // Check if we're running every second.
}

private async void OpenService()
Expand All @@ -42,7 +42,7 @@ private async void OpenService()
private void CheckLastMessage(object state)
{
// Auto close off bridge if we aren't receiving any data. This fixes LSLBridge not being shut down after closing BlueMuse.
if(lastMessageTime != DateTime.MinValue && (DateTime.Now - lastMessageTime).Seconds > 2)
if (lastMessageTime != DateTime.MinValue && (DateTime.Now - lastMessageTime).Seconds > 2)
{
CloseBridge();
}
Expand Down Expand Up @@ -105,7 +105,7 @@ private void LSLService_RequestReceived(AppServiceConnection sender, AppServiceR
data2D[j, i] = data1D[(i * Constants.MUSE_SAMPLE_COUNT) + j];
}
}
double[] timestamps = ((double[])message[Constants.LSL_MESSAGE_CHUNK_TIMESTAMPS]);
double[] timestamps = ((double[])message[Constants.LSL_MESSAGE_CHUNK_TIMESTAMPS]);
stream.PushChunkLSL(data2D, timestamps);
}
}
Expand Down
16 changes: 15 additions & 1 deletion LSLBridge/ViewModels/MainWindowVM.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LSLBridge.Helpers;
using LSLBridge.LSLManagement;
using System.Windows;

namespace LSLBridge.ViewModels
{
Expand All @@ -8,7 +9,20 @@ public class MainWindowVM : ObservableObject
private MuseLSLStreamManager museLSLStreamManager;
public ObservableCollection<MuseLSLStream> MuseStreams { get; set; }
private int museStreamCount;
public int MuseStreamCount { get { return museStreamCount; } set { SetProperty(ref museStreamCount, value); } }
public int MuseStreamCount
{
get { return museStreamCount;
}
set {
SetProperty(ref museStreamCount, value);
if (value < 1)
WindowVisible = Visibility.Hidden;
else
WindowVisible = Visibility.Visible;
}
}
private Visibility windowVisible = Visibility.Hidden;
public Visibility WindowVisible { get { return windowVisible; } set { SetProperty(ref windowVisible, value); } }

public MainWindowVM()
{
Expand Down
2 changes: 1 addition & 1 deletion LSLBridge/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="LSL Bridge" Height="350" Width="525">
Title="LSL Bridge" Height="350" Width="525" Visibility="{Binding Path=WindowVisible, Mode=TwoWay}">

<StackPanel>
<TextBlock FontSize="18" Margin="5, 5, 5, 5">
Expand Down

0 comments on commit 86bd090

Please sign in to comment.