CMake: bump the minimal required version to 3.5
[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)' %
32 (ret, ftdi.get_error_string(ftdic)))
33 os._exit(1)
34print('devices: %d' % ret)
35curnode = devlist
36i = 0
37while(curnode != None):
38 ret, manufacturer, description, serial = ftdi.usb_get_strings(
39 ftdic, curnode.dev)
40 if ret < 0:
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))
46 curnode = curnode.next
47 i += 1
48
49# open usb
50ret = ftdi.usb_open(ftdic, 0x0403, 0x6001)
51if ret < 0:
52 print('unable to open ftdi device: %d (%s)' %
53 (ret, ftdi.get_error_string(ftdic)))
54 os._exit(1)
55
56
57# bitbang
58ret = ftdi.set_bitmode(ftdic, 0xff, ftdi.BITMODE_BITBANG)
59if ret < 0:
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('')
75
76
77# read pins
78ret, pins = ftdi.read_pins(ftdic)
79if (ret == 0):
80 if sys.version_info[0] < 3: # python 2
81 pins = ord(pins)
82 else:
83 pins = pins[0]
84 print('pins: 0x%x' % pins)
85
86
87# read chip id
88ret, chipid = ftdi.read_chipid(ftdic)
89if (ret == 0):
90 print('chip id: %x\n' % chipid)
91
92
93# read eeprom
94eeprom_addr = 1
95ret, eeprom_val = ftdi.read_eeprom_location(ftdic, eeprom_addr)
96if (ret == 0):
97 print('eeprom @ %d: 0x%04x\n' % (eeprom_addr, eeprom_val))
98
99print('eeprom:')
100ret = ftdi.read_eeprom(ftdic)
101size = 128
102ret, eeprom = ftdi.get_eeprom_buf(ftdic, size)
103if (ret == 0):
104 for i in range(size):
105 octet = eeprom[i]
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
113# close usb
114ret = ftdi.usb_close(ftdic)
115if ret < 0:
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)