.. _program_listing_file_src_i2c.cpp: Program Listing for File i2c.cpp ================================ |exhale_lsh| :ref:`Return to documentation for file ` (``src/i2c.cpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #include #include #include extern "C" { #include #include #include #ifdef __linux__ #include #include #include #endif }; #include "i2c_bus/i2c.hh" using iplo::I2C; I2C::I2C(const char* fileName) : _fileName(fileName) { if ((_fd = ::open(_fileName, O_RDWR | O_NONBLOCK)) < 0) { std::perror("Error opening I2C bus"); std::exit(-1); } } void I2C::selectDevice(std::uint8_t address) { #ifdef __linux__ // Mockup Code for development on other platforms. if (::ioctl(_fd, I2C_SLAVE, address) < 0) { std::perror("Error selecting device on I2C bus"); std::exit(-1); } #endif } std::int32_t I2C::readByte() { #ifdef __linux__ // Mockup Code for development on other platforms. return i2c_smbus_read_byte(_fd); #else return _fd; #endif } std::int32_t I2C::readByteData(std::uint8_t reg) { #ifdef __linux__ // Mockup Code for development on other platforms. return i2c_smbus_read_byte_data(_fd, reg); #else return _fd; #endif } std::int32_t I2C::readWordData(std::uint16_t reg) { #ifdef __linux__ // Mockup Code for development on other platforms. return i2c_smbus_read_word_data(_fd, reg); #else return _fd; #endif } std::int32_t I2C::writeByte(std::uint8_t val) { #ifdef __linux__ // Mockup Code for development on other platforms. return i2c_smbus_write_byte(_fd, val); #else return _fd; #endif } std::int32_t I2C::writeByteData(std::uint8_t reg, std::uint8_t val) { #ifdef __linux__ // Mockup Code for development on other platforms. return i2c_smbus_write_byte_data(_fd, reg, val); #else return _fd; #endif } std::int32_t I2C::writeWordData(std::uint8_t reg, std::uint16_t val) { #ifdef __linux__ // Mockup Code for development on other platforms. return i2c_smbus_write_word_data(_fd, reg, val); #else return _fd; #endif } I2C::~I2C() { if (::close(_fd) < 0) { std::perror("Error closing I2C bus"); std::exit(-1); } }