Skip to content

Commit

Permalink
Merge pull request #53 from David032/51-megacon-birmingham-2024-integ…
Browse files Browse the repository at this point in the history
…ration

51 MegaCon Birmingham 2024 Integration
  • Loading branch information
David032 authored Mar 22, 2024
2 parents 72711d5 + b71d746 commit 2656413
Show file tree
Hide file tree
Showing 29 changed files with 313 additions and 126 deletions.
20 changes: 20 additions & 0 deletions Mk1Pinout-Outdated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
|Env Sensor|Grey |x|x| | |
|Env Sensor|Green |x|x|White |RGB Array |
|Env Sensor|Blue |x|x|Grey |RGB Array |
| | |x|x| | |
|Env Sensor|Purple|x|x| | |
| | |x|x| | |
| | |x|x|Brown |Power LED |
| | |x|x|Red |Power LED |
| | |x|x| | |
|RGB Array |Black |x|x|Yellow |Button |
| | |x|x|Yellow |Button |
| | |x|x| | |
|Buzzer |Black |x|x| | |
| | |x|x| | |
| | |x|x|Orange |Notification LED|
| | |x|x|Yellow |Notification LED|
|Buzzer |White |x|x|Green |Warning LED |
| | |x|x|Dark Blue|Warning LED |
|White LED |Red |x|x| | |
|White LED |Brown |x|x| | |
20 changes: 0 additions & 20 deletions Pinout.md

This file was deleted.

2 changes: 1 addition & 1 deletion ServoSkull.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EE38A10C-F0BB-40B5-B1E6-8CB16C6E452C}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Pinout.md = Pinout.md
Mk1Pinout-Outdated.md = Mk1Pinout-Outdated.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "skullOS.API", "skullOS.API\skullOS.API.csproj", "{5E1EC385-4B61-4E8C-82C7-46695A33AC72}"
Expand Down
10 changes: 4 additions & 6 deletions skullOS.API/Controllers/CameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ public CameraController(ILogger<CameraController> logger, ICameraModule camera)
}

[HttpGet("TakePicture")]
public string TakePicture()
public async Task TakePicture()
{
_module.TakePicture();
return "Picture Taken!";
await _module.TakePicture();
}

[HttpGet("RecordVideo")]
public string RecordVideo()
public async Task RecordVideo()
{
_module.RecordShortVideo();
return "Video Recorded!";
await _module.RecordShortVideo();
}
}
}
12 changes: 9 additions & 3 deletions skullOS.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using skullOS.Core;
using skullOS.Modules;
using skullOS.Modules.Interfaces;

Expand All @@ -15,13 +16,18 @@ public static void Main(string[] args)
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.WebHost.UseUrls("http://*:5000;https://*:5001");
builder.WebHost.UseUrls("http://*:5000;");

#region Skull Configuration
FileManager.CreateSkullDirectory(false);
#endregion

#region Skull Modules
builder.Services.AddScoped<ICameraModule, Camera>();
builder.Services.AddScoped<IBuzzerModule, Buzzer>();
builder.Services.AddSingleton<ICameraModule, Camera>();
builder.Services.AddSingleton<IBuzzerModule, Buzzer>();
#endregion


var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
71 changes: 53 additions & 18 deletions skullOS.HardwareServices/CameraService.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,91 @@
using Iot.Device.Camera.Settings;
using Iot.Device.Camera.Settings;
using Iot.Device.Common;
using Iot.Device.Media;
using skullOS.HardwareServices.Exceptions;
using skullOS.HardwareServices.Interfaces;
using System.Diagnostics;

namespace skullOS.HardwareServices
{
public class CameraService : ICameraService
{
public VideoDevice Camera { get; private set; }
private ProcessSettings _processSettings;
int xResolution;
int yResolution;

private readonly ProcessSettings _processSettings;

public CameraService(int x = 2592, int y = 1944)
{
_processSettings = ProcessSettingsFactory.CreateForLibcamerastill();
xResolution = x;
yResolution = y;
}

public CameraService(VideoConnectionSettings? cameraSettings = null)
//Why are we back to hard-calling libcamera again?! :(
//Remember that this needs rpicam-apps full to be installed(lite os doesn't come with it)
//TODO: Still needs quality setting
public async Task<string> RecordShortVideoAsync(string fileLocation, bool useMic)
{
cameraSettings ??= new(busId: 0, captureSize: (2592, 1944), pixelFormat: VideoPixelFormat.JPEG);
Camera = VideoDevice.Create(cameraSettings);
Camera.Settings.HorizontalFlip = true;
Camera.Settings.VerticalFlip = true;
try
{
using Process videoRecording = new();
string args = string.Empty;
if (useMic)
{
args = " --codec libav --hflip --vflip --libav-audio --width 1920 --height 1080 -t 30000 -o " + $"{fileLocation}"
+ DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp4";
}
else
{
args = " --codec libav --hflip --vflip --width 1920 --height 1080 -t 30000 -o " + $"{fileLocation}"
+ DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp4";
}
videoRecording.StartInfo.UseShellExecute = false;
videoRecording.StartInfo.FileName = "libcamera-vid";
videoRecording.StartInfo.Arguments = args;
videoRecording.EnableRaisingEvents = true;
#if DEBUG
await Console.Out.WriteLineAsync("Running:");
await Console.Out.WriteLineAsync(videoRecording.StartInfo.FileName + videoRecording.StartInfo.Arguments);
#endif
videoRecording.Start();
await Task.WhenAny(Task.Delay(30000));
}
catch (CameraErrorException e)
{
await Console.Out.WriteLineAsync(e.Message);
return "Camera errored when recording video!";
}

_processSettings = ProcessSettingsFactory.CreateForLibcamerastill();
return $"({DateTime.Now}) Short video recorded!";
}

public async Task<string> TakePictureAsync(string fileLocation, int x = 2592, int y = 1944)
public async Task<string> TakePictureAsync(string fileLocation)
{
var builder = new CommandOptionsBuilder()
.WithTimeout(1)
.WithVflip()
.WithHflip()
.WithPictureOptions(quality: 100)
.WithResolution(x, y);
.WithResolution(xResolution, yResolution);
var args = builder.GetArguments();

_processSettings = ProcessSettingsFactory.CreateForLibcamerastill();
using var proc = new ProcessRunner(_processSettings);
Console.WriteLine("Using the following command line:");
Console.WriteLine(proc.GetFullCommandLine(args));
Console.WriteLine();

var timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
string? filename = fileLocation + timestamp + ".jpg";
//string? filename = $"{DateTime.Now:yyyyMMddHHmmss}.jpg"; //Fakename

try
{
using var file = File.OpenWrite(filename);
await proc.ExecuteAsync(args, file);
}
catch (Exception)
{
await Console.Out.WriteLineAsync("Cam errored!");
await Console.Out.WriteLineAsync("Cam errored when taking picture!");
}

return filename;
return $"({DateTime.Now}) Picture taken!";
}
}
}
14 changes: 14 additions & 0 deletions skullOS.HardwareServices/Exceptions/CameraErrorException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace skullOS.HardwareServices.Exceptions
{
public enum CameraMode
{
Picture,
ShortVideo,
LongVideo
}

public class CameraErrorException(string message, CameraMode modeWhenFailed) : Exception(message)

Check warning on line 10 in skullOS.HardwareServices/Exceptions/CameraErrorException.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Parameter 'modeWhenFailed' is unread.

Check warning on line 10 in skullOS.HardwareServices/Exceptions/CameraErrorException.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Parameter 'modeWhenFailed' is unread.
{
public CameraMode Mode;
}
}
3 changes: 2 additions & 1 deletion skullOS.HardwareServices/Interfaces/ICameraService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace skullOS.HardwareServices.Interfaces
{
public interface ICameraService
{
Task<string> TakePictureAsync(string fileLocation, int x = 2592, int y = 1944);
Task<string> TakePictureAsync(string fileLocation);
Task<string> RecordShortVideoAsync(string fileLocation, bool useMic);
}
}
4 changes: 2 additions & 2 deletions skullOS.HardwareServices/Interfaces/ILedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
public interface ILedService
{
void BlinkLight(string light);
void TurnOff(int Pin);
void TurnOn(int Pin);
void TurnOff(string Pin);
void TurnOn(string Pin);
Dictionary<string, int> GetLeds();
}
}
23 changes: 12 additions & 11 deletions skullOS.HardwareServices/LedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@ public LedService(Dictionary<string, int> leds)

public async void BlinkLight(string light)
{
int pin = LEDs[light];
TurnOn(pin);
TurnOn(light);
await Task.Delay(750);
TurnOff(pin);
TurnOff(light);
}

public void TurnOn(int Pin)
public void TurnOn(string light)
{
if (!controller.IsPinOpen(Pin))
int pin = LEDs[light];
if (!controller.IsPinOpen(pin))
{
controller.OpenPin(Pin, PinMode.Output);
controller.OpenPin(pin, PinMode.Output);
}
controller.Write(Pin, PinValue.High);
controller.Write(pin, PinValue.High);
}

public void TurnOff(int Pin)
public void TurnOff(string light)
{
if (!controller.IsPinOpen(Pin))
int pin = LEDs[light];
if (!controller.IsPinOpen(pin))
{
controller.OpenPin(Pin, PinMode.Output);
controller.OpenPin(pin, PinMode.Output);
}
controller.Write(Pin, PinValue.Low);
controller.Write(pin, PinValue.Low);
}

public Dictionary<string, int> GetLeds()
Expand Down
3 changes: 2 additions & 1 deletion skullOS.HardwareServices/SpeakerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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
/// Requires `sudo apt install --no-install-recommends vlc-bin vlc-plugin-base` to have been ran first which isn't on lite os
/// If deploying to a pizero, may also need to do the following: https://learn.adafruit.com/adafruit-max98357-i2s-class-d-mono-amp/raspberry-pi-usage
/// </summary>
public class SpeakerService : ISpeakerService
{
Expand Down
2 changes: 1 addition & 1 deletion skullOS.Modules/Adventure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Adventure()

private void TakePicture_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
cameraService.TakePictureAsync(directory, 1920, 1080);
cameraService.TakePictureAsync(directory);
}

public override void OnAction(object? sender, EventArgs e)
Expand Down
Loading

0 comments on commit 2656413

Please sign in to comment.