Search in sources :

Example 11 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class Gamepad method fromByteArray.

@Override
public void fromByteArray(byte[] byteArray) throws RobotCoreException {
    if (byteArray.length < BUFFER_SIZE) {
        throw new RobotCoreException("Expected buffer of at least " + BUFFER_SIZE + " bytes, received " + byteArray.length);
    }
    ByteBuffer byteBuffer = getReadBuffer(byteArray);
    int buttons = 0;
    byte version = byteBuffer.get();
    // extract version 1 values
    if (version >= 1) {
        id = byteBuffer.getInt();
        timestamp = byteBuffer.getLong();
        left_stick_x = byteBuffer.getFloat();
        left_stick_y = byteBuffer.getFloat();
        right_stick_x = byteBuffer.getFloat();
        right_stick_y = byteBuffer.getFloat();
        left_trigger = byteBuffer.getFloat();
        right_trigger = byteBuffer.getFloat();
        buttons = byteBuffer.getInt();
        left_stick_button = (buttons & 0x04000) != 0;
        right_stick_button = (buttons & 0x02000) != 0;
        dpad_up = (buttons & 0x01000) != 0;
        dpad_down = (buttons & 0x00800) != 0;
        dpad_left = (buttons & 0x00400) != 0;
        dpad_right = (buttons & 0x00200) != 0;
        a = (buttons & 0x00100) != 0;
        b = (buttons & 0x00080) != 0;
        x = (buttons & 0x00040) != 0;
        y = (buttons & 0x00020) != 0;
        guide = (buttons & 0x00010) != 0;
        start = (buttons & 0x00008) != 0;
        back = (buttons & 0x00004) != 0;
        left_bumper = (buttons & 0x00002) != 0;
        right_bumper = (buttons & 0x00001) != 0;
    }
    // extract version 2 values
    if (version >= 2) {
        user = byteBuffer.get();
    }
    callCallback();
}
Also used : RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) ByteBuffer(java.nio.ByteBuffer)

Example 12 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class FtcConfigurationActivity method onDoneButtonPressed.

/**
 * A button-specific method, this gets called when you click the "Done" button.
 * This writes the current objects into an XML file located in the Configuration File Directory.
 * The user is prompted for the name of the file.
 *
 * @param v the View from which this was called
 */
public void onDoneButtonPressed(View v) {
    RobotLog.vv(TAG, "onDoneButtonPressed()");
    // Generate the XML. If that failed, we will already have complained to the user.
    final String data = robotConfigFileManager.toXml(getRobotConfigMap());
    if (data == null) {
        return;
    }
    String message = getString(R.string.configNamePromptBanter);
    final EditText input = new EditText(this);
    input.setText(currentCfgFile.isNoConfig() ? "" : currentCfgFile.getName());
    AlertDialog.Builder builder = utility.buildBuilder(getString(R.string.configNamePromptTitle), message);
    builder.setView(input);
    DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int button) {
            String newConfigurationName = input.getText().toString();
            RobotConfigFileManager.ConfigNameCheckResult checkResult = robotConfigFileManager.isPlausibleConfigName(currentCfgFile, newConfigurationName, extantRobotConfigurations);
            if (!checkResult.success) {
                // User hasn't given us file name that works
                String message = String.format(checkResult.errorFormat, newConfigurationName);
                appUtil.showToast(UILocation.ONLY_LOCAL, context, String.format("%s %s", message, getString(R.string.configurationNotSaved)));
                return;
            }
            try {
                /*
                     * If the user changed the name then we create a new set of metadata for the
                     * new name and set the active config to be the new metadata
                     */
                if (!currentCfgFile.getName().equals(newConfigurationName)) {
                    currentCfgFile = new RobotConfigFile(robotConfigFileManager, newConfigurationName);
                }
                robotConfigFileManager.writeToFile(currentCfgFile, remoteConfigure, data);
                robotConfigFileManager.setActiveConfigAndUpdateUI(remoteConfigure, currentCfgFile);
            } catch (DuplicateNameException e) {
                warnDuplicateNames(e.getMessage());
                RobotLog.ee(TAG, e.getMessage());
                return;
            } catch (RobotCoreException | IOException e) {
                appUtil.showToast(UILocation.ONLY_LOCAL, context, e.getMessage());
                RobotLog.ee(TAG, e.getMessage());
                return;
            }
            clearDuplicateWarning();
            confirmSave();
            pauseAfterSave();
            finishOk();
        }
    };
    builder.setPositiveButton(getString(R.string.buttonNameOK), okListener);
    builder.setNegativeButton(getString(R.string.buttonNameCancel), doNothingAndCloseListener);
    builder.show();
}
Also used : EditText(android.widget.EditText) AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) IOException(java.io.IOException) DuplicateNameException(com.qualcomm.robotcore.exception.DuplicateNameException) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException)

Example 13 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class FtcConfigurationActivity method onCreate.

// ------------------------------------------------------------------------------------------------
// Life cycle
// ------------------------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RobotLog.vv(TAG, "onCreate()");
    setContentView(R.layout.activity_ftc_configuration);
    try {
        EditParameters parameters = EditParameters.fromIntent(this, getIntent());
        deserialize(parameters);
        Button scanButton = (Button) findViewById(R.id.scanButton);
        scanButton.setVisibility(View.VISIBLE);
        Button doneButton = (Button) findViewById(R.id.doneButton);
        doneButton.setText(R.string.buttonNameSave);
        startExecutorService();
    } catch (RobotCoreException e) {
        RobotLog.ee(TAG, "exception thrown during FtcConfigurationActivity.onCreate()");
        finishCancel();
    }
}
Also used : Button(android.widget.Button) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException)

Example 14 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class HardwareDeviceManager method createUsbLegacyModule.

/* (non-Javadoc)
     * @see com.qualcomm.hardware.DeviceManager#createUsbLegacyModule(com.qualcomm.robotcore.util.SerialNumber)
     */
@Override
public LegacyModule createUsbLegacyModule(final SerialNumber serialNumber, String name) throws RobotCoreException, InterruptedException {
    HardwareFactory.noteSerialNumberType(context, serialNumber, context.getString(R.string.moduleDisplayNameLegacyModule));
    RobotLog.v("Creating %s", HardwareFactory.getDeviceDisplayName(context, serialNumber));
    ModernRoboticsUsbDevice.OpenRobotUsbDevice openRobotUsbDevice = new ModernRoboticsUsbDevice.OpenRobotUsbDevice() {

        @Override
        public RobotUsbDevice open() throws RobotCoreException, InterruptedException {
            RobotUsbDevice dev = null;
            try {
                dev = ModernRoboticsUsbUtil.openUsbDevice(true, usbManager, serialNumber);
                byte[] deviceHeader = getModernRoboticsDeviceHeader(dev);
                DeviceType type = getModernRoboticsDeviceType(dev, deviceHeader);
                if (type != DeviceType.MODERN_ROBOTICS_USB_LEGACY_MODULE) {
                    closeAndThrowOnFailedDeviceTypeCheck(dev, serialNumber);
                }
                dev.setFirmwareVersion(getModernRoboticsFirmwareVersion(deviceHeader));
            } catch (RobotCoreException e) {
                if (dev != null) {
                    // avoid leakage of open FT_Devices
                    dev.close();
                }
                throw e;
            } catch (RuntimeException e) {
                if (dev != null) {
                    // avoid leakage of open FT_Devices
                    dev.close();
                }
                throw e;
            }
            return dev;
        }
    };
    ModernRoboticsUsbLegacyModule legacyModule = new ModernRoboticsUsbLegacyModule(context, serialNumber, openRobotUsbDevice, manager);
    legacyModule.armOrPretend();
    legacyModule.initializeHardware();
    return legacyModule;
}
Also used : ModernRoboticsUsbDevice(com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDevice) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) ModernRoboticsUsbLegacyModule(com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbLegacyModule) RobotUsbDevice(com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)

Example 15 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class LynxI2cDeviceSynch method internalWrite.

private void internalWrite(int ireg, byte[] data, final I2cWaitControl waitControl) {
    if (// paranoia, but safe
    data.length > 0) {
        // For register-based I2c devices: convention: first byte in a write is the initial register number
        byte[] payload = Util.concatenateByteArrays(new byte[] { (byte) ireg }, data);
        // We use the single-byte case when we can out of paranoia about the LynxI2cWriteMultipleBytesCommand
        // not being able to handle a byte count of one (that has not been verified with the firmware
        // programmers, but the corresponding read case has been)
        final LynxCommand<?> writeTx = payload.length == 1 ? new LynxI2cWriteSingleByteCommand(this.getModule(), this.bus, this.i2cAddr, payload[0]) : new LynxI2cWriteMultipleBytesCommand(this.getModule(), this.bus, this.i2cAddr, payload);
        try {
            acquireI2cLockWhile(new Supplier<Object>() {

                @Override
                public Object get() throws InterruptedException, RobotCoreException, LynxNackException {
                    sendI2cWriteTx(writeTx);
                    internalWaitForWriteCompletions(waitControl);
                    return null;
                }
            });
        } catch (InterruptedException | LynxNackException | RobotCoreException | RuntimeException e) {
            handleException(e);
        }
    }
}
Also used : LynxI2cWriteSingleByteCommand(com.qualcomm.hardware.lynx.commands.core.LynxI2cWriteSingleByteCommand) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) LynxI2cWriteMultipleBytesCommand(com.qualcomm.hardware.lynx.commands.core.LynxI2cWriteMultipleBytesCommand)

Aggregations

RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)32 RobotUsbDevice (com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)10 ModernRoboticsUsbDevice (com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDevice)5 IOException (java.io.IOException)5 CallbackResult (org.firstinspires.ftc.robotcore.internal.network.CallbackResult)4 DuplicateNameException (com.qualcomm.robotcore.exception.DuplicateNameException)3 ControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 RobotUsbException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException)3 RobotConfigFile (com.qualcomm.ftccommon.configuration.RobotConfigFile)2 LegacyModuleControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.LegacyModuleControllerConfiguration)2 MotorControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.MotorControllerConfiguration)2 ReadXMLFileHandler (com.qualcomm.robotcore.hardware.configuration.ReadXMLFileHandler)2 ServoControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ServoControllerConfiguration)2 Command (com.qualcomm.robotcore.robocol.Command)2 SerialNumber (com.qualcomm.robotcore.util.SerialNumber)2 ArrayList (java.util.ArrayList)2 CancellationException (java.util.concurrent.CancellationException)2 AlertDialog (android.app.AlertDialog)1