Skip to content

Commit 9a9de94

Browse files
committed
Merge branch 'steelee-master'
2 parents e9260d1 + 564b131 commit 9a9de94

File tree

4 files changed

+123
-2
lines changed

4 files changed

+123
-2
lines changed

Adafruit_GPIO/GPIO.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,63 @@ def cleanup(self, pin=None):
305305
else:
306306
self.bbio_gpio.cleanup(pin)
307307

308+
class AdafruitMinnowAdapter(BaseGPIO):
309+
"""GPIO implementation for the Minnowboard + MAX using the mraa library"""
310+
311+
def __init__(self,mraa_gpio):
312+
self.mraa_gpio = mraa_gpio
313+
# Define mapping of Adafruit GPIO library constants to mraa constants
314+
self._dir_mapping = { OUT: self.mraa_gpio.DIR_OUT,
315+
IN: self.mraa_gpio.DIR_IN }
316+
self._pud_mapping = { PUD_OFF: self.mraa_gpio.MODE_STRONG,
317+
PUD_UP: self.mraa_gpio.MODE_HIZ,
318+
PUD_DOWN: self.mraa_gpio.MODE_PULLDOWN }
319+
self._edge_mapping = { RISING: self.mraa_gpio.EDGE_RISING,
320+
FALLING: self.mraa_gpio.EDGE_FALLING,
321+
BOTH: self.mraa_gpio.EDGE_BOTH }
322+
323+
def setup(self,pin,mode):
324+
"""Set the input or output mode for a specified pin. Mode should be
325+
either DIR_IN or DIR_OUT.
326+
"""
327+
self.mraa_gpio.Gpio.dir(self.mraa_gpio.Gpio(pin),self._dir_mapping[mode])
328+
329+
def output(self,pin,value):
330+
"""Set the specified pin the provided high/low value. Value should be
331+
either 1 (ON or HIGH), or 0 (OFF or LOW) or a boolean.
332+
"""
333+
self.mraa_gpio.Gpio.write(self.mraa_gpio.Gpio(pin), value)
334+
335+
def input(self,pin):
336+
"""Read the specified pin and return HIGH/true if the pin is pulled high,
337+
or LOW/false if pulled low.
338+
"""
339+
return self.mraa_gpio.Gpio.read(self.mraa_gpio.Gpio(pin))
340+
341+
def add_event_detect(self, pin, edge, callback=None, bouncetime=-1):
342+
"""Enable edge detection events for a particular GPIO channel. Pin
343+
should be type IN. Edge must be RISING, FALLING or BOTH. Callback is a
344+
function for the event. Bouncetime is switch bounce timeout in ms for
345+
callback
346+
"""
347+
kwargs = {}
348+
if callback:
349+
kwargs['callback']=callback
350+
if bouncetime > 0:
351+
kwargs['bouncetime']=bouncetime
352+
self.mraa_gpio.Gpio.isr(self.mraa_gpio.Gpio(pin), self._edge_mapping[edge], **kwargs)
353+
354+
def remove_event_detect(self, pin):
355+
"""Remove edge detection for a particular GPIO channel. Pin should be
356+
type IN.
357+
"""
358+
self.mraa_gpio.Gpio.isrExit(self.mraa_gpio.Gpio(pin))
359+
360+
def wait_for_edge(self, pin, edge):
361+
"""Wait for an edge. Pin should be type IN. Edge must be RISING,
362+
FALLING or BOTH.
363+
"""
364+
self.bbio_gpio.wait_for_edge(self.mraa_gpio.Gpio(pin), self._edge_mapping[edge])
308365

309366
def get_platform_gpio(**keywords):
310367
"""Attempt to return a GPIO instance for the platform which the code is being
@@ -320,5 +377,8 @@ def get_platform_gpio(**keywords):
320377
elif plat == Platform.BEAGLEBONE_BLACK:
321378
import Adafruit_BBIO.GPIO
322379
return AdafruitBBIOAdapter(Adafruit_BBIO.GPIO, **keywords)
380+
elif plat == Platform.MINNOWBOARD:
381+
import mraa
382+
return AdafruitMinnowAdapter(mraa, **keywords)
323383
elif plat == Platform.UNKNOWN:
324384
raise RuntimeError('Could not determine platform.')

Adafruit_GPIO/Platform.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
UNKNOWN = 0
2626
RASPBERRY_PI = 1
2727
BEAGLEBONE_BLACK = 2
28-
28+
MINNOWBOARD = 3
2929

3030
def platform_detect():
3131
"""Detect if running on the Raspberry Pi or Beaglebone Black and return the
@@ -45,6 +45,15 @@ def platform_detect():
4545
return BEAGLEBONE_BLACK
4646
elif plat.lower().find('armv7l-with-glibc2.4') > -1:
4747
return BEAGLEBONE_BLACK
48+
49+
# Handle Minnowboard
50+
# Assumption is that mraa is installed
51+
try:
52+
import mraa
53+
if mraa.getPlatformName()=='MinnowBoard MAX':
54+
return MINNOWBOARD
55+
except ImportError:
56+
pass
4857

4958
# Couldn't figure out the platform, just return unknown.
5059
return UNKNOWN

Adafruit_GPIO/SPI.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,58 @@ def transfer(self, data):
9494
"""
9595
return bytearray(self._device.xfer2(data))
9696

97+
class SpiDevMraa(object):
98+
"""Hardware SPI implementation with the mraa library on Minnowboard"""
99+
def __init__(self, port, device, max_speed_hz=500000):
100+
import mraa
101+
self._device = mraa.Spi(0)
102+
self._device.mode(0)
103+
104+
def set_clock_hz(self, hz):
105+
"""Set the speed of the SPI clock in hertz. Note that not all speeds
106+
are supported and a lower speed might be chosen by the hardware.
107+
"""
108+
self._device.frequency(hz)
109+
110+
def set_mode(self,mode):
111+
"""Set SPI mode which controls clock polarity and phase. Should be a
112+
numeric value 0, 1, 2, or 3. See wikipedia page for details on meaning:
113+
http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
114+
"""
115+
if mode < 0 or mode > 3:
116+
raise ValueError('Mode must be a value 0, 1, 2, or 3.')
117+
self._device.mode(mode)
118+
119+
def set_mode(self,mode):
120+
"""Set SPI mode which controls clock polarity and phase. Should be a
121+
numeric value 0, 1, 2, or 3. See wikipedia page for details on meaning:
122+
http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
123+
"""
124+
if mode < 0 or mode > 3:
125+
raise ValueError('Mode must be a value 0, 1, 2, or 3.')
126+
self._device.mode(mode)
127+
128+
def set_bit_order(self, order):
129+
"""Set order of bits to be read/written over serial lines. Should be
130+
either MSBFIRST for most-significant first, or LSBFIRST for
131+
least-signifcant first.
132+
"""
133+
if order == MSBFIRST:
134+
self._device.lsbmode(False)
135+
elif order == LSBFIRST:
136+
self._device.lsbmode(True)
137+
else:
138+
raise ValueError('Order must be MSBFIRST or LSBFIRST.')
139+
140+
def close(self):
141+
"""Close communication with the SPI device."""
142+
self._device.Spi()
143+
144+
def write(self, data):
145+
"""Half-duplex SPI write. The specified array of bytes will be clocked
146+
out the MOSI line.
147+
"""
148+
self._device.write(bytearray(data))
97149

98150
class BitBang(object):
99151
"""Software-based implementation of the SPI protocol over GPIO pins."""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
requires.append('spidev')
1212

1313
setup(name = 'Adafruit_GPIO',
14-
version = '0.9.0',
14+
version = '0.9.1',
1515
author = 'Tony DiCola',
1616
author_email = 'tdicola@adafruit.com',
1717
description = 'Library to provide a cross-platform GPIO interface on the Raspberry Pi and Beaglebone Black using the RPi.GPIO and Adafruit_BBIO libraries.',

0 commit comments

Comments
 (0)