Skip to content

Commit

Permalink
attempt to convert to i2c
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrumartin committed Nov 3, 2024
1 parent 2fa3844 commit 0260263
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.opensourcewithslu.outputdevices.FourDigitSevenSegmentDisplayHelper;
import com.opensourcewithslu.utilities.MultiPinConfiguration;
import com.pi4j.context.Context;
import com.pi4j.io.i2c.I2CConfig;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import jakarta.inject.Named;
Expand All @@ -11,9 +13,9 @@
public class FourDigitSevenSegmentDisplayController {
private final FourDigitSevenSegmentDisplayHelper fourDigitSevenSegmentDisplayHelper;

public FourDigitSevenSegmentDisplayController(@Named("4digit7segment") MultiPinConfiguration fourdigsevenseg
public FourDigitSevenSegmentDisplayController(@Named("4digit7segment") I2CConfig fourdigsevenseg, Context pi4jContext
) {
this.fourDigitSevenSegmentDisplayHelper = new FourDigitSevenSegmentDisplayHelper(fourdigsevenseg);
this.fourDigitSevenSegmentDisplayHelper = new FourDigitSevenSegmentDisplayHelper(fourdigsevenseg, pi4jContext);
}

@Get("/displayNumber/{value}")
Expand Down
56 changes: 49 additions & 7 deletions components/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,48 @@ pi4j:
name: lcd # <2>
bus: 1 # <3>
device: 0x27 # <4>
4digit7segment:
name: 4 Digit 7 Segment Display
bus: 1
device: 0x3F
segments:
segmentA:
pin: 18
state: "LOW"
segmentB:
pin: 22
state: "LOW"
segmentC:
pin: 24
state: "LOW"
segmentD:
pin: 26
state: "LOW"
segmentE:
pin: 28
state: "LOW"
segmentF:
pin: 32
state: "LOW"
segmentG:
pin: 36
state: "LOW"
decimal-point:
pin: 38
state: "LOW"
digits:
digit0:
pin: 11
state: "LOW"
digit1:
pin: 13
state: "LOW"
digit2:
pin: 15
state: "LOW"
digit3:
pin: 17
state: "LOW"
# end::i2c[]

# tag::digitalInput[]
Expand Down Expand Up @@ -209,11 +251,11 @@ pi4j:
# end::multiPWM[]

# tag::multiOutput[]
multi-digital-output:
4digit7segment:
name: 4 Digit 7 Segment Display
addresses: 18, 22, 24, 26, 28, 32, 36, 38, 11, 13, 15, 17 # A, B, C, D, E, F, G, DP, DIGIT1, DIGIT2, DIGIT3, DIGIT4
shutdowns: LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW
initials: HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH
provider: pigpio-digital-output
# multi-digital-output:
# 4digit7segment:
# name: 4 Digit 7 Segment Display
# addresses: 18, 22, 24, 26, 28, 32, 36, 38, 11, 13, 15, 17 # A, B, C, D, E, F, G, DP, DIGIT1, DIGIT2, DIGIT3, DIGIT4
# shutdowns: LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW
# initials: HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH
# provider: pigpio-digital-output
# end::multiOutput[]
Original file line number Diff line number Diff line change
@@ -1,95 +1,50 @@
package com.opensourcewithslu.outputdevices;

import com.pi4j.io.gpio.digital.DigitalOutput;
import com.pi4j.io.pwm.Pwm;
import com.pi4j.context.Context;
import com.pi4j.io.i2c.I2C;
import com.pi4j.io.i2c.I2CConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.opensourcewithslu.utilities.MultiPinConfiguration;

public class FourDigitSevenSegmentDisplayHelper {
private static Logger log = LoggerFactory.getLogger(FourDigitSevenSegmentDisplayHelper.class);
private static final Logger log = LoggerFactory.getLogger(FourDigitSevenSegmentDisplayHelper.class);

/* private int[][] charMap = {
{1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 1},
{1, 1, 1, 1, 0, 0, 1},
{0, 1, 1, 0, 0, 1, 1},
{1, 0, 1, 1, 0, 1, 1},
{1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 1}
};*/
private final I2C i2c;

private enum CharMap {
ZERO(new int[]{1, 1, 1, 1, 1, 1, 0}),
ONE(new int[]{0, 1, 1, 0, 0, 0, 0}),
TWO(new int[]{1, 1, 0, 1, 1, 0, 1}),
THREE(new int[]{1, 1, 1, 1, 0, 0, 1}),
FOUR(new int[]{0, 1, 1, 0, 0, 1, 1}),
FIVE(new int[]{1, 0, 1, 1, 0, 1, 1}),
SIX(new int[]{1, 0, 1, 1, 1, 1, 1}),
SEVEN(new int[]{1, 1, 1, 0, 0, 0, 0}),
EIGHT(new int[]{1, 1, 1, 1, 1, 1, 1}),
NINE(new int[]{1, 1, 1, 1, 0, 1, 1}),
HYPHEN(new int[]{0, 0, 0, 0, 0, 0, 1});

private int[] segmentArray;

public int[] getSegmentArray() {
return this.segmentArray;
ZERO(0x3F),
ONE(0x06),
TWO(0x5B),
THREE(0x4F),
FOUR(0x66),
FIVE(0x6D),
SIX(0x7D),
SEVEN(0x07),
EIGHT(0x7F),
NINE(0x6F),
HYPHEN(0x40);

private final int segmentValue;

CharMap(int segmentValue) {
this.segmentValue = segmentValue;
}

CharMap(int[] segmentArray) {
this.segmentArray = segmentArray;
public int getSegmentValue() {
return segmentValue;
}
}

private final DigitalOutput segmentA;
private final DigitalOutput segmentB;
private final DigitalOutput segmentC;
private final DigitalOutput segmentD;
private final DigitalOutput segmentE;
private final DigitalOutput segmentF;
private final DigitalOutput segmentG;
private final DigitalOutput DP;
private final DigitalOutput digit0;
private final DigitalOutput digit1;
private final DigitalOutput digit2;
private final DigitalOutput digit3;

//tag::const[]
public FourDigitSevenSegmentDisplayHelper(MultiPinConfiguration fourDigitSevenSeg)
//end::const[]
{
DigitalOutput[] components = (DigitalOutput[]) fourDigitSevenSeg.getComponents();
this.segmentA = components[0];
this.segmentB = components[1];
this.segmentC = components[2];
this.segmentD = components[3];
this.segmentE = components[4];
this.segmentF = components[5];
this.segmentG = components[6];
this.DP = components[7];
this.digit0 = components[8];
this.digit1 = components[9];
this.digit2 = components[10];
this.digit3 = components[11];
public FourDigitSevenSegmentDisplayHelper(I2CConfig i2cConfig, Context pi4jContext) {
this.i2c = pi4jContext.create(i2cConfig);
}

//tag::method[]
public void displayValue(String value)
//end::method[]
{
char[] num = value.toCharArray();

if (num.length > 4) {
public void displayValue(String value) {
if (value.length() > 4) {
log.error("Number is too long");
return;
}
if (num.length == 0) {
if (value.length() == 0) {
log.error("Number is too short");
return;
}
Expand All @@ -106,119 +61,14 @@ public void displayValue(String value)

log.info("Displaying number: {}", value);

// TODO: replace with actual implementation
setValue(digit0, CharMap.FOUR);

// Render numbers with leading zeroes, e.g. 1 as 0001
/* switch (num.length) {
case 1:
*//*sevenSegmentDisplay1.displayNumber(0);
sevenSegmentDisplay2.displayNumber(0);
sevenSegmentDisplay3.displayNumber(0);
sevenSegmentDisplay4.displayNumber((int) num[0]);*//*
break;
case 2:
*//*sevenSegmentDisplay1.displayNumber(0);
sevenSegmentDisplay2.displayNumber(0);
sevenSegmentDisplay3.displayNumber((int) num[0]);
sevenSegmentDisplay4.displayNumber((int) num[1]);*//*
break;
case 3:
*//*sevenSegmentDisplay1.displayNumber(0);
sevenSegmentDisplay2.displayNumber((int) num[0]);
sevenSegmentDisplay3.displayNumber((int) num[1]);
sevenSegmentDisplay4.displayNumber((int) num[2]);*//*
break;
case 4:
*//*sevenSegmentDisplay1.displayNumber((int) num[0]);
sevenSegmentDisplay2.displayNumber((int) num[1]);
sevenSegmentDisplay3.displayNumber((int) num[2]);
sevenSegmentDisplay4.displayNumber((int) num[3]);*//*
break;
}*/
for (int i = 0; i < value.length(); i++) {
CharMap charMap = CharMap.valueOf(String.valueOf(value.charAt(i)));
setValue(i, charMap);
}
}

/*//tag::method[]
public String getValue()
//end::method[]
{
*//*int[] values = {sevenSegmentDisplay1.getValue(), sevenSegmentDisplay2.getValue(), sevenSegmentDisplay3.getValue(), sevenSegmentDisplay4.getValue()};
return Integer.parseInt(String.valueOf(values));*//*
}*/

private void setValue(DigitalOutput digit, CharMap character) {
/*segmentA.off();
segmentB.off();
segmentC.off();
segmentD.off();
segmentE.off();
segmentF.off();
segmentG.off();
digit.on(100, 50);
for (int i = 0; i < 7; i++) {
if (character.getSegmentArray()[i] == 1) {
switch (i) {
case 0:
segmentA.on(100, 50);
break;
case 1:
segmentB.on(100, 50);
break;
case 2:
segmentC.on(100, 50);
break;
case 3:
segmentD.on(100, 50);
break;
case 4:
segmentE.on(100, 50);
break;
case 5:
segmentF.on(100, 50);
break;
case 6:
segmentG.on(100, 50);
break;
}
}
}*/

segmentA.low();
segmentB.low();
segmentC.low();
segmentD.low();
segmentE.low();
segmentF.low();
segmentG.low();

digit.high();
for (int i = 0; i < 7; i++) {
if (character.getSegmentArray()[i] == 1) {
switch (i) {
case 0:
segmentA.high();
break;
case 1:
segmentB.high();
break;
case 2:
segmentC.high();
break;
case 3:
segmentD.high();
break;
case 4:
segmentE.high();
break;
case 5:
segmentF.high();
break;
case 6:
segmentG.high();
break;
}
}
}
private void setValue(int digit, CharMap character) {
int segmentValue = character.getSegmentValue();
i2c.writeRegister(digit, (byte) segmentValue);
}
}

0 comments on commit 0260263

Please sign in to comment.