Skip to content

Commit

Permalink
#47 Prop test? Prop test!
Browse files Browse the repository at this point in the history
  • Loading branch information
David032 committed Mar 28, 2024
1 parent c84dee9 commit d485ceb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 16 deletions.
4 changes: 4 additions & 0 deletions ModuleTests/ModuleTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@
<ProjectReference Include="..\skullOS.Tests\skullOS.Tests.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="TestData\" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions ModuleTests/PropTest.cs
Original file line number Diff line number Diff line change
@@ -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<ISpeakerService> speakerMock = new();
speakerMock.Setup(speaker => speaker.PlayAudio(It.IsAny<string>())).Returns(Task.CompletedTask);
Mock<ILedService> ledMock = new();

sut = new Prop(speakerMock.Object, ledMock.Object, pathToTestData);
}

[Fact]
public void CanCreateProp()
{
Assert.NotNull(sut);
}
}
}
Empty file.
1 change: 1 addition & 0 deletions skullOS.HardwareServices/Interfaces/ILedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public interface ILedService
void TurnOff(string Pin);
void TurnOn(string Pin);
Dictionary<string, int> GetLeds();
void SetLeds(Dictionary<string, int> ledsToControl);
}
}
5 changes: 5 additions & 0 deletions skullOS.HardwareServices/LedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@ public Dictionary<string, int> GetLeds()
{
return LEDs;
}

public void SetLeds(Dictionary<string, int> ledsToControl)
{
LEDs = ledsToControl;
}
}
}
50 changes: 34 additions & 16 deletions skullOS.Modules/Prop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,23 +22,31 @@ public class Prop : Module, IPropModule
ServoMotor rightFlap;

Dictionary<string, string> 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();
}
Expand All @@ -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<string, int> pins = new Dictionary<string, int>
{
//LEDs should be read from the file as well
Dictionary<string, int> 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;
Expand All @@ -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();
}
Expand Down

0 comments on commit d485ceb

Please sign in to comment.