i2c  1.4.2
RoadNarrows Robotics I2C Package
i2ccore.i
1 /******************************************************************************
2  *
3  * Package: i2c
4  *
5  * File: i2ccore.i
6  *
7  * $LastChangedDate$
8  * $Rev$
9  */
10 
11 /*!
12  * \file
13  *
14  * \brief Core I2C python swig interface definitions file.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \par Copyright:
19  * (C) 2016. RoadNarrows LLC.
20  * (http://www.roadnarrows.com)
21  * All Rights Reserved
22  */
23 
24 /*
25  * @EulaBegin@
26  * Permission is hereby granted, without written agreement and without
27  * license or royalty fees, to use, copy, modify, and distribute this
28  * software and its documentation for any purpose, provided that
29  * (1) The above copyright notice and the following two paragraphs
30  * appear in all copies of the source code and (2) redistributions
31  * including binaries reproduces these notices in the supporting
32  * documentation. Substantial modifications to this software may be
33  * copyrighted by their authors and need not follow the licensing terms
34  * described here, provided that the new terms are clearly indicated in
35  * all files where they apply.
36  *
37  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
38  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
39  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
40  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
41  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
42  * THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
45  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
46  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
47  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
48  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
49  * @EulaEnd@
50  *
51  ******************************************************************************/
52 
53 %module i2ccore
54 %{
55 #include "rnr/rnrconfig.h"
56 %}
57 
58 %begin
59 %{
60 /*! \file
61  * \brief Swig generated core wrapper c file.
62  */
63 %}
64 
65 /*
66  * Required RNR C types
67  */
68 typedef unsigned char byte_t;
69 typedef unsigned short ushort_t;
70 typedef unsigned int uint_t;
71 typedef unsigned long ulong_t;
72 typedef int bool_t;
73 
74 %include "carrays.i"
75 %include "cpointer.i"
76 
77 
78 %array_functions(byte_t, byteArray);
79 %pointer_functions(uint_t, uintp);
80 
81 %inline
82 %{
83 #include <errno.h>
84 
85 #include "rnr/rnrconfig.h"
86 #include "rnr/i2c.h"
87 
88 #include "i2ccore.h"
89 
90 /*!
91  * \brief Open a I2C bus device.
92  *
93  * \param device Device name.
94  *
95  * \return
96  * On success, returns open file descriptor.
97  * On failure, returns -errno.
98  */
99 int i2ccore_open(const char *device)
100 {
101  i2c_t i2c;
102  int rc;
103 
104  rc = i2c_open(&i2c, device);
105 
106  return rc < 0? -errno: i2c.fd;
107 }
108 
109 /*!
110  * \brief Close an open I2C bus device.
111  *
112  * \return
113  * On success, returns 0.
114  * On failure, returns -errno.
115  */
116 int i2ccore_close(int fd)
117 {
118  i2c_t i2c;
119 
120  i2c.fd = fd;
121 
122  i2c_close(&i2c);
123 
124  return OK;
125 }
126 
127 /*!
128  * \brief Read data from an attached device connected to the open I2C bus.
129  *
130  * \param fd File descriptor.
131  * \param cur_addr I2C address of last I/O operation.
132  * \param addr I2C device address to read.
133  * \param [out] buf Output buffer.
134  * \param count Number of byte to read.
135  *
136  * \return
137  * On success, returns number of bytes read.
138  * On failure, returns -errno.
139  */
140 int i2ccore_read(int fd, unsigned short cur_addr, unsigned short addr,
141  byte_t buf[], unsigned int count)
142 {
143  i2c_t i2c;
144  int n;
145 
146  i2c.fd = fd;
147  i2c.addr = cur_addr;
148 
149  n = i2c_read(&i2c, addr, buf, count);
150 
151  return n < 0? -errno: n;
152 }
153 
154 /*!
155  * \brief Write data to an attached device connected to the open I2C bus.
156  *
157  * \param fd File descriptor.
158  * \param cur_addr I2C address of last I/O operation.
159  * \param addr I2C device address to read.
160  * \param [in] buf Input buffer.
161  * \param count Number of byte to write.
162  *
163  * \return
164  * On success, returns number of bytes written.
165  * On failure, returns -errno.
166  */
167 int i2ccore_write(int fd, unsigned short cur_addr, unsigned short addr,
168  byte_t buf[], unsigned int count)
169 {
170  i2c_t i2c;
171  int n;
172 
173  i2c.fd = fd;
174  i2c.addr = cur_addr;
175 
176  n = i2c_write(&i2c, addr, buf, count);
177 
178  return n < 0? -errno: n;
179 }
180 
181 /*!
182  * \brief Transfer data to an attached device connected to the open I2C bus
183  * reead back.
184  *
185  * \param fd File descriptor.
186  * \param cur_addr I2C address of last I/O operation.
187  * \param addr I2C device address to read.
188  * \param [in] wbuf Input buffer.
189  * \param wcount Number of byte to write.
190  * \param [out] rbuf Output buffer.
191  * \param rcount Number of byte to read.
192  *
193  * \return
194  * On success, returns 0.
195  * On failure, returns -errno.
196  */
197 int i2ccore_transfer(int fd, unsigned short cur_addr, unsigned short addr,
198  byte_t wbuf[], unsigned int wcount,
199  byte_t rbuf[], unsigned int rcount)
200 {
201  i2c_t i2c;
202  int rc;
203 
204  i2c.fd = fd;
205  i2c.addr = cur_addr;
206 
207  rc = i2c_transfer(&i2c, addr, wbuf, wcount, rbuf, rcount);
208 
209  return rc < 0? -errno: rc;
210 }
211 
212 /*!
213  * \brief Test for the existence of a device with the given address.
214  *
215  * \param fd File descriptor.
216  * \param cur_addr I2C address of last I/O operation.
217  * \param addr I2C device address to read.
218  *
219  * \return If device is found, returns 1, else returns 0.
220  */
221 int i2ccore_check(int fd, unsigned short cur_addr, unsigned short addr)
222 {
223  i2c_t i2c;
224 
225  i2c.fd = fd;
226  i2c.addr = cur_addr;
227 
228  return i2c_exists(&i2c, addr);
229 }
230 
231 %}
232 
233 /*
234  * Higher-level python interface to the core C library.
235  */
236 %pythoncode
237 %{
238 
239 """
240 RoadNarrows Robotics i2ccore Python Inline Extensions and Wrappers.
241 """
242 
243 ## \file
244 ## \package rnr.i2ccore
245 ##
246 ## \brief RoadNarrows Robotics Swigged Core Python Interface Module.
247 ##
248 ## \author Robin Knight (robin.knight@roadnarrows.com)
249 ##
250 ## \par Copyright:
251 ## (C) 2016. RoadNarrows LLC.\n
252 ## (http://www.roadnarrows.com)\n
253 ## All Rights Reserved
254 ##
255 
256 %}