def switch_to_output(
        self,
        value: bool = False,
        drive_mode: DriveMode = DriveMode.PUSH_PULL,
        #drive_mode: digitalio.DriveMode = digitalio.DriveMode.PUSH_PULL,
    ) -> None:
        """Set the drive mode and value and then switch to writing out digital
          values.

          :param bool value: default value to set upon switching
          :param ~digitalio.DriveMode drive_mode: drive mode for the output
          """
        data = 1 << self._mask
        struct.pack_into("<I", self._buffer, 0, data)
        if self._id == 3:
            addr = _evo.PORT_Z_DIRSET_ADDR
        elif self._id == 2:
            addr = _evo.PORT_G_DIRSET_ADDR
        elif self._id == 1:
            addr = _evo.PORT_E_DIRSET_ADDR
        else:
            addr = _evo.D2F_DIRSET_ADDR
        _evo.send_evo_write_trans(addr, self._buffer)
        if addr == _evo.D2F_DIRSET_ADDR:
            self._pin.switch_to_output(value, drive_mode)
    def switch_to_input(self, pull: Pull = None) -> None:
        """Set the pull and then switch to read in digital values.

        :param Pull pull: pull configuration for the input

        Example usage::

          import digitalio
          import board

          switch = digitalio.DigitalInOut(board.SLIDE_SWITCH)
          switch.switch_to_input(pull=digitalio.Pull.UP)
          # Or, after switch_to_input
          switch.pull = digitalio.Pull.UP
          print(switch.value)"""
        data = 1 << self._mask
        struct.pack_into("<I", self._buffer, 0, data)
        if self._id == 3:
            addr = _evo.PORT_Z_DIRCLR_ADDR
        elif self._id == 2:
            addr = _evo.PORT_G_DIRCLR_ADDR
        elif self._id == 1:
            addr = _evo.PORT_E_DIRCLR_ADDR
        else:
            addr = _evo.D2F_DIRCLR_ADDR
        _evo.send_evo_write_trans(addr, self._buffer)
        if addr == _evo.D2F_DIRSET_ADDR:
            self._pin.switch_to_input(pull)
    def __init__(self,
                 pin,
                 n,
                 *,
                 bpp=3,
                 brightness=1.0,
                 auto_write=True,
                 pixel_order=None):

        if not pixel_order:
            pixel_order = GRB if bpp == 3 else GRBW
        else:
            if isinstance(pixel_order, tuple):
                order_list = [RGBW[order] for order in pixel_order]
                pixel_order = "".join(order_list)

        # Call _pixelbuf __init__
        super().__init__(n,
                         brightness=brightness,
                         byteorder=pixel_order,
                         auto_write=auto_write)

        # Make FPGA calls to set Neopixel pin as output
        addr = _evo.D2F_ENSET_ADDR
        _evo.send_evo_write_trans(addr, self._buffer)
        addr = _evo.D2F_DIRSET_ADDR
        _evo.send_evo_write_trans(addr, self._buffer)

        # Make call to NeoPixel class
        self._neopixel = neopixel.NeoPixel(pin[1],
                                           n,
                                           bpp=bpp,
                                           brightness=brightness,
                                           auto_write=auto_write,
                                           pixel_order=pixel_order)
 def deinit(self) -> None:
     """Turn off the DigitalInOut and release the pin for other use."""
     self._mask = pin[0]
     data = 1 << self._mask
     #data = struct.pack("<I", data)
     struct.pack_into("<I", self._buffer, 0, data)
     if self._pin == None:
         pass
     else:
         addr = _evo.D2F_ENCLR_ADDR
         _evo.send_evo_write_trans(addr, self._buffer)
Exemple #5
0
  def __init__(self, pin):
    """(formerly Dallas Semi) OneWire protocol.

    Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126

    .. class:: OneWire(pin)

      Create a OneWire object associated with the given pin. The object
      implements the lowest level timing-sensitive bits of the protocol.

      :param ~microcontroller.Pin pin: Pin connected to the OneWire bus

      Read a short series of pulses::

        from aloriumtech import busio
        from aloriumtech import board

        onewire = busio.OneWire(board.D7)
        onewire.reset()
        onewire.write_bit(True)
        onewire.write_bit(False)
        print(onewire.read_bit())"""
    
    # Make FPGA calls to allow SAMD to control pin
    data = 1 << pin[0]
    struct.pack_into("<I", self._buffer, 0, data)
    addr = _evo.D2F_ENSET_ADDR
    _evo.send_evo_write_trans(addr, self._buffer)

    self._pin = pin

    self._onewire = busio.OneWire(pin[1])

    """Deinitialize the OneWire bus and release any hardware resources for reuse."""
    self.deinit = self._onewire.deinit

    """No-op used by Context Managers."""
    self.__enter__ = self._onewire.__enter__

    """Automatically deinitializes the hardware when exiting a context. See
    :ref:`lifetime-and-contextmanagers` for more info."""
    self.__exit__ = self._onewire.__exit__

    """Reset the OneWire bus and read presence

    :returns: False when at least one device is present
    :rtype: bool"""
    self.reset = self._onewire.reset

    """Write out a bit based on value."""
    self.write_bit = self._onewire.write_bit
 def value(self, value):
     if self._pin != None:
         self._pin.value = value
     else:
         data = 1 << self._mask
         struct.pack_into("<I", self._buffer, 0, data)
         if value == True:
             if self._id == 3:
                 addr = _evo.PORT_Z_OUTSET_ADDR
             elif self._id == 2:
                 addr = _evo.PORT_G_OUTSET_ADDR
             elif self._id == 1:
                 addr = _evo.PORT_E_OUTSET_ADDR
         else:
             if self._id == 3:
                 addr = _evo.PORT_Z_OUTCLR_ADDR
             elif self._id == 2:
                 addr = _evo.PORT_G_OUTCLR_ADDR
             elif self._id == 1:
                 addr = _evo.PORT_E_OUTCLR_ADDR
         _evo.send_evo_write_trans(addr, self._buffer)
Exemple #7
0
  def deinit(self,):

    """Turn off the SPI bus."""

    # Make FPGA calls to clear clock, mosi, and miso pins

    data = 1 << _clock[0]
    struct.pack_into("<I", self._buffer, 0, data)
    addr = _evo.D2F_DIRCLR_ADDR
    _evo.send_evo_write_trans(addr, self._buffer)

    if (_MOSI != None):

      data = 1 << _MOSI[0]
      struct.pack_into("<I", self._buffer, 0, data)
      addr = _evo.D2F_DIRCLR_ADDR
      _evo.send_evo_write_trans(addr, self._buffer)

    if (_MISO != None):

      data = 1 << _MISO[0]
      struct.pack_into("<I", self._buffer, 0, data)
      addr = _evo.D2F_DIRCLR_ADDR
      _evo.send_evo_write_trans(addr, self._buffer)

    self._SPI.deinit()
    def __init__(self, pin):
        """Create a new DigitalInOut object associated with the pin. Defaults to input
        with no pull. Use :py:meth:`switch_to_input` and
        :py:meth:`switch_to_output` to change the direction.

        :param ~microcontroller.Pin pin: The pin to control"""
        self._mask = pin[0]
        self._id = pin[1]
        self._pin = None
        self._direction = None
        data = 1 << self._mask
        struct.pack_into("<I", self._buffer, 0, data)
        if pin[1] == 3:
            pass
        elif pin[1] == 2:
            pass
        elif pin[1] == 1:
            pass
        else:
            addr = _evo.D2F_ENSET_ADDR
            self._pin = digitalio.DigitalInOut(self._id)
            _evo.send_evo_write_trans(addr, self._buffer)
Exemple #9
0
  def __init__(self, tx, rx, *, baudrate=9600, bits=8, parity=None, stop=1, timeout=1, receiver_buffer_size=64):

    """A common bidirectional serial protocol that uses an an agreed upon speed
    rather than a shared clock line.

    :param ~microcontroller.Pin tx: the pin to transmit with, or ``None`` if this ``UART`` is receive-only.
    :param ~microcontroller.Pin rx: the pin to receive on, or ``None`` if this ``UART`` is transmit-only.
    :param ~microcontroller.Pin rts: the pin for rts, or ``None`` if rts not in use.
    :param ~microcontroller.Pin cts: the pin for cts, or ``None`` if cts not in use.
    :param ~microcontroller.Pin rs485_dir: the pin for rs485 direction setting, or ``None`` if rs485 not in use.
    :param bool rs485_invert: set to invert the sense of the rs485_dir pin.
    :param int baudrate: the transmit and receive speed.
    :param int bits:  the number of bits per byte, 7, 8 or 9.
    :param Parity parity:  the parity used for error checking.
    :param int stop:  the number of stop bits, 1 or 2.
    :param float timeout:  the timeout in seconds to wait for the first character and between subsequent characters when reading. Raises ``ValueError`` if timeout >100 seconds.
    :param int receiver_buffer_size: the character length of the read buffer (0 to disable). (When a character is 9 bits the buffer will be 2 * receiver_buffer_size bytes.)

    *New in CircuitPython 4.0:* ``timeout`` has incompatibly changed units from milliseconds to seconds.
    The new upper limit on ``timeout`` is meant to catch mistaken use of milliseconds."""

    # Make FPGA calls to allow SAMD to control tx pin
    data = 1 << tx[0]
    struct.pack_into("<I", self._buffer, 0, data)
    addr = _evo.D2F_ENSET_ADDR
    _evo.send_evo_write_trans(addr, self._buffer)

    self._tx = tx

    # Make FPGA calls to allow SAMD to control rx pin
    data = 1 << rx[0]
    struct.pack_into("<I", self._buffer, 0, data)
    addr = _evo.D2F_ENSET_ADDR
    _evo.send_evo_write_trans(addr, self._buffer)

    self._rx = rx

    self._UART = busio.UART(tx[1], rx[1], baudrate=baudrate, bits=bits, parity=parity, stop=stop, timeout=timeout, receiver_buffer_size=receiver_buffer_size)

    """Deinitialises the UART and releases any hardware resources for reuse."""
    self.deinit = self._UART.deinit

    """No-op used by Context Managers."""
    self.__enter__ = self._UART.__enter__

    """Automatically deinitializes the hardware when exiting a context. See
    :ref:`lifetime-and-contextmanagers` for more info."""
    self.__exit__ = self._UART.__exit__

    """Read characters.  If ``nbytes`` is specified then read at most that many
    bytes. Otherwise, read everything that arrives until the connection
    times out. Providing the number of bytes expected is highly recommended
    because it will be faster.

    :return: Data read
    :rtype: bytes or None"""
    self.read = self._UART.read

    """Read bytes into the ``buf``. Read at most ``len(buf)`` bytes.

    :return: number of bytes read and stored into ``buf``
    :rtype: int or None (on a non-blocking error)

    *New in CircuitPython 4.0:* No length parameter is permitted."""
    self.readinto = self._UART.readinto

    """Read a line, ending in a newline character.

    :return: the line read
    :rtype: int or None"""
    self.readline = self._UART.readline

    """Write the buffer of bytes to the bus.

    *New in CircuitPython 4.0:* ``buf`` must be bytes, not a string.

    :return: the number of bytes written
    :rtype: int or None"""
    self.write = self._UART.write

    """The current baudrate."""
    self.baudrate = self._UART.baudrate

    """The number of bytes in the input buffer, available to be read"""
    self.in_waiting  = self._UART.in_waiting

    """The current timeout, in seconds (float)."""
    self.timeout  = self._UART.timeout

    """Discard any unread characters in the input buffer."""
    self.reset_input_buffer = self._UART.reset_input_buffer
Exemple #10
0
  def __init__(self, scl, sda, *, frequency=400000, timeout=255):

    """I2C is a two-wire protocol for communicating between devices.  At the
    physical level it consists of 2 wires: SCL and SDA, the clock and data
    lines respectively.

    .. seealso:: Using this class directly requires careful lock management.
        Instead, use :class:`~adafruit_bus_device.i2c_device.I2CDevice` to
        manage locks.

    .. seealso:: Using this class to directly read registers requires manual
        bit unpacking. Instead, use an existing driver or make one with
        :ref:`Register <register-module-reference>` data descriptors.

    :param ~microcontroller.Pin scl: The clock pin
    :param ~microcontroller.Pin sda: The data pin
    :param int frequency: The clock frequency in Hertz
    :param int timeout: The maximum clock stretching timeut - (used only for bitbangio.I2C; ignored for busio.I2C)

    .. note:: On the nRF52840, only one I2C object may be created,
       except on the Circuit Playground Bluefruit, which allows two,
       one for the onboard accelerometer, and one for offboard use."""

    # Make FPGA calls to allow the SAMD to control the scl pin
    data = 1 << scl[0]
    struct.pack_into("<I", self._buffer, 0, data)
    addr = _evo.D2F_ENSET_ADDR
    _evo.send_evo_write_trans(addr, self._buffer)

    self._scl = scl

    # Make FPGA calls to allow the SAMD to control the sda pin
    data = 1 << sda[0]
    struct.pack_into("<I", self._buffer, 0, data)
    addr = _evo.D2F_ENSET_ADDR
    _evo.send_evo_write_trans(addr, self._buffer)

    self._sda = sda

    self._I2C = busio.I2C(self._scl[1], self._sda[1], frequency=frequency, timeout=timeout)

    """Releases control of the underlying hardware so other classes can use it."""
    self.deinit = self._I2C.deinit

    """No-op used in Context Managers."""
    self.__enter__ = self._I2C.__enter__

    """Automatically deinitializes the hardware on context exit. See
    :ref:`lifetime-and-contextmanagers` for more info."""
    self.__exit__ = self._I2C.__exit__

    """Scan all I2C addresses between 0x08 and 0x77 inclusive and return a
    list of those that respond.

    :return: List of device ids on the I2C bus
    :rtype: list"""
    self.scan = self._I2C.scan

    """Attempts to grab the I2C lock. Returns True on success.

    :return: True when lock has been grabbed
    :rtype: bool"""
    self.try_lock = self._I2C.try_lock

    """Releases the I2C lock."""
     self.unlock = self._I2C.unlock
Exemple #11
0
  def __init__(self, clock, MOSI=None, MISO=None):

    """Construct an SPI object on the given pins.

    ..note:: The SPI peripherals allocated in order of desirability, if possible,
       such as highest speed and not shared use first. For instance, on the nRF52840,
       there is a single 32MHz SPI peripheral, and multiple 8MHz peripherals,
       some of which may also be used for I2C. The 32MHz SPI peripheral is returned
       first, then the exclusive 8MHz SPI peripheral, and finally the shared 8MHz
       peripherals.

    .. seealso:: Using this class directly requires careful lock management.
        Instead, use :class:`~adafruit_bus_device.spi_device.SPIDevice` to
        manage locks.

    .. seealso:: Using this class to directly read registers requires manual
        bit unpacking. Instead, use an existing driver or make one with
        :ref:`Register <register-module-reference>` data descriptors.

    :param ~microcontroller.Pin clock: the pin to use for the clock.
    :param ~microcontroller.Pin MOSI: the Master Out Slave In pin.
    :param ~microcontroller.Pin MISO: the Master In Slave Out pin."""

    # Make FPGA calls to set clock pin as output
    data = 1 << clock[0]
    struct.pack_into("<I", self._buffer, 0, data)
    addr = _evo.D2F_ENSET_ADDR
    _evo.send_evo_write_trans(addr, self._buffer)
    addr = _evo.D2F_DIRSET_ADDR
    _evo.send_evo_write_trans(addr, self._buffer)

    _clock = clock

    if (MOSI != None):

      if (MOSI[0] != 24):
        # Make FPGA calls to set MOSI pin as output
        data = 1 << MOSI[0]
        struct.pack_into("<I", self._buffer, 0, data)
        addr = _evo.D2F_ENSET_ADDR
        _evo.send_evo_write_trans(addr, self._buffer)
        addr = _evo.D2F_DIRSET_ADDR
        _evo.send_evo_write_trans(addr, self._buffer)

      _MOSI = MOSI

    if (MISO != None):

      if (MISO[0] != 23):
        # Make FPGA calls to set MISO pin as input
        data = 1 << MISO[0]
        struct.pack_into("<I", self._buffer, 0, data)
        addr = _evo.D2F_ENSET_ADDR
        _evo.send_evo_write_trans(addr, self._buffer)
        addr = _evo.D2F_DIRCLR_ADDR
        _evo.send_evo_write_trans(addr, self._buffer)

      _MISO = MISO

    # Need to handle each possible combination separately
    if (MOSI == None and MISO == None):
      self._SPI = busio.SPI(_clock[1])
    elif (MOSI == None and MISO != None):
      self._SPI = busio.SPI(_clock[1], MISO=_MISO[1])
    elif (MOSI != None and MISO == None):
      self._SPI = busio.SPI(_clock[1], MOSI=_MOSI[1])
    else:
      self._SPI = busio.SPI(_clock[1], _MOSI[1], _MISO[1])

    self.frequency = self._SPI.frequency

    """No-op used by Context Managers.
    Provided by context manager helper."""
    self.__enter__ = self._SPI.__enter__

    """Automatically deinitializes the hardware when exiting a context. See
    :ref:`lifetime-and-contextmanagers` for more info."""
    self.__exit__ = self._SPI.__exit__

    """Configures the SPI bus. The SPI object must be locked.

    :param int baudrate: the desired clock rate in Hertz. The actual clock rate may be higher or lower
      due to the granularity of available clock settings.
      Check the `frequency` attribute for the actual clock rate.
    :param int polarity: the base state of the clock line (0 or 1)
    :param int phase: the edge of the clock that data is captured. First (0)
      or second (1). Rising or falling depends on clock polarity.
    :param int bits: the number of bits per word

    .. note:: On the SAMD21, it is possible to set the baudrate to 24 MHz, but that
       speed is not guaranteed to work. 12 MHz is the next available lower speed, and is
       within spec for the SAMD21.

    .. note:: On the nRF52840, these baudrates are available: 125kHz, 250kHz, 1MHz, 2MHz, 4MHz,
      and 8MHz.
      If you pick a a baudrate other than one of these, the nearest lower
      baudrate will be chosen, with a minimum of 125kHz.
      Two SPI objects may be created, except on the Circuit Playground Bluefruit,
      which allows only one (to allow for an additional I2C object)."""
    self.configure = self._SPI.configure

    """Attempts to grab the SPI lock. Returns True on success.

    :return: True when lock has been grabbed
    :rtype: bool"""
    self.try_lock = self._SPI.try_lock

    """Releases the SPI lock."""
    self.unlock = self._SPI.unlock

    """Write the data contained in ``buffer``. The SPI object must be locked.
    If the buffer is empty, nothing happens.

    :param bytearray buffer: Write out the data in this buffer
    :param int start: Start of the slice of ``buffer`` to write out: ``buffer[start:end]``
    :param int end: End of the slice; this index is not included. Defaults to ``len(buffer)``"""
    self.write = self._SPI.write

    """Read into ``buffer`` while writing ``write_value`` for each byte read.
    The SPI object must be locked.
    If the number of bytes to read is 0, nothing happens.

    :param bytearray buffer: Read data into this buffer
    :param int start: Start of the slice of ``buffer`` to read into: ``buffer[start:end]``
    :param int end: End of the slice; this index is not included. Defaults to ``len(buffer)``
    :param int write_value: Value to write while reading. (Usually ignored.)"""
    self.readinto = self._SPI.readinto

    """Write out the data in ``buffer_out`` while simultaneously reading data into ``buffer_in``.
    The SPI object must be locked.
    The lengths of the slices defined by ``buffer_out[out_start:out_end]`` and ``buffer_in[in_start:in_end]``
    must be equal.
    If buffer slice lengths are both 0, nothing happens.

    :param bytearray buffer_out: Write out the data in this buffer
    :param bytearray buffer_in: Read data into this buffer
    :param int out_start: Start of the slice of buffer_out to write out: ``buffer_out[out_start:out_end]``
    :param int out_end: End of the slice; this index is not included. Defaults to ``len(buffer_out)``
    :param int in_start: Start of the slice of ``buffer_in`` to read into: ``buffer_in[in_start:in_end]``
    :param int in_end: End of the slice; this index is not included. Defaults to ``len(buffer_in)``"""
    self.write_readinto = self._SPI.write_readinto