Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit 5482c70

Browse files
committed
Fix bug with failing to close FT232H on exit.
1 parent 91615ca commit 5482c70

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Adafruit_GPIO/FT232H.py

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ def __init__(self, vid=FT232H_VID, pid=FT232H_PID):
103103
self._ctx = ftdi.new()
104104
if self._ctx == 0:
105105
raise RuntimeError('ftdi_new failed! Is libftdi1 installed?')
106+
# Register handler to close and cleanup FTDI context on program exit.
107+
atexit.register(self.close)
108+
# Open USB connection for specified VID and PID.
106109
self._check(ftdi.usb_open, vid, pid)
107110
# Reset device.
108111
self._check(ftdi.usb_reset)
@@ -117,6 +120,12 @@ def __init__(self, vid=FT232H_VID, pid=FT232H_PID):
117120
self._direction = 0x0000
118121
self._level = 0x0000
119122

123+
def close(self):
124+
"""Close the FTDI device. Will be automatically called when the program ends."""
125+
if self._ctx is not None:
126+
ftdi.free(self._ctx)
127+
self._ctx = None
128+
120129
def _write(self, string):
121130
"""Helper function to call write_data on the provided FTDI device and
122131
verify it succeeds.

0 commit comments

Comments
 (0)