Skip to content

Commit

Permalink
exclusion lock and note about myself in header, it's not a direct copy.
Browse files Browse the repository at this point in the history
  • Loading branch information
eastein committed Aug 28, 2012
1 parent ba4c917 commit 85e0a7e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion mcp3008.py
Expand Up @@ -10,8 +10,11 @@
Written by Limor Fried, Kevin Townsend and Mikey Sklar for Adafruit Industries. BSD license, all text above must be included in any redistribution
To download, we suggest logging into your Pi with Internet accessibility and typing: git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
Refactored into a class structure and pluggable access objects added by Eric Stein
"""
import RPi.GPIO as GPIO
import threading

# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
Expand Down Expand Up @@ -63,6 +66,8 @@ def read(self) :
return TMP36.c2k(c)

class MCP3008(object) :
SPI_LOCK = threading.Lock()

def __init__(self, mv_aref) :
self.mv_aref = mv_aref
self.channels = [None] * 8
Expand All @@ -74,7 +79,7 @@ def __init__(self, mv_aref) :
GPIO.setup(SPICS, GPIO.OUT)

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(self, adcnum, clockpin=SPICLK, mosipin=SPIMOSI, misopin=SPIMISO, cspin=SPICS):
def _readadc(self, adcnum, clockpin=SPICLK, mosipin=SPIMOSI, misopin=SPIMISO, cspin=SPICS):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
Expand Down Expand Up @@ -109,6 +114,13 @@ def readadc(self, adcnum, clockpin=SPICLK, mosipin=SPIMOSI, misopin=SPIMISO, csp

return adcout * ( self.mv_aref / 1024.0)

def readadc(self, *args, **kwargs) :
self.SPI_LOCK.acquire(True)
try :
return self._readadc(*args, **kwargs)
finally :
self.SPI_LOCK.release()

def setup_channel(self, channel_number, pinobject) :
pinobject.setup_mcp3008(self, channel_number)
self.channels[channel_number] = pinobject

0 comments on commit 85e0a7e

Please sign in to comment.