Example #1
0
 protected void refreshScale(Scale scale) {
   int min = scale.getMinimum();
   int sel = (int) Math.round((scale.getMaximum() - min) * selectionRatio + min);
   scale.setSelection(sel);
   scale.setToolTipText(getSelectionText());
 }
Example #2
0
  public void createPartControl(final Composite parent) {

    parent.setLayout(new GridLayout(3, false));

    final Composite comp = new Composite(parent, SWT.BORDER);
    comp.setLayout(new FillLayout());
    comp.setLayoutData(new GridData(GridData.FILL_BOTH));

    final Scale scale = new Scale(parent, SWT.BORDER | SWT.VERTICAL);
    scale.setLayoutData(new GridData(GridData.FILL_VERTICAL));
    scale.setMaximum(100);
    scale.setMinimum(0);
    scale.setPageIncrement(1);
    scale.setSelection(100);
    scale.setToolTipText("Throttle Command.");
    scale.addListener(
        SWT.Selection,
        new Listener() {

          @Override
          public void handleEvent(Event event) {
            int value = 100 - scale.getSelection();
            // Inverted! from 100 to 0.
            if (throttleText != null) throttleText.setText(String.valueOf(value) + " %");

            commandThread.getData().setM2(value);
            commandThread.getData().setM3(value);
            commandThread.getData().setM1(value);
            commandThread.getData().setM0(value);
          }
        });

    canvas = new Canvas(comp, SWT.DOUBLE_BUFFERED);

    final Composite data = new Composite(parent, SWT.NONE);
    data.setLayout(new GridLayout(1, true));
    data.setLayoutData(new GridData(GridData.FILL_VERTICAL));

    Group groupThrottle = new Group(data, SWT.NONE);
    groupThrottle.setText("Throttle Command");
    groupThrottle.setLayout(new GridLayout(2, true));
    groupThrottle.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Label label = new Label(groupThrottle, SWT.NONE);
    label.setText("Throttle:");

    throttleText = new Text(groupThrottle, SWT.BORDER);
    throttleText.setEditable(false);
    throttleText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    throttleText.setText("0 %");

    Group groupRoll = new Group(data, SWT.NONE);
    groupRoll.setText("Roll Command");
    groupRoll.setLayout(new GridLayout(2, true));
    groupRoll.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    Group groupPitch = new Group(data, SWT.NONE);
    groupPitch.setText("Pitch Command");
    groupPitch.setLayout(new GridLayout(2, true));
    groupPitch.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    label = new Label(groupRoll, SWT.NONE);
    label.setText("Lock Roll:");

    lockRoll = new Button(groupRoll, SWT.CHECK);

    label = new Label(groupRoll, SWT.NONE);
    label.setText("Roll (°):");

    rollText = new Text(groupRoll, SWT.BORDER);
    rollText.setEditable(false);
    rollText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    label = new Label(groupPitch, SWT.NONE);
    label.setText("Lock Pitch:");

    lockPitch = new Button(groupPitch, SWT.CHECK);

    label = new Label(groupPitch, SWT.NONE);
    label.setText("Pitch (°):");

    pitchText = new Text(groupPitch, SWT.BORDER);
    pitchText.setEditable(false);
    pitchText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    label = new Label(groupRoll, SWT.NONE);
    label.setText("Max Roll (°):");

    rollMax = new Text(groupRoll, SWT.BORDER);
    rollMax.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    rollMax.addFocusListener(
        new FocusListener() {

          @Override
          public void focusLost(FocusEvent e) {
            String value = ((Text) e.widget).getText();
            IPreferenceStore store = Activator.getDefault().getPreferenceStore();

            int intVal = 0;
            try {
              intVal = Integer.valueOf(value);
            } catch (NumberFormatException e1) {
              ((Text) e.widget).setText(store.getString(PreferenceConstants.P_CMD_ROLL));
            }

            if (intVal > 90 || intVal < 0)
              ((Text) e.widget).setText(store.getString(PreferenceConstants.P_CMD_ROLL));
          }

          @Override
          public void focusGained(FocusEvent arg0) {}
        });

    label = new Label(groupPitch, SWT.NONE);
    label.setText("Max Pitch (°):");

    pitchMax = new Text(groupPitch, SWT.BORDER);
    pitchMax.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    pitchMax.addFocusListener(
        new FocusListener() {

          @Override
          public void focusLost(FocusEvent e) {
            String value = ((Text) e.widget).getText();
            IPreferenceStore store = Activator.getDefault().getPreferenceStore();

            int intVal = 0;
            try {
              intVal = Integer.valueOf(value);
            } catch (NumberFormatException e1) {
              ((Text) e.widget).setText(store.getString(PreferenceConstants.P_CMD_PITCH));
            }

            if (intVal > 90 || intVal < 0)
              ((Text) e.widget).setText(store.getString(PreferenceConstants.P_CMD_PITCH));
          }

          @Override
          public void focusGained(FocusEvent arg0) {}
        });

    final Composite buttons = new Composite(data, SWT.NONE);
    buttons.setLayout(new GridLayout(2, true));
    buttons.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    final Button recordBtn = new Button(buttons, SWT.TOGGLE);
    recordBtn.addListener(
        SWT.Selection,
        new Listener() {

          @Override
          public void handleEvent(Event event) {
            if (recordBtn.getSelection()) TelemetryDataSingleton.getInstance().setLogEnabled(true);
            else TelemetryDataSingleton.getInstance().setLogEnabled(false);

            recordBtn.setSelection(TelemetryDataSingleton.getInstance().getLogEnabled());

            logger.logInfo(
                "Data recording " + (recordBtn.getSelection() ? "enabled." : "disabled."),
                this.getClass());
          }
        });
    recordBtn.setImage(
        PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ELCL_STOP));
    recordBtn.setLayoutData(new GridData(GridData.FILL_BOTH));
    recordBtn.setToolTipText("Record telemetry to file.");

    final Button enableBtn = new Button(buttons, SWT.TOGGLE);
    enableBtn.addListener(
        SWT.Selection,
        new Listener() {

          @Override
          public void handleEvent(Event event) {
            if (enableBtn.getSelection()) commandThread.setPause(true);
            else commandThread.setPause(false);

            recordBtn.setSelection(TelemetryDataSingleton.getInstance().getLogEnabled());

            if (enableBtn.getSelection())
              logger.logInfo("Command data sent enabled.", this.getClass());
            else logger.logWarning("Command data sent disabled.", this.getClass());
          }
        });
    enableBtn.setImage(
        PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_ELCL_COLLAPSEALL));
    enableBtn.setLayoutData(new GridData(GridData.FILL_BOTH));
    enableBtn.setToolTipText("Enable command data sent.");

    canvas.setToolTipText("Pitch [left] & Roll [right] Commands.");
    canvas.addPaintListener(
        new PaintListener() {

          @Override
          public void paintControl(PaintEvent e) {
            // Get the canvas for drawing and its width and height
            Canvas canvas = (Canvas) e.widget;

            int width = canvas.getSize().x;
            int height = canvas.getSize().y - comp.getBorderWidth();

            drawAttitudeControls(e.gc, width, height);
          }
        });

    canvas.addMouseListener(
        new MouseListener() {

          @Override
          public void mouseUp(MouseEvent e) {
            enable = false;
            roll = 0;
            pitch = 0;

            update();
          }

          @Override
          public void mouseDown(MouseEvent e) {
            enable = true;
            prevX = e.x;
            prevY = e.y;
          }

          @Override
          public void mouseDoubleClick(MouseEvent e) {
            if (enableDoubleClick) {
              enableDoubleClick = false;
              roll = 0;
              pitch = 0;

              update();
            } else enableDoubleClick = true;
          }
        });

    canvas.addMouseMoveListener(
        new MouseMoveListener() {

          private float pitchAngleStep;
          private float rollAngleStep;

          @Override
          public void mouseMove(MouseEvent e) {

            if (enable || enableDoubleClick) {
              int maxRoll = Integer.valueOf(rollMax.getText());
              int maxPitch = Integer.valueOf(pitchMax.getText());

              // Calculate deltas and scale value
              rollAngleStep = Math.abs(e.x - prevX) * 0.5f;
              pitchAngleStep = Math.abs(e.y - prevY) * 1.5f;

              if (roll >= maxRoll) {
                roll = maxRoll;
              } else if (roll <= -maxRoll) {
                roll = -maxRoll;
              }

              if (pitch >= maxPitch) {
                pitch = maxPitch;
              } else if (pitch <= -maxPitch) {
                pitch = -maxPitch;
              }

              if (e.x < prevX && !lockRoll.getSelection()) roll -= rollAngleStep;
              else if (e.x > prevX && !lockRoll.getSelection()) roll += rollAngleStep;
              else if (e.y < prevY && !lockPitch.getSelection()) pitch -= pitchAngleStep;
              else if (e.y > prevY && !lockPitch.getSelection()) pitch += pitchAngleStep;

              prevX = e.x;
              prevY = e.y;

              update();
            }
          }
        });

    updateFromPreferences();
  }