i2c.hpp: reorder & style header
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
@@ -59,6 +59,7 @@ class I2c {
|
|||||||
throw std::invalid_argument("Invalid i2c bus");
|
throw std::invalid_argument("Invalid i2c bus");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes the I2c Bus used. This does not guarrantee the bus will not
|
* Closes the I2c Bus used. This does not guarrantee the bus will not
|
||||||
* be usable by anyone else or communicates this disconnect to any
|
* be usable by anyone else or communicates this disconnect to any
|
||||||
@@ -67,6 +68,7 @@ class I2c {
|
|||||||
~I2c() {
|
~I2c() {
|
||||||
mraa_i2c_stop(m_i2c);
|
mraa_i2c_stop(m_i2c);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the i2c Frequency for communication. Your board may not support
|
* Sets the i2c Frequency for communication. Your board may not support
|
||||||
* the set frequency. Anyone can change this at any time and this will
|
* the set frequency. Anyone can change this at any time and this will
|
||||||
@@ -78,6 +80,7 @@ class I2c {
|
|||||||
mraa_result_t frequency(int hz) {
|
mraa_result_t frequency(int hz) {
|
||||||
return mraa_i2c_frequency(m_i2c, hz);
|
return mraa_i2c_frequency(m_i2c, hz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the slave to talk to, typically called before every read/write
|
* Set the slave to talk to, typically called before every read/write
|
||||||
* operation
|
* operation
|
||||||
@@ -88,6 +91,7 @@ class I2c {
|
|||||||
mraa_result_t address(uint8_t address) {
|
mraa_result_t address(uint8_t address) {
|
||||||
return mraa_i2c_address(m_i2c, address);
|
return mraa_i2c_address(m_i2c, address);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read exactly one byte from the bus
|
* Read exactly one byte from the bus
|
||||||
*
|
*
|
||||||
@@ -96,6 +100,7 @@ class I2c {
|
|||||||
uint8_t read() {
|
uint8_t read() {
|
||||||
return (uint8_t) mraa_i2c_read_byte(m_i2c);
|
return (uint8_t) mraa_i2c_read_byte(m_i2c);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read mutliple bytes from the bus
|
* Read mutliple bytes from the bus
|
||||||
*
|
*
|
||||||
@@ -106,6 +111,7 @@ class I2c {
|
|||||||
uint8_t read(char *data, size_t length) {
|
uint8_t read(char *data, size_t length) {
|
||||||
return mraa_i2c_read(m_i2c, (uint8_t*) data, (int) 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
|
* Read length bytes from the bus, and return as a std::string note
|
||||||
* that this is not a null terminated string
|
* that this is not a null terminated string
|
||||||
@@ -120,6 +126,7 @@ class I2c {
|
|||||||
free(data);
|
free(data);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write one byte to the bus
|
* Write one byte to the bus
|
||||||
*
|
*
|
||||||
@@ -131,18 +138,6 @@ class I2c {
|
|||||||
return mraa_i2c_write(m_i2c, (const unsigned char *)data, (int) length);
|
return mraa_i2c_write(m_i2c, (const unsigned char *)data, (int) length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Write to an i2c register
|
|
||||||
*
|
|
||||||
* @param reg Register to write to
|
|
||||||
* @param data Value to write to register
|
|
||||||
* @return Result of operation
|
|
||||||
*/
|
|
||||||
mraa_result_t writeReg(uint8_t reg, uint8_t data) {
|
|
||||||
const uint8_t buf[2] = {reg, data};
|
|
||||||
return mraa_i2c_write(m_i2c, buf, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read an i2c register
|
* Read an i2c register
|
||||||
*
|
*
|
||||||
@@ -162,6 +157,18 @@ class I2c {
|
|||||||
mraa_result_t write(uint8_t data) {
|
mraa_result_t write(uint8_t data) {
|
||||||
return mraa_i2c_write_byte(m_i2c, data);
|
return mraa_i2c_write_byte(m_i2c, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write to an i2c register
|
||||||
|
*
|
||||||
|
* @param reg Register to write to
|
||||||
|
* @param data Value to write to register
|
||||||
|
* @return Result of operation
|
||||||
|
*/
|
||||||
|
mraa_result_t writeReg(uint8_t reg, uint8_t data) {
|
||||||
|
const uint8_t buf[2] = {reg, data};
|
||||||
|
return mraa_i2c_write(m_i2c, buf, 2);
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
mraa_i2c_context m_i2c;
|
mraa_i2c_context m_i2c;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user