Skip to content

Capacitive touch iterable attribute #103

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

Closed
kattni opened this issue Mar 24, 2021 · 2 comments · Fixed by #114
Closed

Capacitive touch iterable attribute #103

kattni opened this issue Mar 24, 2021 · 2 comments · Fixed by #114

Comments

@kattni
Copy link
Contributor

kattni commented Mar 24, 2021

This code was designed in response to this issue.

It is a start for another library that could be for the Circuit Playground Express, or usable across multiple boards. I'm posting the code here so anyone who wants to pick this up has a place to start. The idea is to allow for iteration across all capacitive touch inputs (in the current case, the touch pads on the CPX), and return strings, ints or pin instances for use. It could possibly be written into a class that uses touch state. Or it could be expandable to include all inputs that provide boolean values, such as buttons as well.

As it is, it initialises all of the touch pads as touch pads, which on the CPX means that none of those pads can be used anything else, such as digital outputs, at the same time that this function is called. This may not be an issue in the eventual library, but it's something to be aware of.

The functions are:

def touched_padname(self):
    pin = (board.A1, board.A2, board.A3, board.A4, board.A5, board.A6, board.A7)
    touch = tuple(touchio.TouchIn(p) for p in pin)
    return ['A{}'.format(i+1) for (i, v) in enumerate(touch) if v.value]

def touched(self):
    pin = (board.A1, board.A2, board.A3, board.A4, board.A5, board.A6, board.A7)
    touch = tuple(touchio.TouchIn(p) for p in pin)
    return [i for (i, v) in enumerate(touch) if v.value]

This allows for use like the following examples. It is by no means limited to the simplicity of these examples.

for pad in cpx.touched:
    print("pad {0} touched!".format(pad))
if 1 in cpx.touched:
    print("pad 1 touched!")
if 'A1' in cpx.touched:
    print("pad A1 touched!")
COMBO = ('A1', 'A2', 'A5')
if all(pad in cpx.touched for pad in COMBO):
    print("UNLOCKED!")

Opened here from original issue here.

@kattni kattni self-assigned this Mar 24, 2021
@kattni kattni removed their assignment Mar 24, 2021
@tekktrik
Copy link
Member

Oh this looks fun! So I see two ways of doing this:

  1. Make an IterableInput class that leverages __iter__, __next__, __getitem__, and __setitem__. This would allow adding some additional functionalities as needed, and the ability to subclass if also needed eventually. Also allow users to create simple collections of iterables, but not sure on the use cases for that.
  2. Leverage the existing CircuitPlaygroundBase._touches list of touch inputs and just creating the properties you mentioned above to iterate through that collection. This wouldn't require any new class to be made, and which maybe will save memory but I don't know the priority for this issue.

Let me know what you think!

@tekktrik
Copy link
Member

Just posting to say I'm actively working on this! Should have a PR soon! I decided to go with option 1 because it gives a little more flexibility with things like accessing inputs based on index or name, as well as initing and deiniting the touchpads so they can be used as other inputs as needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants