Skip to content

Commit

Permalink
added sample helper code, need to convert it to integrate with pi4mic…
Browse files Browse the repository at this point in the history
…ronaut
  • Loading branch information
SwindleA committed Nov 13, 2023
1 parent 7803daa commit 0e7b683
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.opensourcewithslu.components.controllers;

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 com.pi4j.context.Context;
import jakarta.inject.Named;


//tag::ex[]

@Controller("/4digit7segment")
public class FourDigitSevenSegmentDisplayController {

private final FourDigitSevenSegmentDisplayHelper fourDigitSevenSegmentDisplayHelper;

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

@Get("/test")
public void test(){
fourDigitSevenSegmentDisplayHelper.test();
}

}
//end::ex[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.opensourcewithslu.outputdevices;

import com.opensourcewithslu.utilities.MultipinConfiguration;
import com.pi4j.context.Context;
import com.pi4j.io.gpio.digital.DigitalOutput;

//import com.pi4j.io.gpio.*;
//import com.pi4j.wiringpi.Gpio; replace with pigpio

import java.util.Timer;
import java.util.TimerTask;


// OG code from: https://learn.sunfounder.com/lesson-1-1-5-4-digit-7-segment-display/
// Java version from chatgpt.
public class FourDigitSevenSegmentDisplayHelper {
private static final int SDI = 5; //Serial digital interface
private static final int RCLK = 4; //reference clock
private static final int SRCLK = 1; // serial clock

private static MultipinConfiguration placePin; //pins, t

private static Context pi4jContext;

/// hex codes for the numbers 0,1,2,3,4,5,6,7,8,9
private static final int[] number = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};

private static int counter = 0;


// I think these two need to be converted to something from the Pi4JFactory
//private static GpioController gpio; // replace with pi4jcontext?
private static DigitalOutput[] digitPins;


//tag::const[]
public FourDigitSevenSegmentDisplayHelper(MultipinConfiguration fourdigsevenseg, Context pi4jContext)
//end::const[]
{
placePin = fourdigsevenseg; // = {12, 3, 2, 0}, the pins are gotten from the multipin parameter.

this.pi4jContext = pi4jContext;

// Initialize Pi4J
//Gpio.wiringPiSetup();

// Initialize GPIO pins
//gpio = GpioFactory.getInstance();
initializePins();

}
//tag::method[]
public void test()
//end::method[]
{


// Set up a timer
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
counter++;
updateDisplay();
System.out.println(counter);
}
}, 0, 1000); // Timer updates every 1000 milliseconds (1 second)

// Keep the program running
while (true) {
// Do nothing, let the timer handle the display updates
}
}



private static void initializePins() {
digitPins = new DigitalOutput[4];

for (int i = 0; i < 4; i++) {
digitPins[i] = gpio.gpioSetMode(RaspiPin.getPinByAddress(placePin[i]), PinState.LOW);
}

Gpio.pinMode(SDI, Gpio.OUTPUT);
Gpio.pinMode(RCLK, Gpio.OUTPUT);
Gpio.pinMode(SRCLK, Gpio.OUTPUT);
}

private static void updateDisplay() {
for (int i = 0; i < 4; i++) {
clearDisplay();
pickDigit(i);
hc595Shift(number[counter % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i)]);
}
}

private static void pickDigit(int digit) {
for (int i = 0; i < 4; i++) {
digitPins[i].setState(i == digit);
}
}

private static void hc595Shift(int data) {
for (int i = 7; i >= 0; i--) {
Gpio.digitalWrite(SDI, (data & (1 << i)) != 0 ? Gpio.HIGH : Gpio.LOW);
Gpio.digitalWrite(SRCLK, Gpio.HIGH);
sleep(1);
Gpio.digitalWrite(SRCLK, Gpio.LOW);
}

Gpio.digitalWrite(RCLK, Gpio.HIGH);
sleep(1);
Gpio.digitalWrite(RCLK, Gpio.LOW);
}

private static void clearDisplay() {
for (int i = 0; i < 8; i++) {
Gpio.digitalWrite(SDI, Gpio.HIGH);
Gpio.digitalWrite(SRCLK, Gpio.HIGH);
sleep(1);
Gpio.digitalWrite(SRCLK, Gpio.LOW);
}

Gpio.digitalWrite(RCLK, Gpio.HIGH);
sleep(1);
Gpio.digitalWrite(RCLK, Gpio.LOW);
}

private static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

0 comments on commit 0e7b683

Please sign in to comment.