From d847ae78475a38295e2aa037c3bdf2c689f5df5b Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Fri, 23 May 2014 14:21:53 +0100 Subject: [PATCH] i2c: use uint8_t throughout to make code clearer Signed-off-by: Brendan Le Foll --- api/i2c.h | 9 +++++---- api/i2c.hpp | 8 ++++---- examples/c++/I2c-compass.cpp | 2 +- src/i2c/i2c.c | 11 +++++------ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/api/i2c.h b/api/i2c.h index ae05b53..360f78a 100644 --- a/api/i2c.h +++ b/api/i2c.h @@ -37,6 +37,7 @@ extern "C" { #include #include #include +#include #include "maa.h" #include "gpio.h" @@ -77,7 +78,7 @@ maa_result_t maa_i2c_frequency(maa_i2c_context dev, int hz); * * @return maa_result_t the maa result. */ -maa_result_t maa_i2c_read(maa_i2c_context dev, char *data, int length); +maa_result_t maa_i2c_read(maa_i2c_context dev, uint8_t *data, int length); /** Read a single byte from the i2c context * @@ -85,7 +86,7 @@ maa_result_t maa_i2c_read(maa_i2c_context dev, char *data, int length); * * @return byte the result of the read or -1 if failed. */ -int maa_i2c_read_byte(maa_i2c_context dev); +uint8_t maa_i2c_read_byte(maa_i2c_context dev); /** Write to an i2c context * @@ -95,7 +96,7 @@ int maa_i2c_read_byte(maa_i2c_context dev); * * @return maa_result_t the maa result. */ -maa_result_t maa_i2c_write(maa_i2c_context dev, const char *data, int length); +maa_result_t maa_i2c_write(maa_i2c_context dev, const uint8_t *data, int length); /** Write a single byte to an i2c context * @@ -104,7 +105,7 @@ maa_result_t maa_i2c_write(maa_i2c_context dev, const char *data, int length); * * @return maa_result_t the maa result. */ -maa_result_t maa_i2c_write_byte(maa_i2c_context dev, const int data); +maa_result_t maa_i2c_write_byte(maa_i2c_context dev, const uint8_t data); /** Sets the i2c context address. * diff --git a/api/i2c.hpp b/api/i2c.hpp index 7da3ac0..3fb7c18 100644 --- a/api/i2c.hpp +++ b/api/i2c.hpp @@ -51,16 +51,16 @@ class I2c { maa_result_t address(int address) { return maa_i2c_address(m_i2c, address); } - int read_byte() { + uint8_t read_byte() { return maa_i2c_read_byte(m_i2c); } - maa_result_t read(char *data, int length) { + maa_result_t read(uint8_t* data, int length) { return maa_i2c_read(m_i2c, data, length); } - maa_result_t write(const char *data, int length) { + maa_result_t write(const uint8_t* data, int length) { return maa_i2c_write(m_i2c, data, length); } - maa_result_t write_byte(const int data) { + maa_result_t write_byte(const uint8_t data) { return maa_i2c_write_byte(m_i2c, data); } private: diff --git a/examples/c++/I2c-compass.cpp b/examples/c++/I2c-compass.cpp index 2c3aecd..4dcb587 100644 --- a/examples/c++/I2c-compass.cpp +++ b/examples/c++/I2c-compass.cpp @@ -97,7 +97,7 @@ int main () i2c = new maa::I2c(0); float direction = 0; int16_t x = 0, y = 0, z = 0; - char rx_tx_buf[MAX_BUFFER_LENGTH]; + uint8_t rx_tx_buf[MAX_BUFFER_LENGTH]; i2c->address(HMC5883L_I2C_ADDR); rx_tx_buf[0] = HMC5883L_CONF_REG_B; diff --git a/src/i2c/i2c.c b/src/i2c/i2c.c index 4f3f02c..0fc39ed 100644 --- a/src/i2c/i2c.c +++ b/src/i2c/i2c.c @@ -79,7 +79,7 @@ maa_i2c_frequency(maa_i2c_context dev, int hz) } maa_result_t -maa_i2c_read(maa_i2c_context dev, char *data, int length) +maa_i2c_read(maa_i2c_context dev, uint8_t* data, int length) { // this is the read(3) syscall not maa_i2c_read() if (read(dev->fh, data, length) == length) { @@ -88,11 +88,10 @@ maa_i2c_read(maa_i2c_context dev, char *data, int length) return MAA_ERROR_NO_DATA_AVAILABLE; } -int +uint8_t maa_i2c_read_byte(maa_i2c_context dev) { - int byte; - byte = i2c_smbus_read_byte(dev->fh); + uint8_t byte = i2c_smbus_read_byte(dev->fh); if (byte < 0) { return -1; } @@ -100,7 +99,7 @@ maa_i2c_read_byte(maa_i2c_context dev) } maa_result_t -maa_i2c_write(maa_i2c_context dev, const char* data, int length) +maa_i2c_write(maa_i2c_context dev, const uint8_t* data, int length) { if (i2c_smbus_write_i2c_block_data(dev->fh, data[0], length-1, (uint8_t*) data+1) < 0) { fprintf(stderr, "Failed to write to i2c\n"); @@ -110,7 +109,7 @@ maa_i2c_write(maa_i2c_context dev, const char* data, int length) } maa_result_t -maa_i2c_write_byte(maa_i2c_context dev, const int data) +maa_i2c_write_byte(maa_i2c_context dev, const uint8_t data) { if (i2c_smbus_write_byte(dev->fh, data) < 0) { fprintf(stderr, "Failed to write to i2c\n"); -- 2.7.4