From 38f5485f0de5bfdc10dfe2707eb6206e157ea77b Mon Sep 17 00:00:00 2001 From: Dan White Date: Fri, 2 Nov 2012 18:39:58 -0500 Subject: [PATCH] Add a .close() method to port interfaces --- python-lib/usbio.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/python-lib/usbio.py b/python-lib/usbio.py index 3b8d28b..65c1eab 100644 --- a/python-lib/usbio.py +++ b/python-lib/usbio.py @@ -138,6 +138,25 @@ class I2C(object): self.set_pindir(pindir) self.set_pinstate(pinstate) + def close(self): + """Set all pins to input and release the port. Class is useless after + calling this method. + """ + cmd = chr(self.ftdi.SET_BITS_LOW) + chr(0) + chr(0) + self._raw_write(cmd) + self.ftdi.usb_close(self.context) + + def _raw_write(self, data): + """Write data as + """ + #translate data to a string + if not isinstance(data, str): + data = ''.join(map(chr, data)) + ret = self.ftdi.write_data(self.context, data, len(data)) + if ret < 0: + raise FTDI_Error(self.ftdi.get_error_string(self.context)) + + def _clear_scl(self): self.io[self.scl] = 1 self.ftdi.set_bitmode(self.context, int(self.io), self.ftdi.BITMODE_BITBANG) @@ -405,6 +424,14 @@ class SPI(object): self.ftdi.usb_purge_buffers(self.context) + def close(self): + """Set all pins to input and release the port. Class is useless after + calling this method. + """ + cmd = chr(self.ftdi.SET_BITS_LOW) + chr(0) + chr(0) + self._raw_write(cmd) + self.ftdi.usb_close(self.context) + def _raw_write(self, data): """Write data as """ -- 2.25.1