From: Dan White Date: Fri, 2 Nov 2012 23:39:58 +0000 (-0500) Subject: Add a .close() method to port interfaces X-Git-Tag: bootrom-initial-submission~47 X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=38f5485f0de5bfdc10dfe2707eb6206e157ea77b;p=430.git Add a .close() method to port interfaces --- 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 """