C++ wrapper: Reset internal USB device pointer on Context::close()
[libftdi] / examples / python / complete.py
CommitLineData
4c5afeb9
MZ
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""Python example program.
5
6Complete program to demonstrate the usage
7of the swig generated python wrapper
8
9You need to build and install the wrapper first"""
10
11import os
12import ftdi
13import time
14
15# initialize
16ftdic = ftdi.new()
17if ftdic == 0:
18 print 'new failed: %d', ret
19 os._exit( 1 )
50280d45
MZ
20
21
4c5afeb9 22# list all devices
50280d45 23ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6001 )
4c5afeb9
MZ
24if ret < 0:
25 print 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
26 os._exit( 1 )
27print 'Number of FTDI devices found: %d\n' % ret
50280d45
MZ
28curnode = devlist
29i = 0
30while( 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
40ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 )
41if 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
47ret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG )
48if ret < 0:
49 print 'Cannot enable bitbang'
50 os._exit( 1 )
51print 'turning everything on'
52ftdi.write_data( ftdic, chr(0xff), 1 )
53time.sleep( 1 )
54print 'turning everything off\n'
55ftdi.write_data( ftdic, chr(0x00), 1 )
56time.sleep( 1 )
57for 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 )
62ftdi.disable_bitbang( ftdic )
63print ''
64
65
66# read chip id
67ret, chipid = ftdi.read_chipid( ftdic )
68print 'FDTI chip id: %X\n' % chipid
69
70
71# read eeprom
72eeprom_addr = 1
73ret, eeprom_val = ftdi.read_eeprom_location( ftdic, eeprom_addr )
50280d45
MZ
74if (ret==0):
75 print 'eeprom @ %d: 0x%04x\n' % ( eeprom_addr, eeprom_val )
76
77print 'complete eeprom:'
78ret = ftdi.read_eeprom( ftdic )
79ret, eeprom = ftdi.get_eeprom_buf ( ftdic )
80if ( ret == 0 ):
81 for i in range( 128 ):
82 print '%02x' % ord( eeprom[i] ),
83 if ( i % 8 == 7 ):
84 print ''
85
86
4c5afeb9
MZ
87# close usb
88ret = ftdi.usb_close( ftdic )
89if ret < 0:
90 print 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) )
91 os._exit( 1 )
92ftdi.free( ftdic )