From 1e4516d0266679a67ad8487d6d17448b76d8c211 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Fri, 22 Apr 2016 11:58:57 +0100 Subject: [PATCH] i2c: Add exceptions for C++ i2c errors Signed-off-by: Brendan Le Foll --- api/mraa/i2c.hpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/api/mraa/i2c.hpp b/api/mraa/i2c.hpp index cd3c31c..af02c95 100644 --- a/api/mraa/i2c.hpp +++ b/api/mraa/i2c.hpp @@ -103,12 +103,17 @@ class I2c /** * Read exactly one byte from the bus * + * @throws std::invalid_argument in case of error * @return char read from the bus */ uint8_t readByte() { - return (uint8_t) mraa_i2c_read_byte(m_i2c); + int x = mraa_i2c_read_byte(m_i2c); + if (x == -1) { + throw std::invalid_argument("Unknown error in I2c::readByte()"); + } + return (uint8_t) x; } /** @@ -128,24 +133,36 @@ class I2c * Read byte from an i2c register * * @param reg Register to read from + * + * @throws std::invalid_argument in case of error * @return char read from register */ uint8_t readReg(uint8_t reg) { - return mraa_i2c_read_byte_data(m_i2c, reg); + int x = mraa_i2c_read_byte_data(m_i2c, reg); + if (x == -1) { + throw std::invalid_argument("Unknown error in I2c::readReg()"); + } + return (uint8_t) x; } /** * Read word from an i2c register * * @param reg Register to read from + * + * @throws std::invalid_argument in case of error * @return char read from register */ uint16_t readWordReg(uint8_t reg) { - return mraa_i2c_read_word_data(m_i2c, reg); + int x = mraa_i2c_read_word_data(m_i2c, reg); + if (x == -1) { + throw std::invalid_argument("Unknown error in I2c::readReg()"); + } + return (uint16_t) x; } /**