libftdi-git Archives

Subject: A library to talk to FTDI chips branch, master, updated. v0.17-295-g50280d4

From: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
To: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
Date: Fri, 21 Oct 2011 14:22:57 +0200 (CEST)
The branch, master has been updated
       via  50280d45a136b6915dfdd06843f5f847a7ca468a (commit)
      from  b3f439b157caa175b276d59c991354d90d88d7c6 (commit)


- Log -----------------------------------------------------------------
commit 50280d45a136b6915dfdd06843f5f847a7ca468a
Author: Michel Zou <xantares09@xxxxxxxxxxx>
Date:   Fri Oct 21 14:21:52 2011 +0200

    Python wrapper update
    
    - wrapped ftdi_get_eeprom_buf
    - use of ftdi_usb_find_all is easier
    - useless usb_get_strings size args
    - fixed possible memory leak from wrapping ftdi_new

-----------------------------------------------------------------------

Summary of changes:
 bindings/ftdi.i             |   37 +++++++++++++++++++++++++----------
 examples/python/complete.py |   44 +++++++++++++++++++++++-------------------
 examples/python/simple.py   |    5 +++-
 3 files changed, 54 insertions(+), 32 deletions(-)

diff --git a/bindings/ftdi.i b/bindings/ftdi.i
index edad474..54a45e6 100644
--- a/bindings/ftdi.i
+++ b/bindings/ftdi.i
@@ -2,9 +2,8 @@
 
 %module(docstring="Python interface to libftdi") ftdi
 
-%include "typemaps.i"
-%include "cpointer.i"
-%include "cstring.i"
+%include <typemaps.i>
+%include <cstring.i>
 
 %typemap(in) unsigned char* = char*;
 
@@ -16,19 +15,28 @@
 
 %rename("%(strip:[ftdi_])s") "";
 
-%apply char *OUTPUT { char * manufacturer };
-%apply char *OUTPUT { char * description };
-%apply char *OUTPUT { char * serial };
+%newobject ftdi_new;
+%typemap(newfree) struct ftdi_context *ftdi "ftdi_free($1);";
+%delobject ftdi_free;
+
+%typemap(in,numinputs=0) SWIGTYPE** OUTPUT ($*ltype temp) %{ $1 = &temp; %}
+%typemap(argout) SWIGTYPE** OUTPUT %{ $result = 
SWIG_Python_AppendOutput($result, 
SWIG_NewPointerObj((void*)*$1,$*descriptor,0)); %}
+%apply SWIGTYPE** OUTPUT { struct ftdi_device_list **devlist };
+    int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list 
**devlist,
+                          int vendor, int product);
+%clear struct ftdi_device_list **devlist;
+
+%apply char *OUTPUT { char * manufacturer, char * description, char * serial };
 %cstring_bounded_output( char * manufacturer, 256 );
 %cstring_bounded_output( char * description, 256 );
 %cstring_bounded_output( char * serial, 256 );
+%typemap(default,noblock=1) int mnf_len, int desc_len, int serial_len { $1 = 
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;
+%clear char * manufacturer, char * description, char * serial;
+%clear int mnf_len, int desc_len, int serial_len;
 
 %apply char *OUTPUT { unsigned char *buf };
     int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int 
size);
@@ -55,6 +63,15 @@
     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 *buf };
+%typemap(in,numinputs=0) unsigned char *buf(char temp[FTDI_MAX_EEPROM_SIZE]) 
%{ $1 = ($1_ltype) temp; %}
+%typemap(freearg,match="in") unsigned char *buf "";
+%typemap(argout,fragment="SWIG_FromCharPtrAndSize") unsigned char *buf %{ 
$result = SWIG_Python_AppendOutput($result, 
SWIG_FromCharPtrAndSize((char*)$1,FTDI_MAX_EEPROM_SIZE)); %}
+%typemap(default,noblock=1) int size { $1 = 128; }
+    int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, 
int size);
+%clear unsigned char *buf;
+%clear int size;
+
 %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;
@@ -72,5 +89,3 @@
 %{
 #include <ftdi_i.h>
 %}
-
-%pointer_functions(struct ftdi_device_list *, device_listpp)
diff --git a/examples/python/complete.py b/examples/python/complete.py
index 2bab44d..cf60d4d 100644
--- a/examples/python/complete.py
+++ b/examples/python/complete.py
@@ -17,26 +17,24 @@ 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 )
+ret, devlist = ftdi.usb_find_all( ftdic, 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 )
+curnode = devlist
+i = 0
+while( curnode != None ):
+    ret, manufacturer, description, serial = ftdi.usb_get_strings( ftdic, 
curnode.dev )
     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 )
+    print 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( 
i, manufacturer, description, serial )
+    curnode = curnode.next
+    i += 1
 
 # open usb
 ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 )
@@ -57,8 +55,8 @@ 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
+    print 'enabling bit #%d (0x%02x)' % (i, val)
     ftdi.write_data( ftdic, chr(val), 1 )
     time.sleep ( 1 )
 ftdi.disable_bitbang( ftdic )
@@ -73,13 +71,19 @@ 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)
-
-
+if (ret==0):
+    print 'eeprom @ %d: 0x%04x\n' % ( eeprom_addr, eeprom_val )
+
+print 'complete eeprom:'
+ret = ftdi.read_eeprom( ftdic )
+ret, eeprom = ftdi.get_eeprom_buf ( ftdic )
+if ( ret == 0 ):
+    for i in range( 128 ):
+        print '%02x' % ord( eeprom[i] ),
+        if ( i % 8 == 7 ):
+            print ''
+
+            
 # close usb
 ret = ftdi.usb_close( ftdic )
 if ret < 0:
diff --git a/examples/python/simple.py b/examples/python/simple.py
index 48748e9..ecd5c2f 100644
--- a/examples/python/simple.py
+++ b/examples/python/simple.py
@@ -1,3 +1,6 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
 """Python example program.
 
 Small program to demonstrate the usage
@@ -17,7 +20,7 @@ def main():
                (version_info.major, version_info.minor, version_info.micro,
                version_info.version_str, version_info.snapshot_str))
 
-    print("ftdi.usb_open(): %d" % ftdi.usb_open(context, 0x0403, 0x6001))
+    print("ftdi.usb_open(): %d" % ftdi.usb_open(context, 0x0403, 0x6010))
     print("ftdi.set_baudrate(): %d" % ftdi.set_baudrate(context, 9600))
 
     ftdi.free(context)


hooks/post-receive
-- 
A library to talk to FTDI chips

--
libftdi-git - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi-git+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread
  • A library to talk to FTDI chips branch, master, updated. v0.17-295-g50280d4, libftdi-git <=