Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Active Buzzer #160

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}
}
Loading