Enhance python wrapper
[libftdi] / examples / python / complete.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """Python example program.
5
6 Complete program to demonstrate the usage
7 of the swig generated python wrapper
8
9 You need to build and install the wrapper first"""
10
11 import os
12 import ftdi
13 import time
14
15 # initialize
16 ftdic = ftdi.new()
17 if ftdic == 0:
18   print 'new failed: %d', ret
19   os._exit( 1 )
20
21 # list all devices
22 devlist = ftdi.new_device_listpp()
23 ret = ftdi.usb_find_all( ftdic, devlist, 0x0403, 0x6001 )
24 if ret < 0:
25     print 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
26     os._exit( 1 )
27 print 'Number of FTDI devices found: %d\n' % ret
28 count = ret
29 curdev = devlist
30 for i in range( count ):
31     print 'Checking device: %d' % i
32
33     curnode = ftdi.device_listpp_value( curdev )
34     ret, manufacturer, description, serial = ftdi.usb_get_strings( ftdic, curnode.dev, 128, 128, 128 )
35     if ret < 0:
36         print 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
37         os._exit( 1 )
38     print 'Manufacturer: %s, Description: %s, Serial: %s\n' % ( manufacturer, description, serial )
39     ftdi.device_listpp_assign( curdev, curnode.next )
40
41 # open usb
42 ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 )
43 if ret < 0:
44     print 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
45     os._exit( 1 )
46
47
48 # bitbang
49 ret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG )
50 if ret < 0:
51     print 'Cannot enable bitbang'
52     os._exit( 1 )
53 print 'turning everything on'
54 ftdi.write_data( ftdic, chr(0xff), 1 )
55 time.sleep( 1 )
56 print 'turning everything off\n'
57 ftdi.write_data( ftdic, chr(0x00), 1 )
58 time.sleep( 1 )
59 for i in range( 8 ):
60     print 'enabling bit', i
61     val = 2**i
62     ftdi.write_data( ftdic, chr(val), 1 )
63     time.sleep ( 1 )
64 ftdi.disable_bitbang( ftdic )
65 print ''
66
67
68 # read chip id
69 ret, chipid = ftdi.read_chipid( ftdic )
70 print 'FDTI chip id: %X\n' % chipid
71
72
73 # read eeprom
74 eeprom_addr = 1
75 ret, eeprom_val = ftdi.read_eeprom_location( ftdic, eeprom_addr )
76 print 'eeprom @ %d: 0x%04x\n' % ( eeprom_addr, eeprom_val )
77
78 print 'eeprom:'
79 ret=ftdi.read_eeprom( ftdic )
80 ret=ftdi.eeprom_decode( ftdic ,1)
81
82
83 # close usb
84 ret = ftdi.usb_close( ftdic )
85 if ret < 0:
86     print 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
87     os._exit( 1 )
88 ftdi.free( ftdic )