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