|  | @@ -21,6 +21,9 @@
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  package de.mcs.tools.sps.mnemonic;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import de.mcs.tools.sps.exceptions.IllegalArgument;
 | 
	
		
			
				|  |  |  import de.mcs.tools.sps.exceptions.SyntaxError;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
	
		
			
				|  | @@ -29,14 +32,74 @@ import de.mcs.tools.sps.exceptions.SyntaxError;
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  public class WAIT extends AbstractMnemonic implements Mnemonic {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +  private enum TIME_VALUE {
 | 
	
		
			
				|  |  | +    TIME_1MS("1ms", 0x00), TIME_2MS("2ms", 0x01), TIME_5MS("5ms", 0x02), TIME_10MS("10ms", 0x03), TIME_20MS("20ms",
 | 
	
		
			
				|  |  | +        0x04), TIME_50MS("50ms", 0x05), TIME_100MS("100ms", 0x06), TIME_200MS("200ms", 0x07), TIME_500MS("500ms",
 | 
	
		
			
				|  |  | +            0x08), TIME_1S("1s", 0x09), TIME_2S("2s", 0x0a), TIME_5S("5s",
 | 
	
		
			
				|  |  | +                0x0b), TIME_10S("10s", 0x0c), TIME_20S("20s", 0x0d), TIME_30S("30s", 0x0e), TIME_60S("60s", 0x0f);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    String name;
 | 
	
		
			
				|  |  | +    int value;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    TIME_VALUE(String name, int value) {
 | 
	
		
			
				|  |  | +      this.name = name;
 | 
	
		
			
				|  |  | +      this.value = value;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  };
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  byte output;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |    public WAIT(String line) throws SyntaxError {
 | 
	
		
			
				|  |  |      super(line);
 | 
	
		
			
				|  |  | +    output = 0;
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  @Override
 | 
	
		
			
				|  |  | +  public void checkArgument() throws SyntaxError {
 | 
	
		
			
				|  |  | +    if (StringUtils.isEmpty(getArgument())) {
 | 
	
		
			
				|  |  | +      throw new SyntaxError(getLineNumber(),
 | 
	
		
			
				|  |  | +          String.format("missing argument for %s.", this.getClass().getSimpleName()));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    int value = 0;
 | 
	
		
			
				|  |  | +    String argument = getArgument();
 | 
	
		
			
				|  |  | +    if (isTimeArgument(argument)) {
 | 
	
		
			
				|  |  | +      value = getTime(argument);
 | 
	
		
			
				|  |  | +      if (value < 0) {
 | 
	
		
			
				|  |  | +        throw new IllegalArgument(getLineNumber(),
 | 
	
		
			
				|  |  | +            String.format("argument %s is not in enum for %s.", getArgument(), this.getClass().getSimpleName()));
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +    } else {
 | 
	
		
			
				|  |  | +      value = getArgumentAsNumber();
 | 
	
		
			
				|  |  | +      if ((value < 0) || (value > 15)) {
 | 
	
		
			
				|  |  | +        throw new IllegalArgument(getLineNumber(),
 | 
	
		
			
				|  |  | +            String.format("argument %s is not in range 0..15 for %s.", getArgument(), this.getClass().getSimpleName()));
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    output = (byte) (value & 0xFF);
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  private int getTime(String argument) throws IllegalArgument {
 | 
	
		
			
				|  |  | +    try {
 | 
	
		
			
				|  |  | +      TIME_VALUE timeValue = TIME_VALUE.valueOf(String.format("TIME_%S", argument.trim()));
 | 
	
		
			
				|  |  | +      if (timeValue != null) {
 | 
	
		
			
				|  |  | +        return timeValue.value;
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +    } catch (IllegalArgumentException e) {
 | 
	
		
			
				|  |  | +      throw new IllegalArgument(getLineNumber(),
 | 
	
		
			
				|  |  | +          String.format("argument %s is not in enum for %s.", getArgument(), this.getClass().getSimpleName()));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    return -1;
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  private boolean isTimeArgument(String argument) {
 | 
	
		
			
				|  |  | +    return argument.toLowerCase().indexOf("s") > 0;
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    @Override
 | 
	
		
			
				|  |  |    public int getByte() {
 | 
	
		
			
				|  |  |      // TODO modifier x (0..15)
 | 
	
		
			
				|  |  | -    return 0x20;
 | 
	
		
			
				|  |  | +    return 0x20 + output;
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  }
 |