Différences entre versions de « MicroPython-Hack-button »

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
Ligne 43 : Ligne 43 :
 
You can pass any function (that takes zero arguments) to the switch callback. Above we used the {{fname|lambda}} feature of Python to create an anonymous function on the fly. But we could equally do:
 
You can pass any function (that takes zero arguments) to the switch callback. Above we used the {{fname|lambda}} feature of Python to create an anonymous function on the fly. But we could equally do:
  
  </nowiki>>>> def f():
+
  <nowiki>>>> def f():
 
...  pyb.LED(1).toggle()
 
...  pyb.LED(1).toggle()
 
...
 
...

Version du 24 février 2015 à 11:52


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

Introduction

The pyboard has 2 small switches, labelled USR and RST. The RST switch is a hard-reset switch, and if you press it then it restarts the pyboard from scratch, equivalent to turning the power off then back on.

The USR switch is for general use, and is controlled via a Switch object. To make a switch object do:

>>> sw = pyb.Switch()

Remember that you may need to type import pyb if you get an error that the name pyb does not exist.

With the switch object you can get its status:

>>> sw()
False

This will print False if the switch is not held, or True if it is held. Try holding the USR switch down while running the above command.

Callback bouton

Qu'est-ce qu'un callback?

En informatique, une fonction de rappel (callback en anglais) ou fonction de post-traitement est une fonction qui est passée en argument à une autre fonction. Cette dernière peut alors faire usage de cette fonction de rappel comme de n'importe quelle autre fonction, alors qu'elle ne la connaît pas par avance.
Source: Fonction de rappel sur Wikipedia

L'objet switch

switch est un terme anglais signifiant "bouton" ou "interrupteur".

Nous disions donc que le switch is a very simple object, but it does have one advanced feature: the sw.callback() function. The callback function sets up something to run when the switch is pressed, and uses an interrupt. It’s probably best to start with an example before understanding how interrupts work. Try running the following at the prompt:

>>> sw.callback(lambda:print('press!'))

This tells the switch to print press! each time the switch is pressed down. Go ahead and try it: press the USR switch and watch the output on your PC. Note that this print will interrupt anything you are typing, and is an example of an interrupt routine running asynchronously.

As another example try:

>>> sw.callback(lambda:pyb.LED(1).toggle())

This will toggle the red LED each time the switch is pressed. And it will even work while other code is running.

To disable the switch callback, pass None to the callback function:

>>> sw.callback(None)

You can pass any function (that takes zero arguments) to the switch callback. Above we used the lambda feature of Python to create an anonymous function on the fly. But we could equally do:

>>> def f():
...   pyb.LED(1).toggle()
...
>>> sw.callback(f)

This creates a function called f and assigns it to the switch callback. You can do things this way when your function is more complicated than a lambda will allow.

Note that your callback functions must not allocate any memory (for example they cannot create a tuple or list). Callback functions should be relatively simple. If you need to make a list, make it beforehand and store it in a global variable (or make it local and close over it). If you need to do a long, complicated calculation, then use the callback to set a flag which some other code then responds to.



Source: The Switch, callbacks and interrupts écrit par/written by Damien P.George

Traduit par Meurisse D. pour MCHobby.be - Translated by Meurisse D. for MCHobby.be

Traduit avec l'autorisation de micropython.org - Translated with the authorisation of micropython.org

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.