javascript: fix swig generation of node.js api
[contrib/mraa.git] / api / i2c.h
1 /*
2  * Originally from mbed Microcontroller Library
3  * Copyright (c) 2006-2013 ARM Limited
4  * Copyright (c) 2014 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #pragma once
20
21 /** @file
22  *
23  * This file defines the i2c interface for libmaa
24  *
25  */
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <fcntl.h>
34
35 #include "maa.h"
36 #include "gpio.h"
37
38 typedef struct {
39     int hz;
40     int fh;
41     int addr;
42     maa_gpio_context gpio;
43 } maa_i2c_context;
44
45 maa_i2c_context* maa_i2c_init();
46
47 /** Set the frequency of the I2C interface
48  *
49  *  @param dev the i2c context
50  *  @param hz The bus frequency in hertz
51  */
52 void maa_i2c_frequency(maa_i2c_context* dev, int hz);
53
54 /** Checks to see if this I2C Slave has been addressed.
55  *
56  *  @param dev the i2c context
57  *  @returns
58  *  A status indicating if the device has been addressed, and how
59  *  - NoData            - the slave has not been addressed
60  *  - ReadAddressed     - the master has requested a read from this slave
61  *  - WriteAddressed    - the master is writing to this slave
62  *  - WriteGeneral      - the master is writing to all slave
63  */
64 int maa_i2c_receive(maa_i2c_context* dev);
65
66 /** Read from an I2C master.
67  *
68  *  @param dev the i2c context
69  *  @param data pointer to the byte array to read data in to
70  *  @param length maximum number of bytes to read
71  *
72  *  @returns
73  *       0 on success,
74  *   non-0 otherwise
75  */
76 int maa_i2c_read(maa_i2c_context* dev, char *data, int length);
77
78 /** Read a single byte from an I2C master.
79  *
80  *  @param dev the i2c context
81  *  @returns
82  *    the byte read
83  */
84 int maa_i2c_read_byte(maa_i2c_context* dev);
85
86 /** Write to an I2C master
87  *
88  *  @param dev the i2c context
89  *  @param data pointer to the byte array to be transmitted
90  *  @param length the number of bytes to transmite
91  *
92  *  @returns
93  *       0 on success,
94  *   non-0 otherwise
95  */
96 int maa_i2c_write(maa_i2c_context* dev, const char *data, int length);
97
98 /** Write a single byte to an I2C master.
99  *
100  *  @param dev the i2c context
101  *  @data the byte to write
102  *
103  *  @returns
104  *    '1' if an ACK was received,
105  *    '0' otherwise
106  */
107 int maa_i2c_write_byte(maa_i2c_context* dev, int data);
108
109 /** Sets the I2C slave 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 void maa_i2c_address(maa_i2c_context* dev, int address);
117
118 /** De-inits an maa_i2c_context device
119  *
120  *  @param dev the i2c context
121  */
122 void maa_i2c_stop(maa_i2c_context* dev);
123
124 #ifdef __cplusplus
125 }
126 #endif