java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.BuzzerComponent
Implementation of the CrowPi buzzer using PWM with Pi4J
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final int
If no pin is specified by the user, the default BCM pin 18 is used.protected final com.pi4j.io.pwm.Pwm
-
Constructor Summary
ConstructorDescriptionBuzzerComponent
(com.pi4j.context.Context pi4j) Creates a new buzzer component using the default pin.BuzzerComponent
(com.pi4j.context.Context pi4j, int address) Creates a new buzzer component with a custom BCM pin. -
Method Summary
Modifier and TypeMethodDescriptionprotected static com.pi4j.io.pwm.PwmConfig
buildPwmConfig
(com.pi4j.context.Context pi4j, int address) Builds a new PWM configuration for the buzzerprotected com.pi4j.io.pwm.Pwm
getPwm()
Returns the created PWM instance for the buzzervoid
Silences the buzzer and returns immediately.void
playSilence
(int duration) Silences the buzzer and waits for the given duration.void
playTone
(int frequency) Plays a tone with the given frequency in Hz indefinitely.void
playTone
(int frequency, int duration) Plays a tone with the given frequency in Hz for a specific duration.Methods inherited from class com.pi4j.crowpi.components.Component
sleep, triggerSimpleEvent
-
Field Details
-
DEFAULT_PIN
protected static final int DEFAULT_PINIf no pin is specified by the user, the default BCM pin 18 is used.- See Also:
-
pwm
protected final com.pi4j.io.pwm.Pwm pwm
-
-
Constructor Details
-
BuzzerComponent
public BuzzerComponent(com.pi4j.context.Context pi4j) Creates a new buzzer component using the default pin.- Parameters:
pi4j
- Pi4J context
-
BuzzerComponent
public BuzzerComponent(com.pi4j.context.Context pi4j, int address) Creates a new buzzer component with a custom BCM pin.- Parameters:
pi4j
- Pi4J contextaddress
- Custom BCM pin address
-
-
Method Details
-
playTone
public void playTone(int frequency) Plays a tone with the given frequency in Hz indefinitely. This method is non-blocking and returns immediately. A frequency of zero causes the buzzer to play silence.- Parameters:
frequency
- Frequency in Hz
-
playTone
public void playTone(int frequency, int duration) Plays a tone with the given frequency in Hz for a specific duration. This method is blocking and will sleep until the specified duration has passed. A frequency of zero causes the buzzer to play silence. A duration of zero to play the tone indefinitely and return immediately.- Parameters:
frequency
- Frequency in Hzduration
- Duration in milliseconds
-
playSilence
public void playSilence()Silences the buzzer and returns immediately. -
playSilence
public void playSilence(int duration) Silences the buzzer and waits for the given duration. This method is blocking and will sleep until the specified duration has passed.- Parameters:
duration
- Duration in milliseconds
-
getPwm
protected com.pi4j.io.pwm.Pwm getPwm()Returns the created PWM instance for the buzzer- Returns:
- PWM instance
-
buildPwmConfig
protected static com.pi4j.io.pwm.PwmConfig buildPwmConfig(com.pi4j.context.Context pi4j, int address) Builds a new PWM configuration for the buzzer- Parameters:
pi4j
- Pi4J contextaddress
- BCM pin address- Returns:
- PWM configuration
-