From 10f29fc9acf217157c8e990fba7744fbb72a4d8a Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 14 Feb 2020 15:09:41 -0500 Subject: [PATCH 01/13] Badge and business card work on CLUE. --- adafruit_pybadger.py | 53 ++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/adafruit_pybadger.py b/adafruit_pybadger.py index 8e677d9..32e816e 100755 --- a/adafruit_pybadger.py +++ b/adafruit_pybadger.py @@ -52,9 +52,11 @@ from micropython import const import digitalio import analogio -import audioio +try: + import audioio +except ImportError: + import audiocore as audioio import displayio -from gamepadshift import GamePadShift import neopixel from adafruit_display_shapes.rect import Rect from adafruit_display_text.label import Label @@ -63,10 +65,15 @@ import adafruit_miniqr import adafruit_lis3dh +import adafruit_lsm6ds + __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") +if hasattr(board, "BUTTON_SELECT"): + Buttons = namedtuple("Buttons", "b a start select right down up left") +else: + Buttons = namedtuple("Buttons", "b a") def load_font(fontname, text): """Load a font and glyphs in the text string @@ -100,17 +107,27 @@ def __init__(self, i2c=None, *, pixels_brightness=1.0): 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) + if hasattr(board, "ACCELEROMETER_INTERRUPT"): + 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) + else: + if i2c is not None: + self._accelerometer = adafruit_lsm6ds.LSM6DS33(i2c) # Buttons - self._buttons = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK), - digitalio.DigitalInOut(board.BUTTON_OUT), - digitalio.DigitalInOut(board.BUTTON_LATCH)) + try: + from gamepadshift import GamePadShift + self._buttons = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK), + digitalio.DigitalInOut(board.BUTTON_OUT), + digitalio.DigitalInOut(board.BUTTON_LATCH)) + except ImportError: + from gamepad import GamePad + self._buttons = GamePad(digitalio.DigitalInOut(board.BUTTON_A), + digitalio.DigitalInOut(board.BUTTON_B)) # Display self.display = board.DISPLAY @@ -135,8 +152,11 @@ def __init__(self, i2c=None, *, pixels_brightness=1.0): self._start_time = time.monotonic() # Define audio: - self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) - self._speaker_enable.switch_to_output(value=False) + if hasattr(board, "SPEAKER_ENABLE"): + self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) + self._speaker_enable.switch_to_output(value=False) + else: + self._speaker_enable = None self._sample = None self._sine_wave = None self._sine_wave_sample = None @@ -236,6 +256,9 @@ def button(self): y < 15000, # UP x < 15000 # LEFT ) + elif hasattr(board, "WHITE_LEDS"): + return Buttons(button_values & PyBadger.BUTTON_B, + button_values & PyBadger.BUTTON_A) else: return Buttons(*[button_values & button for button in (PyBadger.BUTTON_B, PyBadger.BUTTON_A, PyBadger.BUTTON_START, @@ -500,3 +523,5 @@ def play_file(self, file_name): while audio.playing: pass self._speaker_enable.value = False + +pybadger = PyBadger() \ No newline at end of file From d5cdc1e32706027f68d0239c73fbeac1fff63068 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Sun, 16 Feb 2020 21:38:17 -0500 Subject: [PATCH 02/13] Basically working on CLUE and PyBadge LC. --- adafruit_pybadger/__init__.py | 33 ++++ adafruit_pybadger/clue.py | 93 +++++++++++ adafruit_pybadger/pybadge.py | 124 +++++++++++++++ .../pybadger_base.py | 148 +++--------------- adafruit_pybadger/pygamer.py | 120 ++++++++++++++ 5 files changed, 396 insertions(+), 122 deletions(-) create mode 100755 adafruit_pybadger/__init__.py create mode 100755 adafruit_pybadger/clue.py create mode 100755 adafruit_pybadger/pybadge.py rename adafruit_pybadger.py => adafruit_pybadger/pybadger_base.py (78%) create mode 100755 adafruit_pybadger/pygamer.py diff --git a/adafruit_pybadger/__init__.py b/adafruit_pybadger/__init__.py new file mode 100755 index 0000000..f4a5228 --- /dev/null +++ b/adafruit_pybadger/__init__.py @@ -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 diff --git a/adafruit_pybadger/clue.py b/adafruit_pybadger/clue.py new file mode 100755 index 0000000..05ded4e --- /dev/null +++ b/adafruit_pybadger/clue.py @@ -0,0 +1,93 @@ +# 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 `_ + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://github.com/adafruit/circuitpython/releases + +""" + +from collections import namedtuple +import board +import audiopwmio +import digitalio +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): + + _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): + 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 Circuit Playground Express.""" + raise NotImplementedError("This feature is not supported on CLUE.") + + # The following is a list of the features available in other Circuit Playground modules but + # not available for Circuit Playground Express. If called while using a Circuit Playground + # Express, they will result in the NotImplementedError raised in the property above. + play_file = _unsupported + light = _unsupported + +clue = Clue() +"""Object that is automatically created on import.""" diff --git a/adafruit_pybadger/pybadge.py b/adafruit_pybadger/pybadge.py new file mode 100755 index 0000000..cfacb7e --- /dev/null +++ b/adafruit_pybadger/pybadge.py @@ -0,0 +1,124 @@ +# 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 and PyBadge LC. +Both 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 `_ +* `Adafruit PyBadge LC `_ + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://github.com/adafruit/circuitpython/releases + +""" + +from collections import namedtuple +import audioio +import board +import digitalio +import analogio +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): + _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) + + @property + def button(self): + """The buttons on the board. + + Example use: + + .. code-block:: python + + from adafruit_pybadger import PyBadger + + pybadger = 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() +"""Object that is automatically created on import.""" diff --git a/adafruit_pybadger.py b/adafruit_pybadger/pybadger_base.py similarity index 78% rename from adafruit_pybadger.py rename to adafruit_pybadger/pybadger_base.py index 32e816e..842580e 100755 --- a/adafruit_pybadger.py +++ b/adafruit_pybadger/pybadger_base.py @@ -1,6 +1,6 @@ # The MIT License (MIT) # -# Copyright (c) 2019 Kattni Rembor for Adafruit Industries +# Copyright (c) 2019-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 @@ -47,15 +47,13 @@ import time import array import math -from collections import namedtuple import board from micropython import const import digitalio -import analogio try: - import audioio + import audiocore except ImportError: - import audiocore as audioio + import audioio as audiocore import displayio import neopixel from adafruit_display_shapes.rect import Rect @@ -63,18 +61,10 @@ from adafruit_bitmap_font import bitmap_font import terminalio import adafruit_miniqr -import adafruit_lis3dh - -import adafruit_lsm6ds __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git" -if hasattr(board, "BUTTON_SELECT"): - Buttons = namedtuple("Buttons", "b a start select right down up left") -else: - Buttons = namedtuple("Buttons", "b a") - def load_font(fontname, text): """Load a font and glyphs in the text string @@ -87,8 +77,12 @@ def load_font(fontname, text): return font # pylint: disable=too-many-instance-attributes -class PyBadger: - """PyBadger class.""" +class PyBadgerBase: + """PyBadger base class.""" + + _audio_out = None + _neopixel_count = None + # Button Constants BUTTON_LEFT = const(128) BUTTON_UP = const(64) @@ -99,52 +93,16 @@ class PyBadger: BUTTON_A = const(2) BUTTON_B = const(1) - def __init__(self, i2c=None, *, pixels_brightness=1.0): - # Accelerometer - if i2c is None: - try: - i2c = board.I2C() - except RuntimeError: - self._accelerometer = None - - if hasattr(board, "ACCELEROMETER_INTERRUPT"): - 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) - else: - if i2c is not None: - self._accelerometer = adafruit_lsm6ds.LSM6DS33(i2c) - - # Buttons - try: - from gamepadshift import GamePadShift - self._buttons = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK), - digitalio.DigitalInOut(board.BUTTON_OUT), - digitalio.DigitalInOut(board.BUTTON_LATCH)) - except ImportError: - from gamepad import GamePad - self._buttons = GamePad(digitalio.DigitalInOut(board.BUTTON_A), - digitalio.DigitalInOut(board.BUTTON_B)) + def __init__(self, *, pixels_brightness=1.0): + self._light_sensor = None + self._accelerometer = None # Display self.display = board.DISPLAY self._display_brightness = 1.0 - # Light sensor - self._light_sensor = analogio.AnalogIn(board.A7) - - # PyGamer joystick - if hasattr(board, "JOYSTICK_X"): - self._pygamer_joystick_x = analogio.AnalogIn(board.JOYSTICK_X) - self._pygamer_joystick_y = analogio.AnalogIn(board.JOYSTICK_Y) - # NeoPixels - # Count is hardcoded - should be based on board ID, currently no board info for PyBadge LC - neopixel_count = 5 - self._neopixels = neopixel.NeoPixel(board.NEOPIXEL, neopixel_count, + self._neopixels = neopixel.NeoPixel(board.NEOPIXEL, self._neopixel_count, brightness=pixels_brightness, pixel_order=neopixel.GRB) # Auto dim display based on movement @@ -155,8 +113,6 @@ def __init__(self, i2c=None, *, pixels_brightness=1.0): if hasattr(board, "SPEAKER_ENABLE"): self._speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) self._speaker_enable.switch_to_output(value=False) - else: - self._speaker_enable = None self._sample = None self._sine_wave = None self._sine_wave_sample = None @@ -211,60 +167,6 @@ def pixels(self): """Sequence like object representing the NeoPixels on the board.""" return self._neopixels - @property - def joystick(self): - """The joystick on the PyGamer.""" - if hasattr(board, "JOYSTICK_X"): - x = self._pygamer_joystick_x.value - y = self._pygamer_joystick_y.value - return x, y - raise RuntimeError("This board does not have a built in joystick.") - - @property - def button(self): - """The buttons on the board. - - Example use: - - .. code-block:: python - - from adafruit_pybadger import PyBadger - - pybadger = 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") - - """ - #pylint: disable=no-else-return - button_values = self._buttons.get_pressed() - if hasattr(board, "JOYSTICK_X"): - x, y = self.joystick - return Buttons(button_values & PyBadger.BUTTON_B, - button_values & PyBadger.BUTTON_A, - button_values & PyBadger.BUTTON_START, - button_values & PyBadger.BUTTON_SELECT, - x > 50000, # RIGHT - y > 50000, # DOWN - y < 15000, # UP - x < 15000 # LEFT - ) - elif hasattr(board, "WHITE_LEDS"): - return Buttons(button_values & PyBadger.BUTTON_B, - button_values & PyBadger.BUTTON_A) - else: - return Buttons(*[button_values & button for button in - (PyBadger.BUTTON_B, PyBadger.BUTTON_A, PyBadger.BUTTON_START, - PyBadger.BUTTON_SELECT, PyBadger.BUTTON_RIGHT, - PyBadger.BUTTON_DOWN, PyBadger.BUTTON_UP, PyBadger.BUTTON_LEFT)]) - @property def light(self): """Light sensor data.""" @@ -463,9 +365,9 @@ def _sine_sample(length): def _generate_sample(self, length=100): if self._sample is not None: return - self._sine_wave = array.array("H", PyBadger._sine_sample(length)) - self._sample = audioio.AudioOut(board.SPEAKER) - self._sine_wave_sample = audioio.RawSample(self._sine_wave) + self._sine_wave = array.array("H", PyBadgerBase._sine_sample(length)) + self._sample = self._audio_out(board.SPEAKER) # pylint: disable=not-callable + self._sine_wave_sample = audiocore.RawSample(self._sine_wave) def play_tone(self, frequency, duration): """ Produce a tone using the speaker. Try changing frequency to change @@ -487,7 +389,8 @@ def start_tone(self, frequency): :param int frequency: The frequency of the tone in Hz """ - self._speaker_enable.value = True + if hasattr(board, "SPEAKER_ENABLE"): + self._speaker_enable.value = True length = 100 if length * frequency > 350000: length = 350000 // frequency @@ -506,7 +409,8 @@ def stop_tone(self): self._sample.stop() self._sample.deinit() self._sample = None - self._speaker_enable.value = False + if hasattr(board, "SPEAKER_ENABLE"): + self._speaker_enable.value = False def play_file(self, file_name): """ Play a .wav file using the onboard speaker. @@ -516,12 +420,12 @@ def play_file(self, file_name): """ # Play a specified file. self.stop_tone() - self._speaker_enable.value = True - with audioio.AudioOut(board.SPEAKER) as audio: - wavefile = audioio.WaveFile(open(file_name, "rb")) + if hasattr(board, "SPEAKER_ENABLE"): + self._speaker_enable.value = True + with self._audio_out(board.SPEAKER) as audio: # pylint: disable=not-callable + wavefile = audiocore.WaveFile(open(file_name, "rb")) audio.play(wavefile) while audio.playing: pass - self._speaker_enable.value = False - -pybadger = PyBadger() \ No newline at end of file + if hasattr(board, "SPEAKER_ENABLE"): + self._speaker_enable.value = False diff --git a/adafruit_pybadger/pygamer.py b/adafruit_pybadger/pygamer.py new file mode 100755 index 0000000..a59a4fc --- /dev/null +++ b/adafruit_pybadger/pygamer.py @@ -0,0 +1,120 @@ +# 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.pygamer` +================================================================================ + +Badge-focused CircuitPython helper library for PyGamer. + + +* Author(s): Kattni Rembor + +Implementation Notes +-------------------- + +**Hardware:** + +* `Adafruit PyGamer `_ + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://github.com/adafruit/circuitpython/releases + +""" + +from collections import namedtuple +import audioio +import board +import analogio +import digitalio +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") + +class PyGamer(PyBadgerBase): + _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) + + self._buttons = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK), + digitalio.DigitalInOut(board.BUTTON_OUT), + digitalio.DigitalInOut(board.BUTTON_LATCH)) + + 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 + + pybadger = 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 & PyBadgerBase.BUTTON_B, + button_values & PyBadgerBase.BUTTON_A, + button_values & PyBadgerBase.BUTTON_START, + button_values & PyBadgerBase.BUTTON_SELECT, + ) + + @property + def joystick(self): + """The joystick on the PyGamer.""" + x = self._pygamer_joystick_x.value + y = self._pygamer_joystick_y.value + return x, y + +pygamer = PyGamer() +"""Object that is automatically created on import.""" From 928e009fd7725f6cfbf5109aa708252960ff0e5f Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 18 Feb 2020 16:53:31 -0500 Subject: [PATCH 03/13] Linting, comments. --- adafruit_pybadger/clue.py | 30 +++++++++++++++++++++++------- adafruit_pybadger/pybadge.py | 13 +++++++------ adafruit_pybadger/pybadger_base.py | 2 -- adafruit_pybadger/pygamer.py | 7 +++++-- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/adafruit_pybadger/clue.py b/adafruit_pybadger/clue.py index 05ded4e..251d288 100755 --- a/adafruit_pybadger/clue.py +++ b/adafruit_pybadger/clue.py @@ -44,8 +44,8 @@ from collections import namedtuple import board -import audiopwmio import digitalio +import audiopwmio from gamepad import GamePad import adafruit_lsm6ds from adafruit_pybadger.pybadger_base import PyBadgerBase @@ -56,7 +56,7 @@ Buttons = namedtuple("Buttons", "a b") class Clue(PyBadgerBase): - + """Class that represents a single CLUE.""" _audio_out = audiopwmio.PWMAudioOut _neopixel_count = 1 @@ -73,6 +73,22 @@ def __init__(self): @property def button(self): + """The buttons on the board. + + Example use: + + .. code-block:: python + + from adafruit_pybadger import PyBadger + + pybadger = 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) @@ -80,14 +96,14 @@ def button(self): @property def _unsupported(self): - """This feature is not supported on Circuit Playground Express.""" + """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 Circuit Playground modules but - # not available for Circuit Playground Express. If called while using a Circuit Playground - # Express, they will result in the NotImplementedError raised in the property above. + # 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() +clue = Clue() # pylint: disable=invalid-name """Object that is automatically created on import.""" diff --git a/adafruit_pybadger/pybadge.py b/adafruit_pybadger/pybadge.py index cfacb7e..adf01a3 100755 --- a/adafruit_pybadger/pybadge.py +++ b/adafruit_pybadger/pybadge.py @@ -23,8 +23,8 @@ `adafruit_pybadger.pybadge` ================================================================================ -Badge-focused CircuitPython helper library for PyBadge and PyBadge LC. -Both boards are included in this module as there is no difference in the +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. @@ -38,6 +38,7 @@ * `Adafruit PyBadge `_ * `Adafruit PyBadge LC `_ +* `Adafruit EdgeBadge `_ **Software and Dependencies:** @@ -47,10 +48,10 @@ """ from collections import namedtuple -import audioio import board import digitalio import analogio +import audioio from gamepadshift import GamePadShift import adafruit_lis3dh from adafruit_pybadger.pybadger_base import PyBadgerBase @@ -61,11 +62,11 @@ Buttons = namedtuple("Buttons", "b a start select right down up left") class PyBadge(PyBadgerBase): - _audio_out = audioio.AudioOut + """Class that represents a single PyBadge, PyBadge LC, or EdgeBadge.""" + _audio_out = audioio.AudioOut _neopixel_count = 5 - def __init__(self): super().__init__() @@ -120,5 +121,5 @@ def button(self): PyBadgerBase.BUTTON_RIGHT, PyBadgerBase.BUTTON_DOWN, PyBadgerBase.BUTTON_UP, PyBadgerBase.BUTTON_LEFT)]) -pybadge = PyBadge() +pybadge = PyBadge() # pylint: disable=invalid-name """Object that is automatically created on import.""" diff --git a/adafruit_pybadger/pybadger_base.py b/adafruit_pybadger/pybadger_base.py index 842580e..fbca18f 100755 --- a/adafruit_pybadger/pybadger_base.py +++ b/adafruit_pybadger/pybadger_base.py @@ -335,7 +335,6 @@ def show_qr_code(self, *, data="https://circuitpython.org"): """Generate a QR code and display it for ``dwell`` seconds. :param string data: A string of data for the QR code - :param int dwell: The amount of time in seconds to display the QR code """ qr_code = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L) @@ -402,7 +401,6 @@ def start_tone(self, frequency): def stop_tone(self): """ Use with start_tone to stop the tone produced. - """ # Stop playing any tones. if self._sample is not None and self._sample.playing: diff --git a/adafruit_pybadger/pygamer.py b/adafruit_pybadger/pygamer.py index a59a4fc..1010d96 100755 --- a/adafruit_pybadger/pygamer.py +++ b/adafruit_pybadger/pygamer.py @@ -43,10 +43,10 @@ """ from collections import namedtuple -import audioio import board import analogio import digitalio +import audioio from gamepadshift import GamePadShift import adafruit_lis3dh from adafruit_pybadger.pybadger_base import PyBadgerBase @@ -57,8 +57,11 @@ Buttons = namedtuple("Buttons", "b a start select") class PyGamer(PyBadgerBase): + """Class that represents a single PyGamer.""" + _audio_out = audioio.AudioOut _neopixel_count = 5 + def __init__(self): super().__init__() @@ -116,5 +119,5 @@ def joystick(self): y = self._pygamer_joystick_y.value return x, y -pygamer = PyGamer() +pygamer = PyGamer() # pylint: disable=invalid-name """Object that is automatically created on import.""" From 9d3d819776b18414807fd7e51fe176aa78d1c51e Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 18 Feb 2020 17:01:28 -0500 Subject: [PATCH 04/13] Update example to use new import. --- examples/pybadger_simpletest.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/pybadger_simpletest.py b/examples/pybadger_simpletest.py index 836fd13..6dba7f8 100644 --- a/examples/pybadger_simpletest.py +++ b/examples/pybadger_simpletest.py @@ -1,6 +1,4 @@ -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) From a6273ff7e2d1168140ef7262cd6c1c0cf5031c9f Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 18 Feb 2020 17:05:10 -0500 Subject: [PATCH 05/13] Appease Sphinx. --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 18cabce..de3f14c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -23,7 +23,7 @@ autodoc_mock_imports = ["audioio", "displayio", "gamepadshift", "neopixel", "analogio", "adafruit_display_shapes", "adafruit_display_text", "terminalio", "adafruit_miniqr", "adafruit_lis3dh", "adafruit_miniqr", - "adafruit_bitmap_font"] + "adafruit_bitmap_font", "adafruit_lsm6ds"] intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} From d426bd39bd0f397f5f99ba10f6130af05839b53f Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 18 Feb 2020 17:11:42 -0500 Subject: [PATCH 06/13] Appease Sphinx. --- docs/conf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index de3f14c..6356179 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -23,7 +23,8 @@ autodoc_mock_imports = ["audioio", "displayio", "gamepadshift", "neopixel", "analogio", "adafruit_display_shapes", "adafruit_display_text", "terminalio", "adafruit_miniqr", "adafruit_lis3dh", "adafruit_miniqr", - "adafruit_bitmap_font", "adafruit_lsm6ds"] + "adafruit_bitmap_font", "adafruit_lsm6ds", "gamepad", "audiocore", + "audiopwmio"] intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} From c5bf2a6b3d58fa5e9ae6771deb374d42a74c318a Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 18 Feb 2020 17:32:11 -0500 Subject: [PATCH 07/13] Automock all the things. --- docs/conf.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 6356179..7e2bb39 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -22,9 +22,11 @@ # autodoc module docs will fail to generate with a warning. autodoc_mock_imports = ["audioio", "displayio", "gamepadshift", "neopixel", "analogio", "adafruit_display_shapes", "adafruit_display_text", "terminalio", - "adafruit_miniqr", "adafruit_lis3dh", "adafruit_miniqr", - "adafruit_bitmap_font", "adafruit_lsm6ds", "gamepad", "audiocore", - "audiopwmio"] + "adafruit_miniqr", "adafruit_lis3dh", "adafruit_bitmap_font", + "adafruit_lsm6ds", "gamepad", "audiocore", "audiopwmio", "micropython", + "collections", "board", "time", "array", "math", + "adafruit_pybadger.pybadger_base", "terminalio", "digitalio", + "adafruit_pybadger"] intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} From b78db488acf5ee22ea7eaccabb8ba2646fecd4bf Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 18 Feb 2020 19:10:00 -0500 Subject: [PATCH 08/13] Still Sphinxing. --- adafruit_pybadger/clue.py | 14 +++++++------- adafruit_pybadger/pybadge.py | 26 +++++++++++++------------- adafruit_pybadger/pybadger_base.py | 5 +++-- adafruit_pybadger/pygamer.py | 26 +++++++++++++------------- docs/conf.py | 6 ++++-- setup.py | 2 +- 6 files changed, 41 insertions(+), 38 deletions(-) diff --git a/adafruit_pybadger/clue.py b/adafruit_pybadger/clue.py index 251d288..89e1e2d 100755 --- a/adafruit_pybadger/clue.py +++ b/adafruit_pybadger/clue.py @@ -79,15 +79,15 @@ def button(self): .. code-block:: python - from adafruit_pybadger import PyBadger + from adafruit_pybadger import PyBadger - pybadger = PyBadger() + pybadger = PyBadger() - while True: - if pybadger.button.a: - print("Button A") - elif pybadger.button.b: - print("Button B") + 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, diff --git a/adafruit_pybadger/pybadge.py b/adafruit_pybadger/pybadge.py index adf01a3..0d27d61 100755 --- a/adafruit_pybadger/pybadge.py +++ b/adafruit_pybadger/pybadge.py @@ -99,19 +99,19 @@ def button(self): .. code-block:: python - from adafruit_pybadger import PyBadger - - pybadger = 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") + from adafruit_pybadger import PyBadger + + pybadger = 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() diff --git a/adafruit_pybadger/pybadger_base.py b/adafruit_pybadger/pybadger_base.py index fbca18f..cd3ac81 100755 --- a/adafruit_pybadger/pybadger_base.py +++ b/adafruit_pybadger/pybadger_base.py @@ -20,10 +20,10 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ -`adafruit_pybadger` +`adafruit_pybadger.pybadger_base` ================================================================================ -Badge-focused CircuitPython helper library for PyBadge and PyGamer. +Base class for badge-focused CircuitPython helper library. * Author(s): Kattni Rembor @@ -33,6 +33,7 @@ **Hardware:** +* `Adafruit CLUE `_ * `Adafruit PyBadge `_ * `Adafruit PyBadge LC `_ * `Adafruit PyGamer `_ diff --git a/adafruit_pybadger/pygamer.py b/adafruit_pybadger/pygamer.py index 1010d96..8ec5486 100755 --- a/adafruit_pybadger/pygamer.py +++ b/adafruit_pybadger/pygamer.py @@ -90,19 +90,19 @@ def button(self): .. code-block:: python - from adafruit_pybadger import PyBadger - - pybadger = 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") + from adafruit_pybadger import PyBadger + + pybadger = 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() diff --git a/docs/conf.py b/docs/conf.py index 7e2bb39..52e7e44 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -24,9 +24,11 @@ "adafruit_display_shapes", "adafruit_display_text", "terminalio", "adafruit_miniqr", "adafruit_lis3dh", "adafruit_bitmap_font", "adafruit_lsm6ds", "gamepad", "audiocore", "audiopwmio", "micropython", + "terminalio", "digitalio", "collections", "board", "time", "array", "math", - "adafruit_pybadger.pybadger_base", "terminalio", "digitalio", - "adafruit_pybadger"] + "adafruit_pybadger.pybadger_base", + "adafruit_pybadger", "adafruit_pybadger.clue", "adafruit_pybadger.pygamer", + "adafruit_pybadger.pybadge"] intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} diff --git a/setup.py b/setup.py index 5b7dd13..6ef62d0 100644 --- a/setup.py +++ b/setup.py @@ -59,5 +59,5 @@ # simple. Or you can use find_packages(). # TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER, # CHANGE `py_modules=['...']` TO `packages=['...']` - py_modules=['adafruit_pybadger'], + packages=['adafruit_pybadger'], ) From 0aada9eb102ec6f58bb7f04fae24d28699b10fab Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Tue, 18 Feb 2020 19:42:56 -0500 Subject: [PATCH 09/13] SPHINX FINALLY BUILDS. Locally... --- docs/api.rst | 11 ++++++++++- docs/conf.py | 6 +----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index fc22c47..c264f39 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -4,5 +4,14 @@ .. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py) .. use this format as the module name: "adafruit_foo.foo" -.. automodule:: adafruit_pybadger +.. automodule:: adafruit_pybadger.pybadger_base + :members: + +.. automodule:: adafruit_pybadger.clue + :members: + +.. automodule:: adafruit_pybadger.pybadge + :members: + +.. automodule:: adafruit_pybadger.pygamer :members: diff --git a/docs/conf.py b/docs/conf.py index 52e7e44..a1bcd90 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -24,11 +24,7 @@ "adafruit_display_shapes", "adafruit_display_text", "terminalio", "adafruit_miniqr", "adafruit_lis3dh", "adafruit_bitmap_font", "adafruit_lsm6ds", "gamepad", "audiocore", "audiopwmio", "micropython", - "terminalio", "digitalio", - "collections", "board", "time", "array", "math", - "adafruit_pybadger.pybadger_base", - "adafruit_pybadger", "adafruit_pybadger.clue", "adafruit_pybadger.pygamer", - "adafruit_pybadger.pybadge"] + "terminalio", "digitalio", "board"] intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} From 391f5d13ab531899b8fa16c3f723aae261f978bf Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 19 Feb 2020 12:09:49 -0500 Subject: [PATCH 10/13] Update README. --- README.rst | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index a5eec5a..4082d3d 100644 --- a/README.rst +++ b/README.rst @@ -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 @@ -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 `_. To install for current user: @@ -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) From 18489e7da63f8832469aca26e6d2a19fd19c5397 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 19 Feb 2020 19:47:57 -0500 Subject: [PATCH 11/13] Add 'd-pad' to PyGamer, fix docs. --- adafruit_pybadger/clue.py | 4 +--- adafruit_pybadger/pybadge.py | 4 +--- adafruit_pybadger/pygamer.py | 10 ++++++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/adafruit_pybadger/clue.py b/adafruit_pybadger/clue.py index 89e1e2d..ba6aeab 100755 --- a/adafruit_pybadger/clue.py +++ b/adafruit_pybadger/clue.py @@ -79,9 +79,7 @@ def button(self): .. code-block:: python - from adafruit_pybadger import PyBadger - - pybadger = PyBadger() + from adafruit_pybadger import pybadger while True: if pybadger.button.a: diff --git a/adafruit_pybadger/pybadge.py b/adafruit_pybadger/pybadge.py index 0d27d61..deb75ba 100755 --- a/adafruit_pybadger/pybadge.py +++ b/adafruit_pybadger/pybadge.py @@ -99,9 +99,7 @@ def button(self): .. code-block:: python - from adafruit_pybadger import PyBadger - - pybadger = PyBadger() + from adafruit_pybadger import pybadger while True: if pybadger.button.a: diff --git a/adafruit_pybadger/pygamer.py b/adafruit_pybadger/pygamer.py index 8ec5486..419dc2b 100755 --- a/adafruit_pybadger/pygamer.py +++ b/adafruit_pybadger/pygamer.py @@ -54,7 +54,7 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git" -Buttons = namedtuple("Buttons", "b a start select") +Buttons = namedtuple("Buttons", "b a start select right down up left") class PyGamer(PyBadgerBase): """Class that represents a single PyGamer.""" @@ -90,9 +90,7 @@ def button(self): .. code-block:: python - from adafruit_pybadger import PyBadger - - pybadger = PyBadger() + from adafruit_pybadger import pybadger while True: if pybadger.button.a: @@ -110,6 +108,10 @@ def button(self): button_values & PyBadgerBase.BUTTON_A, button_values & PyBadgerBase.BUTTON_START, button_values & PyBadgerBase.BUTTON_SELECT, + x > 50000, # RIGHT + y > 50000, # DOWN + y < 15000, # UP + x < 15000 # LEFT ) @property From 778828f695620286ad5e63968bae995c7836e943 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Wed, 19 Feb 2020 19:57:32 -0500 Subject: [PATCH 12/13] PyGamer joystick fix. --- adafruit_pybadger/pygamer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_pybadger/pygamer.py b/adafruit_pybadger/pygamer.py index 419dc2b..94fffba 100755 --- a/adafruit_pybadger/pygamer.py +++ b/adafruit_pybadger/pygamer.py @@ -104,6 +104,7 @@ def button(self): """ button_values = self._buttons.get_pressed() + x, y = self.joystick return Buttons(button_values & PyBadgerBase.BUTTON_B, button_values & PyBadgerBase.BUTTON_A, button_values & PyBadgerBase.BUTTON_START, From ee0b46a63e88cf9c49e6b0496c401415eac1f267 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Thu, 20 Feb 2020 10:58:00 -0500 Subject: [PATCH 13/13] Create enable_speaker method. --- adafruit_pybadger/pybadger_base.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/adafruit_pybadger/pybadger_base.py b/adafruit_pybadger/pybadger_base.py index 9505b91..98c2517 100755 --- a/adafruit_pybadger/pybadger_base.py +++ b/adafruit_pybadger/pybadger_base.py @@ -374,6 +374,11 @@ def _generate_sample(self, length=100): self._sample = self._audio_out(board.SPEAKER) # pylint: disable=not-callable self._sine_wave_sample = audiocore.RawSample(self._sine_wave) + def _enable_speaker(self, enable): + if not hasattr(board, "SPEAKER_ENABLE"): + return + self._speaker_enable.value = enable + def play_tone(self, frequency, duration): """ Produce a tone using the speaker. Try changing frequency to change the pitch of the tone. @@ -394,8 +399,7 @@ def start_tone(self, frequency): :param int frequency: The frequency of the tone in Hz """ - if hasattr(board, "SPEAKER_ENABLE"): - self._speaker_enable.value = True + self._enable_speaker(enable=True) length = 100 if length * frequency > 350000: length = 350000 // frequency @@ -413,8 +417,7 @@ def stop_tone(self): self._sample.stop() self._sample.deinit() self._sample = None - if hasattr(board, "SPEAKER_ENABLE"): - self._speaker_enable.value = False + self._enable_speaker(enable=False) def play_file(self, file_name): """ Play a .wav file using the onboard speaker. @@ -424,12 +427,10 @@ def play_file(self, file_name): """ # Play a specified file. self.stop_tone() - if hasattr(board, "SPEAKER_ENABLE"): - self._speaker_enable.value = True + self._enable_speaker(enable=True) with self._audio_out(board.SPEAKER) as audio: # pylint: disable=not-callable wavefile = audiocore.WaveFile(open(file_name, "rb")) audio.play(wavefile) while audio.playing: pass - if hasattr(board, "SPEAKER_ENABLE"): - self._speaker_enable.value = False + self._enable_speaker(enable=True)