From c45388b602f4599c50c783d33c046b07477aec38 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Tue, 6 Jan 2015 11:17:36 +0000 Subject: [PATCH] i2c.hpp: Change I2c class API to ressemble C API I2c class now uses more C like constructs for added efficiency when using C++ and no longer returns a binary string when doing read() calls. This change also removes the use of std::string in the SWIG API for read/write which never worked very well. This also renames single write/read calls to {write,read}Byte as whilst overloading works in C++ well it's a little confusing and only works because {write,read}() calls have 2 arguments which will not be the case in the SWIG API where those calls take a single argument (bytearray or node::Buffer), especially in js where functions have no explicit args this does not work well. Signed-off-by: Brendan Le Foll --- api/mraa/i2c.hpp | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/api/mraa/i2c.hpp b/api/mraa/i2c.hpp index 7f3b99a..9069816 100644 --- a/api/mraa/i2c.hpp +++ b/api/mraa/i2c.hpp @@ -97,35 +97,19 @@ class I2c { * * @return char read from the bus */ - uint8_t read() { + uint8_t readByte() { return (uint8_t) mraa_i2c_read_byte(m_i2c); } /** - * Read mutliple bytes from the bus - * - * @param data Buffer to write into - * @param length Size of read - * @param command The i2c command - * @return length of the read or 0 if failed - */ - uint8_t read(char *data, size_t length) { - return mraa_i2c_read(m_i2c, (uint8_t*) data, (int) length); - } - - /** - * Read length bytes from the bus, and return as a std::string note - * that this is not a null terminated string + * Read length bytes from the bus into *data pointer * + * @param data Data to read into * @param length Size of read in bytes to make * @return pointer to std::string */ - std::string read(int length) { - uint8_t* data = (uint8_t*) malloc(sizeof(uint8_t) * length); - mraa_i2c_read(m_i2c, data, length); - std::string str((char*)data, length); - free(data); - return str; + int read(uint8_t *data, int length) { + return mraa_i2c_read(m_i2c, data, length); } /** @@ -154,7 +138,7 @@ class I2c { * @param data The byte to send on the bus * @return Result of operation */ - mraa_result_t write(uint8_t data) { + mraa_result_t writeByte(uint8_t data) { return mraa_i2c_write_byte(m_i2c, data); } @@ -166,8 +150,8 @@ class I2c { * @param length Size of buffer to send * @return Result of operation */ - mraa_result_t write(char* data, size_t length) { - return mraa_i2c_write(m_i2c, (const unsigned char *)data, (int) length); + mraa_result_t write(const uint8_t* data, int length) { + return mraa_i2c_write(m_i2c, data, length); } /**