#import Adafruit_GPIO as GPIO #import Adafruit_GPIO.SPI as SPI class ADS7952(object): """Class to represent an ADS 7952 analog to digital onverter.""" def __init__(self, clk=None, cs=None, miso=None, mosi=None, spi=None, gpio=None): """Initialize MAX31855 device with software SPI on the specified CLK, CS, and DO pins. Alternatively can specify hardware SPI by sending an Adafruit_GPIO.SPI.SpiDev device in the spi parameter.""" self._spi = None # Handle hardware SPI if spi is not None: self._spi = spi elif clk is not None and cs is not None and miso is not None and mosi is not None: # Default to platform GPIO if not provided. if gpio is None: gpio = GPIO.get_platform_gpio() self._spi = SPI.BitBang(gpio, clk, mosi, miso, cs) else: raise ValueError('Must specify either spi for for hardware SPI or clk, cs, miso, and mosi for softwrare SPI!') # Set SPI clock in Hz self._spi.set_clock_hz(500000) self._spi.set_mode(0) self._spi.set_bit_order(SPI.MSBFIRST) def read_adc(self, adc_number): """Read the current value of the specified ADC channel (0-11). The values can range from 0 to 4095 (12-bits).""" # Setting Auto-1 mode command = 0b0100000000000001 resp = self._spi.transfer([command, 0x0, 0x0]) #send to SPI bus command = 0b1001001011000000 resp = self._spi.transfer([command, 0x0, 0x0]) command = 0b0010110000010001 #set Auto-1 mode resp = self._spi.transfer([command, 0x0, 0x0]) assert 0 <= adc_number <= 11, 'ADC number must be a value of 0-11!' command = 0b0000000000000000 #remain in Auto-1 mode resp = self._spi.transfer([command, 0x0, 0x0]) # Parse out the 12 bits of response data and return it. result = (resp[0] & 0x01) << 11 result |= resp[1] >> 3 result |= resp[2] >> 5 return (result & 0x0FFF)