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