Commits

Ben Bass  committed c0f1a4c

initial files

  • Participants

Comments (0)

Files changed (4)

+Copyright (c) 2010 Ben Bass
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+
+
+http://www.opensource.org/licenses/mit-license.php
+pylibftdi
+=========
+
+Copyright (c) 2010 Ben Bass <benbass@codedstructure.net>
+
+All rights reserved.
+
+
+License information
+-------------------
+
+See the file "LICENSE" for information terms & conditions
+for usage, and a DISCLAIMER OF ALL WARRANTIES.
+
+All trademarks referenced herein are property of their respective
+holders.
+
+libFTDI itself is developed by Intra2net AG.  No association with
+Intra2net is claimed or implied, but I have found their library
+helpful and had fun with it...
+
+History
+-------
+At least two other open-source Python FTDI wrappers exist, and each
+of these may be best for some projects.
+
+ftd2xx - http://pypi.python.org/pypi/ftd2xx
+ - ctypes binding to FTDI's own D2XX driver
+pyftdi - http://git.marcansoft.com/?p=pyftdi.git
+ - a C extension libftdi binding
+
+pylibftdi exists in the gap between these two projects; ftd2xx uses
+the (closed-source) D2XX driver, but provides a high-level Python
+interface, while pyftdi works with libftdi but is very low-level.
+The aim for pylibftdi is to work with the libftdi, but to provide
+a high-level Pythonic interface.  Various wrappers and utility
+functions are also part of the distribution; following Python's
+batteries included approach, there are various interesting devices
+supported out-of-the-box.
+
+
+Changes
+-------
+
+0.1
+ * first release. Tested with libftdi 0.18 on Mac OS X 10.6 and Linux
+  (stock EEEPC 701 Xandros Linux, Ubuntu 10.04)

File pylibftdi/__init__.py

+"""
+pylibftdi - python wrapper for libftdi
+
+
+pylibftdi: http://bitbucket.org/codedstructure/pylibftdi
+
+
+libftdi can be found at:
+ http://www.intra2net.com/en/developer/libftdi/
+Neither libftdi or Intra2net are associated with this project;
+if something goes wrong here, it's almost definitely my fault
+rather than a problem with the libftdi library.
+"""
+
+__VERSION__ = "0.1"
+__AUTHOR__ = "Ben Bass"
+
+
+
+__ALL__ = ['Driver', 'BitBangDriver', 'ALL_OUTPUTS', 'ALL_INPUTS']
+
+from ctypes import *
+from ctypes.util import find_library
+
+class DeadParrot(object):
+    def __getattribute__(self, key):
+        # perhaps we should produce an appropriate quote at random...
+        raise TypeError("This object is no more!")
+    def __setattr__(self, key, val):
+        raise TypeError("This object is no more!")
+    def __call__(self, *o, **kw):
+        raise TypeError("This object is no more!")
+
+
+class Driver(object):
+    def __init__(self):
+        self.ctx = None
+        self.fdll = None
+        self.opened = False
+
+    def open(self):
+        "open connection to a FTDI device"
+        if self.opened:
+            return self
+        # TODO: provide parameter to select required device
+        # (if multiple are attached)
+        self.fdll = CDLL(find_library('libftdi'))
+        # most args/return types are fine with the implicit
+        # int/void* which ctypes uses, but some need setting here
+        self.fdll.ftdi_get_error_string.restype = c_char_p
+        # sizeof(struct ftdi_context) seems to be 112 on x86_64, 84 on i386
+        # provide a generous 1K buffer for (hopefully) all possibles...
+        self.ctx = create_string_buffer(1024)
+        self.fdll.ftdi_init(byref(self.ctx))
+        self.fdll.ftdi_usb_open(byref(self.ctx), 0x0403, 0x6001)
+        self.opened = True
+        return self
+
+    def close(self):
+        "close our connection, free resources"
+        self.opened = False
+        self.fdll.ftdi_usb_close(byref(self.ctx))
+        self.fdll.ftdi_deinit(byref(self.ctx))
+        self.fdll = DeadParrot()
+
+    def read(self, length):
+        "read a string of upto length bytes from the FTDI device"
+        z = create_string_buffer(length)
+        self.fdll.ftdi_read_data(byref(self.ctx), byref(z), length)
+        return z.value
+
+    def write(self, data):
+        "write given data string to the FTDI device"
+        z = create_string_buffer(data)
+        return self.fdll.ftdi_write_data(byref(self.ctx), byref(z), len(data))
+
+    def get_error(self):
+        "return error string from libftdi driver"
+        return self.fdll.ftdi_get_error_string(self.ctx)
+
+    def __enter__(self):
+        "support for context manager"
+        return self.open()
+
+    def __exit__(self, exc_type, exc_val, tb):
+        "support for context manager"
+        self.close()
+
+ALL_OUTPUTS = 0xFF
+ALL_INPUTS = 0x00
+
+class BitBangDriver(Driver):
+    def __init__(self, direction = ALL_OUTPUTS):
+        super(BitBangDriver, self).__init__()
+        self.direction = direction
+
+    @property
+    def direction(self):
+        return self._direction
+    @direction.setter
+    def direction(self, dir):
+        self._direction = dir
+        if self.opened:
+            self.fdll.ftdi_set_bitmode(byref(self.ctx), dir, 0x01)
+
+    def open(self):
+        super(BitBangDriver, self).open()
+        if self._direction:
+            self.direction = self._direction
+        return self
+
+    def read(self):
+        return ord(super(BitBangDriver, self).read(1)[0])
+
+    def write(self, value):
+        return super(BitBangDriver, self).write(chr(value))
+#!/usr/bin/python
+
+from distutils.core import setup
+
+setup(
+    name="pylibftdi",
+    version="0.1",
+    description="Pythonic interface to FTDI devices using libftdi",
+    author="Ben Bass",
+    author_email="benbass@codedstructure.net",
+    url="http://bitbucket.org/codedstructure/pylibftdi",
+    packages=["pylibftdi"],
+    classifiers=[
+        "Development Status :: 3 - Alpha",
+        "Environment :: Console",
+        "Intended Audience :: Developers",
+        "License :: OSI Approved :: MIT License",
+        "Operating System :: Microsoft :: Windows",
+        "Operating System :: POSIX",
+        "Programming Language :: Python",
+        "Topic :: Scientific/Engineering",
+        "Topic :: Software Development :: Embedded Systems",
+        "Topic :: System :: Hardware"
+    ]
+)