Skip to content

Commit

Permalink
Active Buzzer (#160)
Browse files Browse the repository at this point in the history
* Link to Buzzer issue. Helper and Controller classes have been tested and functionality is verified.

* Resolved javadoc comments and messages. Removed errant commented code. Tested and verified functionality on pi with jdk 17

* Fixed comments in both files. Comments didn't provide an accuarate description.

* Fixed the comment at the top of the file so it will be recognized as a javadoc comment.
  • Loading branch information
adelgadoj3 authored Feb 26, 2024
1 parent cc167fb commit ab1f474
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
7 changes: 7 additions & 0 deletions components/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}

0 comments on commit ab1f474

Please sign in to comment.