/ adafruit_pybadger / pygamer.py
pygamer.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.pygamer` 24 ================================================================================ 25 26 Badge-focused CircuitPython helper library for PyGamer. 27 28 29 * Author(s): Kattni Rembor 30 31 Implementation Notes 32 -------------------- 33 34 **Hardware:** 35 36 * `Adafruit PyGamer <https://www.adafruit.com/product/4277>`_ 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 analogio 48 import digitalio 49 import audioio 50 import neopixel 51 from gamepadshift import GamePadShift 52 import adafruit_lis3dh 53 from adafruit_pybadger.pybadger_base import PyBadgerBase 54 55 __version__ = "0.0.0-auto.0" 56 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyBadger.git" 57 58 Buttons = namedtuple("Buttons", "b a start select right down up left") 59 60 61 class PyGamer(PyBadgerBase): 62 """Class that represents a single PyGamer.""" 63 64 _audio_out = audioio.AudioOut 65 _neopixel_count = 5 66 67 def __init__(self): 68 super().__init__() 69 70 i2c = board.I2C() 71 72 int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) 73 try: 74 self._accelerometer = adafruit_lis3dh.LIS3DH_I2C( 75 i2c, address=0x19, int1=int1 76 ) 77 except ValueError: 78 self._accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) 79 80 # NeoPixels 81 self._neopixels = neopixel.NeoPixel( 82 board.NEOPIXEL, self._neopixel_count, brightness=1, pixel_order=neopixel.GRB 83 ) 84 85 self._buttons = GamePadShift( 86 digitalio.DigitalInOut(board.BUTTON_CLOCK), 87 digitalio.DigitalInOut(board.BUTTON_OUT), 88 digitalio.DigitalInOut(board.BUTTON_LATCH), 89 ) 90 91 self._pygamer_joystick_x = analogio.AnalogIn(board.JOYSTICK_X) 92 self._pygamer_joystick_y = analogio.AnalogIn(board.JOYSTICK_Y) 93 94 self._light_sensor = analogio.AnalogIn(board.A7) 95 96 @property 97 def button(self): 98 """The buttons on the board. 99 100 Example use: 101 102 .. code-block:: python 103 104 from adafruit_pybadger import pybadger 105 106 while True: 107 if pybadger.button.a: 108 print("Button A") 109 elif pybadger.button.b: 110 print("Button B") 111 elif pybadger.button.start: 112 print("Button start") 113 elif pybadger.button.select: 114 print("Button select") 115 116 """ 117 button_values = self._buttons.get_pressed() 118 x, y = self.joystick 119 return Buttons( 120 button_values & PyBadgerBase.BUTTON_B, 121 button_values & PyBadgerBase.BUTTON_A, 122 button_values & PyBadgerBase.BUTTON_START, 123 button_values & PyBadgerBase.BUTTON_SELECT, 124 x > 50000, # RIGHT 125 y > 50000, # DOWN 126 y < 15000, # UP 127 x < 15000, # LEFT 128 ) 129 130 @property 131 def joystick(self): 132 """The joystick on the PyGamer.""" 133 x = self._pygamer_joystick_x.value 134 y = self._pygamer_joystick_y.value 135 return x, y 136 137 138 pygamer = PyGamer() # pylint: disable=invalid-name 139 """Object that is automatically created on import."""