Modifications

Sauter à la navigation Sauter à la recherche
1 687 octets ajoutés ,  24 février 2015 à 11:54
Ligne 52 : Ligne 52 :  
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.
 
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.
 
}}
 
}}
 +
 +
== Les interruptions: détails techniques ==
 +
Let’s step through the details of what is happening with the switch callback. When you register a function with {{fname|sw.callback()}}, the switch sets up an external interrupt trigger (falling edge) on the pin that the switch is connected to. This means that the microcontroller will listen on the pin for any changes, and the following will occur:
 +
 +
# When the switch is pressed a change occurs on the pin (the pin goes from low to high), and the microcontroller registers this change.
 +
# The microcontroller finishes executing the current machine instruction, stops execution, and saves its current state (pushes the registers on the stack). This has the effect of pausing any code, for example your running Python script.
 +
# The microcontroller starts executing the special interrupt handler associated with the switch’s external trigger. This interrupt handler get the function that you registered with sw.callback() and executes it.
 +
# Your callback function is executed until it finishes, returning control to the switch interrupt handler.
 +
# The switch interrupt handler returns, and the microcontroller is notified that the interrupt has been dealt with.
 +
# The microcontroller restores the state that it saved in step 2.
 +
# Execution continues of the code that was running at the beginning. Apart from the pause, this code does not notice that it was interrupted.
 +
 +
The above sequence of events gets a bit more complicated when multiple interrupts occur at the same time. In that case, the interrupt with the highest priority goes first, then the others in order of their priority. The switch interrupt is set at the lowest priority.
    
{{MicroPython-Hack-Bouton-TRAILER}}
 
{{MicroPython-Hack-Bouton-TRAILER}}
29 836

modifications

Menu de navigation