示例#1
0
class CPB_Gizmo(PyBadgerBase):
    """Class that represents a single Circuit Playground Bluefruit with TFT Gizmo."""

    display = None
    _audio_out = audiopwmio.PWMAudioOut
    _neopixel_count = 10

    def __init__(self):
        super().__init__()

        _i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
        _int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
        self.accelerometer = adafruit_lis3dh.LIS3DH_I2C(_i2c,
                                                        address=0x19,
                                                        int1=_int1)
        self.accelerometer.range = adafruit_lis3dh.RANGE_8_G

        self.display = tft_gizmo.TFT_Gizmo()
        self._display_brightness = 1.0

        # NeoPixels
        self._neopixels = neopixel.NeoPixel(board.NEOPIXEL,
                                            self._neopixel_count,
                                            brightness=1,
                                            pixel_order=neopixel.GRB)

        self._keys = keypad.Keys([board.BUTTON_A, board.BUTTON_B],
                                 value_when_pressed=True,
                                 pull=True)
        self._buttons = KeyStates(self._keys)
        self._light_sensor = analogio.AnalogIn(board.LIGHT)

    @property
    def button(self):
        """The buttons on the board.

        Example use:

        .. code-block:: python

          from adafruit_pybadger import pybadger

          while True:
              if pybadger.button.a:
                  print("Button A")
              elif pybadger.button.b:
                  print("Button B")
        """
        self._buttons.update()
        button_values = tuple(
            self._buttons.was_pressed(i) for i in range(self._keys.key_count))
        return Buttons(button_values[0], button_values[1])

    @property
    def _unsupported(self):
        """This feature is not supported on CPB Gizmo."""
        raise NotImplementedError(
            "This feature is not supported on CPB Gizmo.")
class Clue(PyBadgerBase):
    """Class that represents a single CLUE."""

    _audio_out = audiopwmio.PWMAudioOut
    _neopixel_count = 1

    def __init__(self) -> None:
        super().__init__()

        i2c = board.I2C()

        if i2c is not None:
            self._accelerometer = adafruit_lsm6ds.lsm6ds33.LSM6DS33(i2c)

        # NeoPixels
        self._neopixels = neopixel.NeoPixel(board.NEOPIXEL,
                                            self._neopixel_count,
                                            brightness=1,
                                            pixel_order=neopixel.GRB)

        self._keys = keypad.Keys([board.BUTTON_A, board.BUTTON_B],
                                 value_when_pressed=False,
                                 pull=True)
        self._buttons = KeyStates(self._keys)

    @property
    def button(self) -> Type[tuple]:
        """The buttons on the board.

        Example use:

        .. code-block:: python

          from adafruit_pybadger import pybadger

          while True:
              if pybadger.button.a:
                  print("Button A")
              elif pybadger.button.b:
                  print("Button B")
        """
        self._buttons.update()
        button_values = tuple(
            self._buttons.was_pressed(i) for i in range(self._keys.key_count))
        return Buttons(button_values[0], button_values[1])

    @property
    def _unsupported(self):
        """This feature is not supported on CLUE."""
        raise NotImplementedError("This feature is not supported on CLUE.")

    # The following is a list of the features available in other PyBadger modules but
    # not available for CLUE. If called while using a CLUE, they will result in the
    # NotImplementedError raised in the property above.
    play_file = _unsupported
    light = _unsupported
示例#3
0
class PyBadge(PyBadgerBase):
    """Class that represents a single PyBadge, PyBadge LC, or EdgeBadge."""

    _audio_out = audioio.AudioOut
    _neopixel_count = 5

    def __init__(self) -> None:
        super().__init__()

        i2c = None

        if i2c is None:
            try:
                i2c = board.I2C()
            except RuntimeError:
                self._accelerometer = None

        if i2c is not None:
            _i2c_devices = []

            for _ in range(10):
                # try lock 10 times to avoid infinite loop in sphinx build
                if i2c.try_lock():
                    _i2c_devices = i2c.scan()
                    i2c.unlock()
                    break

            # PyBadge LC doesn't have accelerometer
            if int(0x18) in _i2c_devices or int(0x19) in _i2c_devices:
                int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
                try:
                    self._accelerometer = adafruit_lis3dh.LIS3DH_I2C(
                        i2c, address=0x19, int1=int1)
                except ValueError:
                    self._accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c,
                                                                     int1=int1)

        # NeoPixels
        self._neopixels = neopixel.NeoPixel(board.NEOPIXEL,
                                            self._neopixel_count,
                                            brightness=1,
                                            pixel_order=neopixel.GRB)

        self._keys = keypad.ShiftRegisterKeys(
            clock=board.BUTTON_CLOCK,
            data=board.BUTTON_OUT,
            latch=board.BUTTON_LATCH,
            key_count=8,
            value_when_pressed=True,
        )
        self._buttons = KeyStates(self._keys)

        self._light_sensor = analogio.AnalogIn(board.A7)

    @property
    def button(self) -> Type[tuple]:
        """The buttons on the board.

        Example use:

        .. code-block:: python

          from adafruit_pybadger import pybadger

          while True:
              if pybadger.button.a:
                  print("Button A")
              elif pybadger.button.b:
                  print("Button B")
              elif pybadger.button.start:
                  print("Button start")
              elif pybadger.button.select:
                  print("Button select")

        """
        self._buttons.update()
        button_values = tuple(
            self._buttons.was_pressed(i) for i in range(self._keys.key_count))
        return Buttons(
            button_values[0],
            button_values[1],
            button_values[2],
            button_values[3],
            button_values[4],
            button_values[5],
            button_values[6],
            button_values[7],
        )
示例#4
0
class PyGamer(PyBadgerBase):
    """Class that represents a single PyGamer."""

    _audio_out = audioio.AudioOut
    _neopixel_count = 5

    def __init__(self):
        super().__init__()

        i2c = board.I2C()

        int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
        try:
            self._accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c,
                                                             address=0x19,
                                                             int1=int1)
        except ValueError:
            self._accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)

        # NeoPixels
        self._neopixels = neopixel.NeoPixel(board.NEOPIXEL,
                                            self._neopixel_count,
                                            brightness=1,
                                            pixel_order=neopixel.GRB)

        self._keys = keypad.ShiftRegisterKeys(
            clock=board.BUTTON_CLOCK,
            data=board.BUTTON_OUT,
            latch=board.BUTTON_LATCH,
            key_count=4,
            value_when_pressed=True,
        )
        self._buttons = KeyStates(self._keys)

        self._pygamer_joystick_x = analogio.AnalogIn(board.JOYSTICK_X)
        self._pygamer_joystick_y = analogio.AnalogIn(board.JOYSTICK_Y)

        self._light_sensor = analogio.AnalogIn(board.A7)

    @property
    def button(self):
        """The buttons on the board.

        Example use:

        .. code-block:: python

          from adafruit_pybadger import pybadger

          while True:
              if pybadger.button.a:
                  print("Button A")
              elif pybadger.button.b:
                  print("Button B")
              elif pybadger.button.start:
                  print("Button start")
              elif pybadger.button.select:
                  print("Button select")

        """
        self._buttons.update()
        button_values = tuple(
            self._buttons.was_pressed(i) for i in range(self._keys.key_count))
        x, y = self.joystick
        return Buttons(
            button_values[0],
            button_values[1],
            button_values[2],
            button_values[3],
            x > 50000,  # RIGHT
            y > 50000,  # DOWN
            y < 15000,  # UP
            x < 15000,  # LEFT
        )

    @property
    def joystick(self):
        """The joystick on the PyGamer."""
        x = self._pygamer_joystick_x.value
        y = self._pygamer_joystick_y.value
        return x, y
示例#5
0
class PewPewM4(PyBadgerBase):
    """Class that represents a single Pew Pew M4."""

    _audio_out = audioio.AudioOut
    _neopixel_count = 0

    def __init__(self):
        super().__init__()

        self._keys = keypad.Keys(
            [
                board.BUTTON_O,
                board.BUTTON_X,
                board.BUTTON_Z,
                board.BUTTON_RIGHT,
                board.BUTTON_DOWN,
                board.BUTTON_UP,
                board.BUTTON_LEFT,
            ],
            value_when_pressed=False,
            pull=True,
        )

        self._buttons = KeyStates(self._keys)

    @property
    def button(self):
        """The buttons on the board.

        Example use:

        .. code-block:: python

          from adafruit_pybadger import pybadger

          while True:
              if pybadger.button.x:
                  print("Button X")
              elif pybadger.button.o:
                  print("Button O")
        """
        self._buttons.update()
        button_values = tuple(
            self._buttons.was_pressed(i) for i in range(self._keys.key_count)
        )
        return Buttons(
            button_values[0],
            button_values[1],
            button_values[2],
            button_values[3],
            button_values[4],
            button_values[5],
            button_values[6],
        )

    @property
    def _unsupported(self):
        """This feature is not supported on PewPew M4."""
        raise NotImplementedError("This feature is not supported on PewPew M4.")

    # The following is a list of the features available in other PyBadger modules but
    # not available for CLUE. If called while using a CLUE, they will result in the
    # NotImplementedError raised in the property above.
    light = _unsupported
    acceleration = _unsupported
    pixels = _unsupported