Class 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 int DEFAULT_PIN
      If no pin is specified by the user, the default BCM pin 18 is used.
      protected com.pi4j.io.pwm.Pwm pwm  
    • 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.
    • Field Detail

      • 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:
        Constant Field Values
      • pwm

        protected final com.pi4j.io.pwm.Pwm pwm
    • Constructor Detail

      • 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 Detail

      • 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