From 5e548c1a08cc559e30507b6dbe5c408a9bf96f51 Mon Sep 17 00:00:00 2001 From: Alex Williams Date: Tue, 16 Oct 2018 08:23:35 -0700 Subject: mpm: i2c: Add vector-based transfer function This could lead to a less-restricted implementation for use in Python. --- mpm/include/mpm/i2c/i2c_iface.hpp | 12 ++++++++++++ mpm/include/mpm/i2c/i2c_python.hpp | 11 +++++++++++ mpm/lib/i2c/i2cdev_iface.cpp | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+) (limited to 'mpm') diff --git a/mpm/include/mpm/i2c/i2c_iface.hpp b/mpm/include/mpm/i2c/i2c_iface.hpp index f19711242..aca5994b1 100644 --- a/mpm/include/mpm/i2c/i2c_iface.hpp +++ b/mpm/include/mpm/i2c/i2c_iface.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace mpm { namespace i2c { @@ -39,6 +40,17 @@ namespace mpm { namespace i2c { * \param rx_len Number of bytes to read */ virtual int transfer(uint8_t *tx, size_t tx_len, uint8_t *rx, size_t rx_len) = 0; + + /*! + * \param tx Buffer of data to send + * \param rx Buffer to hold read data + * + * All data in tx will be transmitted. + * The amount of data read will be determined by the number of elements + * in the rx vector. Those elements will be overwritten with the data. + * Use the resize() function for a new rx vector. + */ + virtual int transfer(std::vector *tx, std::vector *rx) = 0; }; }}; /* namespace mpm::i2c */ diff --git a/mpm/include/mpm/i2c/i2c_python.hpp b/mpm/include/mpm/i2c/i2c_python.hpp index c4211e238..d50b1d4d6 100644 --- a/mpm/include/mpm/i2c/i2c_python.hpp +++ b/mpm/include/mpm/i2c/i2c_python.hpp @@ -13,6 +13,17 @@ void export_i2c() { LIBMPM_BOOST_PREAMBLE("i2c") bp::def("make_i2cdev_regs_iface", &mpm::i2c::make_i2cdev_regs_iface); +/* + bp::def("make_i2cdev", &mpm::i2c::i2c_iface::make_i2cdev); + int (mpm::i2c::i2c_iface::*transfer_vec)(std::vector*, + std::vector*) = + &mpm::i2c::i2c_iface::transfer; + + bp::class_ >("i2c_iface", bp::no_init) + .def("transfer", transfer_vec) + ; +*/ } diff --git a/mpm/lib/i2c/i2cdev_iface.cpp b/mpm/lib/i2c/i2cdev_iface.cpp index 43aeea5e2..d47d0f788 100644 --- a/mpm/lib/i2c/i2cdev_iface.cpp +++ b/mpm/lib/i2c/i2cdev_iface.cpp @@ -72,6 +72,24 @@ public: return ret; } + int transfer(std::vector *tx, std::vector *rx) + { + uint8_t *tx_data = NULL, *rx_data = NULL; + size_t tx_len = 0, rx_len = 0; + + if (tx) { + tx_data = tx->data(); + tx_len = tx->size(); + } + + if (rx) { + rx_data = rx->data(); + rx_len = rx->size(); + } + int ret = transfer(tx_data, tx_len, rx_data, rx_len); + return ret; + } + private: int _fd; const uint16_t _addr; -- cgit v1.2.3