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