mraa  0.6.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Typedefs Enumerations Enumerator Pages
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 <stdexcept>
29 
30 namespace mraa {
31 
41 class I2c {
42  public:
51  I2c(int bus, bool raw=false) {
52  if (raw) {
53  m_i2c = mraa_i2c_init_raw(bus);
54  }
55  else {
56  m_i2c = mraa_i2c_init(bus);
57  }
58  if (m_i2c == NULL) {
59  throw std::invalid_argument("Invalid i2c bus");
60  }
61  }
62 
68  ~I2c() {
69  mraa_i2c_stop(m_i2c);
70  }
71 
81  return mraa_i2c_frequency(m_i2c, mode);
82  }
83 
92  return mraa_i2c_address(m_i2c, address);
93  }
94 
100  uint8_t readByte() {
101  return (uint8_t) mraa_i2c_read_byte(m_i2c);
102  }
103 
111  int read(uint8_t *data, int length) {
112  return mraa_i2c_read(m_i2c, data, length);
113  }
114 
121  uint8_t readReg(uint8_t reg) {
122  return mraa_i2c_read_byte_data(m_i2c, reg);
123  }
124 
131  uint16_t readWordReg(uint8_t reg) {
132  return mraa_i2c_read_word_data(m_i2c, reg);
133  }
134 
141  mraa_result_t writeByte(uint8_t data) {
142  return mraa_i2c_write_byte(m_i2c, data);
143  }
144 
153  mraa_result_t write(const uint8_t* data, int length) {
154  return mraa_i2c_write(m_i2c, data, length);
155  }
156 
164  mraa_result_t writeReg(uint8_t reg, uint8_t data) {
165  return mraa_i2c_write_byte_data(m_i2c, data, reg);
166  }
167 
175  mraa_result_t writeWordReg(uint8_t reg, uint16_t data) {
176  return mraa_i2c_write_word_data(m_i2c, data, reg);
177  }
178  private:
179  mraa_i2c_context m_i2c;
180 };
181 
182 }
mraa_i2c_mode_t
Definition: types.h:180
mraa_result_t writeWordReg(uint8_t reg, uint16_t data)
Definition: i2c.hpp:175
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)
uint8_t mraa_i2c_read_byte_data(mraa_i2c_context dev, const uint8_t command)
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)
uint16_t mraa_i2c_read_word_data(mraa_i2c_context dev, const uint8_t command)
uint8_t readReg(uint8_t reg)
Definition: i2c.hpp:121
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:131
mraa_i2c_context mraa_i2c_init_raw(unsigned int bus)
struct _i2c * mraa_i2c_context
Definition: i2c.h:53
mraa_result_t mraa_i2c_address(mraa_i2c_context dev, uint8_t address)
mraa_result_t mraa_i2c_stop(mraa_i2c_context dev)
mraa_result_t frequency(mraa_i2c_mode_t mode)
Definition: i2c.hpp:80
mraa_result_t writeReg(uint8_t reg, uint8_t data)
Definition: i2c.hpp:164
mraa_result_t write(const uint8_t *data, int length)
Definition: i2c.hpp:153
uint8_t mraa_i2c_read_byte(mraa_i2c_context dev)
mraa_result_t
Definition: types.h:145
~I2c()
Definition: i2c.hpp:68
API to Inter-Integrated Circuit.
Definition: i2c.hpp:41
mraa_result_t address(uint8_t address)
Definition: i2c.hpp:91
int read(uint8_t *data, int length)
Definition: i2c.hpp:111
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:100
I2c(int bus, bool raw=false)
Definition: i2c.hpp:51
mraa_result_t writeByte(uint8_t data)
Definition: i2c.hpp:141
Inter-Integrated Circuit.