2 # -*- coding: utf-8 -*-
4 """Python example program.
6 Complete program to demonstrate the usage
7 of the swig generated python wrapper
9 You need to build and install the wrapper first"""
19 print( 'new failed: %d', ret )
22 # try to list ftdi devices 0x6010 or 0x6001
23 ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6010 )
25 ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6001)
28 print( 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
30 print( 'Number of FTDI devices found: %d\n' % ret )
33 while( curnode != None ):
34 ret, manufacturer, description, serial = ftdi.usb_get_strings( ftdic, curnode.dev )
36 print( 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
38 print( 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) )
39 curnode = curnode.next
43 ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 )
45 print( 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
50 ret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG )
52 print( 'Cannot enable bitbang' )
54 print( 'turning everything on' )
55 ftdi.write_data( ftdic, chr(0xff), 1 )
57 print( 'turning everything off\n' )
58 ftdi.write_data( ftdic, chr(0x00), 1 )
62 print( 'enabling bit #%d (0x%02x)' % (i, val) )
63 ftdi.write_data( ftdic, chr(val), 1 )
65 ftdi.disable_bitbang( ftdic )
70 # FIXME: read_pins fails with python3, so I disabled it for now
71 # tested on ubuntu 12.04 ( python3.2.3 / swig 2.0.4 )
72 if (sys.version_info[0]<3):
73 ret, pins = ftdi.read_pins( ftdic )
75 print( 'pins: %02x' % ord( pins[0] ) )
79 ret, chipid = ftdi.read_chipid( ftdic )
81 print( 'chip id: %X\n' % chipid )
86 ret, eeprom_val = ftdi.read_eeprom_location( ftdic, eeprom_addr )
88 print( 'eeprom @ %d: 0x%04x\n' % ( eeprom_addr, eeprom_val ) )
91 ret = ftdi.read_eeprom( ftdic )
93 ret, eeprom = ftdi.get_eeprom_buf ( ftdic, size )
95 for i in range( size ):
96 if isinstance(eeprom[i], str):
97 octet = ord( eeprom[i] ) # python2
99 octet = eeprom[i] # python3
100 sys.stdout.write( '%02x ' % octet )
107 ret = ftdi.usb_close( ftdic )
109 print( 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
112 print ('device closed')