Fixed read_pins argout typemap being sensitive to encoding.
authorMichel Zou <xantares09@hotmail.com>
Sat, 9 Feb 2013 16:49:03 +0000 (17:49 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 14 Feb 2013 14:52:34 +0000 (15:52 +0100)
bindings/ftdi1.i
examples/python/complete.py

index ba9aca2..1e820dd 100644 (file)
@@ -79,11 +79,13 @@ PyObject* convertString( const char *v, Py_ssize_t len )
 "read_pins(context) -> (return_code, pins)"
 %enddef
 %feature("autodoc", ftdi_read_pins_docstring) ftdi_read_pins;
-%apply char *OUTPUT { unsigned char *pins };
+%typemap(in,numinputs=0) unsigned char *pins ($*ltype temp) %{ $1 = &temp; %}
+%typemap(argout) (unsigned char *pins) %{ $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, 1)); %}
     int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
 %clear unsigned char *pins;
 
-%apply char *OUTPUT { unsigned char *latency };
+%typemap(in,numinputs=0) unsigned char *latency ($*ltype temp) %{ $1 = &temp; %}
+%typemap(argout) (unsigned char *latency) %{ $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, 1)); %}
     int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
 %clear unsigned char *latency;
 
@@ -114,7 +116,7 @@ PyObject* convertString( const char *v, Py_ssize_t len )
 %feature("autodoc", ftdi_read_eeprom_docstring) ftdi_read_eeprom;
 
 %define ftdi_read_chipid_docstring
-"read_pins(context) -> (return_code, chipid)"
+"ftdi_read_chipid(context) -> (return_code, chipid)"
 %enddef
 %feature("autodoc", ftdi_read_chipid_docstring) ftdi_read_chipid;
 %apply int *OUTPUT { unsigned int *chipid };
index 239188c..afa59fa 100644 (file)
@@ -13,10 +13,13 @@ import sys
 import ftdi1 as ftdi
 import time
 
+# version
+print ( 'version: %s\n' % ftdi.__version__  )
+
 # initialize
 ftdic = ftdi.new()
 if ftdic == 0:
-    print( 'new failed: %d', ret )
+    print( 'new failed: %d' % ret )
     os._exit( 1 )
 
 # try to list ftdi devices 0x6010 or 0x6001
@@ -27,7 +30,7 @@ if ret <= 0:
 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 )
+print( 'devices: %d' % ret )
 curnode = devlist
 i = 0
 while( curnode != None ):
@@ -35,7 +38,7 @@ while( curnode != None ):
     if ret < 0:
         print( 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
         os._exit( 1 )
-    print( 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) )
+    print( '#%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) )
     curnode = curnode.next
     i += 1
 
@@ -67,18 +70,19 @@ print( '' )
 
 
 # read pins
-# FIXME: read_pins fails with python3, so I disabled it for now
-# tested on ubuntu 12.04 ( python3.2.3 / swig 2.0.4 )
-if (sys.version_info[0]<3):
-    ret, pins = ftdi.read_pins( ftdic )
-    if ( ret == 0 ):
-        print( 'pins: %02x' % ord( pins[0] ) )
+ret, pins = ftdi.read_pins( ftdic )
+if ( ret == 0 ):
+    if sys.version_info[0] < 3: # python 2
+        pins = ord( pins )
+    else:
+        pins = pins[0]
+    print( 'pins: 0x%x' % pins )
               
 
 # read chip id
 ret, chipid = ftdi.read_chipid( ftdic )
 if (ret==0):
-    print( 'chip id: %X\n' % chipid )
+    print( 'chip id: %x\n' % chipid )
 
 
 # read eeprom
@@ -93,15 +97,13 @@ size = 128
 ret, eeprom = ftdi.get_eeprom_buf ( ftdic, size )
 if ( ret == 0 ):
     for i in range( size ):
-        if isinstance(eeprom[i], str):
-            octet = ord( eeprom[i] ) # python2
-        else:
-            octet = eeprom[i] # python3
+        octet = eeprom[i]
+        if sys.version_info[0] < 3: # python 2
+            octet = ord( octet )
         sys.stdout.write( '%02x ' % octet )
         if ( i % 8 == 7 ):
             print( '' )
 print( '' )
-
             
 # close usb
 ret = ftdi.usb_close( ftdic )