Skip to content

Commit da84989

Browse files
committed
Merge pull request #84 from ddemidov/bin_data
[cpp][py] Expose sensor.bin_data attrbute
2 parents f9bdd20 + 7017d89 commit da84989

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

cpp/ev3dev.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,45 @@ float sensor::float_value(unsigned index) const
528528
return value(index) * powf(10, -decimals());
529529
}
530530

531+
//-----------------------------------------------------------------------------
532+
const std::vector<char>& sensor::bin_data() const
533+
{
534+
using namespace std;
535+
536+
if (_path.empty())
537+
throw system_error(make_error_code(errc::function_not_supported), "no device connected");
538+
539+
if (_bin_data.empty()) {
540+
static const map<string, int> lookup_table {
541+
{"u8", 1},
542+
{"s8", 1},
543+
{"u16", 2},
544+
{"s16", 2},
545+
{"s16_be", 2},
546+
{"s32", 4},
547+
{"float", 4}
548+
};
549+
550+
int value_size = 1;
551+
552+
auto s = lookup_table.find(bin_data_format());
553+
if (s != lookup_table.end())
554+
value_size = s->second;
555+
556+
_bin_data.resize(num_values() * value_size);
557+
}
558+
559+
static const string fname = _path + "bin_data";
560+
ifstream &is = ifstream_open(fname);
561+
if (is.is_open())
562+
{
563+
is.read(_bin_data.data(), _bin_data.size());
564+
return _bin_data;
565+
}
566+
567+
throw system_error(make_error_code(errc::no_such_device), fname);
568+
}
569+
531570
//-----------------------------------------------------------------------------
532571

533572
i2c_sensor::i2c_sensor(port_type port_) :

cpp/ev3dev.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <map>
3636
#include <set>
3737
#include <string>
38+
#include <vector>
39+
#include <algorithm>
3840
#include <functional>
3941

4042
//-----------------------------------------------------------------------------
@@ -151,6 +153,35 @@ class sensor : protected device
151153
// Human-readable name of the connected sensor.
152154
std::string type_name() const;
153155

156+
// Bin Data Format: read-only
157+
// Returns the format of the values in `bin_data` for the current mode.
158+
// Possible values are:
159+
//
160+
// - `u8`: Unsigned 8-bit integer (byte)
161+
// - `s8`: Signed 8-bit integer (sbyte)
162+
// - `u16`: Unsigned 16-bit integer (ushort)
163+
// - `s16`: Signed 16-bit integer (short)
164+
// - `s16_be`: Signed 16-bit integer, big endian
165+
// - `s32`: Signed 32-bit integer (int)
166+
// - `float`: IEEE 754 32-bit floating point (float)
167+
std::string bin_data_format() const { return get_attr_string("bin_data_format"); };
168+
169+
// Bin Data: read-only
170+
// Returns the unscaled raw values in the `value<N>` attributes as raw byte
171+
// array. Use `bin_data_format`, `num_values` and the individual sensor
172+
// documentation to determine how to interpret the data.
173+
const std::vector<char>& bin_data() const;
174+
175+
// Bin Data: read-only
176+
// Writes the unscaled raw values in the `value<N>` attributes into the
177+
// user-provided struct/buffer. Use `bin_data_format`, `num_values` and the
178+
// individual sensor documentation to determine how to interpret the data.
179+
template <class T>
180+
void bin_data(T *buf) const {
181+
bin_data(); // fills _bin_data
182+
std::copy_n(_bin_data.data(), _bin_data.size(), static_cast<char*>(buf));
183+
}
184+
154185
//~autogen cpp_generic-get-set classes.sensor>currentClass
155186

156187
// Command: write-only
@@ -210,6 +241,8 @@ class sensor : protected device
210241
sensor() {}
211242

212243
bool connect(const std::map<std::string, std::set<std::string>>&) noexcept;
244+
245+
mutable std::vector<char> _bin_data;
213246
};
214247

215248
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)