Home | History | Annotate | Download | only in python
      1 %module(docstring="Python interface to libmraa") mraa
      2 
      3 %feature("autodoc", "3");
      4 
      5 %include typemaps.i
      6 %include carrays.i
      7 
      8 %array_class(uint8_t, uint8Array);
      9 
     10 // uart write()
     11 %typemap(in) (const char* data, int length) {
     12   if (PyByteArray_Check($input)) {
     13     // whilst this may seem 'hopeful' it turns out this is safe
     14     $1 = (char*) PyByteArray_AsString($input);
     15     $2 = PyByteArray_Size($input);
     16   } else {
     17     PyErr_SetString(PyExc_ValueError, "bytearray expected");
     18     return NULL;
     19   }
     20 }
     21 
     22 // i2c write()
     23 %typemap(in) (const uint8_t *data, int length) {
     24   if (PyByteArray_Check($input)) {
     25     // whilst this may seem 'hopeful' it turns out this is safe
     26     $1 = (uint8_t*) PyByteArray_AsString($input);
     27     $2 = PyByteArray_Size($input);
     28   } else {
     29     PyErr_SetString(PyExc_ValueError, "bytearray expected");
     30     return NULL;
     31   }
     32 }
     33 
     34 // Spi write()
     35 %typemap(in) (uint8_t *txBuf, int length) {
     36   if (PyByteArray_Check($input)) {
     37     // whilst this may seem 'hopeful' it turns out this is safe
     38     $1 = (uint8_t*) PyByteArray_AsString($input);
     39     $2 = PyByteArray_Size($input);
     40   } else {
     41     PyErr_SetString(PyExc_ValueError, "bytearray expected");
     42     return NULL;
     43   }
     44 }
     45 
     46 namespace mraa {
     47 class I2c;
     48 %typemap(out) uint8_t*
     49 {
     50   // need to loop over length
     51   $result = PyByteArray_FromStringAndSize((char*) $1, arg2);
     52   free($1);
     53 }
     54 
     55 class Spi;
     56 %typemap(out) uint8_t*
     57 {
     58   // need to loop over length
     59   $result = PyByteArray_FromStringAndSize((char*) $1, arg3);
     60   free($1);
     61 }
     62 }
     63 
     64 %newobject I2c::read(uint8_t *data, int length);
     65 %newobject Spi::write(uint8_t *data, int length);
     66 %newobject Uart::read(char* data, int length);
     67 %newobject Spi::transfer(uint8_t *txBuf, uint8_t *rxBuf, int length);
     68 
     69 // Uart::read()
     70 
     71 %typemap(in) (char* data, int length) {
     72    if (!PyInt_Check($input)) {
     73        PyErr_SetString(PyExc_ValueError, "Expecting an integer");
     74        return NULL;
     75    }
     76    $2 = PyInt_AsLong($input);
     77    if ($2 < 0) {
     78        PyErr_SetString(PyExc_ValueError, "Positive integer expected");
     79        return NULL;
     80    }
     81    $1 = (char*) malloc($2 * sizeof(char));
     82 }
     83 
     84 %typemap(argout) (char* data, int length) {
     85    Py_XDECREF($result);   /* Blow away any previous result */
     86    if (result < 0) {      /* Check for I/O error */
     87        free($1);
     88        PyErr_SetFromErrno(PyExc_IOError);
     89        return NULL;
     90    }
     91    // Append output value $1 to $result
     92    $result = PyByteArray_FromStringAndSize((char*) $1, result);
     93    free($1);
     94 }
     95 
     96 // I2c::read()
     97 
     98 %typemap(in) (uint8_t *data, int length) {
     99    if (!PyInt_Check($input)) {
    100        PyErr_SetString(PyExc_ValueError, "Expecting an integer");
    101        return NULL;
    102    }
    103    $2 = PyInt_AsLong($input);
    104    if ($2 < 0) {
    105        PyErr_SetString(PyExc_ValueError, "Positive integer expected");
    106        return NULL;
    107    }
    108    $1 = (uint8_t*) malloc($2 * sizeof(uint8_t));
    109 }
    110 
    111 %typemap(argout) (uint8_t *data, int length) {
    112    Py_XDECREF($result);   /* Blow away any previous result */
    113    if (result < 0) {      /* Check for I/O error */
    114        free($1);
    115        PyErr_SetFromErrno(PyExc_IOError);
    116        return NULL;
    117    }
    118    // Append output value $1 to $result
    119    $result = PyByteArray_FromStringAndSize((char*) $1, result);
    120    free($1);
    121 }
    122 
    123 // Spi::transfer()
    124 
    125 %typemap(in) (uint8_t* txBuf, uint8_t* rxBuf, int length) {
    126    if (!PyInt_Check($input)) {
    127        PyErr_SetString(PyExc_ValueError, "Expecting an integer");
    128        return NULL;
    129    }
    130    $3 = PyInt_AsLong($input);
    131    if ($3 < 0) {
    132        PyErr_SetString(PyExc_ValueError, "Positive integer expected");
    133        return NULL;
    134    }
    135    $2 = (uint8_t*) malloc($3 * sizeof(uint8_t));
    136 }
    137 
    138 %typemap(argout) (uint8_t* txBuf, uint8_t* rxBuf, int length) {
    139    Py_XDECREF($result);   /* Blow away any previous result */
    140    if (result != MRAA_SUCCESS) {      /* Check for I/O error */
    141        free($2);
    142        PyErr_SetFromErrno(PyExc_IOError);
    143        return NULL;
    144    }
    145    $result = PyByteArray_FromStringAndSize((char*) $2, $3);
    146    free($2);
    147 }
    148 
    149 %include ../mraa.i
    150 
    151 %init %{
    152     //Adding mraa_init() to the module initialisation process
    153     mraa_init();
    154 %}
    155