| 1 |
|
| 2 |
|
| 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 |
| 12 |
import sys |
| 13 |
import ftdi1 as ftdi |
| 14 |
import time |
| 15 |
|
| 16 |
|
| 17 |
print ('version: %s\n' % ftdi.__version__) |
| 18 |
|
| 19 |
|
| 20 |
ftdic = ftdi.new() |
| 21 |
if ftdic == 0: |
| 22 |
print('new failed: %d' % ret) |
| 23 |
os._exit(1) |
| 24 |
|
| 25 |
|
| 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 |
|
| 30 |
if ret < 0: |
| 31 |
print('ftdi_usb_find_all failed: %d (%s)' % |
| 32 |
(ret, ftdi.get_error_string(ftdic))) |
| 33 |
os._exit(1) |
| 34 |
print('devices: %d' % ret) |
| 35 |
curnode = devlist |
| 36 |
i = 0 |
| 37 |
while(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 |
|
| 50 |
ret = ftdi.usb_open(ftdic, 0x0403, 0x6001) |
| 51 |
if 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 |
|
| 58 |
ret = ftdi.set_bitmode(ftdic, 0xff, ftdi.BITMODE_BITBANG) |
| 59 |
if ret < 0: |
| 60 |
print('Cannot enable bitbang') |
| 61 |
os._exit(1) |
| 62 |
print('turning everything on') |
| 63 |
ftdi.write_data(ftdic, chr(0xff), 1) |
| 64 |
time.sleep(1) |
| 65 |
print('turning everything off\n') |
| 66 |
ftdi.write_data(ftdic, chr(0x00), 1) |
| 67 |
time.sleep(1) |
| 68 |
for 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) |
| 73 |
ftdi.disable_bitbang(ftdic) |
| 74 |
print('') |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ret, pins = ftdi.read_pins(ftdic) |
| 79 |
if (ret == 0): |
| 80 |
if sys.version_info[0] < 3: |
| 81 |
pins = ord(pins) |
| 82 |
else: |
| 83 |
pins = pins[0] |
| 84 |
print('pins: 0x%x' % pins) |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
ret, chipid = ftdi.read_chipid(ftdic) |
| 89 |
if (ret == 0): |
| 90 |
print('chip id: %x\n' % chipid) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
eeprom_addr = 1 |
| 95 |
ret, eeprom_val = ftdi.read_eeprom_location(ftdic, eeprom_addr) |
| 96 |
if (ret == 0): |
| 97 |
print('eeprom @ %d: 0x%04x\n' % (eeprom_addr, eeprom_val)) |
| 98 |
|
| 99 |
print('eeprom:') |
| 100 |
ret = ftdi.read_eeprom(ftdic) |
| 101 |
size = 128 |
| 102 |
ret, eeprom = ftdi.get_eeprom_buf(ftdic, size) |
| 103 |
if (ret == 0): |
| 104 |
for i in range(size): |
| 105 |
octet = eeprom[i] |
| 106 |
if sys.version_info[0] < 3: |
| 107 |
octet = ord(octet) |
| 108 |
sys.stdout.write('%02x ' % octet) |
| 109 |
if (i % 8 == 7): |
| 110 |
print('') |
| 111 |
print('') |
| 112 |
|
| 113 |
|
| 114 |
ret = ftdi.usb_close(ftdic) |
| 115 |
if ret < 0: |
| 116 |
print('unable to close ftdi device: %d (%s)' % |
| 117 |
(ret, ftdi.get_error_string(ftdic))) |
| 118 |
os._exit(1) |
| 119 |
|
| 120 |
print ('device closed') |
| 121 |
ftdi.free(ftdic) |