/ adafruit_pybadger / cpb_gizmo.py
cpb_gizmo.py
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."""