uart: move string funcs to {read,write}Str
This commit adds raw binary read/write functions using the same typemaps as I2c functions Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
@@ -93,14 +93,39 @@ class Uart
|
||||
}
|
||||
|
||||
/**
|
||||
* Read bytes from the device into a buffer
|
||||
* Read bytes from the device into char* buffer
|
||||
*
|
||||
* @param data buffer pointer
|
||||
* @param length maximum size of buffer
|
||||
* @return numbers of bytes read
|
||||
*/
|
||||
int
|
||||
read(char* data, int length)
|
||||
{
|
||||
return mraa_uart_read(m_uart, data, (size_t) length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write bytes in String object to a device
|
||||
*
|
||||
* @param data buffer pointer
|
||||
* @param length maximum size of buffer
|
||||
* @return the number of bytes written, or -1 if an error occurred
|
||||
*/
|
||||
int
|
||||
write(const char* data, int length)
|
||||
{
|
||||
return mraa_uart_write(m_uart, data, (size_t) length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read bytes from the device into a String object
|
||||
*
|
||||
* @param length to read
|
||||
* @return string of data
|
||||
*/
|
||||
std::string
|
||||
read(int length)
|
||||
readStr(int length)
|
||||
{
|
||||
char* data = (char*) malloc(sizeof(char) * length);
|
||||
int v = mraa_uart_read(m_uart, data, (size_t) length);
|
||||
@@ -110,14 +135,13 @@ class Uart
|
||||
}
|
||||
|
||||
/**
|
||||
* Write bytes in buffer to a device
|
||||
* Write bytes in String object to a device
|
||||
*
|
||||
* @param data buffer pointer
|
||||
* @param length maximum size of buffer
|
||||
* @param string to write
|
||||
* @return the number of bytes written, or -1 if an error occurred
|
||||
*/
|
||||
int
|
||||
write(std::string data)
|
||||
writeStr(std::string data)
|
||||
{
|
||||
// this is data.length() not +1 because we want to avoid the '\0' char
|
||||
return mraa_uart_write(m_uart, data.c_str(), (data.length()));
|
||||
|
||||
@@ -10,6 +10,14 @@
|
||||
#include <node_buffer.h>
|
||||
%}
|
||||
|
||||
%typemap(in) (const char* data, int length) {
|
||||
if (!node::Buffer::HasInstance($input)) {
|
||||
SWIG_exception_fail(SWIG_ERROR, "Expected a node Buffer");
|
||||
}
|
||||
$1 = (char*) node::Buffer::Data($input);
|
||||
$2 = node::Buffer::Length($input);
|
||||
}
|
||||
|
||||
%typemap(in) (const uint8_t *data, int length) {
|
||||
if (!node::Buffer::HasInstance($input)) {
|
||||
SWIG_exception_fail(SWIG_ERROR, "Expected a node Buffer");
|
||||
@@ -42,9 +50,40 @@ class Spi;
|
||||
}
|
||||
}
|
||||
|
||||
%newobject Uart::read(char* data, int length);
|
||||
%newobject I2c::read(uint8_t *data, int length);
|
||||
%newobject Spi::write(uint8_t *data, int length);
|
||||
|
||||
//Uart::read()
|
||||
%typemap(in) (char* data, int length) {
|
||||
int x;
|
||||
int ecode = SWIG_AsVal_int($input, &x);
|
||||
if (!SWIG_IsOK(ecode)) {
|
||||
SWIG_exception_fail(SWIG_ArgError(ecode), "Expected an int");
|
||||
}
|
||||
$2 = x;
|
||||
if ($2 < 0) {
|
||||
SWIG_exception_fail(SWIG_ERROR, "Positive integer expected");
|
||||
SWIGV8_RETURN(SWIGV8_UNDEFINED());
|
||||
}
|
||||
$1 = (char*) malloc($2 * sizeof(uint8_t));
|
||||
}
|
||||
|
||||
%typemap(argout) (char* data, int length) {
|
||||
if (result < 0) { /* Check for I/O error */
|
||||
free($1);
|
||||
SWIG_exception_fail(SWIG_ERROR, "I2c write failed");
|
||||
SWIGV8_RETURN(SWIGV8_UNDEFINED());
|
||||
}
|
||||
%#if SWIG_V8_VERSION > 0x032872
|
||||
$result = node::Buffer::New((char*) $1, result);
|
||||
%#else
|
||||
$result = node::Buffer::New((char*) $1, result)->handle_;
|
||||
%#endif
|
||||
free($1);
|
||||
}
|
||||
|
||||
//I2c::read()
|
||||
%typemap(in) (uint8_t *data, int length) {
|
||||
int x;
|
||||
int ecode = SWIG_AsVal_int($input, &x);
|
||||
|
||||
@@ -7,6 +7,18 @@
|
||||
|
||||
%array_class(uint8_t, uint8Array);
|
||||
|
||||
// uart write()
|
||||
%typemap(in) (const char* data, int length) {
|
||||
if (PyByteArray_Check($input)) {
|
||||
// whilst this may seem 'hopeful' it turns out this is safe
|
||||
$1 = (char*) PyByteArray_AsString($input);
|
||||
$2 = PyByteArray_Size($input);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_ValueError, "bytearray expected");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// i2c write()
|
||||
%typemap(in) (const uint8_t *data, int length) {
|
||||
if (PyByteArray_Check($input)) {
|
||||
@@ -49,8 +61,36 @@ class Spi;
|
||||
|
||||
%newobject I2c::read(uint8_t *data, int length);
|
||||
%newobject Spi::write(uint8_t *data, int length);
|
||||
%newobject Uart::read(char* data, int length);
|
||||
%newobject Spi::transfer(uint8_t *txBuf, uint8_t *rxBuf, int length);
|
||||
|
||||
// Uart::read()
|
||||
|
||||
%typemap(in) (char* data, int length) {
|
||||
if (!PyInt_Check($input)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Expecting an integer");
|
||||
return NULL;
|
||||
}
|
||||
$2 = PyInt_AsLong($input);
|
||||
if ($2 < 0) {
|
||||
PyErr_SetString(PyExc_ValueError, "Positive integer expected");
|
||||
return NULL;
|
||||
}
|
||||
$1 = (char*) malloc($2 * sizeof(char));
|
||||
}
|
||||
|
||||
%typemap(argout) (char* data, int length) {
|
||||
Py_XDECREF($result); /* Blow away any previous result */
|
||||
if (result < 0) { /* Check for I/O error */
|
||||
free($1);
|
||||
PyErr_SetFromErrno(PyExc_IOError);
|
||||
return NULL;
|
||||
}
|
||||
// Append output value $1 to $result
|
||||
$result = PyByteArray_FromStringAndSize((char*) $1, result);
|
||||
free($1);
|
||||
}
|
||||
|
||||
// I2c::read()
|
||||
|
||||
%typemap(in) (uint8_t *data, int length) {
|
||||
|
||||
Reference in New Issue
Block a user