Ligne 1 : |
Ligne 1 : |
| {{MicroPython-Hack-Prepare-NAV}} | | {{MicroPython-Hack-Prepare-NAV}} |
| | | |
− | == Introduction ==
| + | {{MicroPython-Hack-button-core}} |
− | {{bloc-etroit|text=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:
| |
− | | |
− | <nowiki>>>> sw = pyb.Switch()</nowiki>
| |
− | | |
− | 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:
| |
− | | |
− | <nowiki>>>> sw()
| |
− | False</nowiki>
| |
− | | |
− | This will print {{fname|False}} if the switch is not held, or {{fname|True}} if it is held. Try holding the USR switch down while running the above command.
| |
− | }}
| |
− | | |
− | == Callback bouton ==
| |
− | === Qu'est-ce qu'un callback? ==
| |
− | {{block-etroit|text=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.<small><br />Source: [http://fr.wikipedia.org/wiki/Fonction_de_rappel Fonction de rappel sur Wikipedia]</small>
| |
− | }}
| |
− | === L'objet switch ===
| |
− | {{block-etroit|text='''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:
| |
− | | |
− | <nowiki>>>> sw.callback(lambda:print('press!'))</nowiki>
| |
− | | |
− | 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:
| |
− | | |
− | <nowiki>>>> sw.callback(lambda:pyb.LED(1).toggle())</nowiki>
| |
− | | |
− | 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:
| |
− | | |
− | <nowiki>>>> sw.callback(None)</nowiki>
| |
− | | |
− | 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():
| |
− | ... pyb.LED(1).toggle()
| |
− | ...
| |
− | >>> sw.callback(f)</nowiki>
| |
− | | |
− | This creates a function called {{fname|f}} and assigns it to the switch callback. You can do things this way when your function is more complicated than a {{fname|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.
| |
− | }} | |
| | | |
| {{MicroPython-Hack-Bouton-TRAILER}} | | {{MicroPython-Hack-Bouton-TRAILER}} |