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 DataType.
23 */
24 public enum ArduinoSignalDataType {
25 /**
26 * littleEndianSigned.
27 */
28 LESIGNED("1-"),
29
30 /**
31 * littleEndianUnsSigned.
32 */
33 LEUNSIGNED("1+"),
34
35 /**
36 * bigEndianSigned.
37 */
38 BESIGNED("0-"),
39
40 /**
41 * bigEndianUnsigned.
42 */
43 BEUNSIGNED("0+");
44
45 /**
46 * The definition.
47 */
48 private final String theDef;
49
50 /**
51 * littleEndian.
52 */
53 private final boolean littleEndian;
54
55 /**
56 * Signed.
57 */
58 private final boolean isSigned;
59
60 /**
61 * Constructor.
62 * @param pDef the definition
63 */
64 ArduinoSignalDataType(final String pDef) {
65 theDef = pDef;
66 littleEndian = pDef.charAt(0) == '1';
67 isSigned = pDef.charAt(1) == '-';
68 }
69
70 /**
71 * Obtain the definition.
72 * @return the definition
73 */
74 public String getDefinition() {
75 return theDef;
76 }
77
78 /**
79 * Is littleEndian?
80 * @return true/false
81 */
82 public boolean isLittleEndian() {
83 return littleEndian;
84 }
85
86 /**
87 * Is Signed?
88 * @return true/false
89 */
90 public boolean isSigned() {
91 return isSigned;
92 }
93
94 /**
95 * Parse dataType.
96 * @param pDataType the dataType representation
97 * @return the dataType
98 * @throws ArduinoParserException on error
99 */
100 static ArduinoSignalDataType parseDataType(final String pDataType) throws ArduinoParserException {
101 /* Loop through the values */
102 for (ArduinoSignalDataType myType : values()) {
103 if (pDataType.equals(myType.theDef)) {
104 return myType;
105 }
106 }
107 throw new ArduinoParserException("Invalid DataType", pDataType);
108 }
109
110 @Override
111 public String toString() {
112 return theDef;
113 }
114 }