public String toCode() throws SocketNullException {
    TranslatorBlock tb = this.getRequiredTranslatorBlockAtSocket(0);
    if (!(tb instanceof VariableNumberBlock) && !(tb instanceof GetGlobalVarNumberBlock)) {
      throw new BlockException(blockId, "var must be var");
    }

    String ret = "\t" + tb.toCode();
    tb = this.getRequiredTranslatorBlockAtSocket(1);
    ret = ret + " *= " + tb.toCode() + " ;\n";
    return ret;
  }
示例#2
0
 public String translate(Long blockId) throws SocketNullException, SubroutineNotDeclaredException {
   TranslatorBlockFactory translatorBlockFactory = new TranslatorBlockFactory();
   Block block = workspace.getEnv().getBlock(blockId);
   TranslatorBlock rootTranslatorBlock =
       translatorBlockFactory.buildTranslatorBlock(
           this, blockId, block.getGenusName(), "", "", block.getBlockLabel());
   return rootTranslatorBlock.toCode();
 }
 public String toCode() throws SocketNullException {
   translator.addSemaphore(label);
   TranslatorBlock tb = getRequiredTranslatorBlockAtSocket(0);
   String ret = "";
   if (tb != null) {
     ret = ret + tb.toCode();
   }
   return ret;
 }
  @Override
  public String toCode() throws SocketNullException {
    translator.addSetupCommand("Serial1.begin(38400);");
    int i = 0;
    TranslatorBlock tb = getTranslatorBlockAtSocket(0);
    // tb=this.nextTranslatorBlock();
    String ret = "";
    while (tb != null) {
      ret = ret + "\tSerial1.print(" + tb.toCode() + ");\n";
      i = i + 1;
      tb = getTranslatorBlockAtSocket(i);
    }
    // ret = ret.replace("\n", "\n\t");
    ret = ret + "\tSerial1.print(\"\\t\");\n";

    return ret;
  }
示例#5
0
  @Override
  public String toCode() throws SocketNullException, SubroutineNotDeclaredException {
    translator.addHeaderFile("Makeblock.h");
    translator.addHeaderFile("SoftwareSerial.h");
    translator.addHeaderFile("Wire.h");
    TranslatorBlock block = this.getRequiredTranslatorBlockAtSocket(0);
    String servo = "servoDriver" + block.toCode();

    block = this.getRequiredTranslatorBlockAtSocket(1);
    String device = block.toCode();
    if (block instanceof NumberBlock) {
      int deviceId = Integer.parseInt(block.toCode());
      if (deviceId > 2 || deviceId < 1) {
        throw new BlockException(this.blockId, "the Device Id of Servo must be in Range(1,2)");
      }
      deviceId = deviceId > 2 ? 2 : (deviceId < 1 ? 1 : deviceId);
      device = "" + deviceId;
    } else {
      device = "1";
    }
    String ret = "MeServo " + servo + "(PORT_" + block.toCode() + "," + device + ");";
    translator.addDefinitionCommand(ret);
    String output = "";
    block = this.getRequiredTranslatorBlockAtSocket(2);
    if (block instanceof NumberBlock) {
      int angle = Integer.parseInt(block.toCode());
      if (angle > 180 || angle < 0) {
        throw new BlockException(this.blockId, "the angle of Servo must be in Range(0,180)");
      }
      angle = angle > 180 ? 180 : (angle < 0 ? 0 : angle);
      output += servo + ".write(" + angle + ");\n";
    } else {
      output += servo + ".write(" + block.toCode() + ");\n";
    }
    return output;
  }