Package com.ardublock.translator.block

Source Code of com.ardublock.translator.block.ServoMg996rBlock

package com.ardublock.translator.block;

import com.ardublock.translator.Translator;
import com.ardublock.translator.block.exception.BlockException;
import com.ardublock.translator.block.exception.SocketNullException;
import com.ardublock.translator.block.exception.SubroutineNotDeclaredException;

public class ServoMg996rBlock extends TranslatorBlock {

  public ServoMg996rBlock(Long blockId, Translator translator, String codePrefix, String codeSuffix, String label)
  {
    super(blockId, translator, codePrefix, codeSuffix, label);
  }

  @Override
  public String toCode() throws SocketNullException, SubroutineNotDeclaredException
  {
    TranslatorBlock tb = this.getRequiredTranslatorBlockAtSocket(0);

    String servoSpecs = ",620,2200";

    if (!( tb instanceof NumberBlock ) )
    {
      throw new BlockException(this.blockId, "the Pin# of Servo must be a number");
    }

    String pinNumber = tb.toCode();
    String servoName = "servo_pin_" + pinNumber;

    tb = this.getRequiredTranslatorBlockAtSocket(1);

    String ret = servoName + ".write( " + tb.toCode() + " );\n";
    translator.addHeaderFile("Servo.h");
    translator.addDefinitionCommand("Servo " + servoName + ";");
    translator.addSetupCommand(servoName + ".attach(" + pinNumber + servoSpecs + ");");
    return ret;
  }

}
TOP

Related Classes of com.ardublock.translator.block.ServoMg996rBlock

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.