object-oriented programming

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
mschulz
Posts: 35
Joined: Fri Apr 17, 2015 11:26 am
Location: Germany

Re: object-oriented programming

Post by mschulz » Mon Apr 27, 2015 7:56 am

With an other pins it works,

But the switch callback

Code: Select all

# USR Schalter
print ('INIT des Schalters')
switch = pyb.Switch()

def switchcallback():
    print('INFO-Switch: Switch Callback aufgerufen!')

switch.callback(lambda :switchcallback())
only starts one time.

Which is the best solution to stop the scheduler with a push button (the USR or an hardware interrupt (a button I can braze on other pins)
(and restart it)

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

Re: object-oriented programming

Post by dhylands » Thu Apr 19, 2018 4:49 pm

You can pass switchcallback directly to the callback function.

What you coded:

Code: Select all

switch.callback(lambda :switchcallback())
is equivalent to:

Code: Select all

x = switchcallback()
switch.callback(lamba: x)
You can code it like this:

Code: Select all

switch.callback(switchcallback)
Note that there are no parenthesis so it passes in the address of the function whereas with the parenthesis it calls the function.

Post Reply