machine.Pin , pyb.Pin and pyb.ExtInt changes ?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
allankliu
Posts: 11
Joined: Fri Mar 30, 2018 11:13 am

machine.Pin , pyb.Pin and pyb.ExtInt changes ?

Post by allankliu » Sun Apr 08, 2018 2:29 am

I browsed micropython's document http://docs.micropython.org/en/latest/p ... ne.Pin.irq. And I modified the code to run my pyboard.

Code: Select all

from machine import Pin
p0 = Pin('X0', Pin.OUT)
print("p0 = 0")
p0.value(0)
print("p0 = 1")
p0.value(1)

p2 = Pin('Y0', Pin.IN, Pin.PULL_UP)
print("p2 =",p2.value())

#p0.mode(p0.IN) # TypeError: function takes 1 positional arguments but 2 were given
#p0.irq(lambda p:print(p))

p2.irq(lambda p:print(p)) # AttributeError: 'Pin' object has no attribute 'irq'
Obviously, the code seems out of date. machine.pin.mode() and machine.pin.irq() have been changed from certain version. Then I found another thread in forum .viewtopic.php?t=857

The new code is

Code: Select all

from pyb import Pin, ExtInt

import micropython
micropython.alloc_emergency_exception_buf(100)

red = Pin('X0', Pin.OUT)
sw = Pin('Y0', Pin.IN)
flag = False

def callback(line):
    print('irq',line)
    global flag
    flag = True
    
ext = ExtInt(sw, ExtInt.IRQ_FALLING, Pin.PULL_UP, callback)

while True:
    if flag:
        flag = False
        s = red.value()
        if s:
            red.value(0)
        else:
            red.value(1)
        print('sw pressed')
The code works. Also I find its documentation at http://docs.micropython.org/en/latest/p ... ght=extint

I am just confused about differences between machine.Pin and pyb.Pin. I have tried the dir() to find the differences. From attribute list, there are no differences.

There is an extra question, Has pin.irq() method been removed officially?


allankliu
Posts: 11
Joined: Fri Mar 30, 2018 11:13 am

Re: machine.Pin , pyb.Pin and pyb.ExtInt changes ?

Post by allankliu » Sun Apr 08, 2018 8:23 am

Thanks for your reply.

Post Reply