Hi,
I tried to enhance the python wrapper:
- updated functions prototypes in regard to the lib
- made OUTPUT* typemaps active as they were misplaced regarding headers order
- allowed use of ftdi_usb_get_strings
- added an advanced example script
- stripped the 'ftdi_' suffix to avoid to write "ftdi.ftdi_usb_open(...)"; it
becomes just "ftdi.usb_open(...)"
- removed useless pointer functions since valid OUTPUT typemaps are there
- some cleanup
Here is the patch against the current trunk and the advanced example script to
test many more functionalities.
Regards.
--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [email protected]
diff --git a/bindings/ftdi.i b/bindings/ftdi.i
index ace244b..edad474 100644
--- a/bindings/ftdi.i
+++ b/bindings/ftdi.i
@@ -1,29 +1,34 @@
-/* File: example.i */
-%module ftdi
+/* File: ftdi.i */
+
+%module(docstring="Python interface to libftdi") ftdi
+
%include "typemaps.i"
%include "cpointer.i"
-%pointer_functions(unsigned int, uintp);
-%pointer_functions(unsigned char *, ucharp);
-%pointer_functions(char *, charp);
+%include "cstring.i"
%typemap(in) unsigned char* = char*;
+
%ignore ftdi_write_data_async;
%ignore ftdi_async_complete;
%immutable ftdi_version_info::version_str;
%immutable ftdi_version_info::snapshot_str;
-%include ftdi.h
-%{
-#include <ftdi.h>
-%}
-
-%include ftdi_i.h
-%{
-#include <ftdi_i.h>
-%}
+%rename("%(strip:[ftdi_])s") "";
-extern "C" {
+%apply char *OUTPUT { char * manufacturer };
+%apply char *OUTPUT { char * description };
+%apply char *OUTPUT { char * serial };
+%cstring_bounded_output( char * manufacturer, 256 );
+%cstring_bounded_output( char * description, 256 );
+%cstring_bounded_output( char * serial, 256 );
+ int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev,
+ char * manufacturer, int mnf_len,
+ char * description, int desc_len,
+ char * serial, int serial_len);
+%clear char * manufacturer;
+%clear char * description;
+%clear char * serial;
%apply char *OUTPUT { unsigned char *buf };
int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
@@ -34,8 +39,6 @@ extern "C" {
int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
%clear unsigned int *chunksize;
- //int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size);
- //void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more);
%apply char *OUTPUT { unsigned char *pins };
int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
%clear unsigned char *pins;
@@ -44,21 +47,30 @@ extern "C" {
int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
%clear unsigned char *latency;
-%apply char *OUTPUT { unsigned short *status };
+%apply short *OUTPUT { unsigned short *status };
int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
%clear unsigned short *status;
-%apply char *OUTPUT { unsigned char *output };
- int ftdi_eeprom_build(struct ftdi_context *ftdi);
-%clear unsigned char *output;
+%apply int *OUTPUT { int* value };
+ int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value);
+%clear int* value;
-%apply char *OUTPUT { unsigned char *eeprom };
- int ftdi_read_eeprom(struct ftdi_context *ftdi);
- int ftdi_write_eeprom(struct ftdi_context *ftdi);
-%clear unsigned char *eeprom;
+%apply short *OUTPUT { unsigned short *eeprom_val };
+ int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val);
+%clear unsigned short *eeprom_val;
%apply int *OUTPUT { unsigned int *chipid };
int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
%clear unsigned int *chipid;
-}
+%include ftdi.h
+%{
+#include <ftdi.h>
+%}
+
+%include ftdi_i.h
+%{
+#include <ftdi_i.h>
+%}
+
+%pointer_functions(struct ftdi_device_list *, device_listpp)
diff --git a/examples/python/simple.py b/examples/python/simple.py
index fe4babc..48748e9 100644
--- a/examples/python/simple.py
+++ b/examples/python/simple.py
@@ -9,18 +9,17 @@ import ftdi
def main():
"""Main program"""
- context = ftdi.ftdi_context()
- ftdi.ftdi_init(context)
+ context = ftdi.new()
- version_info = ftdi.ftdi_get_library_version()
+ version_info = ftdi.get_library_version()
print("[FTDI version] major: %d, minor: %d, micro: %d" \
", version_str: %s, snapshot_str: %s" %
(version_info.major, version_info.minor, version_info.micro,
version_info.version_str, version_info.snapshot_str))
- print("ftdi_open(): %d" % ftdi.ftdi_usb_open(context, 0x403, 0x6010))
- print("ftdi_set_baudrate(): %d" % ftdi.ftdi_set_baudrate(context, 9600))
+ print("ftdi.usb_open(): %d" % ftdi.usb_open(context, 0x0403, 0x6001))
+ print("ftdi.set_baudrate(): %d" % ftdi.set_baudrate(context, 9600))
- ftdi.ftdi_deinit(context)
+ ftdi.free(context)
main()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Python example program.
Complete program to demonstrate the usage
of the swig generated python wrapper
You need to build and install the wrapper first"""
import os
import ftdi
import time
# initialize
ftdic = ftdi.new()
if ftdic == 0:
print 'new failed: %d', ret
os._exit( 1 )
# list all devices
devlist = ftdi.new_device_listpp()
ret = ftdi.usb_find_all( ftdic, devlist, 0x0403, 0x6001 )
if ret < 0:
print 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
os._exit( 1 )
print 'Number of FTDI devices found: %d\n' % ret
count = ret
curdev = devlist
for i in range( count ):
print 'Checking device: %d' % i
curnode = ftdi.device_listpp_value( curdev )
ret, manufacturer, description, serial = ftdi.usb_get_strings( ftdic, curnode.dev, 128, 128, 128 )
if ret < 0:
print 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
os._exit( 1 )
print 'Manufacturer: %s, Description: %s, Serial: %s\n' % ( manufacturer, description, serial )
ftdi.device_listpp_assign( curdev, curnode.next )
# open usb
ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 )
if ret < 0:
print 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
os._exit( 1 )
# bitbang
ret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG )
if ret < 0:
print 'Cannot enable bitbang'
os._exit( 1 )
print 'turning everything on'
ftdi.write_data( ftdic, chr(0xff), 1 )
time.sleep( 1 )
print 'turning everything off\n'
ftdi.write_data( ftdic, chr(0x00), 1 )
time.sleep( 1 )
for i in range( 8 ):
print 'enabling bit', i
val = 2**i
ftdi.write_data( ftdic, chr(val), 1 )
time.sleep ( 1 )
ftdi.disable_bitbang( ftdic )
print ''
# read chip id
ret, chipid = ftdi.read_chipid( ftdic )
print 'FDTI chip id: %X\n' % chipid
# read eeprom
eeprom_addr = 1
ret, eeprom_val = ftdi.read_eeprom_location( ftdic, eeprom_addr )
print 'eeprom @ %d: 0x%04x\n' % ( eeprom_addr, eeprom_val )
print 'eeprom:'
ret=ftdi.read_eeprom( ftdic )
ret=ftdi.eeprom_decode( ftdic ,1)
# close usb
ret = ftdi.usb_close( ftdic )
if ret < 0:
print 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
os._exit( 1 )
ftdi.free( ftdic )