Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit 82bb799

Browse files
committed
Initial commit.
1 parent b63b4b2 commit 82bb799

File tree

9 files changed

+1145
-0
lines changed

9 files changed

+1145
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

Adafruit_GPIO/GPIO.py

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Copyright (c) 2014 Adafruit Industries
2+
# Author: Tony DiCola
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
22+
import platform
23+
24+
25+
OUT = 0
26+
IN = 1
27+
HIGH = True
28+
LOW = False
29+
30+
31+
class BaseGPIO(object):
32+
"""Base class for implementing simple digital IO for a platform.
33+
Implementors are expected to subclass from this and provide an implementation
34+
of the setup, output, and input functions."""
35+
36+
def setup(self, pin, mode):
37+
"""Set the input or output mode for a specified pin. Mode should be
38+
either OUT or IN."""
39+
raise NotImplementedError
40+
41+
def output(self, pin, value):
42+
"""Set the specified pin the provided high/low value. Value should be
43+
either HIGH/LOW or a boolean (true = high)."""
44+
raise NotImplementedError
45+
46+
def input(self, pin):
47+
"""Read the specified pin and return HIGH/true if the pin is pulled high,
48+
or LOW/false if pulled low."""
49+
raise NotImplementedError
50+
51+
def set_high(self, pin):
52+
"""Set the specified pin HIGH."""
53+
self.output(pin, HIGH)
54+
55+
def set_low(self, pin):
56+
"""Set the specified pin LOW."""
57+
self.output(pin, LOW)
58+
59+
def is_high(self, pin):
60+
"""Return true if the specified pin is pulled high."""
61+
return self.input(pin) == HIGH
62+
63+
def is_low(self, pin):
64+
"""Return true if the specified pin is pulled low."""
65+
return self.input(pin) == LOW
66+
67+
68+
class RPiGPIOAdapter(BaseGPIO):
69+
"""GPIO implementation for the Raspberry Pi using the RPi.GPIO library."""
70+
71+
def __init__(self, rpi_gpio, mode=None):
72+
self.rpi_gpio = rpi_gpio
73+
# Suppress warnings about GPIO in use.
74+
rpi_gpio.setwarnings(False)
75+
if mode == rpi_gpio.BOARD or mode == rpi_gpio.BCM:
76+
rpi_gpio.setmode(mode)
77+
elif mode is not None:
78+
raise ValueError('Unexpected value for mode. Must be BOARD or BCM.')
79+
else:
80+
# Default to BCM numbering if not told otherwise.
81+
rpi_gpio.setmode(rpi_gpio.BCM)
82+
83+
def setup(self, pin, mode):
84+
"""Set the input or output mode for a specified pin. Mode should be
85+
either OUTPUT or INPUT.
86+
"""
87+
self.rpi_gpio.setup(pin, self.rpi_gpio.IN if mode == IN else \
88+
self.rpi_gpio.OUT)
89+
90+
def output(self, pin, value):
91+
"""Set the specified pin the provided high/low value. Value should be
92+
either HIGH/LOW or a boolean (true = high).
93+
"""
94+
self.rpi_gpio.output(pin, value)
95+
96+
def input(self, pin):
97+
"""Read the specified pin and return HIGH/true if the pin is pulled high,
98+
or LOW/false if pulled low.
99+
"""
100+
return self.rpi_gpio.input(pin)
101+
102+
103+
class AdafruitBBIOAdapter(BaseGPIO):
104+
"""GPIO implementation for the Beaglebone Black using the Adafruit_BBIO
105+
library.
106+
"""
107+
108+
def __init__(self, bbio_gpio):
109+
self.bbio_gpio = bbio_gpio
110+
111+
def setup(self, pin, mode):
112+
"""Set the input or output mode for a specified pin. Mode should be
113+
either OUTPUT or INPUT.
114+
"""
115+
self.bbio_gpio.setup(pin, self.bbio_gpio.IN if mode == IN else \
116+
self.bbio_gpio.OUT)
117+
118+
def output(self, pin, value):
119+
"""Set the specified pin the provided high/low value. Value should be
120+
either HIGH/LOW or a boolean (true = high).
121+
"""
122+
self.bbio_gpio.output(pin, value)
123+
124+
def input(self, pin):
125+
"""Read the specified pin and return HIGH/true if the pin is pulled high,
126+
or LOW/false if pulled low.
127+
"""
128+
return self.bbio_gpio.input(pin)
129+
130+
131+
def get_platform_gpio(plat=platform.platform(), **keywords):
132+
"""Attempt to return a GPIO instance for the platform which the code is being
133+
executed on. Currently supports only the Raspberry Pi using the RPi.GPIO
134+
library and Beaglebone Black using the Adafruit_BBIO library. Will throw an
135+
exception if a GPIO instance can't be created for the current platform. The
136+
returned GPIO object is an instance of BaseGPIO.
137+
"""
138+
if plat is None:
139+
raise RuntimeError('Could not determine platform type.')
140+
141+
# TODO: Is there a better way to check if running on BBB or Pi? Relying on
142+
# the architecture name is brittle because new boards running armv6 or armv7
143+
# might come along and conflict with this simple identification scheme.
144+
145+
# Handle Raspberry Pi
146+
# Platform output on Raspbian testing/jessie ~May 2014:
147+
# Linux-3.10.25+-armv6l-with-debian-7.4
148+
if plat.lower().find('armv6l-with-debian') > -1:
149+
import RPi.GPIO
150+
return RPiGPIOAdapter(RPi.GPIO, **keywords)
151+
152+
# Handle Beaglebone Black
153+
# Platform output on Debian ~May 2014:
154+
# Linux-3.8.13-bone47-armv7l-with-debian-7.4
155+
if plat.lower().find('armv7l-with-debian') > -1:
156+
import Adafruit_BBIO.GPIO
157+
return AdafruitBBIOAdapter(Adafruit_BBIO.GPIO, **keywords)
158+
159+
# Couldn't determine platform, raise error.
160+
raise RuntimeError('Unsupported platform: {0}'.format(plat))

0 commit comments

Comments
 (0)