From d485ceb16b959b7bd3e97468cd145be69a8222f5 Mon Sep 17 00:00:00 2001 From: David K Date: Thu, 28 Mar 2024 12:00:02 +0000 Subject: [PATCH] #47 Prop test? Prop test! --- ModuleTests/ModuleTests.csproj | 4 ++ ModuleTests/PropTest.cs | 27 ++++++++++ ModuleTests/TestData/PropSettings.txt | 0 .../Interfaces/ILedService.cs | 1 + skullOS.HardwareServices/LedService.cs | 5 ++ skullOS.Modules/Prop.cs | 50 +++++++++++++------ 6 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 ModuleTests/PropTest.cs create mode 100644 ModuleTests/TestData/PropSettings.txt diff --git a/ModuleTests/ModuleTests.csproj b/ModuleTests/ModuleTests.csproj index c1ae6b1..5778424 100644 --- a/ModuleTests/ModuleTests.csproj +++ b/ModuleTests/ModuleTests.csproj @@ -30,4 +30,8 @@ + + + + diff --git a/ModuleTests/PropTest.cs b/ModuleTests/PropTest.cs new file mode 100644 index 0000000..3748f8b --- /dev/null +++ b/ModuleTests/PropTest.cs @@ -0,0 +1,27 @@ +using Moq; +using skullOS.HardwareServices.Interfaces; +using skullOS.Modules; + +namespace ModuleTests +{ + public class PropTest + { + Prop sut; + string pathToTestData = @"TestData/PropSettings.txt"; + + public PropTest() + { + Mock speakerMock = new(); + speakerMock.Setup(speaker => speaker.PlayAudio(It.IsAny())).Returns(Task.CompletedTask); + Mock ledMock = new(); + + sut = new Prop(speakerMock.Object, ledMock.Object, pathToTestData); + } + + [Fact] + public void CanCreateProp() + { + Assert.NotNull(sut); + } + } +} diff --git a/ModuleTests/TestData/PropSettings.txt b/ModuleTests/TestData/PropSettings.txt new file mode 100644 index 0000000..e69de29 diff --git a/skullOS.HardwareServices/Interfaces/ILedService.cs b/skullOS.HardwareServices/Interfaces/ILedService.cs index 9aa3781..0f637b4 100644 --- a/skullOS.HardwareServices/Interfaces/ILedService.cs +++ b/skullOS.HardwareServices/Interfaces/ILedService.cs @@ -6,5 +6,6 @@ public interface ILedService void TurnOff(string Pin); void TurnOn(string Pin); Dictionary GetLeds(); + void SetLeds(Dictionary ledsToControl); } } \ No newline at end of file diff --git a/skullOS.HardwareServices/LedService.cs b/skullOS.HardwareServices/LedService.cs index b250799..c0e5ad4 100644 --- a/skullOS.HardwareServices/LedService.cs +++ b/skullOS.HardwareServices/LedService.cs @@ -45,5 +45,10 @@ public Dictionary GetLeds() { return LEDs; } + + public void SetLeds(Dictionary ledsToControl) + { + LEDs = ledsToControl; + } } } diff --git a/skullOS.Modules/Prop.cs b/skullOS.Modules/Prop.cs index 3fe5481..7b92b1d 100644 --- a/skullOS.Modules/Prop.cs +++ b/skullOS.Modules/Prop.cs @@ -10,8 +10,8 @@ namespace skullOS.Modules { public class Prop : Module, IPropModule { - public ISpeakerService SpeakerService { get; set; } - public ILedService LedService { get; set; } + ISpeakerService SpeakerService; + ILedService LedService; static Timer? PlayIdleSound; double interval = 30000; @@ -22,23 +22,31 @@ public class Prop : Module, IPropModule ServoMotor rightFlap; Dictionary propSettings; -#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. - public Prop() -#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. + public Prop(ISpeakerService speaker = null, ILedService leds = null, string pathToSettings = @"Data/PropSettings.txt") { - propSettings = SettingsLoader.LoadConfig(@"Data/PropSettings.txt"); + propSettings = SettingsLoader.LoadConfig(pathToSettings); propSettings.TryGetValue("Sounds", out string soundsState); bool useSounds = bool.Parse(soundsState); if (propSettings.ContainsKey("Sounds") && useSounds) { - SpeakerService = new SpeakerService(); + if (speaker == null) + { + SpeakerService = new SpeakerService(); + } + else + { + SpeakerService = speaker; + } + SpeakerService.PlayAudio(@"Resources/computer-startup-music.mp3"); //This one won't await :( - sounds = Directory.GetFiles(@"Resources/Haro/Idles"); + sounds = Directory.GetFiles(@"Resources/Haro/Idles"); //This shouldn't be hardcoded like this at all >:( numberOfIdles = sounds.Length; - PlayIdleSound = new Timer(interval); - PlayIdleSound.AutoReset = true; + PlayIdleSound = new Timer(interval) + { + AutoReset = true + }; PlayIdleSound.Elapsed += PlayIdleSound_Elapsed; PlayIdleSound.Start(); } @@ -47,13 +55,23 @@ public Prop() bool useLights = bool.Parse(lightsState); if (propSettings.ContainsKey("Lights") && useLights) { - //Left and right eye, these are next to each other so it should be easy to tell - Dictionary pins = new Dictionary - { + //LEDs should be read from the file as well + Dictionary pins = new() + { { "LeftEye", 26 }, {"RightEye", 26 } }; - LedService = new LedService(pins); + + if (leds == null) + { + LedService = new LedService(pins); + } + else + { + LedService = leds; + LedService.SetLeds(pins); + } + foreach (var item in LedService.GetLeds()) { string pin = item.Key; @@ -65,10 +83,10 @@ public Prop() bool useServos = bool.Parse(servosState); if (propSettings.ContainsKey("Servos") && useServos) { - SoftwarePwmChannel leftPWM = new SoftwarePwmChannel(5, 50); + SoftwarePwmChannel leftPWM = new(5, 50); leftFlap = new ServoMotor(leftPWM); leftFlap.Start(); - SoftwarePwmChannel rightPWM = new SoftwarePwmChannel(6, 50); + SoftwarePwmChannel rightPWM = new(6, 50); rightFlap = new ServoMotor(rightPWM); rightFlap.Start(); }