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

Adding the new components from develop #191

Merged
merged 8 commits into from
Mar 14, 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,60 @@
package com.opensourcewithslu.components.controllers;

import com.opensourcewithslu.inputdevices.PIRSensorHelper;
import com.opensourcewithslu.outputdevices.RGBLEDHelper;
import com.opensourcewithslu.utilities.MultipinConfiguration;
import com.pi4j.io.gpio.digital.DigitalInput;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import jakarta.inject.Named;

/**
* The PIRSensorController class is used with the PIRSensorHelper class and RGBHelper class to implement a PIR motion sensor with an RGB LED light.
*/
@Controller("/pirSensor")
public class PIRSensorController {

private final PIRSensorHelper pirSensorHelper;

private final RGBLEDHelper rgbledHelper;

/**
* The PirSensorController constructor.
* @param pirSensor A Pi4J DigitalInput object.
* @param rgbLed A MultiPinConfiguration object.
*/
public PIRSensorController(@Named("pir-sensor")DigitalInput pirSensor,
@Named("rgb-led-2")MultipinConfiguration rgbLed) {
this.pirSensorHelper = new PIRSensorHelper(pirSensor);
this.rgbledHelper = new RGBLEDHelper(rgbLed);
}

/**
* Enables the PIR sensor by adding an event listener which sets the RGB LED to red when movement is detected and green otherwise.
*/
@Get("/enable")
public void enablePIRSensor() {

int[] red = {255,0,0};
int[] green = {0,255,0};

pirSensorHelper.addEventListener(e -> {
if (pirSensorHelper.isMoving) {
rgbledHelper.setColor(red);
}
else {
rgbledHelper.setColor(green);
}
});
}

/**
* Disables the controller by removing the event listener and turning off the RGB LED.
*/
@Get("/disable")
public void disablePIRSensor() {
pirSensorHelper.removeEventListener();
rgbledHelper.ledOff();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.opensourcewithslu.components.controllers;

import com.opensourcewithslu.outputdevices.PassiveBuzzerHelper;
import com.pi4j.io.pwm.Pwm;
import io.micronaut.http.annotation.*;
import jakarta.inject.Named;
import java.io.File;

@Controller("/passive-buzzer")
public class PassiveBuzzerController {

private final PassiveBuzzerHelper passiveBuzzerHelper;

protected int passBuzzFreq = 440;

protected int passBuzzDC = 50;

public PassiveBuzzerController(@Named("passive-buzzer") Pwm passiveBuzzerOutput){
this.passiveBuzzerHelper = new PassiveBuzzerHelper(passiveBuzzerOutput);
}

/**
* Enables passive buzzer
*/

@Get("/enable")
public void enablePassiveBuzzer(){

passiveBuzzerHelper.passiveBuzzerOn(passBuzzDC, passBuzzFreq);

}

/**
* Disables passive buzzer
*/
@Get("/disable")
public void disablePassiveBuzzer(){

passiveBuzzerHelper.passiveBuzzerOff();

}

/**
*
* Displays the current frequency of the passive buzzer.
*/

@Get("/showFreq")
public void passiveBuzzerFreq(){

passiveBuzzerHelper.getFrequency();

}

/**
*
* Takes one file arg, function will allow use to set their own frequencies
* to be played by the passive buzzer. In order for the frequencies to be played
* the user must do the following:
* - Place their frequencies into a text file with the frequencies separated by commas
* - use the scp command to copy the file over to the raspberrypi
* - (i.e.: scp C:\Users\CompName\filename.txt [email protected]:/home/CompName)
* - Once file is copied over to the pi use the curl -X POST command to play the file
* - (i.e.: curl -X POST "http://localhost:8080/passive-buzzer/setFreq/filename.txt"
*/

@Post("/setFreq/{frequenciesFile}")
public void defineFrequency(String frequenciesFile){
passiveBuzzerHelper.setFrequencies(new File(frequenciesFile));
}

/**
* Validates the functionality of the passive buzzer
*/
@Get("/passBuzz")
public void singlePassiveBuzz(){
passiveBuzzerHelper.passiveBuzzTone();
}

/**
* Ensures that the passive buzzer can cycle through different frequencies
*/
@Get("/freqIter")
public void passiveFreqIter(){
passiveBuzzerHelper.toneIterator();
}

/**
* Calls toneSequence function to play a pre-defined song.
*/
@Get("/playPiSeq")
public void playPiTone(){

passiveBuzzerHelper.piToneSequence();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.opensourcewithslu.components.controllers;


import com.opensourcewithslu.inputdevices.UltraSonicSensorHelper;
import com.pi4j.io.gpio.digital.DigitalInput;
import com.pi4j.io.gpio.digital.DigitalOutput;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import jakarta.inject.Named;


@Controller("/ultraSound")
public class UltraSonicSensorController {

private final UltraSonicSensorHelper ultraSonicSensorHelper;

public UltraSonicSensorController(@Named("ultra-sonic-trig") DigitalOutput trig,
@Named("ultra-sonic-echo") DigitalInput echo) {
this.ultraSonicSensorHelper = new UltraSonicSensorHelper(trig,echo);

}


/**
* Enables the ultrasonic sensor
*/
@Get("/enable")
public String enableUltraSonicSensor() {
this.ultraSonicSensorHelper.startMeasuring();
return "Ultra Sonic Sensor Enabled \nIf the distance is constantly Zero, make sure the sensor field of view is clear \n";
}

/**
* Returns distance from object in centimeters
*/
@Get("/distance/cm")
public String getDistanceInCentimeter() {
return this.ultraSonicSensorHelper.getDistanceInCentimeter() + " cm\n";
}

/**
* Returns distance from object in meters
*/
@Get("/distance/m")
public String getDistanceInMeter() {
return this.ultraSonicSensorHelper.getDistanceInMeters() + " m\n";
}

/**
* Disables ultrasonic sensor
*/
@Get("/disable")
public String disableUltrasoundSensor() {
this.ultraSonicSensorHelper.stopMeasuring();
return "Ultra Sonic Sensor Disabled";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,66 @@
import com.pi4j.io.i2c.I2CConfig;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
import jakarta.inject.Named;

//tag::ex[]
/**
* Controller for managing LCD1602 display operations via HTTP requests.
*/
@Controller("/lcd")
public class lcdController {
private final LCD1602Helper lcdHelper;

public lcdController(@Named("lcd")I2CConfig i2cConfig, Context pi4jContext){
public lcdController(@Named("lcd") I2CConfig i2cConfig, Context pi4jContext) {
this.lcdHelper = new LCD1602Helper(i2cConfig, pi4jContext);
}

@Get("/write/{text}")
public void writeData(String text){
public String writeData(@PathVariable String text) {
lcdHelper.writeText(text);
return "Text written to LCD: " + text + "\n";
}

@Get("/write/{text}/{line}")
public void writeDataAtLine(String text, int line){
public String writeDataAtLine(@PathVariable String text, @PathVariable int line) {
lcdHelper.writeTextAtLine(text, line);
return "Text written to line " + line + ": " + text + "\n";
}

@Get("/backlight/on")
public void backlightOn(){
lcdHelper.setBackLight(true);
@Get("/write/{text}/{line}/{pos}")
public String writeDataAtPos(@PathVariable String text, @PathVariable int line, @PathVariable int pos) {
lcdHelper.displayTextAtPos(text, line, pos);
return "Text written at line " + line + ", position " + pos + ": " + text + "\n";
}

@Get("/backlight/off")
public void backlightOff(){
lcdHelper.setBackLight(false);
@Get("/write/character/{charvalue}")
public String writeCharacter(@PathVariable char charvalue) {
lcdHelper.writeCharacter(charvalue);
return "Character '" + charvalue + "' written to LCD\n";
}

@Get("/backlight/{state}")
public String setBacklight(@PathVariable String state) {
boolean isOn = "on".equalsIgnoreCase(state);
lcdHelper.setBackLight(isOn);
return "Backlight turned " + (isOn ? "on" : "off") + "\n";
}

@Get("/clear/all")
public void clearDisplay(){
public String clearDisplay() {
lcdHelper.clearDisplay();
return "Display cleared\n";
}

@Get("/clear/{line}")
public void clearLine(int line){
public String clearLine(@PathVariable int line) {
lcdHelper.clearLine(line);
return "Line " + line + " cleared\n";
}

@Get("/turnOff")
public String turnOff() {
lcdHelper.turnOff();
return "Display turned off\n";
}
}
//end::ex[]
Loading
Loading