Skip to content

Commit

Permalink
Merge pull request #12 from mk868/drive-mode
Browse files Browse the repository at this point in the history
Drive mode improvements + Javadoc
  • Loading branch information
mk868 authored Nov 2, 2024
2 parents 84250cb + 29c6467 commit 2fb0c69
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LineSessionBiasChangeIT {

@ParameterizedTest
@FieldSource("PINS")
void read_value_should_read_actual_bias_value_on_hanging_line(TestPin inputWithBiasPin) {
void should_read_value_matching_bias_on_hanging_line(TestPin inputWithBiasPin) {
var jgpio = Jgpio.getInstance();
try (var inputChip1 = jgpio.openChipByName(inputWithBiasPin.chipName());
var biasInputLine = inputChip1.getLine(inputWithBiasPin.lineOffset())
Expand All @@ -38,9 +38,37 @@ void read_value_should_read_actual_bias_value_on_hanging_line(TestPin inputWithB
}
}

@ParameterizedTest
@FieldSource("PINS")
void should_read_true_for_pull_up_bias_on_hanging_line(TestPin inputWithBiasPin) {
var jgpio = Jgpio.getInstance();
try (var inputChip1 = jgpio.openChipByName(inputWithBiasPin.chipName());
var biasInputLine = inputChip1.getLine(inputWithBiasPin.lineOffset())
.openAsInput(Bias.PULL_UP);
) {
assertThat(biasInputLine.read())
.as("Input line level")
.isTrue();
}
}

@ParameterizedTest
@FieldSource("PINS")
void should_read_false_for_pull_down_bias_on_hanging_line(TestPin inputWithBiasPin) {
var jgpio = Jgpio.getInstance();
try (var inputChip1 = jgpio.openChipByName(inputWithBiasPin.chipName());
var biasInputLine = inputChip1.getLine(inputWithBiasPin.lineOffset())
.openAsInput(Bias.PULL_DOWN);
) {
assertThat(biasInputLine.read())
.as("Input line level")
.isFalse();
}
}

@ParameterizedTest
@FieldSource("COUPLED_LINES")
void high_impedance_line_should_detect_bias_change_on_coupled_line(TwoPins coupledLines) {
void should_read_value_matching_bias_on_coupled_line(TwoPins coupledLines) {
var inputWithBiasPin = coupledLines.pin1();
var inputNoBiasPin = coupledLines.pin2();

Expand All @@ -62,4 +90,44 @@ void high_impedance_line_should_detect_bias_change_on_coupled_line(TwoPins coupl
.isFalse();
}
}

@ParameterizedTest
@FieldSource("COUPLED_LINES")
void should_read_true_for_pull_up_bias_on_coupled_line(TwoPins coupledLines) {
var inputWithBiasPin = coupledLines.pin1();
var inputNoBiasPin = coupledLines.pin2();

var jgpio = Jgpio.getInstance();
try (var inputChip1 = jgpio.openChipByName(inputWithBiasPin.chipName());
var _ = inputChip1.getLine(inputWithBiasPin.lineOffset())
.openAsInput(Bias.PULL_UP);
var inputChip2 = jgpio.openChipByName(inputNoBiasPin.chipName());
var highImpedanceLine = inputChip2.getLine(inputNoBiasPin.lineOffset())
.openAsInput(Bias.HIGH_IMPEDANCE);
) {
assertThat(highImpedanceLine.read())
.as("Input line level")
.isTrue();
}
}

@ParameterizedTest
@FieldSource("COUPLED_LINES")
void should_read_true_for_pull_down_bias_on_coupled_line(TwoPins coupledLines) {
var inputWithBiasPin = coupledLines.pin1();
var inputNoBiasPin = coupledLines.pin2();

var jgpio = Jgpio.getInstance();
try (var inputChip1 = jgpio.openChipByName(inputWithBiasPin.chipName());
var _ = inputChip1.getLine(inputWithBiasPin.lineOffset())
.openAsInput(Bias.PULL_DOWN);
var inputChip2 = jgpio.openChipByName(inputNoBiasPin.chipName());
var highImpedanceLine = inputChip2.getLine(inputNoBiasPin.lineOffset())
.openAsInput(Bias.HIGH_IMPEDANCE);
) {
assertThat(highImpedanceLine.read())
.as("Input line level")
.isFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static Stream<Arguments> args() {
of(OPEN_DRAIN, Bias.HIGH_IMPEDANCE, false, false),
//of(OPEN_DRAIN, Bias.HIGH_IMPEDANCE, true, undefined),
of(OPEN_DRAIN, Bias.PULL_UP, false, false),
of(OPEN_DRAIN, Bias.PULL_UP, true, true),//???
of(OPEN_DRAIN, Bias.PULL_UP, true, true),
of(OPEN_DRAIN, Bias.PULL_DOWN, false, false),
of(OPEN_DRAIN, Bias.PULL_DOWN, true, false),
// OPEN_DRAIN_PULL_UP
Expand All @@ -55,7 +55,7 @@ private static Stream<Arguments> args() {
of(OPEN_SOURCE, Bias.HIGH_IMPEDANCE, true, true),
of(OPEN_SOURCE, Bias.PULL_UP, false, true),
of(OPEN_SOURCE, Bias.PULL_UP, true, true),
of(OPEN_SOURCE, Bias.PULL_DOWN, false, false),//???
of(OPEN_SOURCE, Bias.PULL_DOWN, false, false),
of(OPEN_SOURCE, Bias.PULL_DOWN, true, true),
// OPEN_SOURCE_PULL_DOWN
of(OPEN_SOURCE_PULL_DOWN, Bias.HIGH_IMPEDANCE, false, false),
Expand Down
14 changes: 14 additions & 0 deletions jgpio/src/main/java/eu/softpol/lib/jgpio/DriveMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@
*/
package eu.softpol.lib.jgpio;

/// Enum representing the drive modes for a GPIO line.
///
/// The DriveMode defines the configuration of the GPIO line's output driver.
public enum DriveMode {
/// Drive mode where the GPIO line is actively driven to both high and low states.
PUSH_PULL,
/// Drive mode where the GPIO line is set to open-drain. In this mode, the GPIO can either drive
/// the line low or leave it floating.
OPEN_DRAIN,
/// Drive mode where the GPIO line is set to open-drain with an internal pull-up resistor. In this
/// mode, the GPIO can drive the line low, or use the pull-up resistor to bring the line to a high
/// state.
OPEN_DRAIN_PULL_UP,
/// Drive mode where the GPIO line is set to open-source. In this mode, the GPIO can either drive
/// the line high or leave it floating.
OPEN_SOURCE,
/// Drive mode where the GPIO line is set to open-source with an internal pull-down resistor. In
/// this mode, the GPIO can drive the line high, or use the pull-down resistor to bring the line
/// to a low state.
OPEN_SOURCE_PULL_DOWN;
}
4 changes: 4 additions & 0 deletions jgpio/src/main/java/eu/softpol/lib/jgpio/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public interface Line {
/// @return the session for the output line, which must be closed after use
LineOutputSession openAsOutput();

/// Opens the GPIO line as an output with the specified drive mode.
///
/// @param driveMode the drive mode to be set for the GPIO line
/// @return the session for the output line, which must be closed after use
LineOutputSession openAsOutput(DriveMode driveMode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
/// The methods of this interface should not be called after [Chip#close()].
public interface LineOutputSession extends Closeable {

/// Sets the drive mode for the output line.
///
/// @param driveMode the drive mode to be set for the GPIO line
void setDriveMode(DriveMode driveMode);

/// Writes the new value of the output line.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package eu.softpol.lib.jgpio.internal.gpiod;

import eu.softpol.lib.jgpio.DriveMode;
import eu.softpol.lib.jgpio.JgpioException;
import eu.softpol.lib.jgpio.LineOutputSession;
import eu.softpol.lib.jgpio.DriveMode;
import eu.softpol.lib.jgpio.internal.ffm.libgpiod.gpiod_h;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
Expand Down Expand Up @@ -75,11 +75,13 @@ public void write(boolean value) {

private int toFlags(DriveMode driveMode) {
return switch (driveMode) {
case PUSH_PULL -> 0;
case OPEN_DRAIN -> gpiod_h.GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN();
case PUSH_PULL -> gpiod_h.GPIOD_LINE_REQUEST_FLAG_BIAS_DISABLE();
case OPEN_DRAIN -> gpiod_h.GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN()
+ gpiod_h.GPIOD_LINE_REQUEST_FLAG_BIAS_DISABLE();
case OPEN_DRAIN_PULL_UP -> gpiod_h.GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN()
+ gpiod_h.GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP();
case OPEN_SOURCE -> gpiod_h.GPIOD_LINE_REQUEST_FLAG_OPEN_SOURCE();
case OPEN_SOURCE -> gpiod_h.GPIOD_LINE_REQUEST_FLAG_OPEN_SOURCE()
+ gpiod_h.GPIOD_LINE_REQUEST_FLAG_BIAS_DISABLE();
case OPEN_SOURCE_PULL_DOWN -> gpiod_h.GPIOD_LINE_REQUEST_FLAG_OPEN_SOURCE()
+ gpiod_h.GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_DOWN();
};
Expand Down

0 comments on commit 2fb0c69

Please sign in to comment.