@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;
}