Java Example

Overview

This documentation explains how to use the MitoCANdria Java API to control and monitor your MitoCANdria device. The example demonstrates basic channel operations including reading current/voltage, enabling/disabling channels, and setting voltage levels.

Basic Setup

try (MitoCANdria mito = new MitoCANdria(1)) {
    // Device operations here
}

The MitoCANdria class implements AutoCloseable, allowing use of try-with-resources for automatic resource cleanup. The constructor parameter (1) represents the device ID.

Available Channels

The API provides several predefined channels:

  • MITOCANDRIA_CHANNEL_USB1: USB Port 1

  • MITOCANDRIA_CHANNEL_USB2: USB Port 2

  • MITOCANDRIA_CHANNEL_5VA: 5V Rail A

  • MITOCANDRIA_CHANNEL_5VB: 5V Rail B

  • MITOCANDRIA_CHANNEL_ADJ: Adjustable Voltage Channel

API Operations

1. Reading Current

mito.getChannelCurrent(MitoCANdria.MITOCANDRIA_CHANNEL_USB1)
    .ifPresentOrElse(
        current -> System.out.println("USB1 current: " + current + " A"),
        () -> System.out.println("Couldn't get USB1 current")
    );
  • Uses getChannelCurrent() to read current draw from a channel

  • Returns an Optional containing the current in amperes

  • Uses ifPresentOrElse() for handling both successful and failed readings

2. Reading Voltage

mito.getChannelVoltage(MitoCANdria.MITOCANDRIA_CHANNEL_5VA)
    .ifPresentOrElse(
        voltage -> System.out.println("5VA voltage: " + voltage + " V"),
        () -> System.out.println("Couldn't get 5VA voltage")
    );
  • Uses getChannelVoltage() to read voltage from a channel

  • Returns an Optional containing the voltage in volts

  • Uses ifPresentOrElse() for error handling

3. Enabling/Disabling Channels

mito.setChannelEnabled(MitoCANdria.MITOCANDRIA_CHANNEL_USB2, true);
  • Uses setChannelEnabled() to control channel state

  • Parameters: channel constant, boolean enable state (true = enabled)

4. Setting Voltage

mito.setChannelVoltage(MitoCANdria.MITOCANDRIA_CHANNEL_ADJ, 15);
  • Uses setChannelVoltage() to set voltage on adjustable channels

  • Only works with MITOCANDRIA_CHANNEL_ADJ

  • Second parameter is desired voltage in volts

5. Reading Voltage Setpoint

mito.getChannelVoltageSetpoint(MitoCANdria.MITOCANDRIA_CHANNEL_ADJ)
    .ifPresentOrElse(
        setpoint -> System.out.println("ADJ channel setpoint: " + setpoint + " V"),
        () -> System.out.println("Couldn't get ADJ channel setpoint")
    );
  • Uses getChannelVoltageSetpoint() to read the configured voltage

  • Useful for verifying voltage settings on adjustable channels

6. Checking Channel State

mito.getChannelEnabled(MitoCANdria.MITOCANDRIA_CHANNEL_5VB)
    .ifPresentOrElse(
        enabled -> System.out.println("5VB channel enabled: " + (enabled == 1)),
        () -> System.out.println("Couldn't check if 5VB channel is enabled")
    );
  • Uses getChannelEnabled() to check if a channel is active

  • Returns 1 for enabled, 0 for disabled

Error Handling

The example uses a try-catch block to handle potential exceptions:

try {
    // Device operations
} catch (Exception e) {
    System.out.println("An error occurred: " + e.getMessage());
}

Support

For additional assistance or questions about the MitoCANdria Java API, please contact:

  • Email: contact@thethriftybot.com

Code Block:

public class MitoCANdriaExample {
    public static void main(String[] args) {
        try (MitoCANdria mito = new MitoCANdria(1)) {
            // Get and print USB1 current
            mito.getChannelCurrent(MitoCANdria.MITOCANDRIA_CHANNEL_USB1)
                .ifPresentOrElse(
                    current -> System.out.println("USB1 current: " + current + " A"),
                    () -> System.out.println("Couldn't get USB1 current")
                );

            // Get and print 5VA voltage
            mito.getChannelVoltage(MitoCANdria.MITOCANDRIA_CHANNEL_5VA)
                .ifPresentOrElse(
                    voltage -> System.out.println("5VA voltage: " + voltage + " V"),
                    () -> System.out.println("Couldn't get 5VA voltage")
                );

            // Enable USB2 channel
            mito.setChannelEnabled(MitoCANdria.MITOCANDRIA_CHANNEL_USB2, true);
            System.out.println("USB2 channel enabled");

            // Set ADJ channel voltage
            mito.setChannelVoltage(MitoCANdria.MITOCANDRIA_CHANNEL_ADJ, 3.3);
            System.out.println("ADJ channel voltage set to 3.3V");

            // Get and print ADJ channel setpoint
            mito.getChannelVoltageSetpoint(MitoCANdria.MITOCANDRIA_CHANNEL_ADJ)
                .ifPresentOrElse(
                    setpoint -> System.out.println("ADJ channel setpoint: " + setpoint + " V"),
                    () -> System.out.println("Couldn't get ADJ channel setpoint")
                );

            // Check if 5VB channel is enabled
            mito.getChannelEnabled(MitoCANdria.MITOCANDRIA_CHANNEL_5VB)
                .ifPresentOrElse(
                    enabled -> System.out.println("5VB channel enabled: " + (enabled == 1)),
                    () -> System.out.println("Couldn't check if 5VB channel is enabled")
                );

        } catch (Exception e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}

Last updated