All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zaber.motion.ascii.DeviceIO Maven / Gradle / Ivy

Go to download

A library that aims to provide easy-to-use API for communication with Zaber devices using Zaber ASCII Protocol.

There is a newer version: 6.7.0
Show newest version
// ===== THIS FILE IS GENERATED FROM A TEMPLATE ===== //
// ============== DO NOT EDIT DIRECTLY ============== //

package zaber.motion.ascii;

import zaber.motion.ArrayUtility;
import zaber.motion.protobufs.Main;
import zaber.motion.gateway.Call;
import zaber.motion.exceptions.MotionLibException;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * Class providing access to the I/O channels of the device.
 */
public class DeviceIO {
    private Device device;

    public DeviceIO(
        Device device) {
        this.device = device;
    }

    /**
     * Returns the current values of all digital input channels.
     * @return A CompletableFuture that can be completed to get the result:
     * True if voltage is present on the input channel and false otherwise.
     */
    public CompletableFuture getAllDigitalInputsAsync() {
        Main.DeviceGetAllDigitalIORequest.Builder builder = Main.DeviceGetAllDigitalIORequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());
        builder = builder.setChannelType("di");

        CompletableFuture response = Call.callAsync(
            "device/get_all_digital_io",
            builder.build(),
            Main.DeviceGetAllDigitalIOResponse.parser());
        return response
            .thenApply(r -> ArrayUtility.toPrimitiveArray(r.getValuesList().stream().toArray(Boolean[]::new)));
    }

    /**
     * Returns the current values of all digital input channels.
     * @return True if voltage is present on the input channel and false otherwise.
     */
    public boolean[] getAllDigitalInputs() {
        try {
            return getAllDigitalInputsAsync().get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Returns the current values of all digital output channels.
     * @return A CompletableFuture that can be completed to get the result:
     * True if the output channel is conducting and false otherwise.
     */
    public CompletableFuture getAllDigitalOutputsAsync() {
        Main.DeviceGetAllDigitalIORequest.Builder builder = Main.DeviceGetAllDigitalIORequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());
        builder = builder.setChannelType("do");

        CompletableFuture response = Call.callAsync(
            "device/get_all_digital_io",
            builder.build(),
            Main.DeviceGetAllDigitalIOResponse.parser());
        return response
            .thenApply(r -> ArrayUtility.toPrimitiveArray(r.getValuesList().stream().toArray(Boolean[]::new)));
    }

    /**
     * Returns the current values of all digital output channels.
     * @return True if the output channel is conducting and false otherwise.
     */
    public boolean[] getAllDigitalOutputs() {
        try {
            return getAllDigitalOutputsAsync().get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Returns the current values of all analog input channels.
     * @return A CompletableFuture that can be completed to get the result:
     *  Measurements of the voltage present on the input channels.
     */
    public CompletableFuture getAllAnalogInputsAsync() {
        Main.DeviceGetAllAnalogIORequest.Builder builder = Main.DeviceGetAllAnalogIORequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());
        builder = builder.setChannelType("ai");

        CompletableFuture response = Call.callAsync(
            "device/get_all_analog_io",
            builder.build(),
            Main.DeviceGetAllAnalogIOResponse.parser());
        return response
            .thenApply(r -> ArrayUtility.toPrimitiveArray(r.getValuesList().stream().toArray(Double[]::new)));
    }

    /**
     * Returns the current values of all analog input channels.
     * @return  Measurements of the voltage present on the input channels.
     */
    public double[] getAllAnalogInputs() {
        try {
            return getAllAnalogInputsAsync().get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Returns the current values of all analog output channels.
     * @return A CompletableFuture that can be completed to get the result:
     *  Measurements of voltage that the output channels are conducting.
     */
    public CompletableFuture getAllAnalogOutputsAsync() {
        Main.DeviceGetAllAnalogIORequest.Builder builder = Main.DeviceGetAllAnalogIORequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());
        builder = builder.setChannelType("ao");

        CompletableFuture response = Call.callAsync(
            "device/get_all_analog_io",
            builder.build(),
            Main.DeviceGetAllAnalogIOResponse.parser());
        return response
            .thenApply(r -> ArrayUtility.toPrimitiveArray(r.getValuesList().stream().toArray(Double[]::new)));
    }

    /**
     * Returns the current values of all analog output channels.
     * @return  Measurements of voltage that the output channels are conducting.
     */
    public double[] getAllAnalogOutputs() {
        try {
            return getAllAnalogOutputsAsync().get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Returns the current value of the specified digital input channel.
     * @param channelNumber Channel number starting at 1.
     * @return A CompletableFuture that can be completed to get the result:
     * True if voltage is present on the input channel and false otherwise.
     */
    public CompletableFuture getDigitalInputAsync(
        int channelNumber) {
        Main.DeviceGetDigitalIORequest.Builder builder = Main.DeviceGetDigitalIORequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());
        builder = builder.setChannelType("di");

        builder = builder.setChannelNumber(channelNumber);
        CompletableFuture response = Call.callAsync(
            "device/get_digital_io",
            builder.build(),
            Main.DeviceGetDigitalIOResponse.parser());
        return response
            .thenApply(r -> r.getValue());
    }

    /**
     * Returns the current value of the specified digital input channel.
     * @param channelNumber Channel number starting at 1.
     * @return True if voltage is present on the input channel and false otherwise.
     */
    public boolean getDigitalInput(
        int channelNumber) {
        try {
            return getDigitalInputAsync(channelNumber).get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Returns the current value of the specified digital output channel.
     * @param channelNumber Channel number starting at 1.
     * @return A CompletableFuture that can be completed to get the result:
     * True if the output channel is conducting and false otherwise.
     */
    public CompletableFuture getDigitalOutputAsync(
        int channelNumber) {
        Main.DeviceGetDigitalIORequest.Builder builder = Main.DeviceGetDigitalIORequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());
        builder = builder.setChannelType("do");

        builder = builder.setChannelNumber(channelNumber);
        CompletableFuture response = Call.callAsync(
            "device/get_digital_io",
            builder.build(),
            Main.DeviceGetDigitalIOResponse.parser());
        return response
            .thenApply(r -> r.getValue());
    }

    /**
     * Returns the current value of the specified digital output channel.
     * @param channelNumber Channel number starting at 1.
     * @return True if the output channel is conducting and false otherwise.
     */
    public boolean getDigitalOutput(
        int channelNumber) {
        try {
            return getDigitalOutputAsync(channelNumber).get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Returns the current value of the specified analog input channel.
     * @param channelNumber Channel number starting at 1.
     * @return A CompletableFuture that can be completed to get the result:
     *  A measurementsof the voltage present on the input channel.
     */
    public CompletableFuture getAnalogInputAsync(
        int channelNumber) {
        Main.DeviceGetAnalogIORequest.Builder builder = Main.DeviceGetAnalogIORequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());
        builder = builder.setChannelType("ai");

        builder = builder.setChannelNumber(channelNumber);
        CompletableFuture response = Call.callAsync(
            "device/get_analog_io",
            builder.build(),
            Main.DeviceGetAnalogIOResponse.parser());
        return response
            .thenApply(r -> r.getValue());
    }

    /**
     * Returns the current value of the specified analog input channel.
     * @param channelNumber Channel number starting at 1.
     * @return  A measurementsof the voltage present on the input channel.
     */
    public double getAnalogInput(
        int channelNumber) {
        try {
            return getAnalogInputAsync(channelNumber).get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Returns the current values of the specified analog output channel.
     * @param channelNumber Channel number starting at 1.
     * @return A CompletableFuture that can be completed to get the result:
     * A measurement of voltage that the output channel is conducting.
     */
    public CompletableFuture getAnalogOutputAsync(
        int channelNumber) {
        Main.DeviceGetAnalogIORequest.Builder builder = Main.DeviceGetAnalogIORequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());
        builder = builder.setChannelType("ao");

        builder = builder.setChannelNumber(channelNumber);
        CompletableFuture response = Call.callAsync(
            "device/get_analog_io",
            builder.build(),
            Main.DeviceGetAnalogIOResponse.parser());
        return response
            .thenApply(r -> r.getValue());
    }

    /**
     * Returns the current values of the specified analog output channel.
     * @param channelNumber Channel number starting at 1.
     * @return A measurement of voltage that the output channel is conducting.
     */
    public double getAnalogOutput(
        int channelNumber) {
        try {
            return getAnalogOutputAsync(channelNumber).get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Sets values for all digital output channels.
     * @param values True to set the output channel to conducting and false to turn it off.
     * @return A CompletableFuture that can be completed to know when the work is complete.
     */
    public CompletableFuture setAllDigitalOutputsAsync(
        boolean[] values) {
        Main.DeviceSetAllDigitalOutputsRequest.Builder builder = Main.DeviceSetAllDigitalOutputsRequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());

        builder = builder.addAllValues(
            Arrays.asList(ArrayUtility.toObjectArray(values))
        );
        return Call.callAsync("device/set_all_digital_outputs", builder.build(), null)
            .thenApply(r -> (Void) null);
    }

    /**
     * Sets values for all digital output channels.
     * @param values True to set the output channel to conducting and false to turn it off.
     */
    public void setAllDigitalOutputs(
        boolean[] values) {
        try {
            setAllDigitalOutputsAsync(values).get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Sets values for all analog output channels.
     * @param values Voltage values to set the output channels to.
     * @return A CompletableFuture that can be completed to know when the work is complete.
     */
    public CompletableFuture setAllAnalogOutputsAsync(
        double[] values) {
        Main.DeviceSetAllAnalogOutputsRequest.Builder builder = Main.DeviceSetAllAnalogOutputsRequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());

        builder = builder.addAllValues(
            Arrays.asList(ArrayUtility.toObjectArray(values))
        );
        return Call.callAsync("device/set_all_analog_outputs", builder.build(), null)
            .thenApply(r -> (Void) null);
    }

    /**
     * Sets values for all analog output channels.
     * @param values Voltage values to set the output channels to.
     */
    public void setAllAnalogOutputs(
        double[] values) {
        try {
            setAllAnalogOutputsAsync(values).get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Sets value for the specified digital output channel.
     * @param channelNumber Channel number starting at 1.
     * @param value True to set the output channel to conducting and false to turn it off.
     * @return A CompletableFuture that can be completed to know when the work is complete.
     */
    public CompletableFuture setDigitalOutputAsync(
        int channelNumber, boolean value) {
        Main.DeviceSetDigitalOutputRequest.Builder builder = Main.DeviceSetDigitalOutputRequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());

        builder = builder.setChannelNumber(channelNumber);
        builder = builder.setValue(value);
        return Call.callAsync("device/set_digital_output", builder.build(), null)
            .thenApply(r -> (Void) null);
    }

    /**
     * Sets value for the specified digital output channel.
     * @param channelNumber Channel number starting at 1.
     * @param value True to set the output channel to conducting and false to turn it off.
     */
    public void setDigitalOutput(
        int channelNumber, boolean value) {
        try {
            setDigitalOutputAsync(channelNumber, value).get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Sets value for the specified analog output channel.
     * @param channelNumber Channel number starting at 1.
     * @param value Value to set the output channel voltage to.
     * @return A CompletableFuture that can be completed to know when the work is complete.
     */
    public CompletableFuture setAnalogOutputAsync(
        int channelNumber, double value) {
        Main.DeviceSetAnalogOutputRequest.Builder builder = Main.DeviceSetAnalogOutputRequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());

        builder = builder.setChannelNumber(channelNumber);
        builder = builder.setValue(value);
        return Call.callAsync("device/set_analog_output", builder.build(), null)
            .thenApply(r -> (Void) null);
    }

    /**
     * Sets value for the specified analog output channel.
     * @param channelNumber Channel number starting at 1.
     * @param value Value to set the output channel voltage to.
     */
    public void setAnalogOutput(
        int channelNumber, double value) {
        try {
            setAnalogOutputAsync(channelNumber, value).get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

    /**
     * Returns the number of I/O channels the device has.
     * @return A CompletableFuture that can be completed to get the result:
     * An object containing the number of I/O channels the device has.
     */
    public CompletableFuture getChannelsInfoAsync() {
        Main.DeviceGetIOChannelsInfoRequest.Builder builder = Main.DeviceGetIOChannelsInfoRequest.newBuilder();
        builder = builder.setInterfaceId(this.device.getConnection().getInterfaceId());
        builder = builder.setDevice(this.device.getDeviceAddress());

        CompletableFuture response = Call.callAsync(
            "device/get_io_info",
            builder.build(),
            Main.DeviceIOInfo.parser());
        return response
            .thenApply(r -> DeviceIOInfo.fromProtobuf(r));
    }

    /**
     * Returns the number of I/O channels the device has.
     * @return An object containing the number of I/O channels the device has.
     */
    public DeviceIOInfo getChannelsInfo() {
        try {
            return getChannelsInfoAsync().get();
        } catch (ExecutionException e) {
            if (e.getCause() instanceof MotionLibException) {
                throw (MotionLibException) e.getCause();
            } else {
                throw new MotionLibException(e.getCause());
            }
        } catch (InterruptedException e) {
            throw new MotionLibException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy