@Override
  protected void onStop() {
    SensorHandler.unregisterListener(this);

    ServiceProvider.getService(CatroidService.BLUETOOTH_DEVICE_SERVICE).pause();

    FaceDetectionHandler.stopFaceDetection();
    super.onStop();
  }
  public void setFormula(Formula formula) {
    formulaToCompute = formula;

    if (formula.containsElement(ElementType.SENSOR)) {
      SensorHandler.startSensorListener(context);
      SensorHandler.registerListener(this);
    }
    int resources = formula.getRequiredResources();
    if ((resources & Brick.FACE_DETECTION) > 0) {
      FaceDetectionHandler.startFaceDetection(getContext());
    }

    if ((resources & Brick.BLUETOOTH_LEGO_NXT) > 0) {
      BluetoothDeviceService btService =
          ServiceProvider.getService(CatroidService.BLUETOOTH_DEVICE_SERVICE);
      btService.connectDevice(BluetoothDevice.LEGO_NXT, this.getContext());
    }
  }
Example #3
0
  private Object interpretFunction(Functions function, Sprite sprite) {
    Object left = null;
    Object right = null;

    Double doubleValueOfLeftChild = null;
    Double doubleValueOfRightChild = null;

    if (leftChild != null) {
      left = leftChild.interpretRecursive(sprite);
      if (left instanceof String) {
        try {
          doubleValueOfLeftChild = Double.valueOf((String) left);
        } catch (NumberFormatException numberFormatException) {
          Log.d(getClass().getSimpleName(), "Couldn't parse String", numberFormatException);
        }
      } else {
        doubleValueOfLeftChild = (Double) left;
      }
    }

    if (rightChild != null) {
      right = rightChild.interpretRecursive(sprite);
      if (right instanceof String) {
        try {
          doubleValueOfRightChild = Double.valueOf((String) right);
        } catch (NumberFormatException numberFormatException) {
          Log.d(getClass().getSimpleName(), "Couldn't parse String", numberFormatException);
        }
      } else {
        doubleValueOfRightChild = (Double) right;
      }
    }

    switch (function) {
      case SIN:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.sin(Math.toRadians(doubleValueOfLeftChild));
      case COS:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.cos(Math.toRadians(doubleValueOfLeftChild));
      case TAN:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.tan(Math.toRadians(doubleValueOfLeftChild));
      case LN:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.log(doubleValueOfLeftChild);
      case LOG:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.log10(doubleValueOfLeftChild);
      case SQRT:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.sqrt(doubleValueOfLeftChild);
      case RAND:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : interpretFunctionRand(doubleValueOfLeftChild, doubleValueOfRightChild);
      case ABS:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.abs(doubleValueOfLeftChild);
      case ROUND:
        return doubleValueOfLeftChild == null
            ? 0d
            : (double) java.lang.Math.round(doubleValueOfLeftChild);
      case PI:
        return java.lang.Math.PI;
      case MOD:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : interpretFunctionMod(doubleValueOfLeftChild, doubleValueOfRightChild);
      case ARCSIN:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.toDegrees(Math.asin(doubleValueOfLeftChild));
      case ARCCOS:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.toDegrees(Math.acos(doubleValueOfLeftChild));
      case ARCTAN:
        return doubleValueOfLeftChild == null
            ? 0d
            : java.lang.Math.toDegrees(Math.atan(doubleValueOfLeftChild));
      case EXP:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.exp(doubleValueOfLeftChild);
      case POWER:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : java.lang.Math.pow(doubleValueOfLeftChild, doubleValueOfRightChild);
      case FLOOR:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.floor(doubleValueOfLeftChild);
      case CEIL:
        return doubleValueOfLeftChild == null ? 0d : java.lang.Math.ceil(doubleValueOfLeftChild);
      case MAX:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : java.lang.Math.max(doubleValueOfLeftChild, doubleValueOfRightChild);
      case MIN:
        return (doubleValueOfLeftChild == null || doubleValueOfRightChild == null)
            ? 0d
            : java.lang.Math.min(doubleValueOfLeftChild, doubleValueOfRightChild);
      case TRUE:
        return 1d;
      case FALSE:
        return 0d;
      case LETTER:
        return interpretFunctionLetter(right, left);
      case LENGTH:
        return interpretFunctionLength(left, sprite);
      case JOIN:
        return interpretFunctionJoin(sprite);
      case ARDUINODIGITAL:
        Arduino arduinoDigital =
            ServiceProvider.getService(CatroidService.BLUETOOTH_DEVICE_SERVICE)
                .getDevice(BluetoothDevice.ARDUINO);
        if (arduinoDigital != null && left != null) {
          if (doubleValueOfLeftChild < 0 || doubleValueOfLeftChild > 13) {
            return 0d;
          }
          return arduinoDigital.getDigitalArduinoPin(doubleValueOfLeftChild.intValue());
        }
        break;
      case ARDUINOANALOG:
        Arduino arduinoAnalog =
            ServiceProvider.getService(CatroidService.BLUETOOTH_DEVICE_SERVICE)
                .getDevice(BluetoothDevice.ARDUINO);
        if (arduinoAnalog != null && left != null) {
          if (doubleValueOfLeftChild < 0 || doubleValueOfLeftChild > 5) {
            return 0d;
          }
          return arduinoAnalog.getAnalogArduinoPin(doubleValueOfLeftChild.intValue());
        }
        break;
      case RASPIDIGITAL:
        RPiSocketConnection connection = RaspberryPiService.getInstance().connection;
        int pin = doubleValueOfLeftChild.intValue();
        try {
          return connection.getPin(pin) ? 1d : 0d;
        } catch (Exception e) {
          Log.e(getClass().getSimpleName(), "RPi: exception during getPin: " + e);
        }
        break;
      case MULTI_FINGER_TOUCHED:
        return TouchUtil.isFingerTouching(doubleValueOfLeftChild.intValue()) ? 1d : 0d;
      case MULTI_FINGER_X:
        return Double.valueOf(TouchUtil.getX(doubleValueOfLeftChild.intValue()));
      case MULTI_FINGER_Y:
        return Double.valueOf(TouchUtil.getY(doubleValueOfLeftChild.intValue()));
      case LIST_ITEM:
        return interpretFunctionListItem(left, sprite);
      case CONTAINS:
        return interpretFunctionContains(right, sprite);
      case NUMBER_OF_ITEMS:
        return interpretFunctionNumberOfItems(left, sprite);
    }
    return 0d;
  }