i2c.hpp: Add C++ wrapper around I2c
[contrib/mraa.git] / api / i2c.h
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 /** @file
28  *
29  * This file defines the i2c interface for libmaa
30  *
31  */
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <fcntl.h>
40
41 #include "maa.h"
42 #include "gpio.h"
43
44 /**
45  * Opaque pointer definition to the internal struct _i2c
46  */
47 typedef struct _i2c* maa_i2c_context;
48
49 /** Initialise i2c context, using board defintions
50  *
51  * @param bus i2c bus to use
52  * @return maa_i2c_context i2c context ready for other calls.
53  */
54 maa_i2c_context maa_i2c_init(int bus);
55
56 /** Initialise i2c context, passing in spi bus to use.
57  *
58  * @param bus The i2c bus to use i.e. /dev/i2c-2 would be "2"
59  * @return maa_i2c_context i2c context ready for other calls.
60  */
61 maa_i2c_context maa_i2c_init_raw(unsigned int bus);
62
63 /** Sets the frequency of the i2c context
64  *
65  *  @param dev the i2c context
66  *  @param hz The bus frequency in hertz
67  *
68  *  @return maa_result_t the maa result.
69  */
70 maa_result_t maa_i2c_frequency(maa_i2c_context dev, int hz);
71
72 /** Read from an i2c context
73  *
74  *  @param dev the i2c context
75  *  @param data pointer to the byte array to read data in to
76  *  @param length max number of bytes to read
77  *
78  *  @return maa_result_t the maa result.
79  */
80 maa_result_t maa_i2c_read(maa_i2c_context dev, const char *data, int length);
81
82 /** Read a single byte from the i2c context
83  *
84  *  @param dev the i2c context
85  *
86  *  @return byte the result of the read or -1 if failed.
87  */
88 int maa_i2c_read_byte(maa_i2c_context dev);
89
90 /** Write to an i2c context
91  *
92  *  @param dev the i2c context
93  *  @param data pointer to the byte array to be written
94  *  @param length the number of bytes to transmit
95  *
96  *  @return maa_result_t the maa result.
97  */
98 maa_result_t maa_i2c_write(maa_i2c_context dev, char *data, int length);
99
100 /** Write a single byte to an i2c context
101  *
102  *  @param dev the i2c context
103  *  @data the byte to write
104  *
105  *  @return maa_result_t the maa result.
106  */
107 maa_result_t maa_i2c_write_byte(maa_i2c_context dev, int data);
108
109 /** Sets the i2c context address.
110  *
111  *  @param dev the i2c context
112  *  @param address The address to set for the slave (ignoring the least
113  *  signifcant bit). If set to 0, the slave will only respond to the
114  *  general call address.
115  *
116  *  @return maa_result_t the maa result.
117  */
118 maa_result_t maa_i2c_address(maa_i2c_context dev, int address);
119
120 /** De-inits an maa_i2c_context device
121  *
122  *  @param dev the i2c context
123  *
124  *  @return maa_result_t the maa result.
125  */
126 maa_result_t maa_i2c_stop(maa_i2c_context dev);
127
128 #ifdef __cplusplus
129 }
130 #endif