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

Seven Segment Display Controller Creation #113

Closed
wants to merge 8 commits into from
Closed
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,58 @@
package com.opensourcewithslu.components.controllers;

/*Import statements can be updated as file structure progresses
---------------------------------------------------------------

*/
//import com.pi4j.component.sevseg.SevenSegment;
//import com.pi4j.crowpi.components.SevenSegmentComponent;
import com.pi4j.io.i2c.I2CConfig;
import com.opensourcewithslu.outputdevices.SevenSegmentDisplayHelper;
import com.pi4j.context.Context;
import com.pi4j.io.gpio.digital.DigitalOutput;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import jakarta.inject.Named;



@Controller("/7SegmentDisplay")
public class SevenSegmentDisplayController {

private final SevenSegmentDisplayHelper sevensegmentdisplayHelper; //Why is this not initialized?

public SevenSegmentDisplayController(@Named("seven-segment-display") I2CConfig i2CConfig, Context pi4jContext){
this.sevensegmentdisplayHelper = new SevenSegmentDisplayHelper(i2CConfig,pi4jContext);
}

@Get("/printInt/{i}")
public void printInt(int i){
/*Outputs the desired integer value*/
sevensegmentdisplayHelper.printInteger(i);
}
@Get("/printStr/{s}")
public void printString(String s){
/*Outputs the desired string value*/
sevensegmentdisplayHelper.printString(s);
}

@Get("/countdown/{val}")
public void countdown(int val){
/*Outputs values in a countdown fashion, user defines the value to begin at*/
sevensegmentdisplayHelper.countdownTimer(val);
}

@Get("/numCtr/{val}")
public void numberCounter(int val){
/*Outputs even or odd values as specified by the user*/

sevensegmentdisplayHelper.numberCounter(val);
}

@Get("/dispAllVals")
public void displayAllValues(){
/*Outputs all values from 0-9 & A-F*/
sevensegmentdisplayHelper.showAllValues();
}

}
6 changes: 6 additions & 0 deletions components/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ pi4j:
name: lcd # <2>
bus: 1 # <3>
device: 0x27 # <4>

seven-segment-display:
name: Seven Segment Display
bus: 2
device: 0x28
# end::i2c[]
digital-input:
# tag::digitalInput[]
Expand Down Expand Up @@ -102,6 +107,7 @@ pi4j:
shutdown: LOW
initial: HIGH
provider: pigpio-digital-output

# tag::multiInput[]
multi-digital-input: # <.>
rotary-encoder: # <.>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.opensourcewithslu.outputdevices;

import com.pi4j.context.Context;
import com.pi4j.crowpi.components.SevenSegmentComponent;
import com.pi4j.io.gpio.digital.DigitalOutput;
import com.pi4j.io.i2c.I2CConfig;

import java.util.HashMap;

public class SevenSegmentDisplayHelper {

private final SevenSegmentComponent segment;

public SevenSegmentDisplayHelper (I2CConfig i2CConfig, Context pi4j) {
segment = new SevenSegmentComponent(pi4j, i2CConfig.bus(), i2CConfig.device());
segment.setEnabled(true);

//Disable blinking
segment.setBlinkRate(0);
segment.setBrightness(15);

segment.setDigit (0, '8'); //This sets the digit in the zero position aka the only available pos.
segment.setDecimalPoint(0, true); //Sets the decimal point for the display

segment.refresh(); //refresh the display

try {
Thread.sleep(2000); // Using Thread.sleep for clarity
} catch (InterruptedException e) {
// Handle the interrupted exception
Thread.currentThread().interrupt();
}
//Fake loading display

HashMap<Character, Boolean> segmentStates = new HashMap<>();

//Initialize the segments
char [] segments = {'A', 'B', 'C', 'D', 'E', 'F'};
int numberOfRotations = 1000;

for (int i = 0; i < segments.length; i++) {
segmentStates.put(segments[i], i % 2 == 0);
}

for (int i=0; i<numberOfRotations; i++) {
for (char seg = 'A'; seg <= 'F'; seg++) {
boolean lastState = segmentStates.get(segments[segments.length - 1]);
for (int j = segments.length - 1; j > 0; j--) {
segmentStates.put(segments[j], segmentStates.get(segments[j - 1]));
}
segmentStates.put(segments[0], lastState);


segment.refresh();

try {
Thread.sleep(1000); // Using Thread.sleep for clarity
} catch (InterruptedException d) {
// Handle the interrupted exception
Thread.currentThread().interrupt();
}

}
}

}
public void printInteger(int i){
segment.print(i);
}

public void printString(String s){
segment.print(s);
}

public void countdownTimer(int value){
/*User can choose integer and function will countdown from the starting point*/

for (int i = value; i >= 0; i--){
printInteger(value);
}
}

public void numberCounter(int value){
/*Value should be 0 for even numbers to be output and 1 for odd numbers to be output*/

for (int i = value; i < 10; i += 2) {
printInteger(value);
}
}

public void showAllValues(){
/*Function will display all values 0-9 then A-F*/
for (int i = 0; i < 10; i ++) {
printInteger(i);
}

for (int j= 65; j< 71; j++){
printString(String.valueOf((char)j));
}
}

}