Skip to content

Refactor to include CLUE #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Introduction
:target: https://github.com/adafruit/Adafruit_CircuitPython_PyBadger/actions/
:alt: Build Status

Badge-focused CircuitPython helper library for PyBadge and PyGamer.
Badge-focused CircuitPython helper library for PyBadge, PyBadge LC, PyGamer and CLUE.


Dependencies
Expand All @@ -28,9 +28,6 @@ This is easily achieved by downloading

Installing from PyPI
=====================
.. note:: This library is not available on PyPI yet. Install documentation is included
as a standard element. Stay tuned for PyPI availability!

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
PyPI <https://pypi.org/project/adafruit-circuitpython-pybadger/>`_. To install for current user:

Expand Down Expand Up @@ -58,9 +55,7 @@ Usage Example

.. code-block:: python

from adafruit_pybadger import PyBadger

pybadger = PyBadger()
from adafruit_pybadger import pybadger

pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)

Expand Down
33 changes: 33 additions & 0 deletions adafruit_pybadger/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# The MIT License (MIT)
#
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
Verifies which board is being used and imports the appropriate module.
"""

import os

if "CLUE" in os.uname().machine:
from .clue import clue as pybadger
elif "Pybadge" in os.uname().machine:
from .pybadge import pybadge as pybadger
elif "PyGamer" in os.uname().machine:
from .pygamer import pygamer as pybadger
107 changes: 107 additions & 0 deletions adafruit_pybadger/clue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# The MIT License (MIT)
#
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_pybadger.clue`
================================================================================

Badge-focused CircuitPython helper library for CLUE.


* Author(s): Kattni Rembor

Implementation Notes
--------------------

**Hardware:**

* `Adafruit CLUE <https://www.adafruit.com/product/4500>`_

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases

"""

from collections import namedtuple
import board
import digitalio
import audiopwmio
from gamepad import GamePad
import adafruit_lsm6ds
from adafruit_pybadger.pybadger_base import PyBadgerBase

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git"

Buttons = namedtuple("Buttons", "a b")

class Clue(PyBadgerBase):
"""Class that represents a single CLUE."""
_audio_out = audiopwmio.PWMAudioOut
_neopixel_count = 1

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

i2c = board.I2C()

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

self._buttons = GamePad(digitalio.DigitalInOut(board.BUTTON_A),
digitalio.DigitalInOut(board.BUTTON_B))

@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")
"""
button_values = self._buttons.get_pressed()
return Buttons(button_values & PyBadgerBase.BUTTON_B,
button_values & PyBadgerBase.BUTTON_A)


@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

clue = Clue() # pylint: disable=invalid-name
"""Object that is automatically created on import."""
123 changes: 123 additions & 0 deletions adafruit_pybadger/pybadge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# The MIT License (MIT)
#
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_pybadger.pybadge`
================================================================================

Badge-focused CircuitPython helper library for PyBadge, PyBadge LC and EdgeBadge.
All three boards are included in this module as there is no difference in the
CircuitPython builds at this time, and therefore no way to differentiate
the boards from within CircuitPython.


* Author(s): Kattni Rembor

Implementation Notes
--------------------

**Hardware:**

* `Adafruit PyBadge <https://www.adafruit.com/product/4200>`_
* `Adafruit PyBadge LC <https://www.adafruit.com/product/3939>`_
* `Adafruit EdgeBadge <https://www.adafruit.com/product/4400>`_

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases

"""

from collections import namedtuple
import board
import digitalio
import analogio
import audioio
from gamepadshift import GamePadShift
import adafruit_lis3dh
from adafruit_pybadger.pybadger_base import PyBadgerBase

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git"

Buttons = namedtuple("Buttons", "b a start select right down up left")

class PyBadge(PyBadgerBase):
"""Class that represents a single PyBadge, PyBadge LC, or EdgeBadge."""

_audio_out = audioio.AudioOut
_neopixel_count = 5

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

i2c = None

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

if i2c is not None:
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)

self._buttons = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
digitalio.DigitalInOut(board.BUTTON_OUT),
digitalio.DigitalInOut(board.BUTTON_LATCH))

self._light_sensor = analogio.AnalogIn(board.A7)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to check if the light sensor isn't present, like for the pybadge LC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyBadge LC has a light sensor. It's at the top, next to the reset button, with a hole over it to pass the front of the sensor through to the front of the board. From the back, it is white with a green symbol printed on it.

The lack of a light sensor would only be detectable in very specific situations (depending on whether the pin is connected to something else, has pull up or down, etc), and is a complicated process to do so. Knowing that the CLUE doesn't have the same light sensor, I included it in the unsupported list within the CLUE module. In the case of PyBadge vs. PyBadge LC, there's no way to tell the difference as they use the same CircuitPython build - there is no separate pin definition for PyBadge LC.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh, I meant the accelerometer, not the light sensor.

If it's possible to auto detect that, then this line from the example could be modified or removed to make the experience on a PyBadge LC a little nicer.

pybadger.auto_dim_display(delay=10) # Remove or comment out this line if you have the PyBadge LC

Copy link
Contributor

@FoamyGuy FoamyGuy Feb 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try checking what pybadger.acceleration returns on the LC. I think it might end up as None on there but I don't have one to check with.

Along the same idea of improving the auto_dim: what if pressing any button would reset the dim timer. That would help all of the boards and allow for at least one type of auto_dim on the LC device.


@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")

"""
button_values = self._buttons.get_pressed()
return Buttons(*[button_values & button for button in
(PyBadgerBase.BUTTON_B, PyBadgerBase.BUTTON_A,
PyBadgerBase.BUTTON_START, PyBadgerBase.BUTTON_SELECT,
PyBadgerBase.BUTTON_RIGHT, PyBadgerBase.BUTTON_DOWN,
PyBadgerBase.BUTTON_UP, PyBadgerBase.BUTTON_LEFT)])

pybadge = PyBadge() # pylint: disable=invalid-name
"""Object that is automatically created on import."""
Loading