Commit | Line | Data |
---|---|---|
4c5afeb9 MZ |
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 | |
bcf4a08b | 12 | import ftdi1 as ftdi |
4c5afeb9 MZ |
13 | import time |
14 | ||
15 | # initialize | |
16 | ftdic = ftdi.new() | |
17 | if ftdic == 0: | |
abc3a514 MZ |
18 | print( 'new failed: %d', ret ) |
19 | os._exit( 1 ) | |
20 | ||
4c5afeb9 | 21 | # list all devices |
50280d45 | 22 | ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6001 ) |
4c5afeb9 | 23 | if ret < 0: |
abc3a514 | 24 | print( 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) ) |
4c5afeb9 | 25 | os._exit( 1 ) |
abc3a514 | 26 | print( 'Number of FTDI devices found: %d\n' % ret ) |
50280d45 MZ |
27 | curnode = devlist |
28 | i = 0 | |
29 | while( curnode != None ): | |
30 | ret, manufacturer, description, serial = ftdi.usb_get_strings( ftdic, curnode.dev ) | |
4c5afeb9 | 31 | if ret < 0: |
abc3a514 | 32 | print( 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) ) |
4c5afeb9 | 33 | os._exit( 1 ) |
abc3a514 | 34 | print( 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) ) |
50280d45 MZ |
35 | curnode = curnode.next |
36 | i += 1 | |
4c5afeb9 MZ |
37 | |
38 | # open usb | |
39 | ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 ) | |
40 | if ret < 0: | |
abc3a514 | 41 | print( 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) ) |
4c5afeb9 MZ |
42 | os._exit( 1 ) |
43 | ||
44 | ||
45 | # bitbang | |
46 | ret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG ) | |
47 | if ret < 0: | |
abc3a514 | 48 | print( 'Cannot enable bitbang' ) |
4c5afeb9 | 49 | os._exit( 1 ) |
abc3a514 | 50 | print( 'turning everything on' ) |
4c5afeb9 MZ |
51 | ftdi.write_data( ftdic, chr(0xff), 1 ) |
52 | time.sleep( 1 ) | |
abc3a514 | 53 | print( 'turning everything off\n' ) |
4c5afeb9 MZ |
54 | ftdi.write_data( ftdic, chr(0x00), 1 ) |
55 | time.sleep( 1 ) | |
56 | for i in range( 8 ): | |
4c5afeb9 | 57 | val = 2**i |
abc3a514 | 58 | print( 'enabling bit #%d (0x%02x)' % (i, val) ) |
4c5afeb9 MZ |
59 | ftdi.write_data( ftdic, chr(val), 1 ) |
60 | time.sleep ( 1 ) | |
61 | ftdi.disable_bitbang( ftdic ) | |
abc3a514 | 62 | print( '\n' ) |
4c5afeb9 MZ |
63 | |
64 | ||
50d77f8f MZ |
65 | # read pins |
66 | ret, pins = ftdi.read_pins( ftdic ) | |
abc3a514 | 67 | print( 'pins:\n' ) |
50d77f8f | 68 | if ( ret == 0 ): |
abc3a514 MZ |
69 | print( '%02x' % ord( pins[0] ) ) |
70 | print( '\n' ) | |
50d77f8f MZ |
71 | |
72 | ||
4c5afeb9 MZ |
73 | # read chip id |
74 | ret, chipid = ftdi.read_chipid( ftdic ) | |
abc3a514 | 75 | print( 'FDTI chip id: %X\n' % chipid ) |
4c5afeb9 MZ |
76 | |
77 | ||
78 | # read eeprom | |
79 | eeprom_addr = 1 | |
80 | ret, eeprom_val = ftdi.read_eeprom_location( ftdic, eeprom_addr ) | |
50280d45 | 81 | if (ret==0): |
abc3a514 | 82 | print( 'eeprom @ %d: 0x%04x\n' % ( eeprom_addr, eeprom_val ) ) |
50280d45 | 83 | |
abc3a514 | 84 | print( 'complete eeprom:' ) |
50280d45 | 85 | ret = ftdi.read_eeprom( ftdic ) |
50d77f8f MZ |
86 | size = 128 |
87 | ret, eeprom = ftdi.get_eeprom_buf ( ftdic, size ) | |
50280d45 | 88 | if ( ret == 0 ): |
50d77f8f | 89 | for i in range( size ): |
abc3a514 | 90 | print( '%02x' % ord( eeprom[i] ) ) |
50280d45 | 91 | if ( i % 8 == 7 ): |
abc3a514 | 92 | print( '\n' ) |
50280d45 | 93 | |
4c5afeb9 MZ |
94 | # close usb |
95 | ret = ftdi.usb_close( ftdic ) | |
96 | if ret < 0: | |
abc3a514 | 97 | print( 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) ) |
4c5afeb9 MZ |
98 | os._exit( 1 ) |
99 | ftdi.free( ftdic ) |