-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataset.cpp
More file actions
93 lines (86 loc) · 1.95 KB
/
dataset.cpp
File metadata and controls
93 lines (86 loc) · 1.95 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
#include "dataset.h"
#include "logging.h"
#include <cstdint>
#include <cstring>
Dataset::Dataset() : formatType_(Format::FormatType::UNKNOWN), intValue_(0), floatValue_(0.0f) {}
void Dataset::readData(Format::FormatType type, const uint8_t* payload) {
formatType_ = type;
switch (type) {
case Format::FormatType::DATA8: {
DEBUG("Parsing DATA8");
// 8-bit signed integer
intValue_ = static_cast<int8_t>(payload[0]);
break;
}
case Format::FormatType::DATA16: {
DEBUG("Parsing DATA16");
// 16-bit signed integer little endian
int16_t value16;
std::memcpy(&value16, payload, 2);
intValue_ = value16;
break;
}
case Format::FormatType::DATA32: {
DEBUG("Parsing DATA32");
// 32-bit signed integer little endian
int32_t value32;
std::memcpy(&value32, payload, 4);
intValue_ = value32;
break;
}
case Format::FormatType::DATAFLOAT: {
DEBUG("Parsing DATAFLOAT");
// 32-bot little endian IEEE 754 floating point
float valueFloat;
std::memcpy(&valueFloat, payload, 4);
floatValue_ = valueFloat;
break;
}
default:
WARN("Unsupported data format");
break;
}
}
int Dataset::getDataAsInt() {
switch (formatType_) {
case Format::FormatType::DATA8: {
return intValue_;
}
case Format::FormatType::DATA16: {
return intValue_;
}
case Format::FormatType::DATA32: {
return intValue_;
}
case Format::FormatType::DATAFLOAT: {
return (int) floatValue_;
}
default: {
WARN("Unsupported data format");
return 0;
}
}
}
float Dataset::getDataAsFloat() {
switch (formatType_) {
case Format::FormatType::DATA8: {
return (float) intValue_;
}
case Format::FormatType::DATA16: {
return (float) intValue_;
}
case Format::FormatType::DATA32: {
return (float) intValue_;
}
case Format::FormatType::DATAFLOAT: {
return floatValue_;
}
default: {
WARN("Unsupported data format");
return 0.0f;
}
}
}
Format::FormatType Dataset::getType() {
return formatType_;
}