Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PiezoSpeaker to take a TimeSpan #368

Merged
merged 1 commit into from
Jul 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Source/Meadow.Foundation.Core/Speakers/PiezoSpeaker.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Meadow.Hardware;
using Meadow.Peripherals.Speakers;
using Meadow.Units;
using System;
using System.Threading.Tasks;

namespace Meadow.Foundation.Audio
Expand Down Expand Up @@ -32,8 +33,6 @@ public PiezoSpeaker(IPwmOutputController device, IPin pin, Frequency frequency,
/// </summary>
/// <param name="device">IPwmOutputController to create PWM port</param>
/// <param name="pin">PWM Pin connected to the PiezoSpeaker</param>
/// <param name="frequency">PWM frequency</param>
/// <param name="dutyCycle">Duty cycle</param>
public PiezoSpeaker(IPwmOutputController device, IPin pin) :
this(device.CreatePwmPort(pin, new Frequency(100, Frequency.UnitType.Hertz), 0))
{ }
Expand All @@ -48,16 +47,25 @@ public PiezoSpeaker(IPwmPort port)
Port.Start();
}

/// <summary>
/// Play a frequency until stopped by StopTone
/// </summary>
/// <param name="frequency">The frequency in hertz of the tone to be played</param>
public Task PlayTone(Frequency frequency)
{
return PlayTone(frequency, TimeSpan.Zero);
}

/// <summary>
/// Play a frequency for a specified duration
/// </summary>
/// <param name="frequency">The frequency in hertz of the tone to be played</param>
/// <param name="duration">How long the note is played in milliseconds, if durration is 0, tone plays indefinitely</param>
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)
Expand All @@ -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;
Expand Down