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) # NeoPixels self._neopixels = neopixel.NeoPixel(board.NEOPIXEL, self._neopixel_count, brightness=1, pixel_order=neopixel.GRB) self._keys = keypad.ShiftRegisterKeys( clock=board.BUTTON_CLOCK, data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=4, value_when_pressed=True, ) self._buttons = KeyStates(self._keys) 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)
def __init__(self) -> None: super().__init__() i2c = None if i2c is None: try: i2c = board.I2C() except RuntimeError: self._accelerometer = None if i2c is not None: _i2c_devices = [] for _ in range(10): # try lock 10 times to avoid infinite loop in sphinx build if i2c.try_lock(): _i2c_devices = i2c.scan() i2c.unlock() break # PyBadge LC doesn't have accelerometer if int(0x18) in _i2c_devices or int(0x19) in _i2c_devices: 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) # NeoPixels self._neopixels = neopixel.NeoPixel(board.NEOPIXEL, self._neopixel_count, brightness=1, pixel_order=neopixel.GRB) self._keys = keypad.ShiftRegisterKeys( clock=board.BUTTON_CLOCK, data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=8, value_when_pressed=True, ) self._buttons = KeyStates(self._keys) self._light_sensor = analogio.AnalogIn(board.A7)
# handle pyGamer buttons and joystick import keypad import board import analogio joystick_x = analogio.AnalogIn(board.JOYSTICK_X) joystick_y = analogio.AnalogIn(board.JOYSTICK_Y) last_joystick = '' # init keypad on PyGamer k = keypad.ShiftRegisterKeys( clock=board.BUTTON_CLOCK, data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=8, value_when_pressed=True, ) # return a value from zero to 127 def getVoltage(pin): return int((pin.value / 65536) * 127) return int((pin.value / 65536) * 127) # Return a direction if the joystick is changed # Must push the joystick to an extreme position, # as it is being used as a 4 way button pad def check_joystick(): global last_joystick