-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
cc167fb
commit ab1f474
Showing
3 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...ts/src/main/java/com/opensourcewithslu/components/controllers/ActiveBuzzerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ActiveBuzzerHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |