Skip to content

Commit

Permalink
Finish abstracting pin changing
Browse files Browse the repository at this point in the history
  • Loading branch information
dberlin committed Sep 28, 2012
1 parent 5683d1a commit 699c063
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions PCA95XX.py
Expand Up @@ -52,7 +52,7 @@ def _changebit(self, bitmap, bit, value):
def _readandchangepin(self, port, pin, value, currvalue = None):
assert pin >= 0 and pin < self.num_gpios, "Pin number %s is invalid, only 0-%s are valid" % (pin, self.num_gpios)
assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
if not currvalue:
if not currvalue:
if self.num_gpios <= 8:
currvalue = self.bus.read_byte_data(self.address, port)
elif self.num_gpios > 8 and self.num_gpios <= 16:
Expand All @@ -68,21 +68,14 @@ def polarity(self, pin, value):
return self._readandchangepin(POLARITY_PORT, pin, value)

# Set pin to either input or output mode
def config(self, pin, mode):
assert pin >= 0 and pin < self.num_gpios, "Pin number %s is invalid, only 0-%s are valid" % (pin, self.num_gpios)
newdirection = self._changebit(self.direction, pin, mode)
if self.num_gpios <= 8:
self.bus.write_byte_data(self.address, CONFIG_PORT, newdirection)
elif self.num_gpios > 8 and self.num_gpios <= 16:
self.bus.write_word_data(self.address, CONFIG_PORT << 1, newdirection)
self.direction = newdirection
def config(self, pin, mode):
self.direction = self._readandchangepin(CONFIG_PORT, pin, mode, self.direction)
return self.direction

def output(self, pin, value):
assert pin >= 0 and pin < self.num_gpios, "Pin number %s is invalid, only 0-%s are valid" % (pin, self.num_gpios)
assert self.direction & (1 << pin) == 0, "Pin %s not set to output" % pin
self.outputvalue = self._readandchangepin(OUTPUT_PORT, pin, value, self.outputvalue)
return self.outputvalue
self.outputvalue = self._readandchangepin(OUTPUT_PORT, pin, value, self.outputvalue)
return self.outputvalue

def input(self, pin):
assert pin >= 0 and pin < self.num_gpios, "Pin number %s is invalid, only 0-%s are valid" % (pin, self.num_gpios)
Expand Down

0 comments on commit 699c063

Please sign in to comment.