Repository navigation
Expand file tree
/
Copy pathsignal.cpp
More file actions
176 lines (153 loc) · 3.9 KB
/
Copy pathsignal.cpp
File metadata and controls
176 lines (153 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* signal.cpp
*
* Created on: 04.10.2013
* Author: downtimes
*
* Modified : 05.21.2020
* Author: Yunjin
*/
#include "signal.hpp"
#include <istream>
#include <sstream>
#include <limits>
#include <iterator>
#include <algorithm>
double Signal::getValue(uint64_t payload){
int64_t result = 0;
uint8_t *data = (uint8_t *)&payload;
unsigned short bit = startBit;
for (int bitpos = 0; bitpos < length; bitpos++) {
if (data[bit / 8] & (1 << (bit % 8))) {
if (order == ByteOrder::INTEL)
{
result |= (1ULL << bitpos);
}
else
{
result |= (1ULL << (length - bitpos - 1));
}
}
bit++;
}
if ((sign == Sign::SIGNED) && (result & (1ULL << (length - 1))))
{
result |= ~((1ULL << length) - 1);
}
return (double)result*factor + offset;
}
std::string& trim(std::string& str, const std::string& toTrim = " ") {
std::string::size_type pos = str.find_last_not_of(toTrim);
if (pos == std::string::npos) {
str.clear();
} else {
str.erase(pos + 1);
str.erase(0, str.find_first_not_of(toTrim));
}
return str;
}
std::vector<std::string>& split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
std::istream& operator>>(std::istream& in, Signal& sig) {
int c = in.peek();
if ('B' == c) {
in.setstate(std::ios_base::failbit);
return in;
}
std::string line;
std::getline(in, line);
if (!line.empty() && *line.rbegin() == '\r') line.erase(line.length() - 1, 1);
if (line.empty()) {
in.setstate(std::ios_base::failbit);
return in;
}
std::istringstream sstream(line);
std::string preamble;
sstream >> preamble;
//Check if we are actually reading a Signal otherwise fail the stream
if (preamble != "SG_") {
sstream.setstate(std::ios_base::failbit);
return in;
}
//Parse the Signal Name
sstream >> sig.name;
std::string multi;
sstream >> multi;
//This case happens if there is no Multiplexor present
if (multi == ":") {
sig.multiplexor = Multiplexor::NONE;
//Case with multiplexor
} else {
if (multi == "M") {
sig.multiplexor = Multiplexor::MULTIPLEXOR;
} else {
//The multiplexor looks like that 'm12' so we ignore the m and parse it as integer
std::istringstream multstream(multi);
multstream.ignore(1);
unsigned short multiNum;
multstream >> multiNum;
sig.multiplexor = Multiplexor::MULTIPLEXED;
sig.multiplexNum = multiNum;
}
//ignore the next thing which is a ':'
sstream >> multi;
}
sstream >> sig.startBit;
sstream.ignore(1);
sstream >> sig.length;
sstream.ignore(1);
int order;
sstream >> order;
if (order == 0) {
sig.order = ByteOrder::MOTOROLA;
int pos = 7 - (sig.startBit % 8) + (sig.length - 1);
if (pos < 8)
{
sig.startBit -= (sig.length - 1);
}
else
{
int cpos = 7 - (pos*8);
int bytes = (int)(pos / 8);
sig.startBit = cpos + (bytes * 8) + (int)(sig.startBit/8) * 8;
}
} else {
sig.order = ByteOrder::INTEL;
}
char sign;
sstream >> sign;
if (sign == '+') {
sig.sign = Sign::UNSIGNED;
} else {
sig.sign = Sign::SIGNED;
}
sstream.ignore(std::numeric_limits<std::streamsize>::max(), '(');
sstream >> sig.factor;
sstream.ignore(1);
sstream >> sig.offset;
sstream.ignore(1);
sstream.ignore(std::numeric_limits<std::streamsize>::max(), '[');
sstream >> sig.minimum;
sstream.ignore(1);
sstream >> sig.maximum;
sstream.ignore(1);
std::string unit;
sstream >> unit;
sig.unit = trim(unit, "\"");
std::string to;
sstream >> to;
std::vector<std::string> toStrings = split(to, ',');
std::move(toStrings.begin(), toStrings.end(), std::inserter(sig.to, sig.to.begin()));
return in;
}