machine.Pin methods?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

machine.Pin methods?

Post by Capstan » Sun Jan 29, 2017 4:23 pm

I'm trying to determine what subset of the methods that might be available in the Pin class are actually implemented for the esp8266.

Here I am seeing quite a few for the pyBoard;
https://docs.micropython.org/en/latest/ ... b.Pin.html

But I am finding that many do not work for the 8266. In particular I am trying to find out which pin was responsible for generating an interrupt on a rising or falling edge. In this code, for example, the callback function will receive pObj as an argument;

pin = 12
pObj = Pin(pin, Pin.IN)
pObj.irq(trigger = Pin.IRQ_FALLING, handler = callback)

def callback(pinObject)
print(pinObject) # would be indented of course

This results in the following being printed;
Pin(12)

What is the appropriate way to get the pin number out of Pin so I can see which pin generated the interrupt?

pinObject.__str__()
AttributeError: 'Pin' object has no attribute '__str__

pinObject.pin()
AttributeError: 'Pin' object has no attribute 'pin'

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.Pin methods?

Post by dhylands » Sun Jan 29, 2017 7:35 pm

You should be able to do

Code: Select all

dir(machine.Pin)
to see all of the available attributes (which includes functions and constants).

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: machine.Pin methods?

Post by deshipu » Sun Jan 29, 2017 8:54 pm

I think a pin has an "id" method that you can call to get its number.

Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

Re: machine.Pin methods?

Post by Capstan » Sun Jan 29, 2017 11:34 pm

>>> dir(machine.Pin)
['init', 'value', 'low', 'high', 'irq', 'IN', 'OUT', 'OPEN_DRAIN', 'PULL_UP', 'IRQ_RISING', 'IRQ_FALLING']

So yeah, a pretty small subset of the pyBoard implementation. I found that, as a workaround, when I set up an IRQ I have to put an entry into a dictionary that is the Pin object keyed by the pin number. Then when the interrupt gets triggered I have to iterate through the dictionary looking for the reported Pin object value, and the key for that is the pin number. Awkward.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.Pin methods?

Post by dhylands » Mon Jan 30, 2017 5:47 am

irq functions can also be class methods. Then you can store any addtional information you want in the instance.

Here is an example (albeit for the pyboard) which demonstrates this: https://github.com/dhylands/upy-example ... eat_irq.py

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: machine.Pin methods?

Post by SpotlightKid » Tue Jan 31, 2017 8:28 am

Or you can use the partial function from functools to bind some data (e.g. the pin number) to the callback:

Code: Select all

from functools import partial
from machine import Pin

def cb_func(pin_no, pin):
    print(pin_no, pin)

pin_no = 12
pin = Pin(pin_no, Pin.IN)
pin.irq(trigger=Pin.IRQ_FALLING, handler=partial(cb_func, pin_no))
Last edited by SpotlightKid on Tue Jan 31, 2017 6:44 pm, edited 1 time in total.

Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

Re: machine.Pin methods?

Post by Capstan » Tue Jan 31, 2017 4:01 pm

>>> from functools import partial
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'functools'


SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: machine.Pin methods?

Post by SpotlightKid » Tue Jan 31, 2017 6:50 pm

If you have a large number of pin callbacks, the class based approach might be more memory efficient. Using partial, you'll need memory for a function object, an empty dict for the kwargs and a tuple with one entry for the args for every callback you define, whereas with one class with a single callback method and an attribute mapping instances (or the hash(obj) value) to your data, you'll probably need much less. But I haven't actually measured it.

scargill
Posts: 1
Joined: Mon May 29, 2017 8:50 pm

Re: machine.Pin methods?

Post by scargill » Mon May 29, 2017 8:59 pm

Well, while appreciating the fantastic amount of work that must've gone into this - I have to say this was not a good start for me.

I downloaded the latest software onto an ESP8266 and went off to the documentation here.

https://docs.micropython.org/en/latest/ ... /repl.html

Unless I'm missing the point - the official docs for the ESP8266.

and there it was, a simple example:

>>> import machine
>>> pin = machine.Pin(2, machine.Pin.OUT)
>>> pin.high()
>>> pin.low()

and it didn't work. No such thing as high and low.

I'm so glad I came here to notice the test to see what is available - dir(machine.Pin) .... not high and low but on and off. I happily tried:

pin.on()

Nothing - no light on the little board.

pin.off()

Wunderbar - the light came on - in other words the instructions are merely inverted and using the wrong methods.

So while this is great - it proves my board works... I'm wondering before I go further with this..

Is there an up to date document anywhere that actually DOES have working examples? Had I not stumbled on this page I might've assumed that there was something wrong.

I have to say.... I'm wondering if I DARE try I2c and SPI for fear or other "differences"? Anyone tried these on an ESP8266?

Post Reply