ecf84ee409c43a4100f9e61f25769898f37d3c9c
[contrib/mraa.git] / api / mraa / 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
32 /**
33  * @brief API to Inter-Integrated Circuit
34  *
35  * An I2c object represents an i2c master and can talk multiple i2c slaves by
36  * selecting the correct address
37  * @htmlinclude i2c.txt
38  *
39  * @snippet I2c-compass.cpp Interesting
40  */
41 class I2c {
42     public:
43         /**
44          * Instantiates an i2c bus. Multiple instances of the same bus can
45          * exist and the bus is not guarranteed to be on the correct address
46          * before read/write.
47          *
48          * @param bus The i2c bus to use
49          * @param raw Whether to disable pinmapper for your board
50          */
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
63         /**
64          * Closes the I2c Bus used. This does not guarrantee the bus will not
65          * be usable by anyone else or communicates this disconnect to any
66          * slaves.
67          */
68         ~I2c() {
69             mraa_i2c_stop(m_i2c);
70         }
71
72         /**
73          * Sets the i2c Frequency for communication. Your board may not support
74          * the set frequency. Anyone can change this at any time and this will
75          * affect every slave on the bus
76          *
77          * @param mode Frequency to set the bus to
78          * @return Result of operation
79          */
80         mraa_result_t frequency(mraa_i2c_mode_t mode) {
81             return mraa_i2c_frequency(m_i2c, mode);
82         }
83
84         /**
85          * Set the slave to talk to, typically called before every read/write
86          * operation
87          *
88          * @param address Communicate to the i2c slave on this address
89          * @return Result of operation
90          */
91         mraa_result_t address(uint8_t address) {
92             return mraa_i2c_address(m_i2c, address);
93         }
94
95         /**
96          * Read exactly one byte from the bus
97          *
98          * @return char read from the bus
99          */
100         uint8_t readByte() {
101             return (uint8_t) mraa_i2c_read_byte(m_i2c);
102         }
103
104         /**
105          * Read length bytes from the bus into *data pointer
106          *
107          * @param data Data to read into
108          * @param length Size of read in bytes to make
109          * @return length of read, should match length
110          */
111         int read(uint8_t *data, int length) {
112             return mraa_i2c_read(m_i2c, data, length);
113         }
114
115         /**
116          * Read byte from an i2c register
117          *
118          * @param reg Register to read from
119          * @return char read from register
120          */
121         uint8_t readReg(uint8_t reg) {
122             return mraa_i2c_read_byte_data(m_i2c, reg);
123         }
124
125         /**
126          * Read word from an i2c register
127          *
128          * @param reg Register to read from
129          * @return char read from register
130          */
131         uint16_t readWordReg(uint8_t reg) {
132             return mraa_i2c_read_word_data(m_i2c, reg);
133         }
134
135         /**
136          * Read length bytes from the bus into *data pointer starting from
137          * an i2c register
138          *
139          * @param reg Register to read from
140          * @param data pointer to the byte array to read data in to
141          * @param length max number of bytes to read
142          * @return length passed to the function or 0
143          */
144         int readBytesReg(uint8_t reg, uint8_t* data, int length) {
145             return mraa_i2c_read_bytes_data(m_i2c, reg, data, length);
146         }
147
148         /**
149          * Write a byte on the bus
150          *
151          * @param data The byte to send on the bus
152          * @return Result of operation
153          */
154         mraa_result_t writeByte(uint8_t data) {
155             return mraa_i2c_write_byte(m_i2c, data);
156         }
157
158         /**
159          * Write length bytes to the bus, the first byte in the array is the
160          * command/register to write
161          *
162          * @param data Buffer to send on the bus, first byte is i2c command
163          * @param length Size of buffer to send
164          * @return Result of operation
165          */
166         mraa_result_t write(const uint8_t* data, int length) {
167             return mraa_i2c_write(m_i2c, data, length);
168         }
169
170         /**
171          * Write a byte to an i2c register
172          *
173          * @param reg Register to write to
174          * @param data Value to write to register
175          * @return Result of operation
176          */
177         mraa_result_t writeReg(uint8_t reg, uint8_t data) {
178             return mraa_i2c_write_byte_data(m_i2c, data, reg);
179         }
180
181         /**
182          * Write a word to an i2c register
183          *
184          * @param reg Register to write to
185          * @param data Value to write to register
186          * @return Result of operation
187          */
188         mraa_result_t writeWordReg(uint8_t reg, uint16_t data) {
189             return mraa_i2c_write_word_data(m_i2c, data, reg);
190         }
191     private:
192         mraa_i2c_context m_i2c;
193 };
194
195 }