Class BuzzerComponent

java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.BuzzerComponent

public class BuzzerComponent extends Component
Implementation of the CrowPi buzzer using PWM with Pi4J
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected 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
     

    Fields inherited from class com.pi4j.crowpi.components.Component

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    BuzzerComponent(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 Type
    Method
    Description
    protected static com.pi4j.io.pwm.PwmConfig
    buildPwmConfig(com.pi4j.context.Context pi4j, int address)
    Builds a new PWM configuration for the buzzer
    protected com.pi4j.io.pwm.Pwm
    Returns the created PWM instance for the buzzer
    void
    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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_PIN

      protected static final int DEFAULT_PIN
      If 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 context
      address - 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 Hz
      duration - 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 context
      address - BCM pin address
      Returns:
      PWM configuration