1 /******************************************************************************
14 * \brief Core I2C python swig interface definitions file.
16 * \author Robin Knight (robin.knight@roadnarrows.com)
19 * (C) 2016. RoadNarrows LLC.
20 * (http://www.roadnarrows.com)
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.
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.
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.
51 ******************************************************************************/
55 #include "rnr/rnrconfig.h"
61 * \brief Swig generated core wrapper c file.
66 * Required RNR C types
68 typedef unsigned char byte_t;
69 typedef unsigned short ushort_t;
70 typedef unsigned int uint_t;
71 typedef unsigned long ulong_t;
78 %array_functions(byte_t, byteArray);
79 %pointer_functions(uint_t, uintp);
85 #include "rnr/rnrconfig.h"
91 * \brief Open a I2C bus device.
93 * \param device Device name.
96 * On success, returns open file descriptor.
97 * On failure, returns -errno.
99 int i2ccore_open(const char *device)
104 rc = i2c_open(&i2c, device);
106 return rc < 0? -errno: i2c.fd;
110 * \brief Close an open I2C bus device.
113 * On success, returns 0.
114 * On failure, returns -errno.
116 int i2ccore_close(int fd)
128 * \brief Read data from an attached device connected to the open I2C bus.
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.
137 * On success, returns number of bytes read.
138 * On failure, returns -errno.
140 int i2ccore_read(int fd, unsigned short cur_addr, unsigned short addr,
141 byte_t buf[], unsigned int count)
149 n = i2c_read(&i2c, addr, buf, count);
151 return n < 0? -errno: n;
155 * \brief Write data to an attached device connected to the open I2C bus.
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.
164 * On success, returns number of bytes written.
165 * On failure, returns -errno.
167 int i2ccore_write(int fd, unsigned short cur_addr, unsigned short addr,
168 byte_t buf[], unsigned int count)
176 n = i2c_write(&i2c, addr, buf, count);
178 return n < 0? -errno: n;
182 * \brief Transfer data to an attached device connected to the open I2C bus
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.
194 * On success, returns 0.
195 * On failure, returns -errno.
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)
207 rc = i2c_transfer(&i2c, addr, wbuf, wcount, rbuf, rcount);
209 return rc < 0? -errno: rc;
213 * \brief Test for the existence of a device with the given address.
215 * \param fd File descriptor.
216 * \param cur_addr I2C address of last I/O operation.
217 * \param addr I2C device address to read.
219 * \return If device is found, returns 1, else returns 0.
221 int i2ccore_check(int fd, unsigned short cur_addr, unsigned short addr)
228 return i2c_exists(&i2c, addr);
234 * Higher-level python interface to the core C library.
240 RoadNarrows Robotics i2ccore Python Inline Extensions and Wrappers.
244 ## \package rnr.i2ccore
246 ## \brief RoadNarrows Robotics Swigged Core Python Interface Module.
248 ## \author Robin Knight (robin.knight@roadnarrows.com)
251 ## (C) 2016. RoadNarrows LLC.\n
252 ## (http://www.roadnarrows.com)\n
253 ## All Rights Reserved