Skip to content

Commit

Permalink
#17 Services & Modules
Browse files Browse the repository at this point in the history
Hardware Services implemented, modules laid out, camera implemented but not tested
Still need to work out what I'm doing about the api
  • Loading branch information
David032 committed Nov 21, 2023
1 parent bca04f1 commit 777a884
Show file tree
Hide file tree
Showing 23 changed files with 340 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ServoSkull.sln
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Pinout.md = Pinout.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "skullOS.API", "skullOS.API\skullOS.API.csproj", "{5E1EC385-4B61-4E8C-82C7-46695A33AC72}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "skullOS.API", "skullOS.API\skullOS.API.csproj", "{5E1EC385-4B61-4E8C-82C7-46695A33AC72}"
ProjectSection(ProjectDependencies) = postProject
{464B162B-62FC-49CC-A6AB-72DBE959DD8C} = {464B162B-62FC-49CC-A6AB-72DBE959DD8C}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "skullOS.Modules", "skullOS.Modules\skullOS.Modules.csproj", "{DE800E5A-E68B-4BCA-AC99-4BC9DB44FD56}"
ProjectSection(ProjectDependencies) = postProject
{3CBE1B63-957B-43B6-939D-070EAC517C7C} = {3CBE1B63-957B-43B6-939D-070EAC517C7C}
{464B162B-62FC-49CC-A6AB-72DBE959DD8C} = {464B162B-62FC-49CC-A6AB-72DBE959DD8C}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "skullOS.HardwareServices", "skullOS.HardwareServices\skullOS.HardwareServices.csproj", "{3CBE1B63-957B-43B6-939D-070EAC517C7C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -109,6 +117,14 @@ Global
{5E1EC385-4B61-4E8C-82C7-46695A33AC72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E1EC385-4B61-4E8C-82C7-46695A33AC72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E1EC385-4B61-4E8C-82C7-46695A33AC72}.Release|Any CPU.Build.0 = Release|Any CPU
{DE800E5A-E68B-4BCA-AC99-4BC9DB44FD56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE800E5A-E68B-4BCA-AC99-4BC9DB44FD56}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE800E5A-E68B-4BCA-AC99-4BC9DB44FD56}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE800E5A-E68B-4BCA-AC99-4BC9DB44FD56}.Release|Any CPU.Build.0 = Release|Any CPU
{3CBE1B63-957B-43B6-939D-070EAC517C7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CBE1B63-957B-43B6-939D-070EAC517C7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CBE1B63-957B-43B6-939D-070EAC517C7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CBE1B63-957B-43B6-939D-070EAC517C7C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions skullOS.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static void Main(string[] args)
builder.Services.AddSwaggerGen();
builder.WebHost.UseUrls("http://*:5000;https://*:5001");


var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
14 changes: 14 additions & 0 deletions skullOS.HardwareServices/BuzzerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Iot.Device.Buzzer;
using skullOS.HardwareServices.Interfaces;

namespace skullOS.HardwareServices
{
public class BuzzerService : IBuzzerService
{
public Buzzer Buzzer { get; private set; }
public BuzzerService(int pinNumber)
{
Buzzer = new Buzzer(pinNumber);
}
}
}
80 changes: 80 additions & 0 deletions skullOS.HardwareServices/CameraService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Iot.Device.Media;
using skullOS.HardwareServices.Interfaces;
using System.Diagnostics;

namespace skullOS.HardwareServices
{
public class CameraService : ICameraService
{
public VideoDevice Camera { get; private set; }
private Process? cameraCommand;
private TaskCompletionSource<bool> eventHandled;

public CameraService(VideoConnectionSettings cameraSettings = null)
{
cameraSettings ??= new(busId: 0, captureSize: (2592, 1944), pixelFormat: VideoPixelFormat.JPEG);
Camera = VideoDevice.Create(cameraSettings);
Camera.Settings.HorizontalFlip = true;
Camera.Settings.VerticalFlip = true;
}

#region SharpCamera code(https://github.com/David032/sharpCamera) for recording video
public void RecordVideo(string outputDirectory, int duration)
{
try
{
using (cameraCommand = new Process())
{
string args = "-o " + outputDirectory + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".h264 -t " + (duration * 1000);
cameraCommand.StartInfo.UseShellExecute = false;
cameraCommand.StartInfo.FileName = "raspivid";
cameraCommand.StartInfo.Arguments = args;

#if DEBUG
Console.Out.WriteLine(cameraCommand.StartInfo.Arguments);
#endif

cameraCommand.Start();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

public async Task RecordVideoAsync(string outputDirectory, int duration)
{
eventHandled = new TaskCompletionSource<bool>();
using (cameraCommand = new Process())
{
try
{
string args = "-o " + outputDirectory + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".h264 -t " + (duration * 1000);
cameraCommand.StartInfo.UseShellExecute = false;
cameraCommand.StartInfo.FileName = "raspivid";
cameraCommand.StartInfo.Arguments = args;
cameraCommand.EnableRaisingEvents = true;
cameraCommand.Exited += new EventHandler(VideoRecorded);
#if DEBUG
Console.Out.WriteLine(cameraCommand.StartInfo.Arguments);
#endif
cameraCommand.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}

await Task.WhenAny(eventHandled.Task, Task.Delay(duration * 1000));
}
}

private void VideoRecorded(object sender, EventArgs e)
{
eventHandled.TrySetResult(true);
}
#endregion
}
}
6 changes: 6 additions & 0 deletions skullOS.HardwareServices/Interfaces/IBuzzerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace skullOS.HardwareServices.Interfaces
{
public interface IBuzzerService
{
}
}
6 changes: 6 additions & 0 deletions skullOS.HardwareServices/Interfaces/ICameraService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace skullOS.HardwareServices.Interfaces
{
public interface ICameraService
{
}
}
6 changes: 6 additions & 0 deletions skullOS.HardwareServices/Interfaces/ILedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace skullOS.HardwareServices.Interfaces
{
public interface ILedService
{
}
}
6 changes: 6 additions & 0 deletions skullOS.HardwareServices/Interfaces/IMicrophoneService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace skullOS.HardwareServices.Interfaces
{
public interface IMicrophoneService
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace skullOS.HardwareServices.Interfaces
{
public interface IProgrammableLedService
{
}
}
6 changes: 6 additions & 0 deletions skullOS.HardwareServices/Interfaces/ISpeakerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace skullOS.HardwareServices.Interfaces
{
public interface ISpeakerService
{
}
}
14 changes: 14 additions & 0 deletions skullOS.HardwareServices/LedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using skullOS.HardwareServices.Interfaces;

namespace skullOS.HardwareServices
{
public class LedService : ILedService
{
public Dictionary<string, int> LEDs { get; private set; }

public LedService(Dictionary<string, int> leds)
{
LEDs = leds;
}
}
}
16 changes: 16 additions & 0 deletions skullOS.HardwareServices/MicrophoneService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Iot.Device.Media;
using skullOS.HardwareServices.Interfaces;

namespace skullOS.HardwareServices
{
public class MicrophoneService : IMicrophoneService
{
public SoundDevice Microphone { get; private set; }

public MicrophoneService(SoundConnectionSettings micSettings = null)
{
micSettings ??= new SoundConnectionSettings();
Microphone = SoundDevice.Create(micSettings);
}
}
}
16 changes: 16 additions & 0 deletions skullOS.HardwareServices/ProgrammableLedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Iot.Device.Ws28xx;
using skullOS.HardwareServices.Interfaces;
using System.Device.Spi;

namespace skullOS.HardwareServices
{
public class ProgrammableLedService : IProgrammableLedService
{
public Ws2812b LedArray { get; private set; }

public ProgrammableLedService(SpiDevice spi, int width = 9)
{
LedArray = new Ws2812b(spi, width);
}
}
}
16 changes: 16 additions & 0 deletions skullOS.HardwareServices/SpeakerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Iot.Device.Media;
using skullOS.HardwareServices.Interfaces;

namespace skullOS.HardwareServices
{
public class SpeakerService : ISpeakerService
{
public SoundDevice Speaker { get; private set; }

public SpeakerService(SoundConnectionSettings micSettings)
{
micSettings ??= new SoundConnectionSettings();
Speaker = SoundDevice.Create(micSettings);
}
}
}
14 changes: 14 additions & 0 deletions skullOS.HardwareServices/skullOS.HardwareServices.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Iot.Device.Bindings" Version="3.1.0" />
<PackageReference Include="System.Device.Gpio" Version="3.1.0" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions skullOS.Modules/Camera.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using skullOS.Core;
using skullOS.HardwareServices;
using skullOS.Modules.Interfaces;

namespace skullOS.Modules
{
public enum CameraMode
{
Image,
ShortVideo,
ContinuousVideo
}

public class Camera : Module, ICameraModule
{
public CameraService CameraService;
public MicrophoneService MicrophoneService;
public CameraMode CameraMode = CameraMode.Image;

public Camera()
{
FileManager.CreateSubDirectory("Captures");
CameraService = new CameraService();
MicrophoneService = new MicrophoneService();
}

public void TakePicture()
{
// Console.WriteLine($"({DateTime.Now}) Picture taken!"); //Maybe think about a better logging solution?
CameraService.Camera.Capture($"{FileManager.GetSkullDirectory()}/Captures/{DateTime.Now:yyyyMMddHHmmss}.jpg");
}

public void RecordShortVideo()
{
string audioLocation = $"{FileManager.GetSkullDirectory()}/Captures/{DateTime.Now:yyyyMMddHHmmss}.mp3";
string videoLocation = $"{FileManager.GetSkullDirectory()}/Captures/{DateTime.Now:yyyyMMddHHmmss}";
CameraService.RecordVideoAsync(FileManager.GetSkullDirectory() + "/Captures", 30);
MicrophoneService.Microphone.Record(30, audioLocation);
}
}
}
12 changes: 12 additions & 0 deletions skullOS.Modules/Downlink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skullOS.Modules
{
internal class Downlink
{
}
}
6 changes: 6 additions & 0 deletions skullOS.Modules/Interfaces/ICameraModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace skullOS.Modules.Interfaces
{
internal interface ICameraModule
{
}
}
7 changes: 7 additions & 0 deletions skullOS.Modules/Module.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace skullOS.Modules
{
public abstract class Module
{

}
}
12 changes: 12 additions & 0 deletions skullOS.Modules/Prop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skullOS.Modules
{
internal class Prop
{
}
}
12 changes: 12 additions & 0 deletions skullOS.Modules/QrCodeReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skullOS.Modules
{
internal class QrCodeReader
{
}
}
12 changes: 12 additions & 0 deletions skullOS.Modules/Uplink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skullOS.Modules
{
internal class Uplink
{
}
}
14 changes: 14 additions & 0 deletions skullOS.Modules/skullOS.Modules.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\skullOS.HardwareServices\skullOS.HardwareServices.csproj" />
<ProjectReference Include="..\skullOS.Core\skullOS.Core.csproj" />
</ItemGroup>

</Project>

0 comments on commit 777a884

Please sign in to comment.