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