From 08510679261101280d98ae7cff4798db21f0b43b Mon Sep 17 00:00:00 2001 From: David K Date: Tue, 14 Nov 2023 12:26:26 +0000 Subject: [PATCH 1/2] #12 Initial refactor But doesn't seem to like the filepath for some reason --- .gitignore | 2 ++ skullOS.Camera/Camera.cs | 2 +- skullOS.Core/FileManager.cs | 27 +++++++++++++++++++++++++++ skullOS/Program.cs | 7 ++++++- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 skullOS.Core/FileManager.cs diff --git a/.gitignore b/.gitignore index 002cef2..3b0d065 100644 --- a/.gitignore +++ b/.gitignore @@ -366,3 +366,5 @@ ServoSkullDemo/Builds skullOS/skullOs/ skullOS/skullOSselfContained/ + +skullOS/builds/ diff --git a/skullOS.Camera/Camera.cs b/skullOS.Camera/Camera.cs index 5d9befb..ce102a4 100644 --- a/skullOS.Camera/Camera.cs +++ b/skullOS.Camera/Camera.cs @@ -66,7 +66,7 @@ public override bool Setup(GpioController controller, I2cDevice i2CDevice) private void TakePicture(object? sender, EventArgs e) { Console.WriteLine($"({DateTime.Now}) Picture taken!"); - device.Capture($"{DateTime.Now:yyyyMMddHHmmss}.jpg"); + device.Capture($"{FileManager.GetSkullDirectory()}/Captures/{DateTime.Now:yyyyMMddHHmmss}.jpg"); } public override void Stop() diff --git a/skullOS.Core/FileManager.cs b/skullOS.Core/FileManager.cs new file mode 100644 index 0000000..4f5688c --- /dev/null +++ b/skullOS.Core/FileManager.cs @@ -0,0 +1,27 @@ +namespace skullOS.Core +{ + public static class FileManager + { + private static string rootDirectoryPath = string.Empty; + + public static void CreateSkullDirectory() + { + DirectoryInfo rootDirectory = null; + string pathToPersonalDir = Environment.GetFolderPath(Environment.SpecialFolder.Personal); + try + { + rootDirectory = Directory.CreateDirectory(@pathToPersonalDir + "/skullOS", unixCreateMode: UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + rootDirectoryPath = rootDirectory.FullName; + } + + public static string GetSkullDirectory() + { + return rootDirectoryPath; + } + } +} \ No newline at end of file diff --git a/skullOS/Program.cs b/skullOS/Program.cs index a815b18..a7911c3 100644 --- a/skullOS/Program.cs +++ b/skullOS/Program.cs @@ -1,4 +1,5 @@ using Iot.Device.Bmxx80; +using skullOS.Core; using skullOS.Core.Interfaces; using System.Device.Gpio; using System.Device.I2c; @@ -34,8 +35,12 @@ public static async Task Main(string[] args) await Task.Delay(Timeout.Infinite); } - static void Run(Modules modulesToLoad = null) + static void Run(Modules modulesToLoad = null, bool shouldCreateDirectory = true) { + if (shouldCreateDirectory) + { + FileManager.CreateSkullDirectory(); + } GpioController controller = new(); const int busId = 1; From 1dc8604966a5fe8fb9bfcdbdf62ce121d016798b Mon Sep 17 00:00:00 2001 From: David K Date: Tue, 14 Nov 2023 14:37:12 +0000 Subject: [PATCH 2/2] #12 Camera now puts out to the correct folder --- skullOS.Camera/Camera.cs | 11 ++++++----- skullOS.Core/FileManager.cs | 12 ++++++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/skullOS.Camera/Camera.cs b/skullOS.Camera/Camera.cs index ce102a4..6331db4 100644 --- a/skullOS.Camera/Camera.cs +++ b/skullOS.Camera/Camera.cs @@ -25,18 +25,19 @@ public override void Run(GpioController controller) public override bool Setup(GpioController controller, I2cDevice i2CDevice) { - var settings = SettingsLoader.LoadConfig(@"Data/Settings.txt"); - var defaultValue = new KeyValuePair("", ""); + FileManager.CreateSubDirectory("Captures"); + Dictionary settings = SettingsLoader.LoadConfig(@"Data/Settings.txt"); + KeyValuePair defaultValue = new("", ""); - var cameraMode = settings + KeyValuePair cameraMode = settings .Select(x => x) .Where(x => x.Key == "Mode") .FirstOrDefault(defaultValue); - var pinToActOn = settings + KeyValuePair pinToActOn = settings .Select(x => x) .Where(x => x.Key == "Pin") .FirstOrDefault(defaultValue); - var ledPin = settings + KeyValuePair ledPin = settings .Select(x => x) .Where(x => x.Key == "LedPin") .FirstOrDefault(defaultValue); diff --git a/skullOS.Core/FileManager.cs b/skullOS.Core/FileManager.cs index 4f5688c..b5cc852 100644 --- a/skullOS.Core/FileManager.cs +++ b/skullOS.Core/FileManager.cs @@ -7,10 +7,10 @@ public static class FileManager public static void CreateSkullDirectory() { DirectoryInfo rootDirectory = null; - string pathToPersonalDir = Environment.GetFolderPath(Environment.SpecialFolder.Personal); + string pathToPersonalDir = @Environment.GetFolderPath(Environment.SpecialFolder.Personal); try { - rootDirectory = Directory.CreateDirectory(@pathToPersonalDir + "/skullOS", unixCreateMode: UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); + rootDirectory = Directory.CreateDirectory(@pathToPersonalDir + @"/skullOS", unixCreateMode: UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); } catch (Exception e) { @@ -23,5 +23,13 @@ public static string GetSkullDirectory() { return rootDirectoryPath; } + + public static void CreateSubDirectory(string directoryName) + { + if (rootDirectoryPath != string.Empty) + { + Directory.CreateDirectory(@rootDirectoryPath + "/" + directoryName); + } + } } } \ No newline at end of file