public static PinState getState(int state) { for (PinState item : PinState.values()) { if (item.getValue() == state) { return item; } } return null; }
@Override public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { System.out.println("handleGpioPinDigitalStateChangeEvent(" + event.getState().getValue() + ")"); } }
public static PinState getState(int state) { for (PinState item : PinState.values()) { if (item.getValue() == state) { return item; } } return null; }
@Override public double getValue(Pin pin) { // the getMode() will validate the pin exists with the hasPin() function PinMode mode = getMode(pin); if (mode == PinMode.DIGITAL_OUTPUT) { return getState(pin).getValue(); } // return cached pin analog value return getPinCache(pin).getAnalogValue(); }
@Override public double getValue(Pin pin) { // the getMode() will validate the pin exists with the hasPin() function PinMode mode = getMode(pin); if (mode == PinMode.DIGITAL_OUTPUT) { return getState(pin).getValue(); } // return cached pin analog value return getPinCache(pin).getAnalogValue(); }
@Override public void setState(Pin pin, PinState state) { // validate pin if (!hasPin(pin)) { throw new InvalidPinException(pin); } // only permit invocation on pins set to DIGITAL_OUTPUT modes if (pinModeCache[pin.getAddress()] != PinMode.DIGITAL_OUTPUT) { throw new InvalidPinModeException(pin, "Invalid pin mode on pin [" + pin.getName() + "]; cannot setState() when pin mode is [" + pinModeCache[pin.getAddress()].getName() + "]"); } // control GPIO pin com.pi4j.wiringpi.Gpio.digitalWrite(pin.getAddress(), state.getValue()); // for digital output pins, we will echo the event feedback dispatchPinDigitalStateChangeEvent(pin, state); // for the Raspberry pi, we will not cache pin state since we never use the cache to get state. }
@Override public void setState(Pin pin, PinState state) { // validate pin if (!hasPin(pin)) { throw new InvalidPinException(pin); } // only permit invocation on pins set to DIGITAL_OUTPUT modes if (pinModeCache[pin.getAddress()] != PinMode.DIGITAL_OUTPUT) { throw new InvalidPinModeException(pin, "Invalid pin mode on pin [" + pin.getName() + "]; cannot setState() when pin mode is [" + pinModeCache[pin.getAddress()].getName() + "]"); } // control GPIO pin com.pi4j.wiringpi.Gpio.digitalWrite(pin.getAddress(), state.getValue()); // for digital output pins, we will echo the event feedback dispatchPinDigitalStateChangeEvent(pin, state); // for the Raspberry pi, we will not cache pin state since we never use the cache to get state. }