maa: change complex C++ write calls to CamelCase for spi & i2c
[contrib/mraa.git] / api / maa / 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
29 namespace maa {
30
31 /**
32  * @brief C++ API to Inter-Integrated Circuit
33  *
34  * This file defines the I2c C++ interface for libmaa
35  *
36  * @snippet I2c-compass.cpp Interesting
37  */
38 class I2c {
39     public:
40         /**
41          * Instantiates an i2c bus. Multiple instances of the same bus can
42          * exist and the bus is not guarranteed to be on the correct address
43          * before read/write.
44          *
45          * @param bus The i2c bus to use
46          * @param raw Whether to disable pinmapper for your board
47          */
48         I2c(int bus, bool raw=false) {
49             if (raw)
50                 m_i2c = maa_i2c_init_raw(bus);
51             else
52                 m_i2c = maa_i2c_init(bus);
53         }
54         /**
55          * Closes the I2c Bus used. This does not guarrantee the bus will not
56          * be usable by anyone else or communicates this disconnect to any
57          * slaves.
58          */
59         ~I2c() {
60             maa_i2c_stop(m_i2c);
61         }
62         /**
63          * Sets the i2c Frequency for communication. Your board may not support
64          * the set frequency. Anyone can change this at any time and this will
65          * affect every slave on the bus
66          *
67          * @param hz Frequency to set the bus to in hz
68          * @return Result of operation
69          */
70         maa_result_t frequency(int hz) {
71             return maa_i2c_frequency(m_i2c, hz);
72         }
73         /**
74          * Set the slave to talk to, typically called before every read/write
75          * operation
76          *
77          * @param address Communicate to the i2c slave on this address
78          * @return Result of operation
79          */
80         maa_result_t address(int address) {
81             return maa_i2c_address(m_i2c, address);
82         }
83         /**
84          * Read exactly one byte from the bus
85          *
86          * @return Char read from the bus
87          */
88         unsigned char readByte() {
89             return (unsigned char) maa_i2c_read_byte(m_i2c);
90         }
91         /**
92          * Read mutliple bytes from the bus
93          *
94          * @param data Buffer to write into
95          * @param length Size of read
96          * @return length of the read or 0 if failed
97          */
98         int read(unsigned char * data, int length) {
99             return maa_i2c_read(m_i2c, data, length);
100         }
101         /**
102          * Write one byte to the bus
103          *
104          * @param data Buffer to send on the bus
105          * @param length Size of buffer to send
106          * @return Result of operation
107          */
108         maa_result_t write(const unsigned char* data, int length) {
109             return maa_i2c_write(m_i2c, data, length);
110         }
111
112         /**
113          * Write to an i2c register
114          *
115          * @param reg Register to write to
116          * @param data Value to write to register
117          * @return Result of operation
118          */
119         maa_result_t writeReg(const unsigned char reg, const unsigned char data) {
120             const unsigned char buf[2] = {reg, data};
121             return maa_i2c_write(m_i2c, buf, 2);
122         }
123
124         /**
125          * Write multiple bytes to the bus
126          *
127          * @param data The byte to send on the bus
128          * @return Result of operation
129          */
130         maa_result_t write(const unsigned char data) {
131             return maa_i2c_write_byte(m_i2c, data);
132         }
133     private:
134         maa_i2c_context m_i2c;
135 };
136
137 }