diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/ActiveBuzzerController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/ActiveBuzzerController.java new file mode 100644 index 00000000..d25c60de --- /dev/null +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/ActiveBuzzerController.java @@ -0,0 +1,68 @@ +package com.opensourcewithslu.components.controllers; + +import com.opensourcewithslu.outputdevices.ActiveBuzzerHelper; +import com.pi4j.io.pwm.Pwm; +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import jakarta.inject.Named; + +@Controller("/active-buzzer") +public class ActiveBuzzerController { + + private final ActiveBuzzerHelper activeBuzzerHelper; + + public ActiveBuzzerController(@Named("active-buzzer") Pwm activeBuzzerOutput){ + this.activeBuzzerHelper = new ActiveBuzzerHelper(activeBuzzerOutput); + } + + /** + * Enables the active buzzer + */ + @Get("/enable") + public void enableActiveBuzzer(){ + + activeBuzzerHelper.activeBuzzerOn(); + + } + + /** + * Disables the active buzzer + */ + + @Get("/disable") + public void disableActiveBuzzer(){ + + activeBuzzerHelper.activeBuzzerOff(); + + } + + /** + * Emits an beep sound from the active buzzer. + */ + @Get("/beepTone") + public void playBeepTone(){ + + activeBuzzerHelper.beep(); + + } + + /** + * Emits an intermittent tone from the active buzzer for a duration of 20 seconds. + * 10 seconds of sound and 10 seconds of silence + */ + @Get("/intermittentTone") + public void playIntermittentTone(){ + + activeBuzzerHelper.intermittentTone(); + + } + + /** + * Tests the active buzzer by emitting the word "pi" in morse code. + */ + @Get("/morseCode") + public void morseCodeTest(){ + + activeBuzzerHelper.morseCodeTone(); + } +} \ No newline at end of file diff --git a/components/src/main/resources/application.yml b/components/src/main/resources/application.yml index 902557bb..c5b41659 100644 --- a/components/src/main/resources/application.yml +++ b/components/src/main/resources/application.yml @@ -34,6 +34,13 @@ pi4j: provider: pigpio-pwm initial: 0 shutdown: 0 + active-buzzer: + name: active-buzzer + address: 17 + pwmType: SOFTWARE + provider: pigpio-pwm + initial: 0 + shutdown: 0 i2c: # tag::i2c[] lcd: # <1> diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ActiveBuzzerHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ActiveBuzzerHelper.java new file mode 100644 index 00000000..ddc3f03b --- /dev/null +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ActiveBuzzerHelper.java @@ -0,0 +1,143 @@ +package com.opensourcewithslu.outputdevices; + + + +import com.pi4j.io.pwm.Pwm; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The ActiveBuzzerHelper class contains methods that pertain to the control of the active buzzer. + * + * IMPORTANT NOTE: WIRING MUST BE DIRECT. USAGE OF TRANSISTOR ALTERS THE FUNCTIONALITIES OF THE HELPER. + */ +public class ActiveBuzzerHelper { + + private static final Logger log = LoggerFactory.getLogger(ActiveBuzzerHelper.class); + + private final Pwm activeBuzzer; + + protected boolean actBuzzCheck; + + + /** + * BuzzerHelper constructor + * @param activeBuzzer instance of a Pwm object + */ + //tag::const[] + public ActiveBuzzerHelper( Pwm activeBuzzer){ + //end::const[] + + this.activeBuzzer = activeBuzzer; + } + + + /** + * Turns the active buzzer on by setting the duty cycle is 100 and frequency to 440hz. + */ + //tag::method[] + public void activeBuzzerOn(){ + //end::method[] + log.info("Active buzzer is on."); + + + this.activeBuzzer.on(100, 440); //Duty Cycle must be 100 and Frequency should be 440. + actBuzzCheck = true; + + + } + /** + * Turns the active buzzer off. + */ + //tag::method[] + public void activeBuzzerOff(){ + //end::method[] + log.info("Active Buzzer is off."); + + this.activeBuzzer.off(); + + actBuzzCheck = false; + + } + + /** + * Beep powers on, plays a single tone from the active buzzer for 2 seconds then powers down. + */ + //tag::method[] + public void beep(){ + //end::method[] + + activeBuzzerOff(); //Turn off buzzer in case it's on + + log.info("Beeps for 2 seconds."); + activeBuzzerOn(); + try { + Thread.sleep(2000); //Wait and alert once for 2 seconds (2000 milliseconds) + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + activeBuzzerOff(); + + } + + /** + * Intermittent tone will play a tone for a 20 seconds duration. During this duration the + * buzzer will be on for 10 seconds and off for 10 seconds. + */ + //tag::method[] + public void intermittentTone(){ + //end::method[] + + int sleepDuration = 1000; //1000 milliseconds equals 1 second + int totalActiveDur = 0; + activeBuzzerOff(); + + log.info("Buzzer turns on for 10 seconds."); + while (true){ + activeBuzzerOn(); + try{ + Thread.sleep(sleepDuration); + totalActiveDur += sleepDuration; + + } catch (InterruptedException e){ + Thread.currentThread().interrupt(); + } + activeBuzzerOff(); + try{ + Thread.sleep(sleepDuration); + } catch (InterruptedException e){ + Thread.currentThread().interrupt(); + } + if (totalActiveDur == 10000){ + break; + } + } + } + + /** + * Uses the active buzzer on and off function to beep the word pi in morse code. + */ + //tag::method[] + public void morseCodeTone(){ + //end::method[] + int [] morseCode = {200,600,600,200,200,200}; // Durations for .--. .. (pi in Morse) + int gapDuration = 200; //Gap between the signals + activeBuzzerOff(); + + log.info("Playing morse code for 'Pi'."); + for (int duration : morseCode){ + activeBuzzerOn(); + try{ + Thread.sleep(duration); //Play the tone + } catch (InterruptedException e){ + Thread.currentThread().interrupt(); + } + activeBuzzerOff(); + try{ + Thread.sleep(gapDuration); //Pause between tones + } catch (InterruptedException e){ + Thread.currentThread().interrupt(); + } + } + } +}