示例#1
0
    def __select_port(self, n):
        """ selects a port.

        Using an int in [0-7], activte the chip select for
        device n.

        Args:
            n (int): the port corresponding to the device to activate.
        """
        # convert int to its binary representation.
        msb, mb, lsb = cream.num2bin(n)
        # set GPIOs to output bit pattern.
        GPIO.output(self._PORTS['msb'], msb)
        GPIO.output(self._PORTS['mb'], mb)
        GPIO.output(self._PORTS['lsb'], lsb)        
#Test SPI transactions

spi = spidev.SpiDev()
spi.open(0,0)

def readADC(nADC):
    if (nADC > 7) or (nADC < 0):
        return -1
    r = spi.xfer2([1, (8+nADC)<<4, 0])
    out = ((r[1]&3) << 8) + r[2]
    return out

k = 0
while True:
    msb, mb, lsb = cream.num2bin(k)
    GPIO.output(A, lsb)
    GPIO.output(B, mb)
    GPIO.output(C, msb)
    D = [0, 0]
    for n in xrange(0, 2):
        D[n] = readADC(n)
    print "\tUP: " + str(D[0]) + "\tRIGHT: " + str(D[1])
    time.sleep(0.5)
    print str(k)
    k = k + 1
    if k == 8:
        k = 0