1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include <linux/i2c-dev.h>
13 tI2c::tI2c(const std::string &Port)
15 // Open the I2C bus file handle
16 m_I2cHandle = open(Port.c_str(), O_RDWR);
18 // TODO: check errno to see what went wrong
19 throw std::runtime_error("Can't open the i2c bus\n");
23 bool tI2c::write(uint8_t ubAddr, const std::vector<uint8_t> &vData) {
24 if(ioctl(m_I2cHandle, I2C_SLAVE, ubAddr) < 0) {
25 // NOTE: check errno to see what went wrong
29 auto BytesWritten = ::write(m_I2cHandle, vData.data(), vData.size());
30 if(BytesWritten != ssize_t(vData.size())) {
37 bool tI2c::read(uint8_t ubAddr, uint8_t *pDest, uint32_t ulReadSize) {
38 if(ioctl(m_I2cHandle, I2C_SLAVE, ubAddr) < 0) {
39 // NOTE: check errno to see what went wrong
43 auto BytesRead = ::read(m_I2cHandle, pDest, ulReadSize);
44 if(BytesRead != ssize_t(ulReadSize)) {