pylibftdi / pylibftdi / util.py

"""
pylibftdi - python wrapper for libftdi

Copyright (c) 2010-2012 Ben Bass <benbass@codedstructure.net>
See LICENSE file for details and (absence of) warranty

pylibftdi: http://bitbucket.org/codedstructure/pylibftdi

"""

# The Bus descriptor class is probably useful outside of
# pylibftdi.  It tries to be to Python what bitfields are
# to C. Its only requirement (which is fairly pylibftdi-ish)
# is a 'device' attribute on the object this is attached
# to, which has a 'port' property which is readable and
# writable.


class Bus(object):
    """
    This class is a descriptor for a bus of a given width starting
    at a given offset (0 = LSB).  The device which does the actual
    reading and writing is assumed to be a BitBangDevice instance
    in the 'device' attribute of the object to which this is attached.
    """
    def __init__(self, offset, width=1):
        self.offset = offset
        self.width = width
        self._mask = ((1 << width) - 1)

    def __get__(self, obj, type):
        val = obj.device.port
        return (val >> self.offset) & self._mask

    def __set__(self, obj, value):
        value = value & self._mask
        # in a multi-threaded environment, would
        # want to ensure following was locked, eg
        # by acquiring a device lock
        val = obj.device.port
        val &= ~(self._mask << self.offset)
        val |= value << self.offset
        obj.device.port = val
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.