Skip to content
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

make board an iterable of pins #13

Open
ramalho opened this issue Apr 29, 2014 · 1 comment
Open

make board an iterable of pins #13

ramalho opened this issue Apr 29, 2014 · 1 comment

Comments

@ramalho
Copy link
Contributor

ramalho commented Apr 29, 2014

This will enable this style of programming:

for pin in my_board: do_something(pin)

However, currently I am not happy with the default of listing the Vcc and Gnd pins...

This should iterate only over the digital pins, which are the most useful for beginners.

@danilobellini
Copy link
Contributor

I think the Board should have an attribute that is an iterable of pins, and perhaps one extra attribute for each useful restriction (like the digital pins), instead of being itself an iterable. Something like:

>>> for pin in board.digital_pins:
...     print(pin)
[...]

That would be explicit. A board.analog_pins also would help. For all pins, why not all_pins? Anyhow, I don't know whether iterating for all pins in a board would make practical sense for a Pingo user: one should know (or control) the iteration order for making use of it. They can be tuples, but probably are better as dictionaries, which allow selecting a single pin by its "key".

Perhaps it should be a class that inherits from dictionary, but that:

  1. Iterates through its values (instead of keys), so the syntax above would work
  2. Allow direct access to a pin by a key value, like board.digital_pins["17"]
  3. "Multiple" key values (e.g. with __missing__), so access would work for both a default "physical" pin number and their aliases ("logical" pin names, both string and int names, etc.), without changing the "size" of the dictionaries

An iterable from all pins can be easily converted (using a list comprehension) to whatever needed (e.g. selecting all pins that are both analog and digital, selecting all power supply pins, etc.). Semantically, I don't think the board itself is a good concept for "pin iterator", but even if it is, it seems to be strangely implicit to put a restriction like "only over the digital pins". Just seen the code example for pin in my_board: do_something(pin) it's not clear whether the "pin" is digital, and it would be worse for something like for p in board: [...].

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

No branches or pull requests

3 participants