@Override public void setMode(int gpio, DeviceMode mode) { int reg = gpio < C2_GPIOX_PIN_START ? C2_GPIOY_FSEL_REG_OFFSET : C2_GPIOX_FSEL_REG_OFFSET; int shift = C2_GP_TO_SHIFT_REG[gpio - C2_GPIOY_PIN_START]; switch (mode) { case DIGITAL_INPUT: gpioIntBuffer.put(reg, gpioIntBuffer.get(reg) | (1 << shift)); break; case DIGITAL_OUTPUT: gpioIntBuffer.put(reg, gpioIntBuffer.get(reg) & ~(1 << shift)); break; default: throw new InvalidModeException("Invalid GPIO mode " + mode + " for pin " + gpio); } }
/** * Set the input / output mode * @param mode new mode, valid values are {@link com.diozero.api.DeviceMode DIGITAL_INPUT} and {@link com.diozero.api.DeviceMode DIGITAL_OUTPUT} */ public void setMode(DeviceMode mode) { if (mode == this.mode) { return; } if (mode != DeviceMode.DIGITAL_INPUT && mode != DeviceMode.DIGITAL_OUTPUT) { throw new InvalidModeException("Invalid mode value, must be DIGITAL_INPUT or DIGITAL_OUTPUT"); } device.setMode(mode); this.mode = mode; }
default GpioDigitalInputOutputDeviceInterface provisionDigitalInputOutputDevice(int gpio, DeviceMode mode) throws RuntimeIOException { PinInfo pin_info = getBoardPinInfo().getByGpioNumber(gpio); if (pin_info == null || ! pin_info.getModes().containsAll(PinInfo.DIGITAL_IN_OUT)) { throw new InvalidModeException("Invalid mode (digital input/output) for GPIO " + gpio); } if (mode != DeviceMode.DIGITAL_INPUT && mode != DeviceMode.DIGITAL_OUTPUT) { throw new InvalidModeException("Invalid mode, must be DIGITAL_INPUT or DIGITAL_OUTPUT"); } String key = createPinKey(pin_info); // Check if this pin is already provisioned if (isDeviceOpened(key)) { throw new DeviceAlreadyOpenedException("Device " + key + " is already in use"); } GpioDigitalInputOutputDeviceInterface device = createDigitalInputOutputDevice(key, pin_info, mode); deviceOpened(device); return device; }
default AnalogOutputDeviceInterface provisionAnalogOutputDevice(int dacNumber) throws RuntimeIOException { PinInfo pin_info = getBoardPinInfo().getByDacNumber(dacNumber); if (pin_info == null || ! pin_info.isSupported(DeviceMode.ANALOG_OUTPUT)) { throw new InvalidModeException("Invalid mode (analog output) for DAC " + dacNumber); } String key = createPinKey(pin_info); // Check if this pin is already provisioned if (isDeviceOpened(key)) { throw new DeviceAlreadyOpenedException("Device " + key + " is already in use"); } AnalogOutputDeviceInterface device = createAnalogOutputDevice(key, pin_info); deviceOpened(device); return device; }
default AnalogInputDeviceInterface provisionAnalogInputDevice(int adcNumber) throws RuntimeIOException { PinInfo pin_info = getBoardPinInfo().getByAdcNumber(adcNumber); if (pin_info == null || ! pin_info.isSupported(DeviceMode.ANALOG_INPUT)) { throw new InvalidModeException("Invalid mode (analog input) for adc " + adcNumber); } String key = createPinKey(pin_info); // Check if this pin is already provisioned if (isDeviceOpened(key)) { throw new DeviceAlreadyOpenedException("Device " + key + " is already in use"); } AnalogInputDeviceInterface device = createAnalogInputDevice(key, pin_info); deviceOpened(device); return device; }
default GpioDigitalInputDeviceInterface provisionDigitalInputDevice(int gpio, GpioPullUpDown pud, GpioEventTrigger trigger) throws RuntimeIOException { PinInfo pin_info = getBoardPinInfo().getByGpioNumber(gpio); if (pin_info == null || !pin_info.isSupported(DeviceMode.DIGITAL_INPUT)) { throw new InvalidModeException("Invalid mode (digital input) for GPIO " + gpio); } String key = createPinKey(pin_info); // Check if this pin is already provisioned if (isDeviceOpened(key)) { throw new DeviceAlreadyOpenedException("Device " + key + " is already in use"); } GpioDigitalInputDeviceInterface device = createDigitalInputDevice(key, pin_info, pud, trigger); deviceOpened(device); return device; }
default GpioDigitalOutputDeviceInterface provisionDigitalOutputDevice(int gpio, boolean initialValue) throws RuntimeIOException { PinInfo pin_info = getBoardPinInfo().getByGpioNumber(gpio); if (pin_info == null || ! pin_info.isSupported(DeviceMode.DIGITAL_OUTPUT)) { throw new InvalidModeException("Invalid mode (digital output) for GPIO " + gpio); } String key = createPinKey(pin_info); // Check if this pin is already provisioned if (isDeviceOpened(key)) { throw new DeviceAlreadyOpenedException("Device " + key + " is already in use"); } GpioDigitalOutputDeviceInterface device = createDigitalOutputDevice(key, pin_info, initialValue); deviceOpened(device); return device; }
@Override public AnalogInputDeviceInterface provisionAnalogInputDevice(int gpio) { // Special case - The Arduino can switch between digital and analog input hence use of gpio rather than adc PinInfo pin_info = getBoardPinInfo().getByGpioNumber(gpio); if (pin_info == null || ! pin_info.isSupported(DeviceMode.ANALOG_INPUT)) { throw new InvalidModeException("Invalid mode (analog input) for GPIO " + gpio); } String key = createPinKey(pin_info); // Check if this pin is already provisioned if (isDeviceOpened(key)) { throw new DeviceAlreadyOpenedException("Device " + key + " is already in use"); } AnalogInputDeviceInterface device = createAnalogInputDevice(key, pin_info); deviceOpened(device); return device; }
default PwmOutputDeviceInterface provisionPwmOutputDevice(int pwmOrGpioNum, int pwmFrequency, float initialValue) throws RuntimeIOException { // Lookup by PWM number first, if not found or doesn't support // PWM_OUTPUT, lookup by GPIO number PinInfo pin_info = getBoardPinInfo().getByPwmNumber(pwmOrGpioNum); if (pin_info == null || !pin_info.isSupported(DeviceMode.PWM_OUTPUT)) { pin_info = getBoardPinInfo().getByGpioNumber(pwmOrGpioNum); } if (pin_info != null && (pin_info.isSupported(DeviceMode.PWM_OUTPUT) || pin_info.isSupported(DeviceMode.DIGITAL_OUTPUT))) { // Ok } else { throw new InvalidModeException("Invalid mode (PWM output) for GPIO " + pwmOrGpioNum); } String key = createPinKey(pin_info); // Check if this pin is already provisioned if (isDeviceOpened(key)) { throw new DeviceAlreadyOpenedException("Device " + key + " is already in use"); } PwmOutputDeviceInterface device = createPwmOutputDevice(key, pin_info, pwmFrequency, initialValue); deviceOpened(device); return device; }