Private
Public Access
2
0

i2c: allow binary strings as parameters to write() functions in scripting

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-07-01 13:51:03 +01:00
parent cf1881a978
commit 70ba5a56ab
4 changed files with 19 additions and 14 deletions

View File

@@ -95,8 +95,8 @@ class I2c {
* @param length Size of read * @param length Size of read
* @return length of the read or 0 if failed * @return length of the read or 0 if failed
*/ */
int read(unsigned char * data, int length) { int read(char * data, size_t length) {
return mraa_i2c_read(m_i2c, data, length); return mraa_i2c_read(m_i2c, (uint8_t*) data, (int) length);
} }
/** /**
* Write one byte to the bus * Write one byte to the bus
@@ -105,8 +105,8 @@ class I2c {
* @param length Size of buffer to send * @param length Size of buffer to send
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t write(const unsigned char* data, int length) { mraa_result_t write(char* data, size_t length) {
return mraa_i2c_write(m_i2c, data, length); return mraa_i2c_write(m_i2c, (const unsigned char *)data, (int) length);
} }
/** /**
@@ -116,8 +116,8 @@ class I2c {
* @param data Value to write to register * @param data Value to write to register
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t writeReg(const unsigned char reg, const unsigned char data) { mraa_result_t writeReg(char reg, char data) {
const unsigned char buf[2] = {reg, data}; const unsigned char buf[2] = {(unsigned char) reg, (unsigned char) data};
return mraa_i2c_write(m_i2c, buf, 2); return mraa_i2c_write(m_i2c, buf, 2);
} }
@@ -127,7 +127,7 @@ class I2c {
* @param data The byte to send on the bus * @param data The byte to send on the bus
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t write(const unsigned char data) { mraa_result_t write(char data) {
return mraa_i2c_write_byte(m_i2c, data); return mraa_i2c_write_byte(m_i2c, data);
} }
private: private:

View File

@@ -75,8 +75,8 @@ class Spi {
* @param data the byte to send * @param data the byte to send
* @return data received on the miso line * @return data received on the miso line
*/ */
unsigned char write(uint8_t data) { unsigned char write(char data) {
return (unsigned char) mraa_spi_write(m_spi, data); return (unsigned char) mraa_spi_write(m_spi, (uint8_t) data);
} }
/** /**
* Write buffer of bytes to SPI device * Write buffer of bytes to SPI device
@@ -85,8 +85,8 @@ class Spi {
* @param length size of buffer to send * @param length size of buffer to send
* @return char* data received on the miso line. Same length as passed in * @return char* data received on the miso line. Same length as passed in
*/ */
unsigned char* write(uint8_t* data, int length) { unsigned char* write(char* data, size_t length) {
return (unsigned char*) mraa_spi_write_buf(m_spi, data, length); return (unsigned char*) mraa_spi_write_buf(m_spi, (uint8_t *) data, (int) length);
} }
/** /**
* Change the SPI lsb mode * Change the SPI lsb mode

View File

@@ -30,7 +30,8 @@ x = mraa.I2c(0)
x.address(0x62) x.address(0x62)
x.writeReg(0,0) x.writeReg(0,0)
x.writeReg(1,0) x.writeReg(1,0)
x.writeReg(0x08,0xAA)
x.writeReg(0x04,255)
x.writeReg(0x02,255)
# Be careful that your i2c device can actually handle a 'batch' handling of
# such data, this is not typical in arduino type devices
s = "\x08\xAA\x04\x255\x02\x255"
x.write(s)

View File

@@ -1,4 +1,6 @@
%include carrays.i %include carrays.i
%include stdint.i
%array_class(char, mraaBuffer);
#ifdef DOXYGEN #ifdef DOXYGEN
%include common_doc.i %include common_doc.i
@@ -32,6 +34,8 @@
%rename(getPlatform) mraa_get_platform_type; %rename(getPlatform) mraa_get_platform_type;
%typemap(in) uint8_t = char; %typemap(in) uint8_t = char;
%typemap(in) unsigned char* = char*;
%apply (char *STRING, size_t LENGTH) { (char *data, size_t length) };
%include "types.h" %include "types.h"