Skip to content

Commit 7ad97ed

Browse files
author
Evan Steele
committed
added support for MinnowBoard
1 parent e9260d1 commit 7ad97ed

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

Adafruit_GPIO/GPIO.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def add_event_detect(self, pin, edge):
9595
"""Enable edge detection events for a particular GPIO channel. Pin
9696
should be type IN. Edge must be RISING, FALLING or BOTH.
9797
"""
98-
raise NotImplementedError
98+
aise NotImplementedError
9999

100100
def remove_event_detect(self, pin):
101101
"""Remove edge detection for a particular GPIO channel. Pin should be
@@ -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+
#probably don't need to import the mraa library again (did it in platform.py)
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,57 @@ 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+
self._device = mraa.Spi()
101+
self._device.mode(0)
102+
103+
def set_clock_hz(self, hz):
104+
"""Set the speed of the SPI clock in hertz. Note that not all speeds
105+
are supported and a lower speed might be chosen by the hardware.
106+
"""
107+
self._device.frequency(hz)
108+
109+
def set_mode(self,mode):
110+
"""Set SPI mode which controls clock polarity and phase. Should be a
111+
numeric value 0, 1, 2, or 3. See wikipedia page for details on meaning:
112+
http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
113+
"""
114+
if mode < 0 or mode > 3:
115+
raise ValueError('Mode must be a value 0, 1, 2, or 3.')
116+
self._device.mode(mode)
117+
118+
def set_mode(self,mode):
119+
"""Set SPI mode which controls clock polarity and phase. Should be a
120+
numeric value 0, 1, 2, or 3. See wikipedia page for details on meaning:
121+
http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
122+
"""
123+
if mode < 0 or mode > 3:
124+
raise ValueError('Mode must be a value 0, 1, 2, or 3.')
125+
self._device.mode(mode)
126+
127+
def set_bit_order(self, order):
128+
"""Set order of bits to be read/written over serial lines. Should be
129+
either MSBFIRST for most-significant first, or LSBFIRST for
130+
least-signifcant first.
131+
"""
132+
if order == MSBFIRST:
133+
self._device.lsbmode(False)
134+
elif order == LSBFIRST:
135+
self._device.lsbmode(True)
136+
else:
137+
raise ValueError('Order must be MSBFIRST or LSBFIRST.')
138+
139+
def close(self):
140+
"""Close communication with the SPI device."""
141+
self._device.Spi()
142+
143+
def write(self, data):
144+
"""Half-duplex SPI write. The specified array of bytes will be clocked
145+
out the MOSI line.
146+
"""
147+
self._device.write(bytearray(data))
97148

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

0 commit comments

Comments
 (0)