diff --git a/Source/Meadow.Foundation.Core/Speakers/PiezoSpeaker.cs b/Source/Meadow.Foundation.Core/Speakers/PiezoSpeaker.cs index e7a3ce76f7..e2d3ce3a6f 100644 --- a/Source/Meadow.Foundation.Core/Speakers/PiezoSpeaker.cs +++ b/Source/Meadow.Foundation.Core/Speakers/PiezoSpeaker.cs @@ -1,6 +1,7 @@ using Meadow.Hardware; using Meadow.Peripherals.Speakers; using Meadow.Units; +using System; using System.Threading.Tasks; namespace Meadow.Foundation.Audio @@ -32,8 +33,6 @@ public PiezoSpeaker(IPwmOutputController device, IPin pin, Frequency frequency, /// /// IPwmOutputController to create PWM port /// PWM Pin connected to the PiezoSpeaker - /// PWM frequency - /// Duty cycle public PiezoSpeaker(IPwmOutputController device, IPin pin) : this(device.CreatePwmPort(pin, new Frequency(100, Frequency.UnitType.Hertz), 0)) { } @@ -48,16 +47,25 @@ public PiezoSpeaker(IPwmPort port) Port.Start(); } + /// + /// Play a frequency until stopped by StopTone + /// + /// The frequency in hertz of the tone to be played + public Task PlayTone(Frequency frequency) + { + return PlayTone(frequency, TimeSpan.Zero); + } + /// /// Play a frequency for a specified duration /// /// The frequency in hertz of the tone to be played /// How long the note is played in milliseconds, if durration is 0, tone plays indefinitely - public async Task PlayTone(Frequency frequency, int duration = 0) + public async Task PlayTone(Frequency frequency, TimeSpan duration) { if (frequency.Hertz <= 1) { - throw new System.Exception("Piezo frequency must be greater than 1Hz"); + throw new Exception("Piezo frequency must be greater than 1Hz"); } if (!isPlaying) @@ -67,7 +75,7 @@ public async Task PlayTone(Frequency frequency, int duration = 0) Port.Frequency = frequency; Port.DutyCycle = 0.5f; - if (duration > 0) + if (duration.TotalMilliseconds > 0) { await Task.Delay(duration); Port.DutyCycle = 0f;