Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#29 Speaker support added #40

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 8 additions & 3 deletions skullOS.Core/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public static void CreateSkullDirectory(bool usePersonalDir = true)
{
DirectoryInfo rootDirectory = null;

Check warning on line 11 in skullOS.Core/FileManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Converting null literal or possible null value to non-nullable type.

Check warning on line 11 in skullOS.Core/FileManager.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Converting null literal or possible null value to non-nullable type.
string pathToDir;
if (usePersonalDir)
{
Expand All @@ -23,14 +23,16 @@
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
rootDirectory = Directory.CreateDirectory(pathToDir + @"/skullOS",
unixCreateMode: UnixFileMode.OtherRead | UnixFileMode.OtherWrite | UnixFileMode.OtherExecute);
unixCreateMode: UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute |
UnixFileMode.GroupRead | UnixFileMode.GroupWrite | UnixFileMode.GroupExecute |
UnixFileMode.OtherRead | UnixFileMode.OtherWrite | UnixFileMode.OtherExecute);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
rootDirectoryPath = rootDirectory.FullName;

Check warning on line 35 in skullOS.Core/FileManager.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 35 in skullOS.Core/FileManager.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Dereference of a possibly null reference.
}

public static string GetSkullDirectory()
Expand All @@ -40,9 +42,12 @@

public static void CreateSubDirectory(string directoryName)
{
if (rootDirectoryPath != string.Empty)
if (rootDirectoryPath != string.Empty && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Directory.CreateDirectory(@rootDirectoryPath + "/" + directoryName);
Directory.CreateDirectory(rootDirectoryPath + @"/" + directoryName,
unixCreateMode: UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute |
UnixFileMode.GroupRead | UnixFileMode.GroupWrite | UnixFileMode.GroupExecute |
UnixFileMode.OtherRead | UnixFileMode.OtherWrite | UnixFileMode.OtherExecute);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion skullOS.HardwareServices/Interfaces/ISpeakerService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace skullOS.HardwareServices.Interfaces

namespace skullOS.HardwareServices.Interfaces
{
public interface ISpeakerService
{
Task PlayAudio(string filepath);
}
}
38 changes: 32 additions & 6 deletions skullOS.HardwareServices/SpeakerService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
using Iot.Device.Media;
using skullOS.HardwareServices.Interfaces;
using skullOS.HardwareServices.Interfaces;
using System.Diagnostics;

namespace skullOS.HardwareServices
{
/// <summary>
/// Service to communicate with audio output
/// Requires `sudo apt install --no-install-recommends vlc-bin vlc-plugin-base` to have been ran first
/// </summary>
public class SpeakerService : ISpeakerService
{
public SoundDevice Speaker { get; private set; }
private TaskCompletionSource<bool> eventHandled;

public SpeakerService(SoundConnectionSettings micSettings)
public SpeakerService()

Check warning on line 14 in skullOS.HardwareServices/SpeakerService.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Non-nullable field 'eventHandled' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 14 in skullOS.HardwareServices/SpeakerService.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Non-nullable field 'eventHandled' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
micSettings ??= new SoundConnectionSettings();
Speaker = SoundDevice.Create(micSettings);

}

public async Task PlayAudio(string filepath)
{
eventHandled = new TaskCompletionSource<bool>();
using Process soundPlayback = new Process();

string args = " --play-and-exit " + filepath;
soundPlayback.StartInfo.UseShellExecute = false;
soundPlayback.StartInfo.FileName = "cvlc";
soundPlayback.StartInfo.Arguments = args;
soundPlayback.EnableRaisingEvents = true;
soundPlayback.Exited += SoundPlayback_Exited;
#if DEBUG
await Console.Out.WriteLineAsync(soundPlayback.StartInfo.FileName + soundPlayback.StartInfo.Arguments);
#endif
soundPlayback.Start();
await soundPlayback.WaitForExitAsync();
}

private void SoundPlayback_Exited(object? sender, EventArgs e)
{
eventHandled.TrySetResult(true);
}
}
}
15 changes: 13 additions & 2 deletions skullOS.Modules/Prop.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
namespace skullOS.Modules
using skullOS.HardwareServices;
using skullOS.HardwareServices.Interfaces;

namespace skullOS.Modules
{
public class Prop : Module, IPropModule
{
public ISpeakerService SpeakerService { get; set; }

public Prop()
{
SpeakerService = new SpeakerService();
SpeakerService.PlayAudio(@"Resources/computer-startup-music.mp3"); //This one won't await :(
}

public override void OnAction(object? sender, EventArgs e)
{
throw new NotImplementedException();
Expand All @@ -13,7 +24,7 @@ public override void OnEnable(string[] args)
}
public override string ToString()
{
throw new NotImplementedException();
return "Prop";
}
}
}
Binary file not shown.
4 changes: 4 additions & 0 deletions skullOS.Modules/skullOS.Modules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@

<ItemGroup>
<Folder Include="Data\" />
<Folder Include="Resources\" />
</ItemGroup>

<ItemGroup>
<None Update="Data\CameraSettings.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\computer-startup-music.mp3">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
File renamed without changes.
3 changes: 3 additions & 0 deletions skullOS/Data/HaroModules.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Camera=True
Prop=True
Buzzer=True
7 changes: 6 additions & 1 deletion skullOS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace skullOS
{
public class Program
{
static bool usePersonalDir = false;
public static async Task Main(string[] args)
{
if (args.Length == 0)
Expand All @@ -15,6 +16,10 @@ public static async Task Main(string[] args)
Environment.Exit(-1);
}
string input = args[0].ToLower();
if (args.Length == 2)
{
bool.TryParse(args[1], out usePersonalDir);
}
switch (input)
{
case "run":
Expand Down Expand Up @@ -42,7 +47,7 @@ static void Run(bool shouldCreateDirectory = true)
{
if (shouldCreateDirectory)
{
FileManager.CreateSkullDirectory(false);
FileManager.CreateSkullDirectory(usePersonalDir);
}

SkullLogger logger = new();
Expand Down
Loading