def __init__(self): self.keys = keypad.Keys( (board.BUTTON_X, board.BUTTON_DOWN, board.BUTTON_LEFT, board.BUTTON_RIGHT, board.BUTTON_UP, board.BUTTON_O, board.BUTTON_Z), value_when_pressed=False, interval=0.05) self.last_state = 0 self.event = keypad.Event(0, False) self.last_z_press = None
async def monitor_button(button, controls): """Monitor button that reverses rainbow direction and changes blink speed. Assume button is active low. """ with keypad.Keys((button, ), value_when_pressed=False, pull=True) as key: while True: key_event = key.events.get() if key_event: if key_event.pressed: controls.reverse = True controls.delay = 0.1 elif key_event.released: controls.reverse = False controls.delay = 0.5 await asyncio.sleep(0)
def __init__(self): super().__init__() self._keys = keypad.Keys( [ board.BUTTON_O, board.BUTTON_X, board.BUTTON_Z, board.BUTTON_RIGHT, board.BUTTON_DOWN, board.BUTTON_UP, board.BUTTON_LEFT, ], value_when_pressed=False, pull=True, ) self._buttons = KeyStates(self._keys)
def __init__(self) -> None: super().__init__() i2c = board.I2C() if i2c is not None: self._accelerometer = adafruit_lsm6ds.lsm6ds33.LSM6DS33(i2c) # NeoPixels self._neopixels = neopixel.NeoPixel(board.NEOPIXEL, self._neopixel_count, brightness=1, pixel_order=neopixel.GRB) self._keys = keypad.Keys([board.BUTTON_A, board.BUTTON_B], value_when_pressed=False, pull=True) self._buttons = KeyStates(self._keys)
async def monitor_interval_buttons(pin_slower, pin_faster, interval): """Monitor two buttons: one lengthens the interval, the other shortens it. Change interval.value as appropriate. """ # Assume buttons are active low. with keypad.Keys( (pin_slower, pin_faster), value_when_pressed=False, pull=True ) as keys: while True: key_event = keys.events.get() if key_event and key_event.pressed: if key_event.key_number == 0: # Lengthen the interval. interval.value += 0.1 else: # Shorten the interval. interval.value = max(0.1, interval.value - 0.1) print("interval is now", interval.value) # Let another task run. await asyncio.sleep(0)
def __init__(self) -> None: super().__init__() _i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) _int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) self.accelerometer = adafruit_lis3dh.LIS3DH_I2C(_i2c, address=0x19, int1=_int1) self.accelerometer.range = adafruit_lis3dh.RANGE_8_G self.display = tft_gizmo.TFT_Gizmo() self._display_brightness = 1.0 # NeoPixels self._neopixels = neopixel.NeoPixel( board.NEOPIXEL, self._neopixel_count, brightness=1, pixel_order=neopixel.GRB ) self._keys = keypad.Keys( [board.BUTTON_A, board.BUTTON_B], value_when_pressed=True, pull=True ) self._buttons = KeyStates(self._keys) self._light_sensor = analogio.AnalogIn(board.LIGHT)
async def monitor_buttons(reverse_pin, slower_pin, faster_pin, controls): """Monitor buttons that reverse direction and change animation speed. Assume buttons are active low. """ with keypad.Keys((reverse_pin, slower_pin, faster_pin), value_when_pressed=False, pull=True) as keys: while True: key_event = keys.events.get() if key_event and key_event.pressed: key_number = key_event.key_number if key_number == 0: controls.reverse = not controls.reverse elif key_number == 1: # Lengthen the interval. controls.wait = controls.wait + 0.001 elif key_number == 2: # Shorten the interval. controls.wait = max(0.0, controls.wait - 0.001) # Let another task run. await asyncio.sleep(0)
"""Keypad and rotary encoder example for Adafruit MacroPad""" import board import digitalio import rotaryio import neopixel import keypad from rainbowio import colorwheel key_pins = (board.KEY1, board.KEY2, board.KEY3, board.KEY4, board.KEY5, board.KEY6, board.KEY7, board.KEY8, board.KEY9, board.KEY10, board.KEY11, board.KEY12) keys = keypad.Keys(key_pins, value_when_pressed=False, pull=True) encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB) button = digitalio.DigitalInOut(board.BUTTON) button.switch_to_input(pull=digitalio.Pull.UP) pixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=0.2) last_position = None while True: if not button.value: pixels.brightness = 1.0 else: pixels.brightness = 0.2 position = encoder.position if last_position is None or position != last_position: print("Rotary:", position) last_position = position
# | | 0 | 1 | 2 | # -------------------------------# # | | 3 | 4 | 5 | # -------------------------------# # | 9 | 6 | 7 | 8 | # This part is commented for now. Only used if a diode matrix is in use #km = keypad.KeyMatrix( # row_pins=(board.GP17, board.GP4, board.GP6), # col_pins=(board.GP10, board.GP8)) board_pins = config.board_pins k = keypad.Keys(pins=board_pins, value_when_pressed=False, pull=True, interval=_DEBOUNCE) # Mode switch/pin #MODE_SW = keypad.Event(6, True) # 7-switch layout MODE_SW = keypad.Event(9, True) # 10-switch layout while True: # This part is commented for now. Only used if a diode matrix is in use #km_event = km.events.next() #if km_event: #print(len(km.events), "events queued") #print("matrix", km_event) k_event = k.events.get()
) led = pwmio.PWMOut(board.A3) led.duty_cycle = 0 # Start w/LED off led_sync = 0 # LED behavior for different sounds, see comments later # AudioOut MUST be invoked AFTER PWMOut, for correct WAV playback timing. # Maybe sharing a timer or IRQ. Unsure if bug or just overlooked docs. audio = AudioOut(board.A0) # A0 is DAC pin on M0/M4 boards # To simplify the build, each key is wired to a separate input pin rather # than making an X/Y matrix. CircuitPython's keypad module is still used # (treating the buttons as a 1x10 matrix) as this gives us niceties such # as background processing, debouncing and an event queue! keys = keypad.Keys([x[0] for x in pin_to_wave], value_when_pressed=False, pull=True) event = keypad.Event() # Single key event for re-use keys.events.clear() # Load all the WAV files from the pin_to_wave list, and one more for the # mode selector, sharing a common buffer since only one is used at a time. # Also, play a startup sound. audio_buf = bytearray(1024) waves = [ WaveFile(open(sound_folder + "/" + x[1], "rb"), audio_buf) for x in pin_to_wave ] active_sound = 0 # Index of waves[] to play when trigger is pressed selector_wave = WaveFile(open(sound_folder + "/" + "click.wav", "rb"), audio_buf)
# list of (samples to play, mixer gain level) wav_files = (('wav/amen_22k16b_160bpm.wav', 1.0), ('wav/dnb21580_22k16b_160bpm.wav', 0.9), ('wav/drumloopA_22k16b_160bpm.wav', 1.0), ('wav/femvoc_330662_22k16b_160bpm.wav', 0.8), ('wav/scratch3667_4bar_22k16b_160bpm.wav', 0.5), ('wav/pt_limor_modem_vox_01.wav', 0.4), ('wav/snowpeaks_22k_s16.wav', 0.8), ('wav/dnb21580_22k16b_160bpm_rev.wav', 1.0)) # pins used by keyboard KEY_PINS = (board.RX, board.D2, board.D3, board.D4, board.D5, board.D6, board.D7, board.D8) km = keypad.Keys(KEY_PINS, value_when_pressed=False, pull=True) audio = AudioOut(board.D10) # RP2040 PWM, use RC filter on breadboard mixer = audiomixer.Mixer(voice_count=len(wav_files), sample_rate=22050, channel_count=1, bits_per_sample=16, samples_signed=True) audio.play(mixer) # attach mixer to audio playback for i in range( len(wav_files)): # start all samples at once for use w handle_mixer wave = audiocore.WaveFile(open(wav_files[i][0], "rb")) mixer.voice[i].play(wave, loop=True) mixer.voice[i].level = 0
import os import board import keypad import ulab.numpy as np from adafruit_ble import BLERadio from adafruit_ble.advertising import Advertisement from thermalprinter import CatPrinter from seekablebitmap import imageopen ble = BLERadio() # pylint: disable=no-member buttons = keypad.Keys([board.BUTTON_A, board.BUTTON_B], value_when_pressed=False) def wait_for_press(kbd): """ Wait for a keypress and return the event """ while True: event = kbd.events.get() if event and event.pressed: return event def show(s): """ Display a message on the screen
def __init__(self, keys): self._key_pins = [getattr(board, "KEY%d" % (num + 1)) for num in list(range(12))] self._keys = keypad.Keys(self._key_pins, value_when_pressed=False, pull=True) self._key_map = [item for sublist in list(keys) for item in sublist] self._held_keys = []