Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cpp/ev3dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,45 @@ float sensor::float_value(unsigned index) const
return value(index) * powf(10, -decimals());
}

//-----------------------------------------------------------------------------
const std::vector<char>& sensor::bin_data() const
{
using namespace std;

if (_path.empty())
throw system_error(make_error_code(errc::function_not_supported), "no device connected");

if (_bin_data.empty()) {
static const map<string, int> lookup_table {
{"u8", 1},
{"s8", 1},
{"u16", 2},
{"s16", 2},
{"s16_be", 2},
{"s32", 4},
{"float", 4}
};

int value_size = 1;

auto s = lookup_table.find(bin_data_format());
if (s != lookup_table.end())
value_size = s->second;

_bin_data.resize(num_values() * value_size);
}

static const string fname = _path + "bin_data";
ifstream &is = ifstream_open(fname);
if (is.is_open())
{
is.read(_bin_data.data(), _bin_data.size());
return _bin_data;
}

throw system_error(make_error_code(errc::no_such_device), fname);
}

//-----------------------------------------------------------------------------

i2c_sensor::i2c_sensor(port_type port_) :
Expand Down
33 changes: 33 additions & 0 deletions cpp/ev3dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>

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

// Bin Data Format: read-only
// Returns the format of the values in `bin_data` for the current mode.
// Possible values are:
//
// - `u8`: Unsigned 8-bit integer (byte)
// - `s8`: Signed 8-bit integer (sbyte)
// - `u16`: Unsigned 16-bit integer (ushort)
// - `s16`: Signed 16-bit integer (short)
// - `s16_be`: Signed 16-bit integer, big endian
// - `s32`: Signed 32-bit integer (int)
// - `float`: IEEE 754 32-bit floating point (float)
std::string bin_data_format() const { return get_attr_string("bin_data_format"); };

// Bin Data: read-only
// Returns the unscaled raw values in the `value<N>` attributes as raw byte
// array. Use `bin_data_format`, `num_values` and the individual sensor
// documentation to determine how to interpret the data.
const std::vector<char>& bin_data() const;

// Bin Data: read-only
// Writes the unscaled raw values in the `value<N>` attributes into the
// user-provided struct/buffer. Use `bin_data_format`, `num_values` and the
// individual sensor documentation to determine how to interpret the data.
template <class T>
void bin_data(T *buf) const {
bin_data(); // fills _bin_data
std::copy_n(_bin_data.data(), _bin_data.size(), static_cast<char*>(buf));
}

//~autogen cpp_generic-get-set classes.sensor>currentClass

// Command: write-only
Expand Down Expand Up @@ -210,6 +241,8 @@ class sensor : protected device
sensor() {}

bool connect(const std::map<std::string, std::set<std::string>>&) noexcept;

mutable std::vector<char> _bin_data;
};

//-----------------------------------------------------------------------------
Expand Down