@Override public void export(Pin pin, PinMode mode) { export(pin, mode, null); }
@Override public boolean isExported(Pin pin) { // Odroid AIN pins are not exported if (getMode(pin) == PinMode.ANALOG_INPUT) { return false; } return super.isExported(pin); }
@Override public void unexport(Pin pin) { // no need to unexport an Odroid AIN pin if (pin.getSupportedPinModes().contains(PinMode.ANALOG_INPUT)) { return; } super.unexport(pin); }
@Override public void setPwm(Pin pin, int value) { // validate pin if (!hasPin(pin)) { throw new InvalidPinException(pin); } // get pin configured mode PinMode mode = getMode(pin); // validate mode; set PWM value based on pin mode if (mode == PinMode.PWM_OUTPUT) { // set pin hardware PWM value com.pi4j.wiringpi.Gpio.pwmWrite(pin.getAddress(), value); } else if(mode == PinMode.SOFT_PWM_OUTPUT) { // set pin software emulated PWM value com.pi4j.wiringpi.SoftPwm.softPwmWrite(pin.getAddress(), value); } else { // unsupported pin mode throw new InvalidPinModeException(pin, "Invalid pin mode [" + mode.getName() + "]; unable to setPwm(" + value + ")"); } // cache updated pin PWM value getPinCache(pin).setPwmValue(value); }
@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 setPwmRange(Pin pin, int range){ // validate pin if (!hasPin(pin)) { throw new InvalidPinException(pin); } // get pin configured mode PinMode mode = getMode(pin); // validate mode; set PWM value based on pin mode if (mode == PinMode.PWM_OUTPUT) { // set pin hardware PWM value com.pi4j.wiringpi.Gpio.pwmSetRange(range); } else if(mode == PinMode.SOFT_PWM_OUTPUT) { // first, stop the software emulated PWM driver for this pin com.pi4j.wiringpi.SoftPwm.softPwmStop(pin.getAddress()); // update the PWM range for this pin com.pi4j.wiringpi.SoftPwm.softPwmCreate(pin.getAddress(), 0, range); } else { // unsupported operation throw new UnsupportedOperationException(); } }
@Override public void addListener(Pin pin, PinListener listener) { super.addListener(pin, listener); // update the native interrupt listener thread for callbacks updateInterruptListener(pin); }
@Override public void setMode(Pin pin, PinMode mode) { // no need to export an Odroid AIN pin if (mode == PinMode.ANALOG_INPUT) { if (!pin.getSupportedPinModes().contains(mode)) { throw new InvalidPinModeException(pin, "Invalid pin mode [" + mode.getName() + "]; pin [" + pin.getName() + "] does not support this mode."); } // local pin mode cache pinModeCache[pin.getAddress()] = mode; // cache mode getPinCache(pin).setMode(mode); return; } super.setMode(pin, mode); }
@Override public void pinStateChange(GpioInterruptEvent event) { // iterate over the pin listeners map for (Pin pin : listeners.keySet()) { // dispatch this event to the listener // if a matching pin address is found if (pin.getAddress() == event.getPin()) { dispatchPinDigitalStateChangeEvent(pin, PinState.getState(event.getState())); } } }
@Override public void setMode(Pin pin, PinMode mode) { super.setMode(pin, mode); // local pin mode cache pinModeCache[pin.getAddress()] = mode; if (!pin.getSupportedPinModes().contains(mode)) { throw new InvalidPinModeException(pin, "Invalid pin mode [" + mode.getName() + "]; pin [" + pin.getName() + "] does not support this mode."); } if (!pin.getSupportedPinModes().contains(mode)) { throw new UnsupportedPinModeException(pin, mode); } // cache mode getPinCache(pin).setMode(mode); // set pin mode on hardware com.pi4j.wiringpi.Gpio.pinMode(pin.getAddress(), mode.getValue()); }
@Override public double getValue(Pin pin) { // the getMode() will validate the pin exists with the hasPin() function PinMode mode = getMode(pin); // handle analog input reading for Odroid boards if (mode == PinMode.ANALOG_INPUT) { // read latest analog input value from WiringPi // we need to re-address the pin for Odroid boards (analog_address = assigned_pin_address - AIN_ADDRESS_OFFSET) double value = com.pi4j.wiringpi.Gpio.analogRead(pin.getAddress() - AIN_ADDRESS_OFFSET); // cache latest analog input value getPinCache(pin).setAnalogValue(value); // return latest analog input value return value; } return super.getValue(pin); }
@Override public void setPwm(Pin pin, int value) { // validate pin if (!hasPin(pin)) { throw new InvalidPinException(pin); } // get pin configured mode PinMode mode = getMode(pin); // validate mode; set PWM value based on pin mode if (mode == PinMode.PWM_OUTPUT) { // set pin hardware PWM value com.pi4j.wiringpi.Gpio.pwmWrite(pin.getAddress(), value); } else if(mode == PinMode.SOFT_PWM_OUTPUT) { // set pin software emulated PWM value com.pi4j.wiringpi.SoftPwm.softPwmWrite(pin.getAddress(), value); } else { // unsupported pin mode throw new InvalidPinModeException(pin, "Invalid pin mode [" + mode.getName() + "]; unable to setPwm(" + value + ")"); } // cache updated pin PWM value getPinCache(pin).setPwmValue(value); }
@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 setPwmRange(Pin pin, int range){ // validate pin if (!hasPin(pin)) { throw new InvalidPinException(pin); } // get pin configured mode PinMode mode = getMode(pin); // validate mode; set PWM value based on pin mode if (mode == PinMode.PWM_OUTPUT) { // set pin hardware PWM value com.pi4j.wiringpi.Gpio.pwmSetRange(range); } else if(mode == PinMode.SOFT_PWM_OUTPUT) { // first, stop the software emulated PWM driver for this pin com.pi4j.wiringpi.SoftPwm.softPwmStop(pin.getAddress()); // update the PWM range for this pin com.pi4j.wiringpi.SoftPwm.softPwmCreate(pin.getAddress(), 0, range); } else { // unsupported operation throw new UnsupportedOperationException(); } }
@Override public void removeListener(Pin pin, PinListener listener) { super.removeListener(pin, listener); // update the native interrupt listener thread for callbacks updateInterruptListener(pin); }
@Override public void setMode(Pin pin, PinMode mode) { // no need to export an Odroid AIN pin if (mode == PinMode.ANALOG_INPUT) { if (!pin.getSupportedPinModes().contains(mode)) { throw new InvalidPinModeException(pin, "Invalid pin mode [" + mode.getName() + "]; pin [" + pin.getName() + "] does not support this mode."); } // local pin mode cache pinModeCache[pin.getAddress()] = mode; // cache mode getPinCache(pin).setMode(mode); return; } super.setMode(pin, mode); }
@Override public void pinStateChange(GpioInterruptEvent event) { // iterate over the pin listeners map for (Pin pin : listeners.keySet()) { // dispatch this event to the listener // if a matching pin address is found if (pin.getAddress() == event.getPin()) { dispatchPinDigitalStateChangeEvent(pin, PinState.getState(event.getState())); } } }
@Override public void setMode(Pin pin, PinMode mode) { super.setMode(pin, mode); // local pin mode cache pinModeCache[pin.getAddress()] = mode; if (!pin.getSupportedPinModes().contains(mode)) { throw new InvalidPinModeException(pin, "Invalid pin mode [" + mode.getName() + "]; pin [" + pin.getName() + "] does not support this mode."); } if (!pin.getSupportedPinModes().contains(mode)) { throw new UnsupportedPinModeException(pin, mode); } // cache mode getPinCache(pin).setMode(mode); // set pin mode on hardware com.pi4j.wiringpi.Gpio.pinMode(pin.getAddress(), mode.getValue()); }
@Override public double getValue(Pin pin) { // the getMode() will validate the pin exists with the hasPin() function PinMode mode = getMode(pin); // handle analog input reading for Odroid boards if (mode == PinMode.ANALOG_INPUT) { // read latest analog input value from WiringPi // we need to re-address the pin for Odroid boards (analog_address = assigned_pin_address - AIN_ADDRESS_OFFSET) double value = com.pi4j.wiringpi.Gpio.analogRead(pin.getAddress() - AIN_ADDRESS_OFFSET); // cache latest analog input value getPinCache(pin).setAnalogValue(value); // return latest analog input value return value; } return super.getValue(pin); }
@Override public void export(Pin pin, PinMode mode) { export(pin, mode, null); }