diff --git a/BlueMuse/Bluetooth/BluetoothManager.cs b/BlueMuse/Bluetooth/BluetoothManager.cs index e681e82..9de713e 100644 --- a/BlueMuse/Bluetooth/BluetoothManager.cs +++ b/BlueMuse/Bluetooth/BluetoothManager.cs @@ -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; @@ -44,6 +45,13 @@ public static BluetoothManager Instance private BluetoothManager() { Muses = new ObservableCollection(); + 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() @@ -282,7 +290,6 @@ public async Task StopStreamingAll() { await muse.ToggleStream(false); } - //await DeactivateLSLBridge(); } } diff --git a/BlueMuse/Misc/Constants.cs b/BlueMuse/Misc/Constants.cs index a79c93d..393e6df 100644 --- a/BlueMuse/Misc/Constants.cs +++ b/BlueMuse/Misc/Constants.cs @@ -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); diff --git a/BlueMuse/Package.appxmanifest b/BlueMuse/Package.appxmanifest index 6a5ce68..783de48 100644 --- a/BlueMuse/Package.appxmanifest +++ b/BlueMuse/Package.appxmanifest @@ -15,7 +15,7 @@ - + @@ -27,12 +27,12 @@ - - - + + + diff --git a/BlueMuse/Win32/LSLBridge.exe b/BlueMuse/Win32/LSLBridge.exe index 4c28e3d..17ef4c3 100644 Binary files a/BlueMuse/Win32/LSLBridge.exe and b/BlueMuse/Win32/LSLBridge.exe differ diff --git a/LSLBridge/LSL/MuseLSLStreamManager.cs b/LSLBridge/LSL/MuseLSLStreamManager.cs index 5e3f9d0..e79f0c2 100644 --- a/LSLBridge/LSL/MuseLSLStreamManager.cs +++ b/LSLBridge/LSL/MuseLSLStreamManager.cs @@ -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 museStreams, Action museStreamCountSetter) { @@ -31,7 +31,7 @@ public MuseLSLStreamManager(ObservableCollection 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() @@ -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(); } @@ -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); } } diff --git a/LSLBridge/ViewModels/MainWindowVM.cs b/LSLBridge/ViewModels/MainWindowVM.cs index 91d664f..4c3d16a 100644 --- a/LSLBridge/ViewModels/MainWindowVM.cs +++ b/LSLBridge/ViewModels/MainWindowVM.cs @@ -1,5 +1,6 @@ using LSLBridge.Helpers; using LSLBridge.LSLManagement; +using System.Windows; namespace LSLBridge.ViewModels { @@ -8,7 +9,20 @@ public class MainWindowVM : ObservableObject private MuseLSLStreamManager museLSLStreamManager; public ObservableCollection 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() { diff --git a/LSLBridge/Windows/MainWindow.xaml b/LSLBridge/Windows/MainWindow.xaml index ad75a1b..ebc540a 100644 --- a/LSLBridge/Windows/MainWindow.xaml +++ b/LSLBridge/Windows/MainWindow.xaml @@ -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}">