Skip to content

Remove keypad code, and were_pressed. #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 6 additions & 71 deletions adafruit_circuitplayground/circuit_playground_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,11 @@
import adafruit_thermistor
import neopixel
import touchio
import keypad


__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground.git"


class KeyStates:
"""Convert `keypad.Event` information from the given `keypad` scanner into key-pressed state.

:param scanner: a `keypad` scanner, such as `keypad.Keys`
"""

def __init__(self, scanner):
self._scanner = scanner
self._pressed = [False] * self._scanner.key_count
self.update()

def update(self):
"""Update key information based on pending scanner events."""

# If the event queue overflowed, discard any pending events,
# and assume all keys are now released.
if self._scanner.events.overflowed:
self._scanner.events.clear()
self._scanner.reset()
self._pressed = [False] * self._scanner.key_count

self._was_pressed = self._pressed.copy()

while True:
event = self._scanner.events.get()
if not event:
# Event queue is now empty.
break
self._pressed[event.key_number] = event.pressed
if event.pressed:
self._was_pressed[event.key_number] = True

def was_pressed(self, key_number):
"""True if key was down at any time since the last `update()`,
even if it was later released.
"""
return self._was_pressed[key_number]

def pressed(self, key_number):
"""True if key is currently pressed, as of the last `update()`."""
return self._pressed[key_number]


class Photocell:
"""Simple driver for analog photocell on the Circuit Playground Express and Bluefruit."""

Expand All @@ -101,9 +56,10 @@ class CircuitPlaygroundBase: # pylint: disable=too-many-public-methods
_audio_out = None

def __init__(self):
self._button_pins = [board.BUTTON_A, board.BUTTON_B]
self._keys = keypad.Keys(self._button_pins, value_when_pressed=True, pull=True)
self._states = KeyStates(self._keys)
self._a = digitalio.DigitalInOut(board.BUTTON_A)
self._a.switch_to_input(pull=digitalio.Pull.DOWN)
self._b = digitalio.DigitalInOut(board.BUTTON_B)
self._b.switch_to_input(pull=digitalio.Pull.DOWN)

# Define switch:
self._switch = digitalio.DigitalInOut(board.SLIDE_SWITCH)
Expand Down Expand Up @@ -610,8 +566,7 @@ def button_a(self):
if cp.button_a:
print("Button A pressed!")
"""
self._states.update()
return self._states.pressed(0)
return self._a.value

@property
def button_b(self):
Expand All @@ -630,27 +585,7 @@ def button_b(self):
if cp.button_b:
print("Button B pressed!")
"""
self._states.update()
return self._states.pressed(1)

@property
def were_pressed(self):
"""Returns a set of the buttons that have been pressed

.. image :: ../docs/_static/button_b.jpg
:alt: Button B

To use with the Circuit Playground Express or Bluefruit:

.. code-block:: python

from adafruit_circuitplayground import cp

while True:
print(cp.were_pressed)
"""
self._states.update()
return {("A", "B")[i] for i in range(2) if self._states.was_pressed(i)}
return self._b.value

@property
def switch(self):
Expand Down