Esempio n. 1
0
class SPIBus(object):
    """SPI bus access."""
    def __init__(self, freq, sc, mo, mi):
        self._spi = SPI(sc, mo, mi)
        if not self._spi.try_lock():
            print("ERROR: busio.SPIBus: no lock for configure()")
        else:
            try:
                self._spi.configure(baudrate=freq)
            finally:
                self._spi.unlock()

    @property
    def bus(self):
        return self._spi

    def write_readinto(self, wbuf, rbuf):
        if self._spi.try_lock():
            try:
                self._spi.write_readinto(wbuf, rbuf)
            finally:
                self._spi.unlock()
Esempio n. 2
0
pin_a1 = DigitalInOut(board.D7)
pin_a1.direction = Direction.OUTPUT
pin_b0 = DigitalInOut(board.D5)
pin_b0.direction = Direction.OUTPUT
pin_b1 = DigitalInOut(board.D6)
pin_b1.direction = Direction.OUTPUT

# The h-bridge takes a PWM input, one for each of A and B.
pin_pwm_a = PWMOut(board.D9, frequency=200000, duty_cycle=0)
pin_pwm_b = PWMOut(board.D4, frequency=200000, duty_cycle=0)

# SPI bus connected to the encoder.
spi = SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
spi.try_lock()
spi.configure(baudrate=10000000, polarity=0, phase=1)
spi.unlock()
spi_cs = DigitalInOut(board.A2)
spi_cs.direction = Direction.OUTPUT


def read_encoder():
    """
    Returns averaged encoder value in the range (0,_MICROSTEPS_PER_REV).
    """
    reading = 0
    for _ in range(_ENCODER_SAMPLES):
        # Send SPI signal to read the raw value from the magnetic encoder.
        spi_cs.value = False
        buf = bytearray(2)
        spi.try_lock()
        spi.write_readinto(b'\xFF\xFF', buf)