private MCP23xxxDigitalInputDevice getInputDevice(byte gpio) { return getDevice(createPinKey(getBoardPinInfo().getByGpioNumber(gpio)), MCP23xxxDigitalInputDevice.class); }
PinInfo pin_info = getBoardPinInfo().getByGpioNumber(gpio); DeviceInterface device = getDevice(createPinKey(pin_info)); if (device != null) {
@Override public AnalogInputDeviceInterface provisionAnalogInputDevice(int gpio) throws RuntimeIOException { // Special case - PiconZero can switch between digital and analog input hence use of gpio rather than adc PinInfo pin_info = boardPinInfo.getByGpioNumber(gpio); if (pin_info == null || ! pin_info.isSupported(DeviceMode.ANALOG_INPUT)) { throw new IllegalArgumentException("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 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 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; }
@Override public Response request(GpioPwmWrite request) { Logger.debug("GPIO PWM write request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); if (device == null) { return new Response(Response.Status.ERROR, "GPIO not provisioned", request.getCorrelationId()); } Response response; if (device instanceof PwmOutputDeviceInterface) { try { ((PwmOutputDeviceInterface) device).setValue(request.getValue()); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new Response(Response.Status.ERROR, "Runtime Error: " + e, request.getCorrelationId()); } } else { response = new Response(Response.Status.ERROR, "Invalid mode, device class: " + device.getClass().getName(), request.getCorrelationId()); } return response; }
@Override public Response request(GpioDigitalWrite request) { Logger.debug("GPIO digital write request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); if (device == null) { return new Response(Response.Status.ERROR, "GPIO not provisioned", request.getCorrelationId()); } Response response; // Also covers digital input / output device if (device instanceof GpioDigitalOutputDeviceInterface) { try { ((GpioDigitalOutputDeviceInterface) device).setValue(request.getValue()); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new Response(Response.Status.ERROR, "Runtime Error: " + e, request.getCorrelationId()); } } else { response = new Response(Response.Status.ERROR, "Invalid mode, device class: " + device.getClass().getName(), request.getCorrelationId()); } return response; }
@Override public Response request(GpioAnalogWrite request) { Logger.debug("GPIO analog write request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); if (device == null) { return new Response(Response.Status.ERROR, "GPIO not provisioned", request.getCorrelationId()); } Response response; if (device instanceof AnalogOutputDeviceInterface) { try { ((AnalogOutputDeviceInterface) device).setValue(request.getValue()); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new Response(Response.Status.ERROR, "Runtime Error: " + e, request.getCorrelationId()); } } else { response = new Response(Response.Status.ERROR, "Invalid mode, device class: " + device.getClass().getName(), request.getCorrelationId()); } return response; }
@Override public Response request(GpioClose request) { Logger.debug("GPIO close request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); if (device == null) { return new Response(Response.Status.ERROR, "GPIO not provisioned", request.getCorrelationId()); } Response response; try { device.close(); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new Response(Response.Status.ERROR, "Runtime Error: " + e, request.getCorrelationId()); } return response; }
@Override public GpioDigitalReadResponse request(GpioDigitalRead request) { Logger.debug("GPIO digital read request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); GpioDigitalReadResponse response; if (device == null) { return new GpioDigitalReadResponse("GPIO not provisioned", request.getCorrelationId()); } try { response = new GpioDigitalReadResponse(((GpioDigitalDeviceInterface) device).getValue(), request.getCorrelationId()); } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new GpioDigitalReadResponse("Runtime Error: " + e, request.getCorrelationId()); } return response; }
@Override public GpioPwmReadResponse request(GpioPwmRead request) { Logger.debug("GPIO PWM read request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); GpioPwmReadResponse response; if (device == null) { return new GpioPwmReadResponse("GPIO not provisioned", request.getCorrelationId()); } try { response = new GpioPwmReadResponse(((PwmOutputDeviceInterface) device).getValue(), request.getCorrelationId()); } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new GpioPwmReadResponse("Runtime Error: " + e, request.getCorrelationId()); } return response; }
@Override public GpioAnalogReadResponse request(GpioAnalogRead request) { Logger.debug("GPIO analog read request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); GpioAnalogReadResponse response; if (device == null) { return new GpioAnalogReadResponse("GPIO not provisioned", request.getCorrelationId()); } try { response = new GpioAnalogReadResponse(((AnalogOutputDeviceInterface) device).getValue(), request.getCorrelationId()); } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new GpioAnalogReadResponse("Runtime Error: " + e, request.getCorrelationId()); } return response; }
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; }
@Override public Response request(ProvisionAnalogInputDevice request) { Logger.debug("Analog input request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); Response response; try { if (device == null) { deviceFactory.provisionAnalogInputDevice(request.getGpio()); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else if (device instanceof AnalogInputDeviceInterface) { response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else { response = new Response(Response.Status.ERROR, "GPIO already provisioned", request.getCorrelationId()); } } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new Response(Response.Status.ERROR, "Runtime Error: " + e, request.getCorrelationId()); } return response; }
@Override public Response request(ProvisionPwmOutputDevice request) { Logger.debug("PWM output request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); Response response; try { if (device == null) { deviceFactory.provisionPwmOutputDevice(request.getGpio(), request.getFrequency(), request.getInitialValue()); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else if (device instanceof PwmOutputDeviceInterface) { ((PwmOutputDeviceInterface) device).setValue(request.getInitialValue()); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else { response = new Response(Response.Status.ERROR, "GPIO already provisioned", request.getCorrelationId()); } } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new Response(Response.Status.ERROR, "Runtime Error: " + e, request.getCorrelationId()); } return response; }
@Override public Response request(ProvisionDigitalInputOutputDevice request) { Logger.debug("GPIO input/output request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); Response response; try { if (device == null) { deviceFactory.provisionDigitalInputOutputDevice(request.getGpio(), request.getOutput() ? DeviceMode.DIGITAL_OUTPUT : DeviceMode.DIGITAL_INPUT); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else if (device instanceof GpioDigitalInputOutputDeviceInterface) { GpioDigitalInputOutputDeviceInterface inout = (GpioDigitalInputOutputDeviceInterface) device; inout.setMode(request.getOutput() ? DeviceMode.DIGITAL_OUTPUT : DeviceMode.DIGITAL_INPUT); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else { response = new Response(Response.Status.ERROR, "GPIO already provisioned", request.getCorrelationId()); } } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new Response(Response.Status.ERROR, "Runtime Error: " + e, request.getCorrelationId()); } return response; }
@Override public Response request(ProvisionAnalogOutputDevice request) { Logger.debug("Analog output request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); Response response; try { if (device == null) { AnalogOutputDeviceInterface output = deviceFactory.provisionAnalogOutputDevice(request.getGpio()); output.setValue(request.getInitialValue()); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else if (device instanceof AnalogOutputDeviceInterface) { ((AnalogOutputDeviceInterface) device).setValue(request.getInitialValue()); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else { response = new Response(Response.Status.ERROR, "GPIO already provisioned", request.getCorrelationId()); } } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new Response(Response.Status.ERROR, "Runtime Error: " + e, request.getCorrelationId()); } return response; }
@Override public Response request(ProvisionDigitalInputDevice request) { Logger.debug("GPIO input request"); PinInfo pin_info = deviceFactory.getBoardPinInfo().getByGpioNumber(request.getGpio()); String key = deviceFactory.createPinKey(pin_info); DeviceInterface device = deviceFactory.getDevice(key); Response response; try { if (device == null) { deviceFactory.provisionDigitalInputDevice(request.getGpio(), request.getPud(), request.getTrigger()); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else if (device instanceof GpioDigitalInputOutputDeviceInterface) { GpioDigitalInputOutputDeviceInterface inout = (GpioDigitalInputOutputDeviceInterface) device; inout.setMode(DeviceMode.DIGITAL_INPUT); response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else if (device instanceof GpioDigitalInputDeviceInterface) { // TODO Update the pud / trigger? response = new Response(Response.Status.OK, null, request.getCorrelationId()); } else { response = new Response(Response.Status.ERROR, "GPIO already provisioned", request.getCorrelationId()); } } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); response = new Response(Response.Status.ERROR, "Runtime Error: " + e, request.getCorrelationId()); } return response; }