Skip to content

Commit

Permalink
Added a check for instances and a cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ztime committed Apr 18, 2015
1 parent ee317f0 commit 0374f5d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gpioController.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ class gpioController:
ALLOWED_PINS = []
USED_PINS = []
INITIATED = False
NO_INSTANCES = 0

def __init__(self, pins, boardmode="BCM", showWarnings=False):
# we created a new instance
self.pins = []
self.NO_INSTANCES = self.NO_INSTANCES + 1
#set warnings in io
if showWarnings is False:
io.setwarnings(False)
Expand All @@ -32,20 +36,33 @@ def __init__(self, pins, boardmode="BCM", showWarnings=False):
io.setmode(io.BOARD)
self.INITIATED = True
else:
self.cleanup()
raise ValueError('%s is not an allowed, use "BCM" or "BOARD"' % boardmode)
else: #INITIATED == TRUE
# check that we dont have two conflicting modes
if(boardmode != self.BOARD_MODE):
self.cleanup()
raise ValueError('Board mode is already set to %s , cannot use another mode' % self.BOARD_MODE)
# Check pins
# save pins to a local variable for cleanup
for pin in pins:
if(pin not in self.ALLOWED_PINS):
self.cleanup()
raise ValueError('%s is not an allowed pin for %s' % (pin, self.BOARD_MODE))
if(pin in self.USED_PINS):
self.cleanup()
raise ValueError('Pin %s is already in use by another device' % pin)
# Pin is ready to use
self.USED_PINS.append(pin)

def cleanup():
# cleanup pins so that other things can use it
for pin in self.pins:
self.USED_PINS.remove(pin)
self.NO_INSTANCES = self.NO_INSTANCES - 1
if self.NO_INSTANCES == 0:
io.cleanup()

# Tests
if __name__ == '__main__':

Expand Down

0 comments on commit 0374f5d

Please sign in to comment.