| 1 |
/* File: ftdi1.i */ |
| 2 |
|
| 3 |
%module(docstring="Python interface to libftdi1") ftdi1 |
| 4 |
%feature("autodoc","1"); |
| 5 |
|
| 6 |
#ifdef DOXYGEN |
| 7 |
%include "ftdi1_doc.i" |
| 8 |
#endif |
| 9 |
|
| 10 |
%{ |
| 11 |
#include "Python.h" |
| 12 |
|
| 13 |
inline PyObject* charp2str(const char *v_, long len) |
| 14 |
{ |
| 15 |
#if PY_MAJOR_VERSION >= 3 |
| 16 |
return PyBytes_FromStringAndSize(v_, len); |
| 17 |
#else |
| 18 |
return PyString_FromStringAndSize(v_, len); |
| 19 |
#endif |
| 20 |
} |
| 21 |
|
| 22 |
char * str2charp_size(PyObject* pyObj, int * size) |
| 23 |
{ |
| 24 |
char * v_ = 0; |
| 25 |
Py_ssize_t len = 0; |
| 26 |
#if PY_MAJOR_VERSION >= 3 |
| 27 |
PyBytes_AsStringAndSize(pyObj, &v_, &len); |
| 28 |
#else |
| 29 |
PyString_AsStringAndSize(pyObj, &v_, &len); |
| 30 |
#endif |
| 31 |
*size = len; |
| 32 |
return v_; |
| 33 |
} |
| 34 |
%} |
| 35 |
|
| 36 |
%include <typemaps.i> |
| 37 |
%include <cstring.i> |
| 38 |
|
| 39 |
%typemap(in) unsigned char* = char*; |
| 40 |
|
| 41 |
%immutable ftdi_version_info::version_str; |
| 42 |
%immutable ftdi_version_info::snapshot_str; |
| 43 |
|
| 44 |
%rename("%(strip:[ftdi_])s") ""; |
| 45 |
|
| 46 |
%newobject ftdi_new; |
| 47 |
%typemap(newfree) struct ftdi_context *ftdi "ftdi_free($1);"; |
| 48 |
%delobject ftdi_free; |
| 49 |
|
| 50 |
%define ftdi_usb_find_all_docstring |
| 51 |
"usb_find_all(context, vendor, product) -> (return_code, devlist)" |
| 52 |
%enddef |
| 53 |
%feature("autodoc", ftdi_usb_find_all_docstring) ftdi_usb_find_all; |
| 54 |
%typemap(in,numinputs=0) SWIGTYPE** OUTPUT ($*ltype temp) %{ $1 = &temp; %} |
| 55 |
%typemap(argout,noblock=1) SWIGTYPE** OUTPUT { $result = SWIG_AppendOutput($result, SWIG_NewPointerObj((void*)*$1,$*descriptor,0)); } |
| 56 |
%apply SWIGTYPE** OUTPUT { struct ftdi_device_list **devlist }; |
| 57 |
int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, |
| 58 |
int vendor, int product); |
| 59 |
%clear struct ftdi_device_list **devlist; |
| 60 |
|
| 61 |
%define ftdi_usb_get_strings_docstring |
| 62 |
"usb_get_strings(context, device) -> (return_code, manufacturer, description, serial)" |
| 63 |
%enddef |
| 64 |
%feature("autodoc", ftdi_usb_get_strings_docstring) ftdi_usb_get_strings; |
| 65 |
%feature("autodoc", ftdi_usb_get_strings_docstring) ftdi_usb_get_strings2; |
| 66 |
%feature("autodoc", ftdi_usb_get_strings_docstring) ftdi_eeprom_get_strings; |
| 67 |
%apply char *OUTPUT { char * manufacturer, char * description, char * serial }; |
| 68 |
%cstring_bounded_output( char * manufacturer, 256 ); |
| 69 |
%cstring_bounded_output( char * description, 256 ); |
| 70 |
%cstring_bounded_output( char * product, 256 ); |
| 71 |
%cstring_bounded_output( char * serial, 256 ); |
| 72 |
%typemap(default,noblock=1) int mnf_len, int desc_len, int product_len, int serial_len { $1 = 256; } |
| 73 |
int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev, |
| 74 |
char * manufacturer, int mnf_len, |
| 75 |
char * description, int desc_len, |
| 76 |
char * serial, int serial_len); |
| 77 |
int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev, |
| 78 |
char * manufacturer, int mnf_len, |
| 79 |
char * description, int desc_len, |
| 80 |
char * serial, int serial_len); |
| 81 |
int ftdi_eeprom_get_strings(struct ftdi_context *ftdi, |
| 82 |
char *manufacturer, int mnf_len, |
| 83 |
char *product, int product_len, |
| 84 |
char *serial, int serial_len); |
| 85 |
|
| 86 |
%clear char * manufacturer, char * description, char * serial; |
| 87 |
%clear char * product; |
| 88 |
%clear int mnf_len, int desc_len, int product_len, int serial_len; |
| 89 |
|
| 90 |
%define ftdi_read_data_docstring |
| 91 |
"read_data(context) -> (return_code, buf)" |
| 92 |
%enddef |
| 93 |
%feature("autodoc", ftdi_read_data_docstring) ftdi_read_data; |
| 94 |
%typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %} |
| 95 |
%typemap(argout,noblock=1) (unsigned char *buf, int size) { |
| 96 |
if(result<0) |
| 97 |
$2=0; |
| 98 |
$result = SWIG_AppendOutput($result, charp2str((char*)$1, $2)); |
| 99 |
free($1); |
| 100 |
} |
| 101 |
int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size); |
| 102 |
%clear (unsigned char *buf, int size); |
| 103 |
|
| 104 |
%define ftdi_write_data_docstring |
| 105 |
"write_data(context, data) -> return_code" |
| 106 |
%enddef |
| 107 |
%feature("autodoc", ftdi_write_data_docstring) ftdi_write_data; |
| 108 |
%typemap(in,numinputs=1) (const unsigned char *buf, int size) %{ $1 = (unsigned char*)str2charp_size($input, &$2); %} |
| 109 |
int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size); |
| 110 |
%clear (const unsigned char *buf, int size); |
| 111 |
|
| 112 |
%apply int *OUTPUT { unsigned int *chunksize }; |
| 113 |
int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize); |
| 114 |
int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize); |
| 115 |
%clear unsigned int *chunksize; |
| 116 |
|
| 117 |
%define ftdi_read_pins_docstring |
| 118 |
"read_pins(context) -> (return_code, pins)" |
| 119 |
%enddef |
| 120 |
%feature("autodoc", ftdi_read_pins_docstring) ftdi_read_pins; |
| 121 |
%typemap(in,numinputs=0) unsigned char *pins ($*ltype temp) %{ $1 = &temp; %} |
| 122 |
%typemap(argout,noblock=1) (unsigned char *pins) { $result = SWIG_AppendOutput($result, charp2str((char*)$1, 1)); } |
| 123 |
int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins); |
| 124 |
%clear unsigned char *pins; |
| 125 |
|
| 126 |
%typemap(in,numinputs=0) unsigned char *latency ($*ltype temp) %{ $1 = &temp; %} |
| 127 |
%typemap(argout,noblock=1) (unsigned char *latency) { $result = SWIG_AppendOutput($result, charp2str((char*)$1, 1)); } |
| 128 |
int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency); |
| 129 |
%clear unsigned char *latency; |
| 130 |
|
| 131 |
%apply short *OUTPUT { unsigned short *status }; |
| 132 |
int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status); |
| 133 |
%clear unsigned short *status; |
| 134 |
|
| 135 |
%apply int *OUTPUT { int* value }; |
| 136 |
int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value); |
| 137 |
%clear int* value; |
| 138 |
|
| 139 |
%typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %} |
| 140 |
%typemap(argout,noblock=1) (unsigned char *buf, int size) { |
| 141 |
if(result<0) |
| 142 |
$2=0; |
| 143 |
$result = SWIG_AppendOutput($result, charp2str((char*)$1, $2)); |
| 144 |
free($1); |
| 145 |
} |
| 146 |
int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size); |
| 147 |
%clear (unsigned char *buf, int size); |
| 148 |
|
| 149 |
%define ftdi_read_eeprom_location_docstring |
| 150 |
"read_eeprom_location(context, eeprom_addr) -> (return_code, eeprom_val)" |
| 151 |
%enddef |
| 152 |
%feature("autodoc", ftdi_read_eeprom_location_docstring) ftdi_read_eeprom_location; |
| 153 |
%apply short *OUTPUT { unsigned short *eeprom_val }; |
| 154 |
int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val); |
| 155 |
%clear unsigned short *eeprom_val; |
| 156 |
|
| 157 |
%define ftdi_read_eeprom_docstring |
| 158 |
"read_eeprom(context) -> (return_code, eeprom)" |
| 159 |
%enddef |
| 160 |
%feature("autodoc", ftdi_read_eeprom_docstring) ftdi_read_eeprom; |
| 161 |
|
| 162 |
%define ftdi_read_chipid_docstring |
| 163 |
"ftdi_read_chipid(context) -> (return_code, chipid)" |
| 164 |
%enddef |
| 165 |
%feature("autodoc", ftdi_read_chipid_docstring) ftdi_read_chipid; |
| 166 |
%apply int *OUTPUT { unsigned int *chipid }; |
| 167 |
int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid); |
| 168 |
%clear unsigned int *chipid; |
| 169 |
|
| 170 |
%include ftdi.h |
| 171 |
%{ |
| 172 |
#include <ftdi.h> |
| 173 |
%} |
| 174 |
|
| 175 |
%include ftdi_i.h |
| 176 |
%{ |
| 177 |
#include <ftdi_i.h> |
| 178 |
%} |
| 179 |
|
| 180 |
%pythoncode %{ |
| 181 |
__version__ = get_library_version().version_str |
| 182 |
%} |