From 8ec121b79129ba3afdb8f35a7d7e549117f56d1b Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Mon, 4 Nov 2024 10:50:02 -0600 Subject: [PATCH 01/12] Added SevenSegment Controller,Helper, and yml files --- .../controllers/SevenSegmentController.java | 33 ++++++ components/src/main/resources/application.yml | 31 +++++ pi4micronaut-utils/build.gradle | 1 + .../SevenSegmentDisplayHelper.java | 111 ++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 components/src/main/java/com/opensourcewithslu/components/controllers/SevenSegmentController.java create mode 100644 pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/SevenSegmentController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/SevenSegmentController.java new file mode 100644 index 00000000..2bc6f577 --- /dev/null +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/SevenSegmentController.java @@ -0,0 +1,33 @@ +package com.opensourcewithslu.components.controllers; + +import com.opensourcewithslu.outputdevices.SevenSegmentDisplayHelper; +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import jakarta.inject.Inject; +//solution +@Controller("/sevensegment") +public class SevenSegmentController { + + private final SevenSegmentDisplayHelper displayHelper; + + // Inject SevenSegmentDisplayHelper as a dependency + @Inject + public SevenSegmentController(SevenSegmentDisplayHelper displayHelper) { + this.displayHelper = displayHelper; + } + + @Get("/display/{number}") + public void displayNumber(int number) { + displayHelper.display(number); + } + + @Get("/reset") + public void resetDisplay() { + displayHelper.resetDisplay(); + } + + @Get("/shutdown") + public void shutdownDisplay() { + displayHelper.shutdown(); + } +} diff --git a/components/src/main/resources/application.yml b/components/src/main/resources/application.yml index 9fff77ac..ea6aa7ef 100644 --- a/components/src/main/resources/application.yml +++ b/components/src/main/resources/application.yml @@ -52,6 +52,37 @@ pi4j: name: lcd # <2> bus: 1 # <3> device: 0x27 # <4> + seven-segment-display: + name: "Single Seven-Segment Display" + bus: 1 + device: 0x3F + brightness: 100 + decimal-point: true + segments: + segmentA: + pin: 1 + state: "HIGH" + segmentB: + pin: 2 + state: "HIGH" + segmentC: + pin: 3 + state: "HIGH" + segmentD: + pin: 4 + state: "LOW" + segmentE: + pin: 5 + state: "LOW" + segmentF: + pin: 6 + state: "LOW" + segmentG: + pin: 7 + state: "LOW" + decimal-point: + pin: 8 + state: "LOW" # end::i2c[] # tag::digitalInput[] diff --git a/pi4micronaut-utils/build.gradle b/pi4micronaut-utils/build.gradle index 339297cb..cb13fad4 100644 --- a/pi4micronaut-utils/build.gradle +++ b/pi4micronaut-utils/build.gradle @@ -27,6 +27,7 @@ dependencies { api 'com.pi4j:pi4j-core:2.4.0' api 'com.pi4j:pi4j-plugin-raspberrypi:2.4.0' api 'com.pi4j:pi4j-plugin-pigpio:2.4.0' + testImplementation("org.mockito:mockito-core:5.+") testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.1' testImplementation 'org.junit.jupiter:junit-jupiter' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java new file mode 100644 index 00000000..5ace92fa --- /dev/null +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -0,0 +1,111 @@ +package com.opensourcewithslu.outputdevices; + +import com.pi4j.context.Context; +import com.pi4j.io.gpio.digital.DigitalOutput; +import com.pi4j.io.gpio.digital.DigitalState; +import com.pi4j.Pi4J; +import io.micronaut.context.annotation.Value; +import jakarta.inject.Singleton; + +@Singleton +public class SevenSegmentDisplayHelper { + + private final Context pi4j; + private final DigitalOutput pinA; + private final DigitalOutput pinB; + private final DigitalOutput pinC; + private final DigitalOutput pinD; + private final DigitalOutput pinE; + private final DigitalOutput pinF; + private final DigitalOutput pinG; + + // Segment pattern configuration for displaying numbers 0-9 + private static final boolean[][] DIGIT_SEGMENTS = { + {true, true, true, true, true, true, false}, // 0 + {false, true, true, false, false, false, false}, // 1 + {true, true, false, true, true, false, true}, // 2 + {true, true, true, true, false, false, true}, // 3 + {false, true, true, false, false, true, true}, // 4 + {true, false, true, true, false, true, true}, // 5 + {true, false, true, true, true, true, true}, // 6 + {true, true, true, false, false, false, false}, // 7 + {true, true, true, true, true, true, true}, // 8 + {true, true, true, true, false, true, true} // 9 + }; + + // Constructor that loads GPIO pin addresses from `application.yml` + public SevenSegmentDisplayHelper( + @Value("${pi4j.seven-segment-display.segment-pins.a}") int pinAAddress, + @Value("${pi4j.seven-segment-display.segment-pins.b}") int pinBAddress, + @Value("${pi4j.seven-segment-display.segment-pins.c}") int pinCAddress, + @Value("${pi4j.seven-segment-display.segment-pins.d}") int pinDAddress, + @Value("${pi4j.seven-segment-display.segment-pins.e}") int pinEAddress, + @Value("${pi4j.seven-segment-display.segment-pins.f}") int pinFAddress, + @Value("${pi4j.seven-segment-display.segment-pins.g}") int pinGAddress) { + + // Initialize Pi4J context + this.pi4j = Pi4J.newAutoContext(); + + if (pi4j == null) { + throw new IllegalStateException("Pi4J context failed to initialize"); + } + + // Set up each pin based on addresses from configuration + this.pinA = configurePin(pinAAddress, "PinA"); + this.pinB = configurePin(pinBAddress, "PinB"); + this.pinC = configurePin(pinCAddress, "PinC"); + this.pinD = configurePin(pinDAddress, "PinD"); + this.pinE = configurePin(pinEAddress, "PinE"); + this.pinF = configurePin(pinFAddress, "PinF"); + this.pinG = configurePin(pinGAddress, "PinG"); + } + + private DigitalOutput configurePin(int address, String id) { + return pi4j.create(DigitalOutput.newConfigBuilder(pi4j) + .id(id) + .name(id) + .address(address) + .shutdown(DigitalState.LOW) + .initial(DigitalState.LOW)); + } + + public void display(int number) { + if (number < 0 || number > 9) { + throw new IllegalArgumentException("Number must be between 0 and 9"); + } + setSegments( + DIGIT_SEGMENTS[number][0], + DIGIT_SEGMENTS[number][1], + DIGIT_SEGMENTS[number][2], + DIGIT_SEGMENTS[number][3], + DIGIT_SEGMENTS[number][4], + DIGIT_SEGMENTS[number][5], + DIGIT_SEGMENTS[number][6] + ); + } + + private void setSegments(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g) { + pinA.state(a ? DigitalState.HIGH : DigitalState.LOW); + pinB.state(b ? DigitalState.HIGH : DigitalState.LOW); + pinC.state(c ? DigitalState.HIGH : DigitalState.LOW); + pinD.state(d ? DigitalState.HIGH : DigitalState.LOW); + pinE.state(e ? DigitalState.HIGH : DigitalState.LOW); + pinF.state(f ? DigitalState.HIGH : DigitalState.LOW); + pinG.state(g ? DigitalState.HIGH : DigitalState.LOW); + } + + public void resetDisplay() { + pinA.low(); + pinB.low(); + pinC.low(); + pinD.low(); + pinE.low(); + pinF.low(); + pinG.low(); + } + + public void shutdown() { + pi4j.shutdown(); // showdown the program + } + //done! +} From 1bd5a24bbd6f07f62ebd19308db249e096ef7ceb Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 09:14:53 -0600 Subject: [PATCH 02/12] Required changes made in the Helper class --- .../SevenSegmentDisplayHelper.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java index 5ace92fa..8fd924e6 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -18,6 +18,7 @@ public class SevenSegmentDisplayHelper { private final DigitalOutput pinE; private final DigitalOutput pinF; private final DigitalOutput pinG; + private final DigitalOutput decimalPoint; // Segment pattern configuration for displaying numbers 0-9 private static final boolean[][] DIGIT_SEGMENTS = { @@ -35,13 +36,14 @@ public class SevenSegmentDisplayHelper { // Constructor that loads GPIO pin addresses from `application.yml` public SevenSegmentDisplayHelper( - @Value("${pi4j.seven-segment-display.segment-pins.a}") int pinAAddress, - @Value("${pi4j.seven-segment-display.segment-pins.b}") int pinBAddress, - @Value("${pi4j.seven-segment-display.segment-pins.c}") int pinCAddress, - @Value("${pi4j.seven-segment-display.segment-pins.d}") int pinDAddress, - @Value("${pi4j.seven-segment-display.segment-pins.e}") int pinEAddress, - @Value("${pi4j.seven-segment-display.segment-pins.f}") int pinFAddress, - @Value("${pi4j.seven-segment-display.segment-pins.g}") int pinGAddress) { + @Value("${i2c.seven-segment-display.segments.segmentA.pin}") int pinAAddress, // @ value reads application.yml + @Value("${i2c.seven-segment-display.segments.segmentB.pin}") int pinBAddress, + @Value("${i2c.seven-segment-display.segments.segmentC.pin}") int pinCAddress, + @Value("${i2c.seven-segment-display.segments.segmentD.pin}") int pinDAddress, + @Value("${i2c.seven-segment-display.segments.segmentE.pin}") int pinEAddress, + @Value("${i2c.seven-segment-display.segments.segmentF.pin}") int pinFAddress, + @Value("${i2c.seven-segment-display.segments.segmentG.pin}") int pinGAddress, + @Value("${i2c.seven-segment-display.segments.decimal-point.pin}") int decimalPointPinAddress) { // Initialize Pi4J context this.pi4j = Pi4J.newAutoContext(); @@ -58,14 +60,15 @@ public SevenSegmentDisplayHelper( this.pinE = configurePin(pinEAddress, "PinE"); this.pinF = configurePin(pinFAddress, "PinF"); this.pinG = configurePin(pinGAddress, "PinG"); + this.decimalPoint = configurePin(decimalPointPinAddress, "DecimalPoint"); } - private DigitalOutput configurePin(int address, String id) { - return pi4j.create(DigitalOutput.newConfigBuilder(pi4j) + private DigitalOutput configurePin(int address, String id) { // sets up each pin with a unique ID,name, and address + return pi4j.create(DigitalOutput.newConfigBuilder(pi4j) //creat the configuration pin .id(id) .name(id) .address(address) - .shutdown(DigitalState.LOW) + .shutdown(DigitalState.LOW) // sets all pins are off .initial(DigitalState.LOW)); } @@ -102,10 +105,10 @@ public void resetDisplay() { pinE.low(); pinF.low(); pinG.low(); + decimalPoint.low(); } public void shutdown() { - pi4j.shutdown(); // showdown the program + pi4j.shutdown(); } - //done! } From 28e04100ee6c47914006cc3f9aed859aa472ad2a Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 09:32:49 -0600 Subject: [PATCH 03/12] Required changes made in the Helper class --- .../outputdevices/SevenSegmentDisplayHelper.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java index 8fd924e6..31a4f70b 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -36,14 +36,14 @@ public class SevenSegmentDisplayHelper { // Constructor that loads GPIO pin addresses from `application.yml` public SevenSegmentDisplayHelper( - @Value("${i2c.seven-segment-display.segments.segmentA.pin}") int pinAAddress, // @ value reads application.yml - @Value("${i2c.seven-segment-display.segments.segmentB.pin}") int pinBAddress, - @Value("${i2c.seven-segment-display.segments.segmentC.pin}") int pinCAddress, - @Value("${i2c.seven-segment-display.segments.segmentD.pin}") int pinDAddress, - @Value("${i2c.seven-segment-display.segments.segmentE.pin}") int pinEAddress, - @Value("${i2c.seven-segment-display.segments.segmentF.pin}") int pinFAddress, - @Value("${i2c.seven-segment-display.segments.segmentG.pin}") int pinGAddress, - @Value("${i2c.seven-segment-display.segments.decimal-point.pin}") int decimalPointPinAddress) { + @Value("${i2c.seven-segment-display.segments.segment-a.pin}") int pinAAddress, // @ value reads application.yml + @Value("${i2c.seven-segment-display.segments.segment-b.pin}") int pinBAddress, + @Value("${i2c.seven-segment-display.segments.segment-c.pin}") int pinCAddress, + @Value("${i2c.seven-segment-display.segments.segment-d.pin}") int pinDAddress, + @Value("${i2c.seven-segment-display.segments.segment-e.pin}") int pinEAddress, + @Value("${i2c.seven-segment-display.segments.segment-f.pin}") int pinFAddress, + @Value("${i2c.seven-segment-display.segments.segment-g.pin}") int pinGAddress, + @Value("${i2c.seven-segment-display.segments.segment-dot.pin}") int decimalPointPinAddress) { // Initialize Pi4J context this.pi4j = Pi4J.newAutoContext(); From 44b0d0383446212b53efc1c89bec68b652b282bf Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 09:38:50 -0600 Subject: [PATCH 04/12] changes made for the configuration --- components/src/main/resources/application.yml | 74 +++++++++++++------ 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/components/src/main/resources/application.yml b/components/src/main/resources/application.yml index ea6aa7ef..00a7cccf 100644 --- a/components/src/main/resources/application.yml +++ b/components/src/main/resources/application.yml @@ -59,30 +59,56 @@ pi4j: brightness: 100 decimal-point: true segments: - segmentA: - pin: 1 - state: "HIGH" - segmentB: - pin: 2 - state: "HIGH" - segmentC: - pin: 3 - state: "HIGH" - segmentD: - pin: 4 - state: "LOW" - segmentE: - pin: 5 - state: "LOW" - segmentF: - pin: 6 - state: "LOW" - segmentG: - pin: 7 - state: "LOW" - decimal-point: - pin: 8 - state: "LOW" + digital-output: + # Define each segment of the seven-segment display as a digital output. + segment-a: + name: Segment A + address: 2 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-b: + name: Segment B + address: 3 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-c: + name: Segment C + address: 4 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-d: + name: Segment D + address: 5 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-e: + name: Segment E + address: 6 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-f: + name: Segment F + address: 7 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-g: + name: Segment G + address: 8 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-dot: + name: Segment DOT + address: 9 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output # end::i2c[] # tag::digitalInput[] From 913566a5cd38e217cc89aa2322fd110fe5699b69 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 09:44:31 -0600 Subject: [PATCH 05/12] changes made --- .../outputdevices/SevenSegmentDisplayHelper.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java index 31a4f70b..401063b8 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -36,14 +36,14 @@ public class SevenSegmentDisplayHelper { // Constructor that loads GPIO pin addresses from `application.yml` public SevenSegmentDisplayHelper( - @Value("${i2c.seven-segment-display.segments.segment-a.pin}") int pinAAddress, // @ value reads application.yml - @Value("${i2c.seven-segment-display.segments.segment-b.pin}") int pinBAddress, - @Value("${i2c.seven-segment-display.segments.segment-c.pin}") int pinCAddress, - @Value("${i2c.seven-segment-display.segments.segment-d.pin}") int pinDAddress, - @Value("${i2c.seven-segment-display.segments.segment-e.pin}") int pinEAddress, - @Value("${i2c.seven-segment-display.segments.segment-f.pin}") int pinFAddress, - @Value("${i2c.seven-segment-display.segments.segment-g.pin}") int pinGAddress, - @Value("${i2c.seven-segment-display.segments.segment-dot.pin}") int decimalPointPinAddress) { + @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.initial}") int pinAAddress, // @ value reads application.yml + @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.initial}") int pinBAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.initial}") int pinCAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.initial}") int pinDAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.initial}") int pinEAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.initial}") int pinFAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.initial}") int pinGAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.initial}") int decimalPointPinAddress) { // Initialize Pi4J context this.pi4j = Pi4J.newAutoContext(); From f37babab4a0929db0ef00cc09cb29de1f268bb61 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 09:47:32 -0600 Subject: [PATCH 06/12] replaced address with initials --- .../outputdevices/SevenSegmentDisplayHelper.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java index 401063b8..08908eda 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -36,14 +36,14 @@ public class SevenSegmentDisplayHelper { // Constructor that loads GPIO pin addresses from `application.yml` public SevenSegmentDisplayHelper( - @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.initial}") int pinAAddress, // @ value reads application.yml - @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.initial}") int pinBAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.initial}") int pinCAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.initial}") int pinDAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.initial}") int pinEAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.initial}") int pinFAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.initial}") int pinGAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.initial}") int decimalPointPinAddress) { + @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") int pinAAddress, // @ value reads application.yml + @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") int pinBAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") int pinCAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.address}") int pinDAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.address}") int pinEAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.address}") int pinFAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.address}") int pinGAddress, + @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") int decimalPointPinAddress) { // Initialize Pi4J context this.pi4j = Pi4J.newAutoContext(); From 86607dee3d5867f32e402d8e839db68cfaa5d519 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 09:58:33 -0600 Subject: [PATCH 07/12] hello --- .../SevenSegmentDisplayHelper.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java index 08908eda..6af9b3ef 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -35,15 +35,16 @@ public class SevenSegmentDisplayHelper { }; // Constructor that loads GPIO pin addresses from `application.yml` - public SevenSegmentDisplayHelper( - @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") int pinAAddress, // @ value reads application.yml - @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") int pinBAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") int pinCAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.address}") int pinDAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.address}") int pinEAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.address}") int pinFAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.address}") int pinGAddress, - @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") int decimalPointPinAddress) { + public SevenSegmentDisplayHelper + + @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") int pinAAddress; // @ value reads application.yml + @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") int pinBAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") int pinCAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.address}") int pinDAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.address}") int pinEAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.address}") int pinFAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.address}") int pinGAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") int decimalPointPinAddress; { // Initialize Pi4J context this.pi4j = Pi4J.newAutoContext(); From b31f07546bddc7497c2ad19d3e387c0452d21080 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 09:59:42 -0600 Subject: [PATCH 08/12] a --- .../outputdevices/SevenSegmentDisplayHelper.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java index 6af9b3ef..609a65a3 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -35,8 +35,7 @@ public class SevenSegmentDisplayHelper { }; // Constructor that loads GPIO pin addresses from `application.yml` - public SevenSegmentDisplayHelper - + public SevenSegmentDisplayHelper(){} @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") int pinAAddress; // @ value reads application.yml @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") int pinBAddress; @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") int pinCAddress; From 143c6507e4bfda9c87200553d16af46fa6ad638a Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 10:00:37 -0600 Subject: [PATCH 09/12] a --- .../outputdevices/SevenSegmentDisplayHelper.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java index 609a65a3..a5ab8efa 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -35,7 +35,7 @@ public class SevenSegmentDisplayHelper { }; // Constructor that loads GPIO pin addresses from `application.yml` - public SevenSegmentDisplayHelper(){} + public SevenSegmentDisplayHelper(){ @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") int pinAAddress; // @ value reads application.yml @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") int pinBAddress; @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") int pinCAddress; @@ -43,7 +43,8 @@ public SevenSegmentDisplayHelper(){} @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.address}") int pinEAddress; @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.address}") int pinFAddress; @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.address}") int pinGAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") int decimalPointPinAddress; { + @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") int decimalPointPinAddress; + } { // Initialize Pi4J context this.pi4j = Pi4J.newAutoContext(); From 1611adc7a4df0fd8d66b587155286e8da1a2a604 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 10:02:23 -0600 Subject: [PATCH 10/12] a --- .../SevenSegmentDisplayHelper.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java index a5ab8efa..dc2c4d73 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -35,16 +35,15 @@ public class SevenSegmentDisplayHelper { }; // Constructor that loads GPIO pin addresses from `application.yml` - public SevenSegmentDisplayHelper(){ - @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") int pinAAddress; // @ value reads application.yml - @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") int pinBAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") int pinCAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.address}") int pinDAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.address}") int pinEAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.address}") int pinFAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.address}") int pinGAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") int decimalPointPinAddress; - } { + public SevenSegmentDisplayHelper() { + @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") int pinAAddress; // @ value reads application.yml + @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") int pinBAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") int pinCAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.address}") int pinDAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.address}") int pinEAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.address}") int pinFAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.address}") int pinGAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") int decimalPointPinAddress; // Initialize Pi4J context this.pi4j = Pi4J.newAutoContext(); @@ -64,6 +63,7 @@ public SevenSegmentDisplayHelper(){ this.decimalPoint = configurePin(decimalPointPinAddress, "DecimalPoint"); } + private DigitalOutput configurePin(int address, String id) { // sets up each pin with a unique ID,name, and address return pi4j.create(DigitalOutput.newConfigBuilder(pi4j) //creat the configuration pin .id(id) From 13ff0fd2fc296250226b10dbcadcdb3241fd7224 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Tue, 5 Nov 2024 10:07:02 -0600 Subject: [PATCH 11/12] a --- .../SevenSegmentDisplayHelper.java | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java index dc2c4d73..cae96ed1 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/SevenSegmentDisplayHelper.java @@ -20,6 +20,23 @@ public class SevenSegmentDisplayHelper { private final DigitalOutput pinG; private final DigitalOutput decimalPoint; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") + int pinAAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") + int pinBAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") + int pinCAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.address}") + int pinDAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.address}") + int pinEAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.address}") + int pinFAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.address}") + int pinGAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") + int decimalPointPinAddress; + // Segment pattern configuration for displaying numbers 0-9 private static final boolean[][] DIGIT_SEGMENTS = { {true, true, true, true, true, true, false}, // 0 @@ -34,17 +51,8 @@ public class SevenSegmentDisplayHelper { {true, true, true, true, false, true, true} // 9 }; - // Constructor that loads GPIO pin addresses from `application.yml` + // Constructor public SevenSegmentDisplayHelper() { - @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") int pinAAddress; // @ value reads application.yml - @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") int pinBAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") int pinCAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.address}") int pinDAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.address}") int pinEAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.address}") int pinFAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.address}") int pinGAddress; - @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") int decimalPointPinAddress; - // Initialize Pi4J context this.pi4j = Pi4J.newAutoContext(); @@ -63,31 +71,26 @@ public SevenSegmentDisplayHelper() { this.decimalPoint = configurePin(decimalPointPinAddress, "DecimalPoint"); } - - private DigitalOutput configurePin(int address, String id) { // sets up each pin with a unique ID,name, and address - return pi4j.create(DigitalOutput.newConfigBuilder(pi4j) //creat the configuration pin + // Configures a digital output pin with a specific address and ID + private DigitalOutput configurePin(int address, String id) { + return pi4j.create(DigitalOutput.newConfigBuilder(pi4j) .id(id) .name(id) .address(address) - .shutdown(DigitalState.LOW) // sets all pins are off + .shutdown(DigitalState.LOW) .initial(DigitalState.LOW)); } + // Displays a number on the seven-segment display public void display(int number) { if (number < 0 || number > 9) { throw new IllegalArgumentException("Number must be between 0 and 9"); } - setSegments( - DIGIT_SEGMENTS[number][0], - DIGIT_SEGMENTS[number][1], - DIGIT_SEGMENTS[number][2], - DIGIT_SEGMENTS[number][3], - DIGIT_SEGMENTS[number][4], - DIGIT_SEGMENTS[number][5], - DIGIT_SEGMENTS[number][6] - ); + boolean[] segments = DIGIT_SEGMENTS[number]; + setSegments(segments[0], segments[1], segments[2], segments[3], segments[4], segments[5], segments[6]); } + // Sets each segment's state based on the boolean values provided private void setSegments(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g) { pinA.state(a ? DigitalState.HIGH : DigitalState.LOW); pinB.state(b ? DigitalState.HIGH : DigitalState.LOW); @@ -98,6 +101,7 @@ private void setSegments(boolean a, boolean b, boolean c, boolean d, boolean e, pinG.state(g ? DigitalState.HIGH : DigitalState.LOW); } + // Resets all segments and decimal point to LOW state, effectively turning off the display public void resetDisplay() { pinA.low(); pinB.low(); @@ -109,7 +113,9 @@ public void resetDisplay() { decimalPoint.low(); } + // Shuts down the Pi4J context and cleans up resources public void shutdown() { + resetDisplay(); // Ensure display is off before shutdown pi4j.shutdown(); } } From ff79ebd6ce312fd32d25a106b2eace50e334b2b0 Mon Sep 17 00:00:00 2001 From: Ed0827 Date: Mon, 11 Nov 2024 15:54:09 -0600 Subject: [PATCH 12/12] Added the documentation for SevenSegementDiplay --- .../SevenSegementDisplay.adoc | 292 ++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 pi4micronaut-utils/src/docs/asciidoc/components/outputComponents/SevenSegementDisplay.adoc diff --git a/pi4micronaut-utils/src/docs/asciidoc/components/outputComponents/SevenSegementDisplay.adoc b/pi4micronaut-utils/src/docs/asciidoc/components/outputComponents/SevenSegementDisplay.adoc new file mode 100644 index 00000000..e352b8d0 --- /dev/null +++ b/pi4micronaut-utils/src/docs/asciidoc/components/outputComponents/SevenSegementDisplay.adoc @@ -0,0 +1,292 @@ +:imagesdir: img/ + +ifndef::rootpath[] +:rootpath: ../../ +endif::rootpath[] + +ifdef::rootpath[] +:imagesdir: {rootpath}{imagesdir} +endif::rootpath[] + +==== Seven Segment Display + +[.text-right] +https://github.com/oss-slu/Pi4Micronaut/edit/develop/pi4micronaut-utils/src/docs/asciidoc/components/outputComponents/SevenSegmentDisplay.adoc[Improve this doc] + +===== Overview +This section provides comprehensive details about the Seven Segment Display component, covering its configuration, circuit setup, and usage instructions. + +===== Components +* 1 x Raspberry Pi +* 1 x Seven Segment Display +* Jumper wires +* Breadboard +* Power source + +===== Circuit Setup + +1. Place the Seven Segment Display on the breadboard. +2. Connect each segment of the display to its respective GPIO pin as specified in the YAML configuration. +3. Use a common ground to connect the display to the Raspberry Pi. +4. Refer to the circuit diagram for detailed wiring instructions. + +===== Circuit Diagram + +image::/Users/jeongseyun7/Desktop/Capston1/Sprint4/Refreshed/Pi4Micronaut/Circuit_Diagram.png[] + +===== Schematic Diagram + +image::/Users/jeongseyun7/Desktop/Capston1/Sprint4/Refreshed/Pi4Micronaut/Schematic Diagram.png[] + +===== YAML Configuration +The following YAML configuration is used to set up the Seven Segment Display in `application.yml`. This configuration defines each segment as a digital output and includes the I2C parameters necessary for Raspberry Pi integration. + +[source, yaml] +---- + # tag::i2c[] + i2c: + seven-segment-display: + name: "Single Seven-Segment Display" + bus: 1 + device: 0x3F + brightness: 100 + decimal-point: true + segments: + digital-output: + segment-a: + name: Segment A + address: 2 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-b: + name: Segment B + address: 3 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-c: + name: Segment C + address: 4 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-d: + name: Segment D + address: 5 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-e: + name: Segment E + address: 6 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-f: + name: Segment F + address: 7 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-g: + name: Segment G + address: 8 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + segment-dot: + name: Segment DOT + address: 9 + shutdown: LOW + initial: LOW + provider: pigpio-digital-output + # end::i2c[] +---- + +===== Functionality +The Seven Segment Display can display numbers from 0-9 by controlling each segment. Additionally, the decimal point can be activated as needed. + +===== Testing + +To test the Seven Segment Display component with the Micronaut API, use the following commands: + +[source, bash] +---- +$ curl http://localhost:8080/sevensegment/display/5 +---- +* `/display/{number}` - Displays the specified number (0-9). +* `/reset` - Turns off all segments on the display. +* `/shutdown` - Sets the display state to shutdown, which can deactivate the decimal point or all segments. + +===== Troubleshooting +- **Display not working**: Verify all connections and ensure each segment is correctly wired to its designated GPIO pin. +- **Incorrect display**: Review the YAML configuration for accuracy, ensuring the correct addresses are assigned to each segment. + +===== Constructor and Methods +[source, java] +---- +package com.opensourcewithslu.outputdevices; + +import com.pi4j.context.Context; +import com.pi4j.io.gpio.digital.DigitalOutput; +import com.pi4j.io.gpio.digital.DigitalState; +import com.pi4j.Pi4J; +import io.micronaut.context.annotation.Value; +import jakarta.inject.Singleton; + +@Singleton +public class SevenSegmentDisplayHelper { + + private final Context pi4j; + private final DigitalOutput pinA; + private final DigitalOutput pinB; + private final DigitalOutput pinC; + private final DigitalOutput pinD; + private final DigitalOutput pinE; + private final DigitalOutput pinF; + private final DigitalOutput pinG; + private final DigitalOutput decimalPoint; + + @Value("${i2c.seven-segment-display.segments.digital-output.segment-a.address}") + int pinAAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-b.address}") + int pinBAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-c.address}") + int pinCAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-d.address}") + int pinDAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-e.address}") + int pinEAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-f.address}") + int pinFAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-g.address}") + int pinGAddress; + @Value("${i2c.seven-segment-display.segments.digital-output.segment-dot.address}") + int decimalPointPinAddress; + + // Segment pattern configuration for displaying numbers 0-9 + private static final boolean[][] DIGIT_SEGMENTS = { + {true, true, true, true, true, true, false}, // 0 + {false, true, true, false, false, false, false}, // 1 + {true, true, false, true, true, false, true}, // 2 + {true, true, true, true, false, false, true}, // 3 + {false, true, true, false, false, true, true}, // 4 + {true, false, true, true, false, true, true}, // 5 + {true, false, true, true, true, true, true}, // 6 + {true, true, true, false, false, false, false}, // 7 + {true, true, true, true, true, true, true}, // 8 + {true, true, true, true, false, true, true} // 9 + }; + + // Constructor + public SevenSegmentDisplayHelper() { + // Initialize Pi4J context + this.pi4j = Pi4J.newAutoContext(); + + if (pi4j == null) { + throw new IllegalStateException("Pi4J context failed to initialize"); + } + + // Set up each pin based on addresses from configuration + this.pinA = configurePin(pinAAddress, "PinA"); + this.pinB = configurePin(pinBAddress, "PinB"); + this.pinC = configurePin(pinCAddress, "PinC"); + this.pinD = configurePin(pinDAddress, "PinD"); + this.pinE = configurePin(pinEAddress, "PinE"); + this.pinF = configurePin(pinFAddress, "PinF"); + this.pinG = configurePin(pinGAddress, "PinG"); + this.decimalPoint = configurePin(decimalPointPinAddress, "DecimalPoint"); + } + + // Configures a digital output pin with a specific address and ID + private DigitalOutput configurePin(int address, String id) { + return pi4j.create(DigitalOutput.newConfigBuilder(pi4j) + .id(id) + .name(id) + .address(address) + .shutdown(DigitalState.LOW) + .initial(DigitalState.LOW)); + } + + // Displays a number on the seven-segment display + public void display(int number) { + if (number < 0 || number > 9) { + throw new IllegalArgumentException("Number must be between 0 and 9"); + } + boolean[] segments = DIGIT_SEGMENTS[number]; + setSegments(segments[0], segments[1], segments[2], segments[3], segments[4], segments[5], segments[6]); + } + + // Sets each segment's state based on the boolean values provided + private void setSegments(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f, boolean g) { + pinA.state(a ? DigitalState.HIGH : DigitalState.LOW); + pinB.state(b ? DigitalState.HIGH : DigitalState.LOW); + pinC.state(c ? DigitalState.HIGH : DigitalState.LOW); + pinD.state(d ? DigitalState.HIGH : DigitalState.LOW); + pinE.state(e ? DigitalState.HIGH : DigitalState.LOW); + pinF.state(f ? DigitalState.HIGH : DigitalState.LOW); + pinG.state(g ? DigitalState.HIGH : DigitalState.LOW); + } + + // Resets all segments and decimal point to LOW state, effectively turning off the display + public void resetDisplay() { + pinA.low(); + pinB.low(); + pinC.low(); + pinD.low(); + pinE.low(); + pinF.low(); + pinG.low(); + decimalPoint.low(); + } + + // Shuts down the Pi4J context and cleans up resources + public void shutdown() { + resetDisplay(); // Ensure display is off before shutdown + pi4j.shutdown(); + } +} + +---- + + +===== Example Controller + +The following example controller demonstrates how to set up routes for displaying numbers and controlling the reset and shutdown features of the SevenSegment Display. + +[source, java] +---- +package com.opensourcewithslu.components.controllers; + +import com.opensourcewithslu.outputdevices.SevenSegmentDisplayHelper; +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import jakarta.inject.Inject; + +@Controller("/seven-segment") +public class SevenSegmentController { + + private final SevenSegmentDisplayHelper displayHelper; + + @Inject + public SevenSegmentController(SevenSegmentDisplayHelper displayHelper) { + this.displayHelper = displayHelper; + } + + @Get("/display/{number}") + public void displayNumber(int number) { + displayHelper.display(number); + } + + @Get("/reset") + public void resetDisplay() { + displayHelper.reset(); + } + + @Get("/shutdown") + public void shutdownDisplay() { + displayHelper.shutdown(); + } +} +----