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