View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ******************************************************************************/
17  package net.sourceforge.jarduino.message;
18  
19  import net.sourceforge.jarduino.message.ArduinoParser.ArduinoParserException;
20  
21  /**
22   * Arduino Signal Definition.
23   */
24  public class ArduinoSignalDefinition {
25      /**
26       * The dataType.
27       */
28      private final ArduinoSignalDataType theDataType;
29  
30      /**
31       * The start bit.
32       */
33      private final int theStartBit;
34  
35      /**
36       * The bit length.
37       */
38      private final int theBitLength;
39  
40      /**
41       * Constructor.
42       * @param pDataType the dataType
43       * @param pStartBit the startBit
44       * @param pBitLength the bit length
45       */
46      ArduinoSignalDefinition(final ArduinoSignalDataType pDataType,
47                              final int pStartBit,
48                              final int pBitLength) {
49          /* Store values */
50          theDataType = pDataType;
51          theStartBit = pStartBit;
52          theBitLength = pBitLength;
53      }
54  
55      /**
56       * Obtain the startBit.
57       * @return the startBit
58       */
59      public int getStartBit() {
60          return theStartBit;
61      }
62  
63      /**
64       * Obtain the bitLength.
65       * @return the bitLength
66       */
67      public int getBitLength() {
68          return theBitLength;
69      }
70  
71      /**
72       * Obtain the dataType.
73       * @return the dataType
74       */
75      public ArduinoSignalDataType getDataType() {
76          return theDataType;
77      }
78  
79      /**
80       * Parse data definition.
81       * @param pDataDef the data definition
82       * @return the definition
83       * @throws ArduinoParserException on error
84       */
85      static ArduinoSignalDefinition parseDefinition(final String pDataDef) throws ArduinoParserException {
86          /* Split out dataTypes */
87          int myIndex = pDataDef.indexOf(ArduinoChar.AT);
88          if (myIndex == -1) {
89              throw new ArduinoParserException("Missing " + ArduinoChar.AT + " separator", pDataDef);
90          }
91          final String myLengths = pDataDef.substring(0, myIndex);
92          final String myType = pDataDef.substring(myIndex + 1);
93          final ArduinoSignalDataType myDataType = ArduinoSignalDataType.parseDataType(myType);
94  
95          /* Split out lengths */
96          myIndex = myLengths.indexOf(ArduinoChar.PIPE);
97          if (myIndex == -1) {
98              throw new ArduinoParserException("Missing " + ArduinoChar.PIPE + " separator", pDataDef);
99          }
100 
101         /* Parse values */
102         final String myStart = myLengths.substring(0, myIndex);
103         final String myLength = myLengths.substring(myIndex + 1);
104         final int myStartBit = ArduinoParser.parseNumber(myStart).intValue();
105         final int myBitLength = ArduinoParser.parseNumber(myLength).intValue();
106         if (myBitLength > Long.SIZE) {
107             throw new ArduinoParserException("Field is larger than " + Long.SIZE + " bits", pDataDef);
108         }
109 
110         /* return the parsed Definition */
111         return new ArduinoSignalDefinition(myDataType, myStartBit, myBitLength);
112     }
113 
114     @Override
115     public String toString() {
116         return "" + theStartBit + ArduinoChar.PIPE + theBitLength + ArduinoChar.AT + theDataType;
117     }
118 }