ArduinoSignalDefinition.java
/*******************************************************************************
* jArduino: Arduino C++ Code Generation From Java
* Copyright 2020 Tony Washer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package net.sourceforge.jarduino.message;
import net.sourceforge.jarduino.message.ArduinoParser.ArduinoParserException;
/**
* Arduino Signal Definition.
*/
public class ArduinoSignalDefinition {
/**
* The dataType.
*/
private final ArduinoSignalDataType theDataType;
/**
* The start bit.
*/
private final int theStartBit;
/**
* The bit length.
*/
private final int theBitLength;
/**
* Constructor.
* @param pDataType the dataType
* @param pStartBit the startBit
* @param pBitLength the bit length
*/
ArduinoSignalDefinition(final ArduinoSignalDataType pDataType,
final int pStartBit,
final int pBitLength) {
/* Store values */
theDataType = pDataType;
theStartBit = pStartBit;
theBitLength = pBitLength;
}
/**
* Obtain the startBit.
* @return the startBit
*/
public int getStartBit() {
return theStartBit;
}
/**
* Obtain the bitLength.
* @return the bitLength
*/
public int getBitLength() {
return theBitLength;
}
/**
* Obtain the dataType.
* @return the dataType
*/
public ArduinoSignalDataType getDataType() {
return theDataType;
}
/**
* Parse data definition.
* @param pDataDef the data definition
* @return the definition
* @throws ArduinoParserException on error
*/
static ArduinoSignalDefinition parseDefinition(final String pDataDef) throws ArduinoParserException {
/* Split out dataTypes */
int myIndex = pDataDef.indexOf(ArduinoChar.AT);
if (myIndex == -1) {
throw new ArduinoParserException("Missing " + ArduinoChar.AT + " separator", pDataDef);
}
final String myLengths = pDataDef.substring(0, myIndex);
final String myType = pDataDef.substring(myIndex + 1);
final ArduinoSignalDataType myDataType = ArduinoSignalDataType.parseDataType(myType);
/* Split out lengths */
myIndex = myLengths.indexOf(ArduinoChar.PIPE);
if (myIndex == -1) {
throw new ArduinoParserException("Missing " + ArduinoChar.PIPE + " separator", pDataDef);
}
/* Parse values */
final String myStart = myLengths.substring(0, myIndex);
final String myLength = myLengths.substring(myIndex + 1);
final int myStartBit = ArduinoParser.parseNumber(myStart).intValue();
final int myBitLength = ArduinoParser.parseNumber(myLength).intValue();
if (myBitLength > Long.SIZE) {
throw new ArduinoParserException("Field is larger than " + Long.SIZE + " bits", pDataDef);
}
/* return the parsed Definition */
return new ArduinoSignalDefinition(myDataType, myStartBit, myBitLength);
}
@Override
public String toString() {
return "" + theStartBit + ArduinoChar.PIPE + theBitLength + ArduinoChar.AT + theDataType;
}
}