mraa  2.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
i2c.hpp
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include "i2c.h"
28 #include "types.hpp"
29 #include <stdexcept>
30 
31 namespace mraa
32 {
33 
43 class I2c
44 {
45  public:
54  I2c(int bus, bool raw = false)
55  {
56  if (raw) {
57  m_i2c = mraa_i2c_init_raw(bus);
58  } else {
59  m_i2c = mraa_i2c_init(bus);
60  }
61  if (m_i2c == NULL) {
62  throw std::invalid_argument("Invalid i2c bus");
63  }
64  }
70  I2c(void* i2c_context)
71  {
72  m_i2c = (mraa_i2c_context) i2c_context;
73  if (m_i2c == NULL) {
74  throw std::invalid_argument("Invalid I2C context");
75  }
76  }
77 
83  ~I2c()
84  {
85  mraa_i2c_stop(m_i2c);
86  }
87 
96  Result
98  {
99  return (Result) mraa_i2c_frequency(m_i2c, (mraa_i2c_mode_t) mode);
100  }
101 
109  Result
110  address(uint8_t address)
111  {
112  return (Result) mraa_i2c_address(m_i2c, address);
113  }
114 
121  uint8_t
123  {
124  int x = mraa_i2c_read_byte(m_i2c);
125  if (x == -1) {
126  throw std::invalid_argument("Unknown error in I2c::readByte()");
127  }
128  return (uint8_t) x;
129  }
130 
138  int
139  read(uint8_t* data, int length)
140  {
141  return mraa_i2c_read(m_i2c, data, length);
142  }
143 
152  uint8_t
153  readReg(uint8_t reg)
154  {
155  int x = mraa_i2c_read_byte_data(m_i2c, reg);
156  if (x == -1) {
157  throw std::invalid_argument("Unknown error in I2c::readReg()");
158  }
159  return (uint8_t) x;
160  }
161 
170  uint16_t
171  readWordReg(uint8_t reg)
172  {
173  int x = mraa_i2c_read_word_data(m_i2c, reg);
174  if (x == -1) {
175  throw std::invalid_argument("Unknown error in I2c::readReg()");
176  }
177  return (uint16_t) x;
178  }
179 
189  int
190  readBytesReg(uint8_t reg, uint8_t* data, int length)
191  {
192  return mraa_i2c_read_bytes_data(m_i2c, reg, data, length);
193  }
194 
201  Result
202  writeByte(uint8_t data)
203  {
204  return (Result) mraa_i2c_write_byte(m_i2c, data);
205  }
206 
215  Result
216  write(const uint8_t* data, int length)
217  {
218  return (Result) mraa_i2c_write(m_i2c, data, length);
219  }
220 
228  Result
229  writeReg(uint8_t reg, uint8_t data)
230  {
231  return (Result) mraa_i2c_write_byte_data(m_i2c, data, reg);
232  }
233 
241  Result
242  writeWordReg(uint8_t reg, uint16_t data)
243  {
244  return (Result) mraa_i2c_write_word_data(m_i2c, data, reg);
245  }
246 
247  private:
248  mraa_i2c_context m_i2c;
249 };
250 }
mraa_i2c_mode_t
Definition: types.h:246
Result
Definition: types.hpp:204
int mraa_i2c_read_byte(mraa_i2c_context dev)
Result writeByte(uint8_t data)
Definition: i2c.hpp:202
mraa_result_t mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
int mraa_i2c_read(mraa_i2c_context dev, uint8_t *data, int length)
mraa_i2c_context mraa_i2c_init(int bus)
mraa_result_t mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
mraa_result_t mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
uint8_t readReg(uint8_t reg)
Definition: i2c.hpp:153
Result write(const uint8_t *data, int length)
Definition: i2c.hpp:216
int readBytesReg(uint8_t reg, uint8_t *data, int length)
Definition: i2c.hpp:190
I2cMode
Definition: types.hpp:241
mraa_result_t mraa_i2c_write(mraa_i2c_context dev, const uint8_t *data, int length)
uint16_t readWordReg(uint8_t reg)
Definition: i2c.hpp:171
I2c(void *i2c_context)
Definition: i2c.hpp:70
Result writeWordReg(uint8_t reg, uint16_t data)
Definition: i2c.hpp:242
mraa_i2c_context mraa_i2c_init_raw(unsigned int bus)
struct _i2c * mraa_i2c_context
Definition: i2c.h:53
int mraa_i2c_read_word_data(mraa_i2c_context dev, const uint8_t command)
mraa_result_t mraa_i2c_address(mraa_i2c_context dev, uint8_t address)
Result writeReg(uint8_t reg, uint8_t data)
Definition: i2c.hpp:229
Result address(uint8_t address)
Definition: i2c.hpp:110
mraa_result_t mraa_i2c_stop(mraa_i2c_context dev)
int mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t *data, int length)
Definition: aio.hpp:31
Result frequency(I2cMode mode)
Definition: i2c.hpp:97
~I2c()
Definition: i2c.hpp:83
API to Inter-Integrated Circuit.
Definition: i2c.hpp:43
int mraa_i2c_read_byte_data(mraa_i2c_context dev, const uint8_t command)
int read(uint8_t *data, int length)
Definition: i2c.hpp:139
mraa_result_t mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
uint8_t readByte()
Definition: i2c.hpp:122
I2c(int bus, bool raw=false)
Definition: i2c.hpp:54
Inter-Integrated Circuit.