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