Skip to content

Commit bf25d8b

Browse files
authored
Merge pull request #23 from kattni/clue-adaptation
Refactor to include CLUE
2 parents d3da430 + ee0b46a commit bf25d8b

File tree

10 files changed

+441
-120
lines changed

10 files changed

+441
-120
lines changed

README.rst

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Introduction
1313
:target: https://github.com/adafruit/Adafruit_CircuitPython_PyBadger/actions/
1414
:alt: Build Status
1515

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

1818

1919
Dependencies
@@ -28,9 +28,6 @@ This is easily achieved by downloading
2828

2929
Installing from PyPI
3030
=====================
31-
.. note:: This library is not available on PyPI yet. Install documentation is included
32-
as a standard element. Stay tuned for PyPI availability!
33-
3431
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
3532
PyPI <https://pypi.org/project/adafruit-circuitpython-pybadger/>`_. To install for current user:
3633

@@ -58,9 +55,7 @@ Usage Example
5855

5956
.. code-block:: python
6057
61-
from adafruit_pybadger import PyBadger
62-
63-
pybadger = PyBadger()
58+
from adafruit_pybadger import pybadger
6459
6560
pybadger.show_badge(name_string="Blinka", hello_scale=2, my_name_is_scale=2, name_scale=3)
6661

adafruit_pybadger/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
Verifies which board is being used and imports the appropriate module.
24+
"""
25+
26+
import os
27+
28+
if "CLUE" in os.uname().machine:
29+
from .clue import clue as pybadger
30+
elif "Pybadge" in os.uname().machine:
31+
from .pybadge import pybadge as pybadger
32+
elif "PyGamer" in os.uname().machine:
33+
from .pygamer import pygamer as pybadger

adafruit_pybadger/clue.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_pybadger.clue`
24+
================================================================================
25+
26+
Badge-focused CircuitPython helper library for CLUE.
27+
28+
29+
* Author(s): Kattni Rembor
30+
31+
Implementation Notes
32+
--------------------
33+
34+
**Hardware:**
35+
36+
* `Adafruit CLUE <https://www.adafruit.com/product/4500>`_
37+
38+
**Software and Dependencies:**
39+
40+
* Adafruit CircuitPython firmware for the supported boards:
41+
https://github.com/adafruit/circuitpython/releases
42+
43+
"""
44+
45+
from collections import namedtuple
46+
import board
47+
import digitalio
48+
import audiopwmio
49+
from gamepad import GamePad
50+
import adafruit_lsm6ds
51+
from adafruit_pybadger.pybadger_base import PyBadgerBase
52+
53+
__version__ = "0.0.0-auto.0"
54+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git"
55+
56+
Buttons = namedtuple("Buttons", "a b")
57+
58+
class Clue(PyBadgerBase):
59+
"""Class that represents a single CLUE."""
60+
_audio_out = audiopwmio.PWMAudioOut
61+
_neopixel_count = 1
62+
63+
def __init__(self):
64+
super().__init__()
65+
66+
i2c = board.I2C()
67+
68+
if i2c is not None:
69+
self._accelerometer = adafruit_lsm6ds.LSM6DS33(i2c)
70+
71+
self._buttons = GamePad(digitalio.DigitalInOut(board.BUTTON_A),
72+
digitalio.DigitalInOut(board.BUTTON_B))
73+
74+
@property
75+
def button(self):
76+
"""The buttons on the board.
77+
78+
Example use:
79+
80+
.. code-block:: python
81+
82+
from adafruit_pybadger import pybadger
83+
84+
while True:
85+
if pybadger.button.a:
86+
print("Button A")
87+
elif pybadger.button.b:
88+
print("Button B")
89+
"""
90+
button_values = self._buttons.get_pressed()
91+
return Buttons(button_values & PyBadgerBase.BUTTON_B,
92+
button_values & PyBadgerBase.BUTTON_A)
93+
94+
95+
@property
96+
def _unsupported(self):
97+
"""This feature is not supported on CLUE."""
98+
raise NotImplementedError("This feature is not supported on CLUE.")
99+
100+
# The following is a list of the features available in other PyBadger modules but
101+
# not available for CLUE. If called while using a CLUE, they will result in the
102+
# NotImplementedError raised in the property above.
103+
play_file = _unsupported
104+
light = _unsupported
105+
106+
clue = Clue() # pylint: disable=invalid-name
107+
"""Object that is automatically created on import."""

adafruit_pybadger/pybadge.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_pybadger.pybadge`
24+
================================================================================
25+
26+
Badge-focused CircuitPython helper library for PyBadge, PyBadge LC and EdgeBadge.
27+
All three boards are included in this module as there is no difference in the
28+
CircuitPython builds at this time, and therefore no way to differentiate
29+
the boards from within CircuitPython.
30+
31+
32+
* Author(s): Kattni Rembor
33+
34+
Implementation Notes
35+
--------------------
36+
37+
**Hardware:**
38+
39+
* `Adafruit PyBadge <https://www.adafruit.com/product/4200>`_
40+
* `Adafruit PyBadge LC <https://www.adafruit.com/product/3939>`_
41+
* `Adafruit EdgeBadge <https://www.adafruit.com/product/4400>`_
42+
43+
**Software and Dependencies:**
44+
45+
* Adafruit CircuitPython firmware for the supported boards:
46+
https://github.com/adafruit/circuitpython/releases
47+
48+
"""
49+
50+
from collections import namedtuple
51+
import board
52+
import digitalio
53+
import analogio
54+
import audioio
55+
from gamepadshift import GamePadShift
56+
import adafruit_lis3dh
57+
from adafruit_pybadger.pybadger_base import PyBadgerBase
58+
59+
__version__ = "0.0.0-auto.0"
60+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git"
61+
62+
Buttons = namedtuple("Buttons", "b a start select right down up left")
63+
64+
class PyBadge(PyBadgerBase):
65+
"""Class that represents a single PyBadge, PyBadge LC, or EdgeBadge."""
66+
67+
_audio_out = audioio.AudioOut
68+
_neopixel_count = 5
69+
70+
def __init__(self):
71+
super().__init__()
72+
73+
i2c = None
74+
75+
if i2c is None:
76+
try:
77+
i2c = board.I2C()
78+
except RuntimeError:
79+
self._accelerometer = None
80+
81+
if i2c is not None:
82+
int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
83+
try:
84+
self._accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1)
85+
except ValueError:
86+
self._accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
87+
88+
self._buttons = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
89+
digitalio.DigitalInOut(board.BUTTON_OUT),
90+
digitalio.DigitalInOut(board.BUTTON_LATCH))
91+
92+
self._light_sensor = analogio.AnalogIn(board.A7)
93+
94+
@property
95+
def button(self):
96+
"""The buttons on the board.
97+
98+
Example use:
99+
100+
.. code-block:: python
101+
102+
from adafruit_pybadger import pybadger
103+
104+
while True:
105+
if pybadger.button.a:
106+
print("Button A")
107+
elif pybadger.button.b:
108+
print("Button B")
109+
elif pybadger.button.start:
110+
print("Button start")
111+
elif pybadger.button.select:
112+
print("Button select")
113+
114+
"""
115+
button_values = self._buttons.get_pressed()
116+
return Buttons(*[button_values & button for button in
117+
(PyBadgerBase.BUTTON_B, PyBadgerBase.BUTTON_A,
118+
PyBadgerBase.BUTTON_START, PyBadgerBase.BUTTON_SELECT,
119+
PyBadgerBase.BUTTON_RIGHT, PyBadgerBase.BUTTON_DOWN,
120+
PyBadgerBase.BUTTON_UP, PyBadgerBase.BUTTON_LEFT)])
121+
122+
pybadge = PyBadge() # pylint: disable=invalid-name
123+
"""Object that is automatically created on import."""

0 commit comments

Comments
 (0)