Add pins database

Related to @lurch's comments on #148, this PR contains a database of
pins for each Pi revision, along with various other bits of miscellany
(I might've gotten a bit carried away here...).

Any corrections/extensions welcome!
This commit is contained in:
Dave Jones
2016-04-03 19:53:59 +01:00
parent 365e309af6
commit 50946b417c
9 changed files with 775 additions and 26 deletions

View File

@@ -17,6 +17,7 @@ from threading import Thread, Event, Lock
from collections import Counter
from . import Pin, PINS_CLEANUP
from .data import pi_info
from ..exc import (
PinInvalidPull,
PinInvalidEdges,
@@ -198,10 +199,14 @@ class NativePin(Pin):
GPIO_PULL_UP_NAMES = {v: k for (k, v) in GPIO_PULL_UPS.items()}
GPIO_EDGES_NAMES = {v: k for (k, v) in GPIO_EDGES.items()}
PI_INFO = None
def __new__(cls, number):
if not cls._PINS:
cls._MEM = GPIOMemory()
PINS_CLEANUP.append(cls._MEM.close)
if cls.PI_INFO is None:
cls.PI_INFO = pi_info()
if not (0 <= number < 54):
raise ValueError('invalid pin %d specified (must be 0..53)' % number)
try:
@@ -230,7 +235,7 @@ class NativePin(Pin):
self._change_thread = None
self._change_event = Event()
self.function = 'input'
self.pull = 'up' if number in (2, 3) else 'floating'
self.pull = 'up' if cls.PI_INFO.pulled_up('GPIO%d' % number) else 'floating'
self.bounce = None
self.edges = 'both'
return self
@@ -245,7 +250,7 @@ class NativePin(Pin):
def close(self):
self.when_changed = None
self.function = 'input'
self.pull = 'up' if self.number in (2, 3) else 'floating'
self.pull = 'up' if self.PI_INFO.pulled_up('GPIO%d' % self.number) else 'floating'
def _get_function(self):
return self.GPIO_FUNCTION_NAMES[(self._MEM[self._func_offset] >> self._func_shift) & 7]
@@ -278,7 +283,7 @@ class NativePin(Pin):
def _set_pull(self, value):
if self.function != 'input':
raise PinFixedPull('cannot set pull on non-input pin %r' % self)
if value != 'up' and self._number in (2, 3):
if value != 'up' and self.PI_INFO.pulled_up('GPIO%d' % self.number):
raise PinFixedPull('%r has a physical pull-up resistor' % self)
try:
value = self.GPIO_PULL_UPS[value]