Skip to content

Commit 6878e34

Browse files
authored
Merge pull request #35 from FoamyGuy/adding_cpb_gizmo
Adding cpb gizmo
2 parents 392344e + 352d700 commit 6878e34

File tree

3 files changed

+135
-8
lines changed

3 files changed

+135
-8
lines changed

adafruit_pybadger/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@
3535
from .pewpewm4 import pewpewm4 as pybadger
3636
elif "PyPortal" in os.uname().machine:
3737
from .pyportal import pyportal as pybadger
38+
elif "Circuit Playground Bluefruit" in os.uname().machine:
39+
from .cpb_gizmo import cpb_gizmo as pybadger

adafruit_pybadger/cpb_gizmo.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.cpb_gizmo`
24+
================================================================================
25+
26+
Badge-focused CircuitPython helper library for Circuit Playground Bluefruit with TFT Gizmo.
27+
28+
29+
* Author(s): Kattni Rembor
30+
31+
Implementation Notes
32+
--------------------
33+
34+
**Hardware:**
35+
36+
* `Adafruit Circuit Playground Bluefruit <https://www.adafruit.com/product/4333>`_
37+
* `Adafruit TFT Gizmo <https://www.adafruit.com/product/4367>`_
38+
39+
**Software and Dependencies:**
40+
41+
* Adafruit CircuitPython firmware for the supported boards:
42+
https://github.com/adafruit/circuitpython/releases
43+
44+
"""
45+
46+
from collections import namedtuple
47+
import board
48+
import digitalio
49+
import analogio
50+
import busio
51+
import audiopwmio
52+
from adafruit_gizmo import tft_gizmo
53+
from gamepad import GamePad
54+
import adafruit_lis3dh
55+
import neopixel
56+
from adafruit_pybadger.pybadger_base import PyBadgerBase
57+
58+
__version__ = "0.0.0-auto.0"
59+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git"
60+
61+
Buttons = namedtuple("Buttons", "a b")
62+
63+
64+
class CPB_Gizmo(PyBadgerBase):
65+
"""Class that represents a single Circuit Playground Bluefruit with TFT Gizmo."""
66+
67+
display = None
68+
_audio_out = audiopwmio.PWMAudioOut
69+
_neopixel_count = 10
70+
71+
def __init__(self):
72+
super().__init__()
73+
74+
_i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
75+
_int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
76+
self.accelerometer = adafruit_lis3dh.LIS3DH_I2C(_i2c, address=0x19, int1=_int1)
77+
self.accelerometer.range = adafruit_lis3dh.RANGE_8_G
78+
79+
self.display = tft_gizmo.TFT_Gizmo()
80+
self._display_brightness = 1.0
81+
82+
# NeoPixels
83+
self._neopixels = neopixel.NeoPixel(
84+
board.NEOPIXEL, self._neopixel_count, brightness=1, pixel_order=neopixel.GRB
85+
)
86+
_a_btn = digitalio.DigitalInOut(board.BUTTON_A)
87+
_a_btn.switch_to_input(pull=digitalio.Pull.DOWN)
88+
_b_btn = digitalio.DigitalInOut(board.BUTTON_B)
89+
_b_btn.switch_to_input(pull=digitalio.Pull.DOWN)
90+
self._buttons = GamePad(_a_btn, _b_btn)
91+
self._light_sensor = analogio.AnalogIn(board.LIGHT)
92+
93+
@property
94+
def button(self):
95+
"""The buttons on the board.
96+
97+
Example use:
98+
99+
.. code-block:: python
100+
101+
from adafruit_pybadger import pybadger
102+
103+
while True:
104+
if pybadger.button.a:
105+
print("Button A")
106+
elif pybadger.button.b:
107+
print("Button B")
108+
"""
109+
button_values = self._buttons.get_pressed()
110+
return Buttons(
111+
button_values & PyBadgerBase.BUTTON_B, button_values & PyBadgerBase.BUTTON_A
112+
)
113+
114+
@property
115+
def _unsupported(self):
116+
"""This feature is not supported on CPB Gizmo."""
117+
raise NotImplementedError("This feature is not supported on CPB Gizmo.")
118+
119+
# The following is a list of the features available in other PyBadger modules but
120+
# not available for CPB Gizmo. If called while using a CPB Gizmo, they will result in the
121+
# NotImplementedError raised in the property above.
122+
123+
124+
cpb_gizmo = CPB_Gizmo() # pylint: disable=invalid-name
125+
"""Object that is automatically created on import."""

adafruit_pybadger/pybadger_base.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ def __init__(self):
132132
self._created_background = False
133133

134134
# Display
135-
self.display = board.DISPLAY
136-
self._display_brightness = 1.0
135+
if "DISPLAY" in dir(board):
136+
self.display = board.DISPLAY
137+
self._display_brightness = 1.0
137138

138139
self._neopixels = None
139140

@@ -212,9 +213,8 @@ def badge_background(
212213
)
213214
return self._background_group
214215

215-
@classmethod
216216
def _badge_background(
217-
cls,
217+
self,
218218
background_color=(255, 0, 0),
219219
rectangle_color=(255, 255, 255),
220220
rectangle_drop=0.4,
@@ -223,7 +223,7 @@ def _badge_background(
223223
"""Populate the background color with a rectangle color block over it as the background for
224224
a name badge."""
225225
background_group = displayio.Group(max_size=30)
226-
color_bitmap = displayio.Bitmap(board.DISPLAY.width, board.DISPLAY.height, 1)
226+
color_bitmap = displayio.Bitmap(self.display.width, self.display.height, 1)
227227
color_palette = displayio.Palette(1)
228228
color_palette[0] = background_color
229229

@@ -234,9 +234,9 @@ def _badge_background(
234234

235235
rectangle = Rect(
236236
0,
237-
(int(board.DISPLAY.height * rectangle_drop)),
238-
board.DISPLAY.width,
239-
(int(board.DISPLAY.height * rectangle_height)),
237+
(int(self.display.height * rectangle_drop)),
238+
self.display.width,
239+
(int(self.display.height * rectangle_height)),
240240
fill=rectangle_color,
241241
)
242242
background_group.append(rectangle)

0 commit comments

Comments
 (0)